wip: refactor

This commit is contained in:
2025-02-01 14:54:53 +07:00
parent 944d3b9e46
commit 09a0d089bc
6 changed files with 34 additions and 61 deletions

View File

@@ -140,44 +140,6 @@ impl Dock {
cx.notify(); cx.notify();
} }
pub(super) fn from_state(
dock_area: WeakEntity<DockArea>,
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( fn subscribe_panel_events(
dock_area: WeakEntity<DockArea>, dock_area: WeakEntity<DockArea>,
panel: &DockItem, panel: &DockItem,
@@ -315,6 +277,7 @@ impl Dock {
cx.new(|_| info.clone()) cx.new(|_| info.clone())
}) })
} }
fn resize( fn resize(
&mut self, &mut self,
mouse_position: Point<Pixels>, mouse_position: Point<Pixels>,

View File

@@ -54,7 +54,7 @@ pub struct DockArea {
/// 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,
_subscriptions: Vec<Subscription>, subscriptions: Vec<Subscription>,
} }
/// DockItem is a tree structure that represents the layout of the dock. /// DockItem is a tree structure that represents the layout of the dock.
@@ -316,7 +316,7 @@ impl DockArea {
bottom_dock: None, bottom_dock: None,
is_locked: false, is_locked: false,
panel_style: PanelStyle::Default, panel_style: PanelStyle::Default,
_subscriptions: vec![], subscriptions: vec![],
}; };
this.subscribe_panel(&stack_panel, window, cx); this.subscribe_panel(&stack_panel, window, cx);
@@ -535,6 +535,7 @@ impl DockArea {
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
let weak_self = cx.entity().downgrade(); let weak_self = cx.entity().downgrade();
match placement { match placement {
DockPlacement::Left => { DockPlacement::Left => {
if let Some(dock) = self.left_dock.as_ref() { if let Some(dock) = self.left_dock.as_ref() {
@@ -590,7 +591,7 @@ impl DockArea {
self.subscribe_item(item, window, cx); self.subscribe_item(item, window, cx);
} }
self._subscriptions.push(cx.subscribe_in( self.subscriptions.push(cx.subscribe_in(
view, view,
window, window,
move |_, _, event, window, cx| { move |_, _, event, window, cx| {
@@ -601,6 +602,7 @@ impl DockArea {
}); });
}) })
.detach(); .detach();
cx.emit(DockEvent::LayoutChanged); 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. /// Returns the ID of the dock area.

View File

@@ -25,7 +25,8 @@ pub struct StackPanel {
focus_handle: FocusHandle, focus_handle: FocusHandle,
pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>, pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>,
panel_group: Entity<ResizablePanelGroup>, panel_group: Entity<ResizablePanelGroup>,
_subscriptions: Vec<Subscription>, #[allow(dead_code)]
subscriptions: Vec<Subscription>,
} }
impl Panel for StackPanel { impl Panel for StackPanel {
@@ -49,10 +50,11 @@ impl StackPanel {
}); });
// Bubble up the resize event. // Bubble up the resize event.
let _subscriptions = vec![cx let subscriptions = vec![cx.subscribe_in(
.subscribe(&panel_group, |_, _, _: &ResizablePanelEvent, cx| { &panel_group,
cx.emit(PanelEvent::LayoutChanged) window,
})]; |_, _, _: &ResizablePanelEvent, _, cx| cx.emit(PanelEvent::LayoutChanged),
)];
Self { Self {
axis, axis,
@@ -60,7 +62,7 @@ impl StackPanel {
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
panels: SmallVec::new(), panels: SmallVec::new(),
panel_group, panel_group,
_subscriptions, subscriptions,
} }
} }
@@ -125,6 +127,7 @@ impl StackPanel {
); );
} }
#[allow(clippy::too_many_arguments)]
pub fn insert_panel_at( pub fn insert_panel_at(
&mut self, &mut self,
panel: Arc<dyn PanelView>, panel: Arc<dyn PanelView>,

View File

@@ -698,6 +698,7 @@ impl TabPanel {
.map(|panel| { .map(|panel| {
v_flex() v_flex()
.id("tab-content") .id("tab-content")
.group("")
.overflow_hidden() .overflow_hidden()
.flex_1() .flex_1()
.p_1() .p_1()
@@ -785,7 +786,8 @@ impl TabPanel {
// center to merge into the current tab // center to merge into the current tab
self.will_split_placement = None; self.will_split_placement = None;
} }
cx.notify()
cx.notify();
} }
/// Handle the drop event when dragging a panel /// Handle the drop event when dragging a panel
@@ -803,13 +805,11 @@ impl TabPanel {
let is_same_tab = drag.tab_panel == cx.entity(); let is_same_tab = drag.tab_panel == cx.entity();
// If target is same tab, and it is only one panel, do nothing. // If target is same tab, and it is only one panel, do nothing.
if is_same_tab && ix.is_none() { if is_same_tab
#[allow(clippy::if_same_then_else)] && ix.is_none()
if self.will_split_placement.is_none() { && (self.will_split_placement.is_none() || self.panels.len() == 1)
{
return; return;
} else if self.panels.len() == 1 {
return;
}
} }
// Here is looks like remove_panel on a same item, but it difference. // Here is looks like remove_panel on a same item, but it difference.
@@ -978,6 +978,7 @@ impl TabPanel {
} else { } else {
cx.emit(PanelEvent::ZoomOut) cx.emit(PanelEvent::ZoomOut)
} }
self.is_zoomed = !self.is_zoomed; self.is_zoomed = !self.is_zoomed;
} }
@@ -1010,12 +1011,14 @@ impl EventEmitter<PanelEvent> for TabPanel {}
impl Render for TabPanel { impl Render for TabPanel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl gpui::IntoElement { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl gpui::IntoElement {
let focus_handle = self.focus_handle(cx); let focus_handle = self.focus_handle(cx);
let mut state = TabState { let mut state = TabState {
closeable: self.closeable(cx), closeable: self.closeable(cx),
draggable: self.draggable(cx), draggable: self.draggable(cx),
droppable: self.droppable(cx), droppable: self.droppable(cx),
zoomable: self.zoomable(cx), zoomable: self.zoomable(cx),
}; };
if !state.draggable { if !state.draggable {
state.closeable = false; state.closeable = false;
} }

View File

@@ -225,6 +225,8 @@ pub enum DropdownEvent<D: DropdownDelegate + 'static> {
Confirm(Option<<D::Item as DropdownItem>::Value>), Confirm(Option<<D::Item as DropdownItem>::Value>),
} }
type Empty = Option<Box<dyn Fn(&Window, &App) -> AnyElement + 'static>>;
/// A Dropdown element. /// A Dropdown element.
pub struct Dropdown<D: DropdownDelegate + 'static> { pub struct Dropdown<D: DropdownDelegate + 'static> {
id: ElementId, id: ElementId,
@@ -236,7 +238,7 @@ pub struct Dropdown<D: DropdownDelegate + 'static> {
placeholder: Option<SharedString>, placeholder: Option<SharedString>,
title_prefix: Option<SharedString>, title_prefix: Option<SharedString>,
selected_value: Option<<D::Item as DropdownItem>::Value>, selected_value: Option<<D::Item as DropdownItem>::Value>,
empty: Option<Box<dyn Fn(&Window, &App) -> AnyElement + 'static>>, empty: Empty,
width: Length, width: Length,
menu_width: Length, menu_width: Length,
/// Store the bounds of the input /// Store the bounds of the input
@@ -479,7 +481,7 @@ where
return; return;
} }
self.list.focus_handle(cx).focus(window); 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<Self>) { fn down(&mut self, _: &Down, window: &mut Window, cx: &mut Context<Self>) {
@@ -488,7 +490,7 @@ where
} }
self.list.focus_handle(cx).focus(window); 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<Self>) { fn enter(&mut self, _: &Enter, window: &mut Window, cx: &mut Context<Self>) {
@@ -500,7 +502,7 @@ where
cx.notify(); cx.notify();
} else { } else {
self.list.focus_handle(cx).focus(window); self.list.focus_handle(cx).focus(window);
cx.dispatch_action(&list::Confirm); window.dispatch_action(Box::new(list::Confirm), cx);
} }
} }

View File

@@ -271,7 +271,7 @@ impl PopupMenu {
window.focus(handle); window.focus(handle);
} }
cx.dispatch_action(action.as_ref()); window.dispatch_action(action.boxed_clone(), cx);
}) })
} }