Simplify state interactions

This commit is contained in:
Marshall Bowers 2023-10-11 12:32:05 -04:00
parent 922d1462a8
commit 7478e63ea0
2 changed files with 43 additions and 69 deletions

View file

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

View file

@ -22,21 +22,20 @@ pub struct WorkspaceState {
} }
impl 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 { pub fn is_project_panel_open(&self) -> bool {
self.show_project_panel.load(Ordering::SeqCst) self.show_project_panel.load(Ordering::SeqCst)
} }
pub fn toggle_project_panel(&self) { pub fn toggle_project_panel(&self) {
let is_showing_project_panel = self.show_project_panel.load(Ordering::SeqCst); Self::toggle_value(&self.show_project_panel);
self.show_project_panel
.compare_exchange(
is_showing_project_panel,
!is_showing_project_panel,
Ordering::SeqCst,
Ordering::SeqCst,
)
.unwrap();
self.show_collab_panel.store(false, Ordering::SeqCst); self.show_collab_panel.store(false, Ordering::SeqCst);
} }
@ -46,19 +45,34 @@ impl WorkspaceState {
} }
pub fn toggle_collab_panel(&self) { pub fn toggle_collab_panel(&self) {
let is_showing_collab_panel = self.show_collab_panel.load(Ordering::SeqCst); Self::toggle_value(&self.show_collab_panel);
self.show_collab_panel
.compare_exchange(
is_showing_collab_panel,
!is_showing_collab_panel,
Ordering::SeqCst,
Ordering::SeqCst,
)
.unwrap();
self.show_project_panel.store(false, Ordering::SeqCst); 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 /// HACK: This is just a temporary way to start hooking up interactivity until