From 09a0d089bcd85e028d4c46bd8ee1ad3d74e8e677 Mon Sep 17 00:00:00 2001 From: reya Date: Sat, 1 Feb 2025 14:54:53 +0700 Subject: [PATCH] wip: refactor --- crates/ui/src/dock_area/dock.rs | 39 +------------------------- crates/ui/src/dock_area/mod.rs | 10 ++++--- crates/ui/src/dock_area/stack_panel.rs | 15 ++++++---- crates/ui/src/dock_area/tab_panel.rs | 19 +++++++------ crates/ui/src/dropdown.rs | 10 ++++--- crates/ui/src/popup_menu.rs | 2 +- 6 files changed, 34 insertions(+), 61 deletions(-) diff --git a/crates/ui/src/dock_area/dock.rs b/crates/ui/src/dock_area/dock.rs index 8044755..669e801 100644 --- a/crates/ui/src/dock_area/dock.rs +++ b/crates/ui/src/dock_area/dock.rs @@ -140,44 +140,6 @@ impl Dock { cx.notify(); } - pub(super) fn from_state( - dock_area: WeakEntity, - placement: DockPlacement, - size: Pixels, - panel: DockItem, - open: bool, - window: &mut Window, - cx: &mut App, - ) -> Self { - Self::subscribe_panel_events(dock_area.clone(), &panel, window, cx); - - if !open { - match panel.clone() { - DockItem::Tabs { view, .. } => { - view.update(cx, |panel, cx| { - panel.set_collapsed(true, window, cx); - }); - } - DockItem::Split { items, .. } => { - for item in items { - item.set_collapsed(true, window, cx); - } - } - _ => {} - } - } - - Self { - placement, - dock_area, - panel, - open, - size, - collapsible: true, - is_resizing: false, - } - } - fn subscribe_panel_events( dock_area: WeakEntity, panel: &DockItem, @@ -315,6 +277,7 @@ impl Dock { cx.new(|_| info.clone()) }) } + fn resize( &mut self, mouse_position: Point, diff --git a/crates/ui/src/dock_area/mod.rs b/crates/ui/src/dock_area/mod.rs index 1fed464..7decec6 100644 --- a/crates/ui/src/dock_area/mod.rs +++ b/crates/ui/src/dock_area/mod.rs @@ -54,7 +54,7 @@ pub struct DockArea { /// The panel style, default is [`PanelStyle::Default`](PanelStyle::Default). pub(crate) panel_style: PanelStyle, - _subscriptions: Vec, + subscriptions: Vec, } /// DockItem is a tree structure that represents the layout of the dock. @@ -316,7 +316,7 @@ impl DockArea { bottom_dock: None, is_locked: false, panel_style: PanelStyle::Default, - _subscriptions: vec![], + subscriptions: vec![], }; this.subscribe_panel(&stack_panel, window, cx); @@ -535,6 +535,7 @@ impl DockArea { cx: &mut Context, ) { let weak_self = cx.entity().downgrade(); + match placement { DockPlacement::Left => { if let Some(dock) = self.left_dock.as_ref() { @@ -590,7 +591,7 @@ impl DockArea { self.subscribe_item(item, window, cx); } - self._subscriptions.push(cx.subscribe_in( + self.subscriptions.push(cx.subscribe_in( view, window, move |_, _, event, window, cx| { @@ -601,6 +602,7 @@ impl DockArea { }); }) .detach(); + cx.emit(DockEvent::LayoutChanged); } }, @@ -656,7 +658,7 @@ impl DockArea { }, ); - self._subscriptions.push(subscription); + self.subscriptions.push(subscription); } /// Returns the ID of the dock area. diff --git a/crates/ui/src/dock_area/stack_panel.rs b/crates/ui/src/dock_area/stack_panel.rs index 784b2c7..a953208 100644 --- a/crates/ui/src/dock_area/stack_panel.rs +++ b/crates/ui/src/dock_area/stack_panel.rs @@ -25,7 +25,8 @@ pub struct StackPanel { focus_handle: FocusHandle, pub(crate) panels: SmallVec<[Arc; 2]>, panel_group: Entity, - _subscriptions: Vec, + #[allow(dead_code)] + subscriptions: Vec, } impl Panel for StackPanel { @@ -49,10 +50,11 @@ impl StackPanel { }); // Bubble up the resize event. - let _subscriptions = vec![cx - .subscribe(&panel_group, |_, _, _: &ResizablePanelEvent, cx| { - cx.emit(PanelEvent::LayoutChanged) - })]; + let subscriptions = vec![cx.subscribe_in( + &panel_group, + window, + |_, _, _: &ResizablePanelEvent, _, cx| cx.emit(PanelEvent::LayoutChanged), + )]; Self { axis, @@ -60,7 +62,7 @@ impl StackPanel { focus_handle: cx.focus_handle(), panels: SmallVec::new(), panel_group, - _subscriptions, + subscriptions, } } @@ -125,6 +127,7 @@ impl StackPanel { ); } + #[allow(clippy::too_many_arguments)] pub fn insert_panel_at( &mut self, panel: Arc, diff --git a/crates/ui/src/dock_area/tab_panel.rs b/crates/ui/src/dock_area/tab_panel.rs index 214bbd0..9dd6c2f 100644 --- a/crates/ui/src/dock_area/tab_panel.rs +++ b/crates/ui/src/dock_area/tab_panel.rs @@ -698,6 +698,7 @@ impl TabPanel { .map(|panel| { v_flex() .id("tab-content") + .group("") .overflow_hidden() .flex_1() .p_1() @@ -785,7 +786,8 @@ impl TabPanel { // center to merge into the current tab self.will_split_placement = None; } - cx.notify() + + cx.notify(); } /// Handle the drop event when dragging a panel @@ -803,13 +805,11 @@ impl TabPanel { let is_same_tab = drag.tab_panel == cx.entity(); // If target is same tab, and it is only one panel, do nothing. - if is_same_tab && ix.is_none() { - #[allow(clippy::if_same_then_else)] - if self.will_split_placement.is_none() { - return; - } else if self.panels.len() == 1 { - return; - } + if is_same_tab + && ix.is_none() + && (self.will_split_placement.is_none() || self.panels.len() == 1) + { + return; } // Here is looks like remove_panel on a same item, but it difference. @@ -978,6 +978,7 @@ impl TabPanel { } else { cx.emit(PanelEvent::ZoomOut) } + self.is_zoomed = !self.is_zoomed; } @@ -1010,12 +1011,14 @@ impl EventEmitter for TabPanel {} impl Render for TabPanel { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl gpui::IntoElement { let focus_handle = self.focus_handle(cx); + let mut state = TabState { closeable: self.closeable(cx), draggable: self.draggable(cx), droppable: self.droppable(cx), zoomable: self.zoomable(cx), }; + if !state.draggable { state.closeable = false; } diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index e748f9e..1071c1c 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -225,6 +225,8 @@ pub enum DropdownEvent { Confirm(Option<::Value>), } +type Empty = Option AnyElement + 'static>>; + /// A Dropdown element. pub struct Dropdown { id: ElementId, @@ -236,7 +238,7 @@ pub struct Dropdown { placeholder: Option, title_prefix: Option, selected_value: Option<::Value>, - empty: Option AnyElement + 'static>>, + empty: Empty, width: Length, menu_width: Length, /// Store the bounds of the input @@ -479,7 +481,7 @@ where return; } self.list.focus_handle(cx).focus(window); - cx.dispatch_action(&list::SelectPrev); + window.dispatch_action(Box::new(list::SelectPrev), cx); } fn down(&mut self, _: &Down, window: &mut Window, cx: &mut Context) { @@ -488,7 +490,7 @@ where } self.list.focus_handle(cx).focus(window); - cx.dispatch_action(&list::SelectNext); + window.dispatch_action(Box::new(list::SelectNext), cx); } fn enter(&mut self, _: &Enter, window: &mut Window, cx: &mut Context) { @@ -500,7 +502,7 @@ where cx.notify(); } else { self.list.focus_handle(cx).focus(window); - cx.dispatch_action(&list::Confirm); + window.dispatch_action(Box::new(list::Confirm), cx); } } diff --git a/crates/ui/src/popup_menu.rs b/crates/ui/src/popup_menu.rs index f312b05..c9249a2 100644 --- a/crates/ui/src/popup_menu.rs +++ b/crates/ui/src/popup_menu.rs @@ -271,7 +271,7 @@ impl PopupMenu { window.focus(handle); } - cx.dispatch_action(action.as_ref()); + window.dispatch_action(action.boxed_clone(), cx); }) }