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

View File

@@ -54,7 +54,7 @@ pub struct DockArea {
/// The panel style, default is [`PanelStyle::Default`](PanelStyle::Default).
pub(crate) panel_style: PanelStyle,
_subscriptions: Vec<Subscription>,
subscriptions: Vec<Subscription>,
}
/// 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<Self>,
) {
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.

View File

@@ -25,7 +25,8 @@ pub struct StackPanel {
focus_handle: FocusHandle,
pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>,
panel_group: Entity<ResizablePanelGroup>,
_subscriptions: Vec<Subscription>,
#[allow(dead_code)]
subscriptions: Vec<Subscription>,
}
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<dyn PanelView>,

View File

@@ -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<PanelEvent> for TabPanel {}
impl Render for TabPanel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> 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;
}

View File

@@ -225,6 +225,8 @@ pub enum DropdownEvent<D: DropdownDelegate + 'static> {
Confirm(Option<<D::Item as DropdownItem>::Value>),
}
type Empty = Option<Box<dyn Fn(&Window, &App) -> AnyElement + 'static>>;
/// A Dropdown element.
pub struct Dropdown<D: DropdownDelegate + 'static> {
id: ElementId,
@@ -236,7 +238,7 @@ pub struct Dropdown<D: DropdownDelegate + 'static> {
placeholder: Option<SharedString>,
title_prefix: Option<SharedString>,
selected_value: Option<<D::Item as DropdownItem>::Value>,
empty: Option<Box<dyn Fn(&Window, &App) -> 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<Self>) {
@@ -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<Self>) {
@@ -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);
}
}

View File

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