Move settings subscription to dock

Conrad Irwin created

Reduces likelihood of panels being unable to move themselves

Change summary

crates/assistant/src/assistant_panel.rs    | 11 -----------
crates/collab_ui/src/chat_panel.rs         | 11 -----------
crates/collab_ui/src/collab_panel.rs       | 13 -------------
crates/project_panel/src/project_panel.rs  | 12 ------------
crates/terminal_view/src/terminal_panel.rs |  9 ---------
crates/workspace/src/dock.rs               | 19 +++++++++++++------
6 files changed, 13 insertions(+), 62 deletions(-)

Detailed changes

crates/assistant/src/assistant_panel.rs 🔗

@@ -192,17 +192,6 @@ impl AssistantPanel {
                         retrieve_context_in_next_inline_assist: false,
                     };
 
-                    let mut old_dock_position = this.position(cx);
-                    this.subscriptions =
-                        vec![cx.observe_global::<SettingsStore>(move |this, cx| {
-                            let new_dock_position = this.position(cx);
-                            if new_dock_position != old_dock_position {
-                                old_dock_position = new_dock_position;
-                                cx.emit(PanelEvent::ChangePosition);
-                            }
-                            cx.notify();
-                        })];
-
                     this
                 })
             })

crates/collab_ui/src/chat_panel.rs 🔗

@@ -127,17 +127,6 @@ impl ChatPanel {
                 open_context_menu: None,
             };
 
-            let mut old_dock_position = this.position(cx);
-            this.subscriptions.push(cx.observe_global::<SettingsStore>(
-                move |this: &mut Self, cx| {
-                    let new_dock_position = this.position(cx);
-                    if new_dock_position != old_dock_position {
-                        old_dock_position = new_dock_position;
-                        cx.emit(PanelEvent::ChangePosition);
-                    }
-                    cx.notify();
-                },
-            ));
             this.subscriptions.push(cx.subscribe(
                 &ActiveCall::global(cx),
                 move |this: &mut Self, call, event: &room::Event, cx| match event {

crates/collab_ui/src/collab_panel.rs 🔗

@@ -254,19 +254,6 @@ impl CollabPanel {
 
             this.update_entries(false, cx);
 
-            // Update the dock position when the setting changes.
-            let mut old_dock_position = this.position(cx);
-            this.subscriptions.push(cx.observe_global::<SettingsStore>(
-                move |this: &mut Self, cx| {
-                    let new_dock_position = this.position(cx);
-                    if new_dock_position != old_dock_position {
-                        old_dock_position = new_dock_position;
-                        cx.emit(PanelEvent::ChangePosition);
-                    }
-                    cx.notify();
-                },
-            ));
-
             let active_call = ActiveCall::global(cx);
             this.subscriptions
                 .push(cx.observe(&this.user_store, |this, _, cx| {

crates/project_panel/src/project_panel.rs 🔗

@@ -246,18 +246,6 @@ impl ProjectPanel {
             };
             this.update_visible_entries(None, cx);
 
-            // Update the dock position when the setting changes.
-            let mut old_dock_position = this.position(cx);
-            ProjectPanelSettings::register(cx);
-            cx.observe_global::<SettingsStore>(move |this, cx| {
-                let new_dock_position = this.position(cx);
-                if new_dock_position != old_dock_position {
-                    old_dock_position = new_dock_position;
-                    cx.emit(PanelEvent::ChangePosition);
-                }
-            })
-            .detach();
-
             this
         });
 

crates/terminal_view/src/terminal_panel.rs 🔗

@@ -159,15 +159,6 @@ impl TerminalPanel {
             height: None,
             _subscriptions: subscriptions,
         };
-        let mut old_dock_position = this.position(cx);
-        cx.observe_global::<SettingsStore>(move |this, cx| {
-            let new_dock_position = this.position(cx);
-            if new_dock_position != old_dock_position {
-                old_dock_position = new_dock_position;
-                cx.emit(PanelEvent::ChangePosition);
-            }
-        })
-        .detach();
         this
     }
 

crates/workspace/src/dock.rs 🔗

@@ -7,6 +7,7 @@ use gpui::{
 };
 use schemars::JsonSchema;
 use serde::{Deserialize, Serialize};
+use settings::SettingsStore;
 use std::sync::Arc;
 use ui::{h_stack, ContextMenu, IconButton, Tooltip};
 use ui::{prelude::*, right_click_menu};
@@ -14,7 +15,6 @@ use ui::{prelude::*, right_click_menu};
 const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.);
 
 pub enum PanelEvent {
-    ChangePosition,
     ZoomIn,
     ZoomOut,
     Activate,
@@ -177,7 +177,7 @@ impl DockPosition {
 
 struct PanelEntry {
     panel: Arc<dyn PanelHandle>,
-    _subscriptions: [Subscription; 2],
+    _subscriptions: [Subscription; 3],
 }
 
 pub struct PanelButtons {
@@ -321,9 +321,15 @@ impl Dock {
     ) {
         let subscriptions = [
             cx.observe(&panel, |_, _, cx| cx.notify()),
-            cx.subscribe(&panel, move |this, panel, event, cx| match event {
-                PanelEvent::ChangePosition => {
+            cx.observe_global::<SettingsStore>({
+                let workspace = workspace.clone();
+                let panel = panel.clone();
+
+                move |this, cx| {
                     let new_position = panel.read(cx).position(cx);
+                    if new_position == this.position {
+                        return;
+                    }
 
                     let Ok(new_dock) = workspace.update(cx, |workspace, cx| {
                         if panel.is_zoomed(cx) {
@@ -354,6 +360,8 @@ impl Dock {
                         }
                     });
                 }
+            }),
+            cx.subscribe(&panel, move |this, panel, event, cx| match event {
                 PanelEvent::ZoomIn => {
                     this.set_panel_zoomed(&panel.to_any(), true, cx);
                     if !panel.focus_handle(cx).contains_focused(cx) {
@@ -735,9 +743,8 @@ pub mod test {
             true
         }
 
-        fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
+        fn set_position(&mut self, position: DockPosition, _: &mut ViewContext<Self>) {
             self.position = position;
-            cx.emit(PanelEvent::ChangePosition);
         }
 
         fn size(&self, _: &WindowContext) -> Pixels {