dock: Improve set_parent in StackPanel insert_panel to use defer instead of spawn (#205)

This commit is contained in:
Jason Lee 2024-09-03 11:18:57 +08:00 committed by GitHub
parent 8ece86cd0c
commit 73e82c785a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 32 deletions

View file

@ -55,12 +55,9 @@ impl StoryWorkspace {
let dock_area = cx.new_view(|cx| DockArea::new("main-dock", stack_panel.clone(), cx));
let weak_dock_area = dock_area.downgrade();
let center_tab_panel =
cx.new_view(|cx| TabPanel::new(Some(stack_panel.clone()), weak_dock_area.clone(), cx));
let left_tab_panel =
cx.new_view(|cx| TabPanel::new(Some(stack_panel.clone()), weak_dock_area.clone(), cx));
let right_tab_panel =
cx.new_view(|cx| TabPanel::new(Some(stack_panel.clone()), weak_dock_area.clone(), cx));
let center_tab_panel = cx.new_view(|cx| TabPanel::new(weak_dock_area.clone(), cx));
let left_tab_panel = cx.new_view(|cx| TabPanel::new(weak_dock_area.clone(), cx));
let right_tab_panel = cx.new_view(|cx| TabPanel::new(weak_dock_area.clone(), cx));
stack_panel.update(cx, |view, cx| {
let left_stack_panel = cx.new_view(|cx| StackPanel::new(Axis::Vertical, cx));

View file

@ -191,26 +191,16 @@ impl StackPanel {
})
.detach();
cx.spawn(|view, mut cx| {
let panel = panel.clone();
async move {
if let Some(view) = view.upgrade() {
cx.update(|cx| {
// If the panel is a TabPanel, set its parent to this.
if let Ok(tab_panel) = panel.view().downcast::<TabPanel>() {
tab_panel.update(cx, |tab_panel, _| tab_panel.set_parent(view.clone()));
} else if let Ok(stack_panel) = panel.view().downcast::<Self>() {
stack_panel.update(cx, |stack_panel, _| {
stack_panel.parent = Some(view.clone())
});
}
})
} else {
Ok(())
}
let view = cx.view().clone();
let panel1 = panel.clone();
cx.window_context().defer(move |cx| {
// If the panel is a TabPanel, set its parent to this.
if let Ok(tab_panel) = panel1.view().downcast::<TabPanel>() {
tab_panel.update(cx, |tab_panel, _| tab_panel.set_parent(view));
} else if let Ok(stack_panel) = panel1.view().downcast::<Self>() {
stack_panel.update(cx, |stack_panel, _| stack_panel.parent = Some(view));
}
})
.detach();
});
let ix = if ix > self.panels.len() {
self.panels.len()

View file

@ -71,15 +71,11 @@ pub struct TabPanel {
}
impl TabPanel {
pub fn new(
stack_panel: Option<View<StackPanel>>,
dock_area: WeakView<DockArea>,
cx: &mut ViewContext<Self>,
) -> Self {
pub fn new(dock_area: WeakView<DockArea>, cx: &mut ViewContext<Self>) -> Self {
Self {
focus_handle: cx.focus_handle(),
dock_area,
stack_panel,
stack_panel: None,
panels: Vec::new(),
active_ix: 0,
tab_bar_scroll_handle: ScrollHandle::new(),
@ -455,7 +451,7 @@ impl TabPanel {
) {
let dock_area = self.dock_area.clone();
// wrap the panel in a TabPanel
let new_tab_panel = cx.new_view(|cx| Self::new(None, dock_area.clone(), cx));
let new_tab_panel = cx.new_view(|cx| Self::new(dock_area.clone(), cx));
new_tab_panel.update(cx, |view, cx| {
view.add_panel(panel, cx);
});