chore: Update to use WeakView for avoid dependency issue. (#391)

This commit is contained in:
Jason Lee 2024-11-04 16:36:21 +08:00 committed by GitHub
parent 19f6b82b61
commit ca44ca6311
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 28 deletions

View file

@ -20,7 +20,7 @@ use gpui::{
use smallvec::SmallVec;
pub struct StackPanel {
pub(super) parent: Option<View<StackPanel>>,
pub(super) parent: Option<WeakView<StackPanel>>,
pub(super) axis: Axis,
focus_handle: FocusHandle,
pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>,
@ -180,9 +180,11 @@ impl StackPanel {
move |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));
tab_panel.update(cx, |tab_panel, _| tab_panel.set_parent(view.downgrade()));
} else if let Ok(stack_panel) = panel.view().downcast::<Self>() {
stack_panel.update(cx, |stack_panel, _| stack_panel.parent = Some(view));
stack_panel.update(cx, |stack_panel, _| {
stack_panel.parent = Some(view.downgrade())
});
}
// Subscribe to the panel's layout change event.
@ -258,7 +260,7 @@ impl StackPanel {
let view = cx.view().clone();
if let Some(parent) = self.parent.as_ref() {
parent.update(cx, |parent, cx| {
_ = parent.update(cx, |parent, cx| {
parent.remove_panel(Arc::new(view.clone()), cx);
});
}
@ -291,8 +293,8 @@ impl StackPanel {
) -> bool {
let first_panel = self.panels.first();
if let Some(parent) = &self.parent {
if check_parent {
if check_parent {
if let Some(parent) = self.parent.as_ref().and_then(|parent| parent.upgrade()) {
return parent.read(cx).is_top_left_panel(panel, true, cx);
}
}
@ -320,8 +322,8 @@ impl StackPanel {
self.panels.last()
};
if let Some(parent) = &self.parent {
if check_parent {
if check_parent {
if let Some(parent) = self.parent.as_ref().and_then(|parent| parent.upgrade()) {
return parent.read(cx).is_top_right_panel(panel, true, cx);
}
}

View file

@ -60,7 +60,7 @@ pub struct TabPanel {
focus_handle: FocusHandle,
dock_area: WeakView<DockArea>,
/// The stock_panel can be None, if is None, that means the panels can't be split or move
stack_panel: Option<View<StackPanel>>,
stack_panel: Option<WeakView<StackPanel>>,
pub(crate) panels: Vec<Arc<dyn PanelView>>,
pub(crate) active_ix: usize,
/// If this is true, the Panel closeable will follow the active panel's closeable,
@ -128,7 +128,7 @@ impl Panel for TabPanel {
impl TabPanel {
pub fn new(
stack_panel: Option<View<StackPanel>>,
stack_panel: Option<WeakView<StackPanel>>,
dock_area: WeakView<DockArea>,
cx: &mut ViewContext<Self>,
) -> Self {
@ -146,8 +146,8 @@ impl TabPanel {
}
}
pub(super) fn set_parent(&mut self, parent: View<StackPanel>) {
self.stack_panel = Some(parent);
pub(super) fn set_parent(&mut self, view: WeakView<StackPanel>) {
self.stack_panel = Some(view);
}
/// Return current active_panel View
@ -253,9 +253,9 @@ impl TabPanel {
let tab_view = cx.view().clone();
if let Some(stack_panel) = self.stack_panel.as_ref() {
stack_panel.update(cx, |view, cx| {
_ = stack_panel.update(cx, |view, cx| {
view.remove_panel(Arc::new(tab_view), cx);
})
});
}
}
@ -365,8 +365,11 @@ impl TabPanel {
if self_is_left_dock || self_is_right_dock || self_is_bottom_dock {
return None;
}
if let Some(parent) = self.stack_panel.as_ref() {
if let Some(parent) = self
.stack_panel
.as_ref()
.and_then(|parent| parent.upgrade())
{
if !parent
.read(cx)
.is_top_left_panel(cx.view().clone(), true, cx)
@ -384,7 +387,11 @@ impl TabPanel {
return None;
}
if let Some(parent) = self.stack_panel.as_ref() {
if let Some(parent) = self
.stack_panel
.as_ref()
.and_then(|parent| parent.upgrade())
{
if !parent
.read(cx)
.is_top_right_panel(cx.view().clone(), true, cx)
@ -749,7 +756,11 @@ impl TabPanel {
view.add_panel(panel, cx);
});
let stack_panel = self.stack_panel.as_ref().unwrap();
let stack_panel = match self.stack_panel.as_ref().and_then(|panel| panel.upgrade()) {
Some(panel) => panel,
None => return,
};
let parent_axis = stack_panel.read(cx).axis;
let ix = stack_panel
@ -796,7 +807,7 @@ impl TabPanel {
} else {
cx.new_view(|cx| {
let mut panel = StackPanel::new(placement.axis(), cx);
panel.parent = Some(stack_panel.clone());
panel.parent = Some(stack_panel.downgrade());
panel
})
};
@ -812,7 +823,7 @@ impl TabPanel {
}
});
if *stack_panel != new_stack_panel {
if stack_panel != new_stack_panel {
stack_panel.update(cx, |view, cx| {
view.replace_panel(Arc::new(tab_panel.clone()), new_stack_panel.clone(), cx);
});

View file

@ -4,7 +4,7 @@ use gpui::{
canvas, div, prelude::FluentBuilder, px, relative, Along, AnyElement, AnyView, Axis, Bounds,
Element, Entity, EntityId, EventEmitter, IntoElement, IsZero, MouseMoveEvent, MouseUpEvent,
ParentElement, Pixels, Render, StatefulInteractiveElement as _, Style, Styled, View,
ViewContext, VisualContext as _, WindowContext,
ViewContext, VisualContext as _, WeakView, WindowContext,
};
use crate::{h_flex, v_flex, AxisExt};
@ -96,7 +96,7 @@ impl ResizablePanelGroup {
pub fn add_child(&mut self, panel: ResizablePanel, cx: &mut ViewContext<Self>) {
let mut panel = panel;
panel.axis = self.axis;
panel.group = Some(cx.view().clone());
panel.group = Some(cx.view().downgrade());
self.sizes.push(panel.initial_size.unwrap_or_default());
self.panels.push(cx.new_view(|_| panel));
}
@ -104,7 +104,7 @@ impl ResizablePanelGroup {
pub fn insert_child(&mut self, panel: ResizablePanel, ix: usize, cx: &mut ViewContext<Self>) {
let mut panel = panel;
panel.axis = self.axis;
panel.group = Some(cx.view().clone());
panel.group = Some(cx.view().downgrade());
self.sizes
.insert(ix, panel.initial_size.unwrap_or_default());
@ -128,7 +128,7 @@ impl ResizablePanelGroup {
panel.initial_size = old_panel_initial_size;
panel.size_ratio = old_panel_size_ratio;
panel.axis = self.axis;
panel.group = Some(cx.view().clone());
panel.group = Some(cx.view().downgrade());
self.sizes[ix] = panel.initial_size.unwrap_or_default();
self.panels[ix] = cx.new_view(|_| panel);
cx.notify()
@ -275,7 +275,7 @@ impl Render for ResizablePanelGroup {
}
pub struct ResizablePanel {
group: Option<View<ResizablePanelGroup>>,
group: Option<WeakView<ResizablePanelGroup>>,
/// Initial size is the size that the panel has when it is created.
initial_size: Option<Pixels>,
/// size is the size that the panel has when it is resized or adjusted by flex layout.
@ -332,7 +332,7 @@ impl ResizablePanel {
let panel_view = cx.view().clone();
if let Some(group) = self.group.as_ref() {
group.update(cx, |view, _| {
_ = group.update(cx, |view, _| {
if let Some(ix) = view
.panels
.iter()
@ -340,7 +340,7 @@ impl ResizablePanel {
{
view.sizes[ix] = new_size;
}
})
});
}
cx.notify();
}
@ -351,7 +351,11 @@ impl FluentBuilder for ResizablePanel {}
impl Render for ResizablePanel {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let view = cx.view().clone();
let total_size = self.group.as_ref().map(|group| group.read(cx).total_size());
let total_size = self
.group
.as_ref()
.and_then(|group| group.upgrade())
.map(|group| group.read(cx).total_size());
div()
.flex()