Simplify state interactions

Marshall Bowers created

Change summary

crates/ui2/src/components/status_bar.rs | 58 ++++----------------------
crates/ui2/src/components/workspace.rs  | 54 +++++++++++++++---------
2 files changed, 43 insertions(+), 69 deletions(-)

Detailed changes

crates/ui2/src/components/status_bar.rs 🔗

@@ -1,5 +1,4 @@
 use std::marker::PhantomData;
-use std::sync::atomic::Ordering;
 use std::sync::Arc;
 
 use crate::prelude::*;
@@ -144,20 +143,7 @@ impl<S: 'static + Send + Sync + Clone> StatusBar<S> {
                     .gap_1()
                     .child(Button::new("116:25"))
                     .child(Button::new("Rust").on_click(Arc::new(|_, cx| {
-                        let is_showing_language_selector = workspace_state
-                            .show_language_selector
-                            .load(Ordering::SeqCst);
-
-                        workspace_state
-                            .show_language_selector
-                            .compare_exchange(
-                                is_showing_language_selector,
-                                !is_showing_language_selector,
-                                Ordering::SeqCst,
-                                Ordering::SeqCst,
-                            )
-                            .unwrap();
-
+                        workspace_state.toggle_language_selector();
                         cx.notify();
                     }))),
             )
@@ -184,47 +170,21 @@ impl<S: 'static + Send + Sync + Clone> StatusBar<S> {
                     .gap_1()
                     .child(
                         IconButton::new(Icon::Terminal)
-                            .when(
-                                workspace_state.show_terminal.load(Ordering::SeqCst),
-                                |this| this.color(IconColor::Accent),
-                            )
+                            .when(workspace_state.is_terminal_open(), |this| {
+                                this.color(IconColor::Accent)
+                            })
                             .on_click(|_, cx| {
-                                let is_showing_terminal =
-                                    workspace_state.show_terminal.load(Ordering::SeqCst);
-
-                                workspace_state
-                                    .show_terminal
-                                    .compare_exchange(
-                                        is_showing_terminal,
-                                        !is_showing_terminal,
-                                        Ordering::SeqCst,
-                                        Ordering::SeqCst,
-                                    )
-                                    .unwrap();
-
+                                workspace_state.toggle_terminal();
                                 cx.notify();
                             }),
                     )
                     .child(
                         IconButton::new(Icon::MessageBubbles)
-                            .when(
-                                workspace_state.show_chat_panel.load(Ordering::SeqCst),
-                                |this| this.color(IconColor::Accent),
-                            )
+                            .when(workspace_state.is_chat_panel_open(), |this| {
+                                this.color(IconColor::Accent)
+                            })
                             .on_click(|_, cx| {
-                                let is_showing_chat_panel =
-                                    workspace_state.show_chat_panel.load(Ordering::SeqCst);
-
-                                workspace_state
-                                    .show_chat_panel
-                                    .compare_exchange(
-                                        is_showing_chat_panel,
-                                        !is_showing_chat_panel,
-                                        Ordering::SeqCst,
-                                        Ordering::SeqCst,
-                                    )
-                                    .unwrap();
-
+                                workspace_state.toggle_chat_panel();
                                 cx.notify();
                             }),
                     )

crates/ui2/src/components/workspace.rs 🔗

@@ -22,21 +22,20 @@ pub struct WorkspaceState {
 }
 
 impl WorkspaceState {
+    fn toggle_value(current_value: &AtomicBool) {
+        let value = current_value.load(Ordering::SeqCst);
+
+        current_value
+            .compare_exchange(value, !value, Ordering::SeqCst, Ordering::SeqCst)
+            .unwrap();
+    }
+
     pub fn is_project_panel_open(&self) -> bool {
         self.show_project_panel.load(Ordering::SeqCst)
     }
 
     pub fn toggle_project_panel(&self) {
-        let is_showing_project_panel = self.show_project_panel.load(Ordering::SeqCst);
-
-        self.show_project_panel
-            .compare_exchange(
-                is_showing_project_panel,
-                !is_showing_project_panel,
-                Ordering::SeqCst,
-                Ordering::SeqCst,
-            )
-            .unwrap();
+        Self::toggle_value(&self.show_project_panel);
 
         self.show_collab_panel.store(false, Ordering::SeqCst);
     }
@@ -46,19 +45,34 @@ impl WorkspaceState {
     }
 
     pub fn toggle_collab_panel(&self) {
-        let is_showing_collab_panel = self.show_collab_panel.load(Ordering::SeqCst);
-
-        self.show_collab_panel
-            .compare_exchange(
-                is_showing_collab_panel,
-                !is_showing_collab_panel,
-                Ordering::SeqCst,
-                Ordering::SeqCst,
-            )
-            .unwrap();
+        Self::toggle_value(&self.show_collab_panel);
 
         self.show_project_panel.store(false, Ordering::SeqCst);
     }
+
+    pub fn is_terminal_open(&self) -> bool {
+        self.show_terminal.load(Ordering::SeqCst)
+    }
+
+    pub fn toggle_terminal(&self) {
+        Self::toggle_value(&self.show_terminal);
+    }
+
+    pub fn is_chat_panel_open(&self) -> bool {
+        self.show_chat_panel.load(Ordering::SeqCst)
+    }
+
+    pub fn toggle_chat_panel(&self) {
+        Self::toggle_value(&self.show_chat_panel);
+    }
+
+    pub fn is_language_selector_open(&self) -> bool {
+        self.show_language_selector.load(Ordering::SeqCst)
+    }
+
+    pub fn toggle_language_selector(&self) {
+        Self::toggle_value(&self.show_language_selector);
+    }
 }
 
 /// HACK: This is just a temporary way to start hooking up interactivity until