Preserve panel size when re-docking between left and right

This commit is contained in:
Nathan Sobo 2023-05-12 15:15:11 -06:00
parent 214354b4da
commit bd795d7607
6 changed files with 117 additions and 41 deletions

View file

@ -2,7 +2,8 @@ use crate::{StatusItemView, Workspace};
use context_menu::{ContextMenu, ContextMenuItem};
use gpui::{
elements::*, impl_actions, platform::CursorStyle, platform::MouseButton, AnyViewHandle,
AppContext, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext,
AppContext, Axis, Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
WindowContext,
};
use serde::Deserialize;
use settings::Settings;
@ -117,6 +118,13 @@ impl DockPosition {
Self::Right => HandleSide::Left,
}
}
pub fn axis(&self) -> Axis {
match self {
Self::Left | Self::Right => Axis::Horizontal,
Self::Bottom => Axis::Vertical,
}
}
}
struct PanelEntry {
@ -247,6 +255,23 @@ impl Dock {
}
}
pub fn panel_size(&self, panel: &dyn PanelHandle) -> Option<f32> {
self.panel_entries
.iter()
.find(|entry| entry.panel.id() == panel.id())
.map(|entry| entry.size)
}
pub fn resize_panel(&mut self, panel: &dyn PanelHandle, size: f32) {
let entry = self
.panel_entries
.iter_mut()
.find(|entry| entry.panel.id() == panel.id());
if let Some(entry) = entry {
entry.size = size;
}
}
pub fn active_panel_size(&self) -> Option<f32> {
if self.is_open {
self.panel_entries
@ -281,9 +306,13 @@ impl View for Dock {
ChildView::new(active_panel.as_any(), cx)
.contained()
.with_style(style.container)
.resizable(self.position.to_resize_handle_side(), size, |dock: &mut Self, size, cx| {
dock.resize_active_panel(size, cx);
})
.resizable(
self.position.to_resize_handle_side(),
size,
|dock: &mut Self, size, cx| {
dock.resize_active_panel(size, cx);
},
)
.into_any()
} else {
Empty::new().into_any()
@ -487,7 +516,10 @@ pub(crate) mod test {
}
fn default_size(&self, _: &WindowContext) -> f32 {
300.
match self.position.axis() {
Axis::Horizontal => 300.,
Axis::Vertical => 200.,
}
}
fn icon_path(&self) -> &'static str {

View file

@ -852,10 +852,18 @@ impl Workspace {
cx.subscribe(&panel, {
let mut dock = dock.clone();
let mut prev_position = panel.position(cx);
move |this, panel, event, cx| {
if T::should_change_position_on_event(event) {
let new_position = panel.read(cx).position(cx);
let mut was_visible = false;
let mut size = None;
dock.update(cx, |dock, cx| {
if new_position.axis() == prev_position.axis() {
size = dock.panel_size(&panel);
}
prev_position = new_position;
was_visible = dock.is_open()
&& dock
.active_panel()
@ -869,7 +877,11 @@ impl Workspace {
}
.clone();
dock.update(cx, |dock, cx| {
dock.add_panel(panel, cx);
dock.add_panel(panel.clone(), cx);
if let Some(size) = size {
dock.resize_panel(&panel, size);
}
if was_visible {
dock.set_open(true, cx);
dock.activate_panel(dock.panels_len() - 1, cx);
@ -3678,10 +3690,17 @@ mod tests {
.right_dock()
.update(cx, |right_dock, cx| right_dock.set_open(true, cx));
let left_dock = workspace.left_dock();
assert_eq!(
workspace.left_dock().read(cx).active_panel().unwrap().id(),
left_dock.read(cx).active_panel().unwrap().id(),
panel_1.id()
);
assert_eq!(
left_dock.read(cx).active_panel_size().unwrap(),
panel_1.default_size(cx)
);
left_dock.update(cx, |left_dock, cx| left_dock.resize_active_panel(1337., cx));
assert_eq!(
workspace.right_dock().read(cx).active_panel().unwrap().id(),
panel_2.id()
@ -3700,10 +3719,12 @@ mod tests {
// Since it was the only panel on the left, the left dock should now be closed.
assert!(!workspace.left_dock().read(cx).is_open());
assert!(workspace.left_dock().read(cx).active_panel().is_none());
let right_dock = workspace.right_dock();
assert_eq!(
workspace.right_dock().read(cx).active_panel().unwrap().id(),
right_dock.read(cx).active_panel().unwrap().id(),
panel_1.id()
);
assert_eq!(right_dock.read(cx).active_panel_size().unwrap(), 1337.);
// Now we move panel_2 to the left
panel_2.set_position(DockPosition::Left, cx);
@ -3727,13 +3748,33 @@ mod tests {
workspace.update(cx, |workspace, cx| {
// Since panel_1 was visible on the right, we open the left dock and make panel_1 active.
assert!(workspace.left_dock().read(cx).is_open());
let left_dock = workspace.left_dock();
assert!(left_dock.read(cx).is_open());
assert_eq!(
workspace.left_dock().read(cx).active_panel().unwrap().id(),
left_dock.read(cx).active_panel().unwrap().id(),
panel_1.id()
);
assert_eq!(left_dock.read(cx).active_panel_size().unwrap(), 1337.);
// And right the dock should be closed as it no longer has any panels.
assert!(!workspace.right_dock().read(cx).is_open());
// Now we move panel_1 to the bottom
panel_1.set_position(DockPosition::Bottom, cx);
});
workspace.update(cx, |workspace, cx| {
// Since panel_1 was visible on the left, we close the left dock.
assert!(!workspace.left_dock().read(cx).is_open());
// The bottom dock is sized based on the panel's default size,
// since the panel orientation changed from vertical to horizontal.
assert_eq!(
workspace
.bottom_dock()
.read(cx)
.active_panel_size()
.unwrap(),
panel_1.default_size(cx),
);
});
}
}