chrome: Refactor state fields to remove is_ prefix. (#627)

This commit is contained in:
Jason Lee 2025-02-14 15:20:18 +08:00 committed by GitHub
parent bf300a2880
commit 0ebedbe79f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 102 additions and 106 deletions

View file

@ -147,7 +147,7 @@ struct CompanyListDelegate {
confirmed_index: Option<usize>, confirmed_index: Option<usize>,
query: String, query: String,
loading: bool, loading: bool,
is_eof: bool, eof: bool,
} }
impl ListDelegate for CompanyListDelegate { impl ListDelegate for CompanyListDelegate {
@ -208,7 +208,7 @@ impl ListDelegate for CompanyListDelegate {
} }
fn can_load_more(&self, _: &App) -> bool { fn can_load_more(&self, _: &App) -> bool {
return !self.loading && !self.is_eof; return !self.loading && !self.eof;
} }
fn load_more_threshold(&self) -> usize { fn load_more_threshold(&self) -> usize {
@ -226,7 +226,7 @@ impl ListDelegate for CompanyListDelegate {
.companies .companies
.extend((0..200).map(|_| random_company())); .extend((0..200).map(|_| random_company()));
_ = view.delegate_mut().perform_search(&query, window, cx); _ = view.delegate_mut().perform_search(&query, window, cx);
view.delegate_mut().is_eof = view.delegate().companies.len() >= 6000; view.delegate_mut().eof = view.delegate().companies.len() >= 6000;
}); });
}) })
.detach(); .detach();
@ -281,7 +281,7 @@ impl ListStory {
confirmed_index: None, confirmed_index: None,
query: "".to_string(), query: "".to_string(),
loading: false, loading: false,
is_eof: false, eof: false,
}; };
let company_list = cx.new(|cx| List::new(delegate, window, cx)); let company_list = cx.new(|cx| List::new(delegate, window, cx));

View file

@ -25,7 +25,7 @@ impl_internal_actions!(sidebar_story, [SelectCompany]);
pub struct SidebarStory { pub struct SidebarStory {
active_item: Item, active_item: Item,
active_subitem: Option<SubItem>, active_subitem: Option<SubItem>,
is_collapsed: bool, collapsed: bool,
side: Side, side: Side,
focus_handle: gpui::FocusHandle, focus_handle: gpui::FocusHandle,
} }
@ -39,7 +39,7 @@ impl SidebarStory {
Self { Self {
active_item: Item::Playground, active_item: Item::Playground,
active_subitem: None, active_subitem: None,
is_collapsed: false, collapsed: false,
side: Side::Left, side: Side::Left,
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
} }
@ -231,10 +231,10 @@ impl Render for SidebarStory {
.when(self.side.is_right(), |this| this.flex_row_reverse()) .when(self.side.is_right(), |this| this.flex_row_reverse())
.child( .child(
sidebar sidebar
.collapsed(self.is_collapsed) .collapsed(self.collapsed)
.header( .header(
SidebarHeader::new() SidebarHeader::new()
.collapsed(self.is_collapsed) .collapsed(self.collapsed)
.w_full() .w_full()
.child( .child(
div() div()
@ -246,17 +246,17 @@ impl Render for SidebarStory {
.text_color(white()) .text_color(white())
.size_8() .size_8()
.flex_shrink_0() .flex_shrink_0()
.when(!self.is_collapsed, |this| { .when(!self.collapsed, |this| {
this.child(Icon::new(IconName::GalleryVerticalEnd).size_4()) this.child(Icon::new(IconName::GalleryVerticalEnd).size_4())
}) })
.when(self.is_collapsed, |this| { .when(self.collapsed, |this| {
this.size_4() this.size_4()
.bg(cx.theme().transparent) .bg(cx.theme().transparent)
.text_color(cx.theme().foreground) .text_color(cx.theme().foreground)
.child(Icon::new(IconName::GalleryVerticalEnd).size_5()) .child(Icon::new(IconName::GalleryVerticalEnd).size_5())
}), }),
) )
.when(!self.is_collapsed, |this| { .when(!self.collapsed, |this| {
this.child( this.child(
v_flex() v_flex()
.gap_0() .gap_0()
@ -269,7 +269,7 @@ impl Render for SidebarStory {
.child(div().child("Enterprise").text_xs()), .child(div().child("Enterprise").text_xs()),
) )
}) })
.when(!self.is_collapsed, |this| { .when(!self.collapsed, |this| {
this.child( this.child(
Icon::new(IconName::ChevronsUpDown).size_4().flex_shrink_0(), Icon::new(IconName::ChevronsUpDown).size_4().flex_shrink_0(),
) )
@ -291,15 +291,15 @@ impl Render for SidebarStory {
) )
.footer( .footer(
SidebarFooter::new() SidebarFooter::new()
.collapsed(self.is_collapsed) .collapsed(self.collapsed)
.justify_between() .justify_between()
.child( .child(
h_flex() h_flex()
.gap_2() .gap_2()
.child(IconName::CircleUser) .child(IconName::CircleUser)
.when(!self.is_collapsed, |this| this.child("Jason Lee")), .when(!self.collapsed, |this| this.child("Jason Lee")),
) )
.when(!self.is_collapsed, |this| { .when(!self.collapsed, |this| {
this.child( this.child(
Icon::new(IconName::ChevronsUpDown).size_4().flex_shrink_0(), Icon::new(IconName::ChevronsUpDown).size_4().flex_shrink_0(),
) )
@ -362,9 +362,9 @@ impl Render for SidebarStory {
.child( .child(
SidebarToggleButton::left() SidebarToggleButton::left()
.side(self.side) .side(self.side)
.collapsed(self.is_collapsed) .collapsed(self.collapsed)
.on_click(cx.listener(|this, _, _, cx| { .on_click(cx.listener(|this, _, _, cx| {
this.is_collapsed = !this.is_collapsed; this.collapsed = !this.collapsed;
cx.notify(); cx.notify();
})), })),
) )

View file

@ -183,7 +183,7 @@ struct StockTableDelegate {
loading: bool, loading: bool,
full_loading: bool, full_loading: bool,
fixed_cols: bool, fixed_cols: bool,
is_eof: bool, eof: bool,
visible_rows: Range<usize>, visible_rows: Range<usize>,
visible_cols: Range<usize>, visible_cols: Range<usize>,
} }
@ -247,7 +247,7 @@ impl StockTableDelegate {
fixed_cols: false, fixed_cols: false,
loading: false, loading: false,
full_loading: false, full_loading: false,
is_eof: false, eof: false,
visible_cols: Range::default(), visible_cols: Range::default(),
visible_rows: Range::default(), visible_rows: Range::default(),
} }
@ -255,7 +255,7 @@ impl StockTableDelegate {
fn update_stocks(&mut self, size: usize) { fn update_stocks(&mut self, size: usize) {
self.stocks = random_stocks(size); self.stocks = random_stocks(size);
self.is_eof = size <= 50; self.eof = size <= 50;
self.loading = false; self.loading = false;
self.full_loading = false; self.full_loading = false;
} }
@ -514,7 +514,7 @@ impl TableDelegate for StockTableDelegate {
} }
fn can_load_more(&self, _: &App) -> bool { fn can_load_more(&self, _: &App) -> bool {
return !self.loading && !self.is_eof; return !self.loading && !self.eof;
} }
fn load_more_threshold(&self) -> usize { fn load_more_threshold(&self) -> usize {
@ -532,7 +532,7 @@ impl TableDelegate for StockTableDelegate {
let _ = view.update(cx, |view, _| { let _ = view.update(cx, |view, _| {
view.delegate_mut().stocks.extend(random_stocks(200)); view.delegate_mut().stocks.extend(random_stocks(200));
view.delegate_mut().loading = false; view.delegate_mut().loading = false;
view.delegate_mut().is_eof = view.delegate().stocks.len() >= 6000; view.delegate_mut().eof = view.delegate().stocks.len() >= 6000;
}); });
}) })
}) })
@ -927,7 +927,7 @@ impl Render for TableStory {
.child(format!("Total Rows: {}", rows_count)) .child(format!("Total Rows: {}", rows_count))
.child(format!("Visible Rows: {:?}", delegate.visible_rows)) .child(format!("Visible Rows: {:?}", delegate.visible_rows))
.child(format!("Visible Cols: {:?}", delegate.visible_cols)) .child(format!("Visible Cols: {:?}", delegate.visible_cols))
.when(delegate.is_eof, |this| this.child("All data loaded.")), .when(delegate.eof, |this| this.child("All data loaded.")),
), ),
) )
.child(self.table.clone()) .child(self.table.clone())

View file

@ -74,7 +74,7 @@ pub struct Dock {
// Runtime state // Runtime state
/// Whether the Dock is resizing /// Whether the Dock is resizing
is_resizing: bool, resizing: bool,
} }
impl Dock { impl Dock {
@ -105,7 +105,7 @@ impl Dock {
open: true, open: true,
collapsible: true, collapsible: true,
size: px(200.0), size: px(200.0),
is_resizing: false, resizing: false,
} }
} }
@ -178,7 +178,7 @@ impl Dock {
open, open,
size, size,
collapsible: true, collapsible: true,
is_resizing: false, resizing: false,
} }
} }
@ -284,13 +284,13 @@ impl Dock {
.on_drag(ResizePanel {}, move |info, _, _, cx| { .on_drag(ResizePanel {}, move |info, _, _, cx| {
cx.stop_propagation(); cx.stop_propagation();
view.update(cx, |view, _| { view.update(cx, |view, _| {
view.is_resizing = true; view.resizing = true;
}); });
cx.new(|_| info.deref().clone()) cx.new(|_| info.deref().clone())
}) })
} }
fn resize(&mut self, mouse_position: Point<Pixels>, _: &mut Window, cx: &mut Context<Self>) { fn resize(&mut self, mouse_position: Point<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
if !self.is_resizing { if !self.resizing {
return; return;
} }
@ -349,7 +349,7 @@ impl Dock {
} }
fn done_resizing(&mut self, _window: &mut Window, _cx: &mut Context<Self>) { fn done_resizing(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
self.is_resizing = false; self.resizing = false;
} }
} }
@ -438,9 +438,9 @@ impl Element for DockElement {
) { ) {
window.on_mouse_event({ window.on_mouse_event({
let view = self.view.clone(); let view = self.view.clone();
let is_resizing = view.read(cx).is_resizing; let resizing = view.read(cx).resizing;
move |e: &MouseMoveEvent, phase, window, cx| { move |e: &MouseMoveEvent, phase, window, cx| {
if !is_resizing { if !resizing {
return; return;
} }
if !phase.bubble() { if !phase.bubble() {

View file

@ -58,7 +58,7 @@ pub struct DockArea {
zoom_view: Option<AnyView>, zoom_view: Option<AnyView>,
/// Lock panels layout, but allow to resize. /// Lock panels layout, but allow to resize.
is_locked: bool, locked: bool,
/// The panel style, default is [`PanelStyle::Default`](PanelStyle::Default). /// The panel style, default is [`PanelStyle::Default`](PanelStyle::Default).
pub(crate) panel_style: PanelStyle, pub(crate) panel_style: PanelStyle,
@ -408,7 +408,7 @@ impl DockArea {
left_dock: None, left_dock: None,
right_dock: None, right_dock: None,
bottom_dock: None, bottom_dock: None,
is_locked: false, locked: false,
panel_style: PanelStyle::Default, panel_style: PanelStyle::Default,
_subscriptions: vec![], _subscriptions: vec![],
}; };
@ -514,12 +514,12 @@ impl DockArea {
/// Set locked state of the dock area, if locked, the dock area cannot be split or move, but allows to resize panels. /// Set locked state of the dock area, if locked, the dock area cannot be split or move, but allows to resize panels.
pub fn set_locked(&mut self, locked: bool, _window: &mut Window, _cx: &mut App) { pub fn set_locked(&mut self, locked: bool, _window: &mut Window, _cx: &mut App) {
self.is_locked = locked; self.locked = locked;
} }
/// Determine if the dock area is locked. /// Determine if the dock area is locked.
pub fn is_locked(&self) -> bool { pub fn is_locked(&self) -> bool {
self.is_locked self.locked
} }
/// Determine if the dock area has a dock at the given placement. /// Determine if the dock area has a dock at the given placement.

View file

@ -75,8 +75,8 @@ pub struct TabPanel {
pub(crate) closable: bool, pub(crate) closable: bool,
tab_bar_scroll_handle: ScrollHandle, tab_bar_scroll_handle: ScrollHandle,
is_zoomed: bool, zoomed: bool,
is_collapsed: bool, collapsed: bool,
/// When drag move, will get the placement of the panel to be split /// When drag move, will get the placement of the panel to be split
will_split_placement: Option<Placement>, will_split_placement: Option<Placement>,
} }
@ -153,8 +153,8 @@ impl TabPanel {
active_ix: 0, active_ix: 0,
tab_bar_scroll_handle: ScrollHandle::new(), tab_bar_scroll_handle: ScrollHandle::new(),
will_split_placement: None, will_split_placement: None,
is_zoomed: false, zoomed: false,
is_collapsed: false, collapsed: false,
closable: true, closable: true,
} }
} }
@ -340,7 +340,7 @@ impl TabPanel {
_: &mut Window, _: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
self.is_collapsed = collapsed; self.collapsed = collapsed;
cx.notify(); cx.notify();
} }
@ -353,7 +353,7 @@ impl TabPanel {
return true; return true;
} }
if self.is_zoomed { if self.zoomed {
return true; return true;
} }
@ -404,7 +404,7 @@ impl TabPanel {
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> impl IntoElement { ) -> impl IntoElement {
let is_zoomed = self.is_zoomed; let zoomed = self.zoomed;
let view = cx.entity().clone(); let view = cx.entity().clone();
let zoomable_toolbar_visible = state.zoomable.map_or(false, |v| v.toolbar_visible()); let zoomable_toolbar_visible = state.zoomable.map_or(false, |v| v.toolbar_visible());
@ -417,7 +417,7 @@ impl TabPanel {
this.children(buttons.into_iter().map(|btn| btn.xsmall().ghost())) this.children(buttons.into_iter().map(|btn| btn.xsmall().ghost()))
}) })
.map(|this| { .map(|this| {
let value = if is_zoomed { let value = if zoomed {
Some(("zoom-out", IconName::Minimize, t!("Dock.Zoom Out"))) Some(("zoom-out", IconName::Minimize, t!("Dock.Zoom Out")))
} else if zoomable_toolbar_visible { } else if zoomable_toolbar_visible {
Some(("zoom-in", IconName::Maximize, t!("Dock.Zoom In"))) Some(("zoom-in", IconName::Maximize, t!("Dock.Zoom In")))
@ -453,7 +453,7 @@ impl TabPanel {
view.read(cx) view.read(cx)
.popup_menu(this, window, cx) .popup_menu(this, window, cx)
.when(zoomable, |this| { .when(zoomable, |this| {
let name = if is_zoomed { let name = if zoomed {
t!("Dock.Zoom Out") t!("Dock.Zoom Out")
} else { } else {
t!("Dock.Zoom In") t!("Dock.Zoom In")
@ -476,7 +476,7 @@ impl TabPanel {
_: &mut Window, _: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> Option<impl IntoElement> { ) -> Option<impl IntoElement> {
if self.is_zoomed { if self.zoomed {
return None; return None;
} }
@ -662,14 +662,14 @@ impl TabPanel {
) )
.children(self.panels.iter().enumerate().filter_map(|(ix, panel)| { .children(self.panels.iter().enumerate().filter_map(|(ix, panel)| {
let mut active = state.active_panel.as_ref() == Some(panel); let mut active = state.active_panel.as_ref() == Some(panel);
let disabled = self.is_collapsed; let disabled = self.collapsed;
if !panel.visible(cx) { if !panel.visible(cx) {
return None; return None;
} }
// Always not show active tab style, if the panel is collapsed // Always not show active tab style, if the panel is collapsed
if self.is_collapsed { if self.collapsed {
active = false; active = false;
} }
@ -758,7 +758,7 @@ impl TabPanel {
_: &mut Window, _: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> impl IntoElement { ) -> impl IntoElement {
if self.is_collapsed { if self.collapsed {
return Empty {}.into_any_element(); return Empty {}.into_any_element();
} }
@ -1026,19 +1026,19 @@ impl TabPanel {
return; return;
} }
if !self.is_zoomed { if !self.zoomed {
cx.emit(PanelEvent::ZoomIn) cx.emit(PanelEvent::ZoomIn)
} else { } else {
cx.emit(PanelEvent::ZoomOut) cx.emit(PanelEvent::ZoomOut)
} }
self.is_zoomed = !self.is_zoomed; self.zoomed = !self.zoomed;
cx.spawn_in(window, |view, mut cx| { cx.spawn_in(window, |view, mut cx| {
let is_zoomed = self.is_zoomed; let zoomed = self.zoomed;
async move { async move {
_ = cx.update(|window, cx| { _ = cx.update(|window, cx| {
_ = view.update(cx, |view, cx| { _ = view.update(cx, |view, cx| {
view.set_zoomed(is_zoomed, window, cx); view.set_zoomed(zoomed, window, cx);
}); });
}); });
} }

View file

@ -216,7 +216,7 @@ pub struct TextInput {
/// The text bounds /// The text bounds
pub(super) last_bounds: Option<Bounds<Pixels>>, pub(super) last_bounds: Option<Bounds<Pixels>>,
pub(super) last_selected_range: Option<Range<usize>>, pub(super) last_selected_range: Option<Range<usize>>,
pub(super) is_selecting: bool, pub(super) selecting: bool,
pub(super) disabled: bool, pub(super) disabled: bool,
pub(super) masked: bool, pub(super) masked: bool,
pub(super) appearance: bool, pub(super) appearance: bool,
@ -272,7 +272,7 @@ impl TextInput {
selection_reversed: false, selection_reversed: false,
marked_range: None, marked_range: None,
input_bounds: Bounds::default(), input_bounds: Bounds::default(),
is_selecting: false, selecting: false,
disabled: false, disabled: false,
masked: false, masked: false,
appearance: true, appearance: true,
@ -936,7 +936,7 @@ impl TextInput {
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
self.is_selecting = true; self.selecting = true;
let offset = self.index_for_mouse_position(event.position, window, cx); let offset = self.index_for_mouse_position(event.position, window, cx);
// Double click to select word // Double click to select word
if event.button == MouseButton::Left && event.click_count == 2 { if event.button == MouseButton::Left && event.click_count == 2 {
@ -952,7 +952,7 @@ impl TextInput {
} }
fn on_mouse_up(&mut self, _: &MouseUpEvent, _window: &mut Window, _cx: &mut Context<Self>) { fn on_mouse_up(&mut self, _: &MouseUpEvent, _window: &mut Window, _cx: &mut Context<Self>) {
self.is_selecting = false; self.selecting = false;
self.selected_word_range = None; self.selected_word_range = None;
} }
@ -1364,7 +1364,7 @@ impl TextInput {
return; return;
} }
if !self.is_selecting { if !self.selecting {
return; return;
} }

View file

@ -139,12 +139,12 @@ where
self self
} }
fn render_trigger(&mut self, is_open: bool, window: &mut Window, cx: &mut App) -> AnyElement { fn render_trigger(&mut self, open: bool, window: &mut Window, cx: &mut App) -> AnyElement {
let Some(trigger) = self.trigger.take() else { let Some(trigger) = self.trigger.take() else {
return div().into_any_element(); return div().into_any_element();
}; };
(trigger)(is_open, window, cx) (trigger)(open, window, cx)
} }
fn resolved_corner(&self, bounds: Bounds<Pixels>) -> Point<Pixels> { fn resolved_corner(&self, bounds: Bounds<Pixels>) -> Point<Pixels> {

View file

@ -10,7 +10,7 @@ pub struct SidebarFooter {
id: ElementId, id: ElementId,
base: Div, base: Div,
selected: bool, selected: bool,
is_collapsed: bool, collapsed: bool,
} }
impl SidebarFooter { impl SidebarFooter {
@ -19,7 +19,7 @@ impl SidebarFooter {
id: SharedString::from("sidebar-footer").into(), id: SharedString::from("sidebar-footer").into(),
base: h_flex().gap_2().w_full(), base: h_flex().gap_2().w_full(),
selected: false, selected: false,
is_collapsed: false, collapsed: false,
} }
} }
} }
@ -35,11 +35,11 @@ impl Selectable for SidebarFooter {
} }
impl Collapsible for SidebarFooter { impl Collapsible for SidebarFooter {
fn is_collapsed(&self) -> bool { fn is_collapsed(&self) -> bool {
self.is_collapsed self.collapsed
} }
fn collapsed(mut self, collapsed: bool) -> Self { fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = collapsed; self.collapsed = collapsed;
self self
} }
} }

View file

@ -9,7 +9,7 @@ use gpui::{
pub struct SidebarGroup<E: Collapsible + IntoElement + 'static> { pub struct SidebarGroup<E: Collapsible + IntoElement + 'static> {
base: Div, base: Div,
label: SharedString, label: SharedString,
is_collapsed: bool, collapsed: bool,
children: Vec<E>, children: Vec<E>,
} }
@ -18,7 +18,7 @@ impl<E: Collapsible + IntoElement> SidebarGroup<E> {
Self { Self {
base: div().gap_2().flex_col(), base: div().gap_2().flex_col(),
label: label.into(), label: label.into(),
is_collapsed: false, collapsed: false,
children: Vec::new(), children: Vec::new(),
} }
} }
@ -35,11 +35,11 @@ impl<E: Collapsible + IntoElement> SidebarGroup<E> {
} }
impl<E: Collapsible + IntoElement> Collapsible for SidebarGroup<E> { impl<E: Collapsible + IntoElement> Collapsible for SidebarGroup<E> {
fn is_collapsed(&self) -> bool { fn is_collapsed(&self) -> bool {
self.is_collapsed self.collapsed
} }
fn collapsed(mut self, collapsed: bool) -> Self { fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = collapsed; self.collapsed = collapsed;
self self
} }
} }
@ -48,7 +48,7 @@ impl<E: Collapsible + IntoElement> RenderOnce for SidebarGroup<E> {
v_flex() v_flex()
.relative() .relative()
.p_2() .p_2()
.when(!self.is_collapsed, |this| { .when(!self.collapsed, |this| {
this.child( this.child(
div() div()
.flex_shrink_0() .flex_shrink_0()
@ -64,7 +64,7 @@ impl<E: Collapsible + IntoElement> RenderOnce for SidebarGroup<E> {
self.base.children( self.base.children(
self.children self.children
.into_iter() .into_iter()
.map(|child| child.collapsed(self.is_collapsed)), .map(|child| child.collapsed(self.collapsed)),
), ),
) )
} }

View file

@ -10,7 +10,7 @@ pub struct SidebarHeader {
id: ElementId, id: ElementId,
base: Div, base: Div,
selected: bool, selected: bool,
is_collapsed: bool, collapsed: bool,
} }
impl SidebarHeader { impl SidebarHeader {
@ -19,7 +19,7 @@ impl SidebarHeader {
id: SharedString::from("sidebar-header").into(), id: SharedString::from("sidebar-header").into(),
base: h_flex().gap_2().w_full(), base: h_flex().gap_2().w_full(),
selected: false, selected: false,
is_collapsed: false, collapsed: false,
} }
} }
} }
@ -35,11 +35,11 @@ impl Selectable for SidebarHeader {
} }
impl Collapsible for SidebarHeader { impl Collapsible for SidebarHeader {
fn is_collapsed(&self) -> bool { fn is_collapsed(&self) -> bool {
self.is_collapsed self.collapsed
} }
fn collapsed(mut self, collapsed: bool) -> Self { fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = collapsed; self.collapsed = collapsed;
self self
} }
} }

View file

@ -8,7 +8,7 @@ use std::rc::Rc;
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct SidebarMenu { pub struct SidebarMenu {
is_collapsed: bool, collapsed: bool,
items: Vec<SidebarMenuItem>, items: Vec<SidebarMenuItem>,
} }
@ -16,7 +16,7 @@ impl SidebarMenu {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
items: Vec::new(), items: Vec::new(),
is_collapsed: false, collapsed: false,
} }
} }
@ -32,7 +32,7 @@ impl SidebarMenu {
label: label.into(), label: label.into(),
handler: Rc::new(handler), handler: Rc::new(handler),
active, active,
is_collapsed: self.is_collapsed, collapsed: self.collapsed,
}); });
self self
} }
@ -51,8 +51,8 @@ impl SidebarMenu {
icon, icon,
label: label.into(), label: label.into(),
items: menu.items, items: menu.items,
is_open: open, open,
is_collapsed: self.is_collapsed, collapsed: self.collapsed,
handler: Rc::new(handler), handler: Rc::new(handler),
}); });
self self
@ -60,11 +60,11 @@ impl SidebarMenu {
} }
impl Collapsible for SidebarMenu { impl Collapsible for SidebarMenu {
fn is_collapsed(&self) -> bool { fn is_collapsed(&self) -> bool {
self.is_collapsed self.collapsed
} }
fn collapsed(mut self, collapsed: bool) -> Self { fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = collapsed; self.collapsed = collapsed;
self self
} }
} }
@ -74,10 +74,8 @@ impl RenderOnce for SidebarMenu {
.gap_2() .gap_2()
.children(self.items.into_iter().map(|mut item| { .children(self.items.into_iter().map(|mut item| {
match &mut item { match &mut item {
SidebarMenuItem::Item { is_collapsed, .. } => *is_collapsed = self.is_collapsed, SidebarMenuItem::Item { collapsed, .. } => *collapsed = self.collapsed,
SidebarMenuItem::Submenu { is_collapsed, .. } => { SidebarMenuItem::Submenu { collapsed, .. } => *collapsed = self.collapsed,
*is_collapsed = self.is_collapsed
}
} }
item item
})) }))
@ -92,15 +90,15 @@ enum SidebarMenuItem {
label: SharedString, label: SharedString,
handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>, handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
active: bool, active: bool,
is_collapsed: bool, collapsed: bool,
}, },
Submenu { Submenu {
icon: Option<Icon>, icon: Option<Icon>,
label: SharedString, label: SharedString,
handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>, handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
items: Vec<SidebarMenuItem>, items: Vec<SidebarMenuItem>,
is_open: bool, open: bool,
is_collapsed: bool, collapsed: bool,
}, },
} }
@ -133,16 +131,16 @@ impl SidebarMenuItem {
fn is_open(&self) -> bool { fn is_open(&self) -> bool {
match self { match self {
SidebarMenuItem::Item { .. } => false, SidebarMenuItem::Item { .. } => false,
SidebarMenuItem::Submenu { is_open, items, .. } => { SidebarMenuItem::Submenu { open, items, .. } => {
*is_open || items.iter().any(|item| item.is_active()) *open || items.iter().any(|item| item.is_active())
} }
} }
} }
fn is_collapsed(&self) -> bool { fn is_collapsed(&self) -> bool {
match self { match self {
SidebarMenuItem::Item { is_collapsed, .. } => *is_collapsed, SidebarMenuItem::Item { collapsed, .. } => *collapsed,
SidebarMenuItem::Submenu { is_collapsed, .. } => *is_collapsed, SidebarMenuItem::Submenu { collapsed, .. } => *collapsed,
} }
} }
@ -212,11 +210,9 @@ impl RenderOnce for SidebarMenuItem {
.when(is_open, |this| { .when(is_open, |this| {
this.map(|this| match self { this.map(|this| match self {
SidebarMenuItem::Submenu { SidebarMenuItem::Submenu {
items, items, collapsed, ..
is_collapsed,
..
} => { } => {
if is_collapsed { if collapsed {
this this
} else { } else {
this.child( this.child(

View file

@ -37,7 +37,7 @@ pub struct Sidebar<E: Collapsible + IntoElement + 'static> {
side: Side, side: Side,
collapsible: bool, collapsible: bool,
width: Pixels, width: Pixels,
is_collapsed: bool, collapsed: bool,
} }
impl<E: Collapsible + IntoElement> Sidebar<E> { impl<E: Collapsible + IntoElement> Sidebar<E> {
@ -50,7 +50,7 @@ impl<E: Collapsible + IntoElement> Sidebar<E> {
side, side,
collapsible: true, collapsible: true,
width: DEFAULT_WIDTH, width: DEFAULT_WIDTH,
is_collapsed: false, collapsed: false,
} }
} }
@ -76,7 +76,7 @@ impl<E: Collapsible + IntoElement> Sidebar<E> {
/// Set the sidebar to be collapsed /// Set the sidebar to be collapsed
pub fn collapsed(mut self, collapsed: bool) -> Self { pub fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = collapsed; self.collapsed = collapsed;
self self
} }
@ -109,7 +109,7 @@ impl<E: Collapsible + IntoElement> Sidebar<E> {
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct SidebarToggleButton { pub struct SidebarToggleButton {
btn: Button, btn: Button,
is_collapsed: bool, collapsed: bool,
side: Side, side: Side,
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>, on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
} }
@ -118,7 +118,7 @@ impl SidebarToggleButton {
fn new(side: Side) -> Self { fn new(side: Side) -> Self {
Self { Self {
btn: Button::new("sidebar-collapse").ghost().small(), btn: Button::new("sidebar-collapse").ghost().small(),
is_collapsed: false, collapsed: false,
side, side,
on_click: None, on_click: None,
} }
@ -137,8 +137,8 @@ impl SidebarToggleButton {
self self
} }
pub fn collapsed(mut self, is_collapsed: bool) -> Self { pub fn collapsed(mut self, collapsed: bool) -> Self {
self.is_collapsed = is_collapsed; self.collapsed = collapsed;
self self
} }
@ -153,10 +153,10 @@ impl SidebarToggleButton {
impl RenderOnce for SidebarToggleButton { impl RenderOnce for SidebarToggleButton {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let is_collapsed = self.is_collapsed; let collapsed = self.collapsed;
let on_click = self.on_click.clone(); let on_click = self.on_click.clone();
let icon = if is_collapsed { let icon = if collapsed {
if self.side.is_left() { if self.side.is_left() {
IconName::PanelLeftOpen IconName::PanelLeftOpen
} else { } else {
@ -182,11 +182,11 @@ impl RenderOnce for SidebarToggleButton {
impl<E: Collapsible + IntoElement> RenderOnce for Sidebar<E> { impl<E: Collapsible + IntoElement> RenderOnce for Sidebar<E> {
fn render(mut self, _: &mut Window, cx: &mut App) -> impl IntoElement { fn render(mut self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let is_collapsed = self.is_collapsed; let is_collapsed = self.collapsed;
v_flex() v_flex()
.id("sidebar") .id("sidebar")
.w(self.width) .w(self.width)
.when(self.is_collapsed, |this| this.w(COLLAPSED_WIDTH)) .when(self.collapsed, |this| this.w(COLLAPSED_WIDTH))
.flex_shrink_0() .flex_shrink_0()
.h_full() .h_full()
.overflow_hidden() .overflow_hidden()