chore: Update to use WeakView for avoid dependency issue. (#391)
This commit is contained in:
parent
19f6b82b61
commit
ca44ca6311
3 changed files with 45 additions and 28 deletions
|
|
@ -20,7 +20,7 @@ use gpui::{
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
pub struct StackPanel {
|
pub struct StackPanel {
|
||||||
pub(super) parent: Option<View<StackPanel>>,
|
pub(super) parent: Option<WeakView<StackPanel>>,
|
||||||
pub(super) axis: Axis,
|
pub(super) axis: Axis,
|
||||||
focus_handle: FocusHandle,
|
focus_handle: FocusHandle,
|
||||||
pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>,
|
pub(crate) panels: SmallVec<[Arc<dyn PanelView>; 2]>,
|
||||||
|
|
@ -180,9 +180,11 @@ impl StackPanel {
|
||||||
move |cx| {
|
move |cx| {
|
||||||
// If the panel is a TabPanel, set its parent to this.
|
// If the panel is a TabPanel, set its parent to this.
|
||||||
if let Ok(tab_panel) = panel.view().downcast::<TabPanel>() {
|
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>() {
|
} 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.
|
// Subscribe to the panel's layout change event.
|
||||||
|
|
@ -258,7 +260,7 @@ impl StackPanel {
|
||||||
|
|
||||||
let view = cx.view().clone();
|
let view = cx.view().clone();
|
||||||
if let Some(parent) = self.parent.as_ref() {
|
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);
|
parent.remove_panel(Arc::new(view.clone()), cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -291,8 +293,8 @@ impl StackPanel {
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let first_panel = self.panels.first();
|
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);
|
return parent.read(cx).is_top_left_panel(panel, true, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -320,8 +322,8 @@ impl StackPanel {
|
||||||
self.panels.last()
|
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);
|
return parent.read(cx).is_top_right_panel(panel, true, cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ pub struct TabPanel {
|
||||||
focus_handle: FocusHandle,
|
focus_handle: FocusHandle,
|
||||||
dock_area: WeakView<DockArea>,
|
dock_area: WeakView<DockArea>,
|
||||||
/// The stock_panel can be None, if is None, that means the panels can't be split or move
|
/// 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) panels: Vec<Arc<dyn PanelView>>,
|
||||||
pub(crate) active_ix: usize,
|
pub(crate) active_ix: usize,
|
||||||
/// If this is true, the Panel closeable will follow the active panel's closeable,
|
/// If this is true, the Panel closeable will follow the active panel's closeable,
|
||||||
|
|
@ -128,7 +128,7 @@ impl Panel for TabPanel {
|
||||||
|
|
||||||
impl TabPanel {
|
impl TabPanel {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
stack_panel: Option<View<StackPanel>>,
|
stack_panel: Option<WeakView<StackPanel>>,
|
||||||
dock_area: WeakView<DockArea>,
|
dock_area: WeakView<DockArea>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
|
@ -146,8 +146,8 @@ impl TabPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn set_parent(&mut self, parent: View<StackPanel>) {
|
pub(super) fn set_parent(&mut self, view: WeakView<StackPanel>) {
|
||||||
self.stack_panel = Some(parent);
|
self.stack_panel = Some(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return current active_panel View
|
/// Return current active_panel View
|
||||||
|
|
@ -253,9 +253,9 @@ impl TabPanel {
|
||||||
|
|
||||||
let tab_view = cx.view().clone();
|
let tab_view = cx.view().clone();
|
||||||
if let Some(stack_panel) = self.stack_panel.as_ref() {
|
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);
|
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 {
|
if self_is_left_dock || self_is_right_dock || self_is_bottom_dock {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
if let Some(parent) = self
|
||||||
if let Some(parent) = self.stack_panel.as_ref() {
|
.stack_panel
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|parent| parent.upgrade())
|
||||||
|
{
|
||||||
if !parent
|
if !parent
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.is_top_left_panel(cx.view().clone(), true, cx)
|
.is_top_left_panel(cx.view().clone(), true, cx)
|
||||||
|
|
@ -384,7 +387,11 @@ impl TabPanel {
|
||||||
return None;
|
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
|
if !parent
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.is_top_right_panel(cx.view().clone(), true, cx)
|
.is_top_right_panel(cx.view().clone(), true, cx)
|
||||||
|
|
@ -749,7 +756,11 @@ impl TabPanel {
|
||||||
view.add_panel(panel, cx);
|
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 parent_axis = stack_panel.read(cx).axis;
|
||||||
|
|
||||||
let ix = stack_panel
|
let ix = stack_panel
|
||||||
|
|
@ -796,7 +807,7 @@ impl TabPanel {
|
||||||
} else {
|
} else {
|
||||||
cx.new_view(|cx| {
|
cx.new_view(|cx| {
|
||||||
let mut panel = StackPanel::new(placement.axis(), cx);
|
let mut panel = StackPanel::new(placement.axis(), cx);
|
||||||
panel.parent = Some(stack_panel.clone());
|
panel.parent = Some(stack_panel.downgrade());
|
||||||
panel
|
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| {
|
stack_panel.update(cx, |view, cx| {
|
||||||
view.replace_panel(Arc::new(tab_panel.clone()), new_stack_panel.clone(), cx);
|
view.replace_panel(Arc::new(tab_panel.clone()), new_stack_panel.clone(), cx);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use gpui::{
|
||||||
canvas, div, prelude::FluentBuilder, px, relative, Along, AnyElement, AnyView, Axis, Bounds,
|
canvas, div, prelude::FluentBuilder, px, relative, Along, AnyElement, AnyView, Axis, Bounds,
|
||||||
Element, Entity, EntityId, EventEmitter, IntoElement, IsZero, MouseMoveEvent, MouseUpEvent,
|
Element, Entity, EntityId, EventEmitter, IntoElement, IsZero, MouseMoveEvent, MouseUpEvent,
|
||||||
ParentElement, Pixels, Render, StatefulInteractiveElement as _, Style, Styled, View,
|
ParentElement, Pixels, Render, StatefulInteractiveElement as _, Style, Styled, View,
|
||||||
ViewContext, VisualContext as _, WindowContext,
|
ViewContext, VisualContext as _, WeakView, WindowContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{h_flex, v_flex, AxisExt};
|
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>) {
|
pub fn add_child(&mut self, panel: ResizablePanel, cx: &mut ViewContext<Self>) {
|
||||||
let mut panel = panel;
|
let mut panel = panel;
|
||||||
panel.axis = self.axis;
|
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.sizes.push(panel.initial_size.unwrap_or_default());
|
||||||
self.panels.push(cx.new_view(|_| panel));
|
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>) {
|
pub fn insert_child(&mut self, panel: ResizablePanel, ix: usize, cx: &mut ViewContext<Self>) {
|
||||||
let mut panel = panel;
|
let mut panel = panel;
|
||||||
panel.axis = self.axis;
|
panel.axis = self.axis;
|
||||||
panel.group = Some(cx.view().clone());
|
panel.group = Some(cx.view().downgrade());
|
||||||
|
|
||||||
self.sizes
|
self.sizes
|
||||||
.insert(ix, panel.initial_size.unwrap_or_default());
|
.insert(ix, panel.initial_size.unwrap_or_default());
|
||||||
|
|
@ -128,7 +128,7 @@ impl ResizablePanelGroup {
|
||||||
panel.initial_size = old_panel_initial_size;
|
panel.initial_size = old_panel_initial_size;
|
||||||
panel.size_ratio = old_panel_size_ratio;
|
panel.size_ratio = old_panel_size_ratio;
|
||||||
panel.axis = self.axis;
|
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.sizes[ix] = panel.initial_size.unwrap_or_default();
|
||||||
self.panels[ix] = cx.new_view(|_| panel);
|
self.panels[ix] = cx.new_view(|_| panel);
|
||||||
cx.notify()
|
cx.notify()
|
||||||
|
|
@ -275,7 +275,7 @@ impl Render for ResizablePanelGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResizablePanel {
|
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 is the size that the panel has when it is created.
|
||||||
initial_size: Option<Pixels>,
|
initial_size: Option<Pixels>,
|
||||||
/// size is the size that the panel has when it is resized or adjusted by flex layout.
|
/// 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();
|
let panel_view = cx.view().clone();
|
||||||
if let Some(group) = self.group.as_ref() {
|
if let Some(group) = self.group.as_ref() {
|
||||||
group.update(cx, |view, _| {
|
_ = group.update(cx, |view, _| {
|
||||||
if let Some(ix) = view
|
if let Some(ix) = view
|
||||||
.panels
|
.panels
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -340,7 +340,7 @@ impl ResizablePanel {
|
||||||
{
|
{
|
||||||
view.sizes[ix] = new_size;
|
view.sizes[ix] = new_size;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
@ -351,7 +351,11 @@ impl FluentBuilder for ResizablePanel {}
|
||||||
impl Render for ResizablePanel {
|
impl Render for ResizablePanel {
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
let view = cx.view().clone();
|
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()
|
div()
|
||||||
.flex()
|
.flex()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue