Don't snap back to old position when resizing drawer

Eric Holk created

Change summary

crates/agent_ui/src/agent_panel.rs |  9 ++++++++-
crates/workspace/src/workspace.rs  | 32 ++++++++++++++++----------------
crates/zed/src/zed.rs              |  2 ++
3 files changed, 26 insertions(+), 17 deletions(-)

Detailed changes

crates/agent_ui/src/agent_panel.rs 🔗

@@ -787,7 +787,10 @@ impl AgentPanel {
             return;
         };
 
-        let width = self.width;
+        let width = self
+            .workspace
+            .upgrade()
+            .and_then(|workspace| workspace.read(cx).drawer_width::<AgentPanel>());
         let selected_agent_type = self.selected_agent_type.clone();
         let start_thread_in = Some(self.start_thread_in);
 
@@ -1188,6 +1191,10 @@ impl AgentPanel {
         &self.context_server_registry
     }
 
+    pub fn width(&self) -> Option<Pixels> {
+        self.width
+    }
+
     pub fn is_visible(workspace: &Entity<Workspace>, cx: &App) -> bool {
         workspace.read(cx).drawer_is_open::<Self>()
     }

crates/workspace/src/workspace.rs 🔗

@@ -54,9 +54,9 @@ use gpui::{
     Action, AnyEntity, AnyView, AnyWeakView, App, AsyncApp, AsyncWindowContext, Bounds, Context,
     CursorStyle, Decorations, DragMoveEvent, Entity, EntityId, EventEmitter, FocusHandle,
     Focusable, Global, HitboxBehavior, Hsla, KeyContext, Keystroke, ManagedView, MouseButton,
-    MouseUpEvent, PathPromptOptions, Point, PromptLevel, Render, ResizeEdge, Size, Stateful,
-    Subscription, SystemWindowTabController, Task, Tiling, WeakEntity, WindowBounds, WindowHandle,
-    WindowId, WindowOptions, actions, canvas, deferred, point, relative, size, transparent_black,
+    PathPromptOptions, Point, PromptLevel, Render, ResizeEdge, Size, Stateful, Subscription,
+    SystemWindowTabController, Task, Tiling, WeakEntity, WindowBounds, WindowHandle, WindowId,
+    WindowOptions, actions, canvas, deferred, point, relative, size, transparent_black,
 };
 pub use history_manager::*;
 pub use item::{
@@ -7176,6 +7176,18 @@ impl Workspace {
         }
     }
 
+    pub fn drawer_width<T: 'static>(&self) -> Option<Pixels> {
+        self.drawer_ref::<T>()
+            .and_then(|(_, drawer)| drawer.custom_width)
+    }
+
+    pub fn set_drawer_width<T: 'static>(&mut self, width: Option<Pixels>, cx: &mut Context<Self>) {
+        if let Some((_, drawer)) = self.drawer_mut::<T>() {
+            drawer.custom_width = width;
+            cx.notify();
+        }
+    }
+
     pub fn drawer_is_open<T: 'static>(&self) -> bool {
         if let Some((_, drawer)) = self.drawer_ref::<T>() {
             drawer.open
@@ -7290,19 +7302,6 @@ impl Workspace {
                 .on_mouse_down(MouseButton::Left, |_, _, cx| {
                     cx.stop_propagation();
                 })
-                .on_mouse_up(
-                    MouseButton::Left,
-                    cx.listener(move |workspace, _: &MouseUpEvent, _, cx| {
-                        let drawer = match position {
-                            DrawerPosition::Left => &mut workspace.left_drawer,
-                            DrawerPosition::Right => &mut workspace.right_drawer,
-                        };
-                        if let Some(drawer) = drawer {
-                            drawer.custom_width = None;
-                            cx.notify();
-                        }
-                    }),
-                )
                 .occlude();
             match position {
                 DrawerPosition::Left => deferred(
@@ -8182,6 +8181,7 @@ impl Render for Workspace {
                                                 );
                                             }
                                         }
+                                        workspace.serialize_workspace(window, cx);
                                     },
                                 ))
                             })

crates/zed/src/zed.rs 🔗

@@ -688,6 +688,7 @@ fn setup_or_teardown_ai_panels(
                     let position = PanelHandle::position(&threads_panel, window, cx);
                     if !disable_ai && !have_panel {
                         workspace.add_panel(threads_panel, window, cx);
+                        let width = agent_drawer.read(cx).width();
                         match position {
                             workspace::dock::DockPosition::Left => {
                                 workspace.set_left_drawer(agent_drawer, cx)
@@ -699,6 +700,7 @@ fn setup_or_teardown_ai_panels(
                                 unreachable!("drawers cannot go on the bottom")
                             }
                         }
+                        workspace.set_drawer_width::<agent_ui::AgentPanel>(width, cx);
                     }
                 })
             });