Add ListItem and update PickerStory base on it.
This commit is contained in:
parent
7bbc28b98e
commit
085b8e578b
6 changed files with 125 additions and 12 deletions
|
|
@ -8,10 +8,11 @@ use ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
h_flex,
|
h_flex,
|
||||||
label::Label,
|
label::Label,
|
||||||
|
list::ListItem,
|
||||||
picker::{Picker, PickerDelegate},
|
picker::{Picker, PickerDelegate},
|
||||||
switch::{LabelSide, Switch},
|
switch::{LabelSide, Switch},
|
||||||
theme::{ActiveTheme, Colorize},
|
theme::{ActiveTheme, Colorize},
|
||||||
v_flex, Clickable as _, Disableable as _, StyledExt,
|
v_flex, Clickable as _, Disableable as _, Selectable, StyledExt,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::story_case;
|
use super::story_case;
|
||||||
|
|
@ -23,7 +24,7 @@ pub struct ListItemDeletegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PickerDelegate for ListItemDeletegate {
|
impl PickerDelegate for ListItemDeletegate {
|
||||||
type ListItem = Div;
|
type ListItem = ListItem;
|
||||||
|
|
||||||
fn match_count(&self) -> usize {
|
fn match_count(&self) -> usize {
|
||||||
self.matches.len()
|
self.matches.len()
|
||||||
|
|
@ -43,17 +44,13 @@ impl PickerDelegate for ListItemDeletegate {
|
||||||
selected: bool,
|
selected: bool,
|
||||||
cx: &mut ViewContext<Picker<Self>>,
|
cx: &mut ViewContext<Picker<Self>>,
|
||||||
) -> Option<Self::ListItem> {
|
) -> Option<Self::ListItem> {
|
||||||
|
let is_selected = ix == self.selected_index;
|
||||||
if let Some(item) = self.matches.get(ix) {
|
if let Some(item) = self.matches.get(ix) {
|
||||||
let list_item = div()
|
let list_item = ListItem::new(format!("item-{}", ix))
|
||||||
|
.selected(is_selected)
|
||||||
.py_1()
|
.py_1()
|
||||||
.px_3()
|
.px_3()
|
||||||
.when(!selected, |this| {
|
.child(item.clone());
|
||||||
this.hover(|this| this.bg(cx.theme().accent))
|
|
||||||
})
|
|
||||||
.child(item.clone())
|
|
||||||
.text_base()
|
|
||||||
.text_color(cx.theme().foreground)
|
|
||||||
.when(selected, |this| this.bg(cx.theme().accent));
|
|
||||||
Some(list_item)
|
Some(list_item)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
|
||||||
|
pub struct DropdownItemDelegate {
|
||||||
|
items: Vec<ListItem>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Dropdown {
|
||||||
|
picker:
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use gpui::{svg, IntoElement, RenderOnce, SharedString, Styled as _, Svg, WindowContext};
|
use gpui::{svg, Element, IntoElement, RenderOnce, SharedString, Styled as _, Svg, WindowContext};
|
||||||
|
|
||||||
|
#[derive(IntoElement)]
|
||||||
pub enum IconName {
|
pub enum IconName {
|
||||||
Check,
|
Check,
|
||||||
Minus,
|
Minus,
|
||||||
|
|
@ -23,6 +24,12 @@ impl IconName {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RenderOnce for IconName {
|
||||||
|
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||||
|
Icon::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(IntoElement)]
|
#[derive(IntoElement)]
|
||||||
pub struct Icon {
|
pub struct Icon {
|
||||||
base: Svg,
|
base: Svg,
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,8 @@ pub mod theme;
|
||||||
pub mod title_bar;
|
pub mod title_bar;
|
||||||
pub use styled_ext::StyledExt;
|
pub use styled_ext::StyledExt;
|
||||||
pub mod divider;
|
pub mod divider;
|
||||||
pub mod dropdown;
|
// pub mod dropdown;
|
||||||
|
pub mod list;
|
||||||
pub mod picker;
|
pub mod picker;
|
||||||
pub mod switch;
|
pub mod switch;
|
||||||
pub mod tab;
|
pub mod tab;
|
||||||
|
|
|
||||||
96
crates/ui/src/list/list_item.rs
Normal file
96
crates/ui/src/list/list_item.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
use gpui::{
|
||||||
|
div, prelude::FluentBuilder as _, ClickEvent, Div, InteractiveElement, IntoElement,
|
||||||
|
MouseButton, MouseDownEvent, ParentElement, RenderOnce, SharedString, Stateful,
|
||||||
|
StatefulInteractiveElement as _, Style, Styled, WindowContext,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{h_flex, theme::ActiveTheme, Disableable, IconName, Selectable};
|
||||||
|
|
||||||
|
#[derive(IntoElement)]
|
||||||
|
pub struct ListItem {
|
||||||
|
base: Stateful<Div>,
|
||||||
|
disabled: bool,
|
||||||
|
selected: bool,
|
||||||
|
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
|
||||||
|
on_secondary_mouse_down: Option<Box<dyn Fn(&MouseDownEvent, &mut WindowContext) + 'static>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListItem {
|
||||||
|
pub fn new(id: impl Into<SharedString>) -> Self {
|
||||||
|
Self {
|
||||||
|
base: h_flex().id(id.into()),
|
||||||
|
disabled: false,
|
||||||
|
selected: false,
|
||||||
|
on_click: None,
|
||||||
|
on_secondary_mouse_down: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
|
||||||
|
self.on_click = Some(Box::new(handler));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_secondary_mouse_down(
|
||||||
|
mut self,
|
||||||
|
handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
|
||||||
|
) -> Self {
|
||||||
|
self.on_secondary_mouse_down = Some(Box::new(handler));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Disableable for ListItem {
|
||||||
|
fn disabled(mut self, disabled: bool) -> Self {
|
||||||
|
self.disabled = disabled;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Selectable for ListItem {
|
||||||
|
fn selected(mut self, selected: bool) -> Self {
|
||||||
|
self.selected = selected;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Styled for ListItem {
|
||||||
|
fn style(&mut self) -> &mut gpui::StyleRefinement {
|
||||||
|
self.base.style()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ParentElement for ListItem {
|
||||||
|
fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) {
|
||||||
|
self.base.extend(elements)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RenderOnce for ListItem {
|
||||||
|
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||||
|
self.base
|
||||||
|
.w_full()
|
||||||
|
.relative()
|
||||||
|
.gap_x_2()
|
||||||
|
.text_base()
|
||||||
|
.text_color(cx.theme().foreground)
|
||||||
|
.when_some(self.on_click, |this, on_click| {
|
||||||
|
this.cursor_pointer().on_click(on_click)
|
||||||
|
})
|
||||||
|
// Right click
|
||||||
|
.when_some(self.on_secondary_mouse_down, |this, on_mouse_down| {
|
||||||
|
this.on_mouse_down(MouseButton::Right, move |ev, cx| (on_mouse_down)(ev, cx))
|
||||||
|
})
|
||||||
|
.when(!self.selected, |this| {
|
||||||
|
this.hover(|this| this.bg(cx.theme().accent))
|
||||||
|
})
|
||||||
|
.when(self.selected, |this| this.bg(cx.theme().accent))
|
||||||
|
.map(|this| {
|
||||||
|
if self.selected {
|
||||||
|
this.child(IconName::Check)
|
||||||
|
} else {
|
||||||
|
this.child(div())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
3
crates/ui/src/list/mod.rs
Normal file
3
crates/ui/src/list/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
mod list_item;
|
||||||
|
|
||||||
|
pub use list_item::*;
|
||||||
Loading…
Reference in a new issue