From d5f4f916beb66f755fc23044ac69149e4b282392 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 16 May 2024 14:17:38 -0700 Subject: [PATCH] feat(stackbar): make label configurable This commit introduces a new stackbar label configuration option backed by the StackbarLabel enum, which now has two variants, Process and Title. The state tracker for this option is kept in an AtomicCell, and the state tracker for StackbarMode has also been changed from an Arc> to an AtomicCell to match. resolve #826 --- komorebi-core/src/lib.rs | 10 ++++++++++ komorebi/src/container.rs | 6 +++--- komorebi/src/lib.rs | 5 ++++- komorebi/src/process_command.rs | 7 +++++-- komorebi/src/stackbar.rs | 14 +++++++++++--- komorebi/src/static_config.rs | 14 ++++++++++---- komorebi/src/window_manager.rs | 2 +- 7 files changed, 44 insertions(+), 14 deletions(-) diff --git a/komorebi-core/src/lib.rs b/komorebi-core/src/lib.rs index 3f243139..d5f94229 100644 --- a/komorebi-core/src/lib.rs +++ b/komorebi-core/src/lib.rs @@ -142,6 +142,7 @@ pub enum SocketMessage { BorderOffset(i32), InvisibleBorders(Rect), StackbarMode(StackbarMode), + StackbarLabel(StackbarLabel), StackbarFocusedTextColour(u32, u32, u32), StackbarUnfocusedTextColour(u32, u32, u32), StackbarBackgroundColour(u32, u32, u32), @@ -203,6 +204,15 @@ pub enum StackbarMode { OnStack, } +#[derive( + Debug, Copy, Default, Clone, Eq, PartialEq, Display, Serialize, Deserialize, JsonSchema, +)] +pub enum StackbarLabel { + #[default] + Process, + Title, +} + #[derive( Default, Copy, Clone, Debug, Eq, PartialEq, Display, Serialize, Deserialize, JsonSchema, )] diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 6dbd2c16..01f4c947 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -30,7 +30,7 @@ impl Default for Container { Self { id: nanoid!(), windows: Ring::default(), - stackbar: match *STACKBAR_MODE.lock() { + stackbar: match STACKBAR_MODE.load() { StackbarMode::Always => Stackbar::create().ok(), StackbarMode::Never | StackbarMode::OnStack => None, }, @@ -124,7 +124,7 @@ impl Container { pub fn remove_window_by_idx(&mut self, idx: usize) -> Option { let window = self.windows_mut().remove(idx); - if matches!(*STACKBAR_MODE.lock(), StackbarMode::OnStack) && self.windows().len() <= 1 { + if matches!(STACKBAR_MODE.load(), StackbarMode::OnStack) && self.windows().len() <= 1 { if let Some(stackbar) = &self.stackbar { let _ = WindowsApi::close_window(stackbar.hwnd()); self.stackbar = None; @@ -146,7 +146,7 @@ impl Container { pub fn add_window(&mut self, window: Window) { self.windows_mut().push_back(window); - if matches!(*STACKBAR_MODE.lock(), StackbarMode::OnStack) + if matches!(STACKBAR_MODE.load(), StackbarMode::OnStack) && self.windows().len() > 1 && self.stackbar.is_none() { diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index e53431eb..675c8bfa 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -51,6 +51,7 @@ pub use windows_api::WindowsApi; pub use windows_api::*; use color_eyre::Result; +use crossbeam_utils::atomic::AtomicCell; use komorebi_core::config_generation::IdWithIdentifier; use komorebi_core::config_generation::MatchingRule; use komorebi_core::config_generation::MatchingStrategy; @@ -58,6 +59,7 @@ use komorebi_core::ApplicationIdentifier; use komorebi_core::HidingBehaviour; use komorebi_core::Rect; use komorebi_core::SocketMessage; +use komorebi_core::StackbarLabel; use komorebi_core::StackbarMode; use os_info::Version; use parking_lot::Mutex; @@ -201,7 +203,6 @@ lazy_static! { // eg. Windows Terminal, IntelliJ IDEA, Firefox static ref NO_TITLEBAR: Arc>> = Arc::new(Mutex::new(vec![])); - static ref STACKBAR_MODE: Arc> = Arc::new(Mutex::new(StackbarMode::Never)); static ref WINDOWS_BY_BAR_HWNDS: Arc>>> = Arc::new(Mutex::new(HashMap::new())); @@ -223,6 +224,8 @@ pub static STACKBAR_UNFOCUSED_TEXT_COLOUR: AtomicU32 = AtomicU32::new(11776947); pub static STACKBAR_TAB_BACKGROUND_COLOUR: AtomicU32 = AtomicU32::new(3355443); // gray pub static STACKBAR_TAB_HEIGHT: AtomicI32 = AtomicI32::new(40); pub static STACKBAR_TAB_WIDTH: AtomicI32 = AtomicI32::new(200); +pub static STACKBAR_LABEL: AtomicCell = AtomicCell::new(StackbarLabel::Process); +pub static STACKBAR_MODE: AtomicCell = AtomicCell::new(StackbarMode::Never); #[must_use] pub fn current_virtual_desktop() -> Option> { diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 717a55e5..34a23b4e 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -65,6 +65,7 @@ use crate::NO_TITLEBAR; use crate::OBJECT_NAME_CHANGE_ON_LAUNCH; use crate::REMOVE_TITLEBARS; use crate::STACKBAR_FOCUSED_TEXT_COLOUR; +use crate::STACKBAR_LABEL; use crate::STACKBAR_MODE; use crate::STACKBAR_TAB_BACKGROUND_COLOUR; use crate::STACKBAR_TAB_HEIGHT; @@ -1246,8 +1247,7 @@ impl WindowManager { border_manager::BORDER_OFFSET.store(offset, Ordering::SeqCst); } SocketMessage::StackbarMode(mode) => { - let mut stackbar_mode = STACKBAR_MODE.lock(); - *stackbar_mode = mode; + STACKBAR_MODE.store(mode); for m in self.monitors_mut() { for w in m.workspaces_mut() { @@ -1257,6 +1257,9 @@ impl WindowManager { } } } + SocketMessage::StackbarLabel(label) => { + STACKBAR_LABEL.store(label); + } SocketMessage::StackbarFocusedTextColour(r, g, b) => { let rgb = Rgb::new(r, g, b); STACKBAR_FOCUSED_TEXT_COLOUR.store(rgb.into(), Ordering::SeqCst); diff --git a/komorebi/src/stackbar.rs b/komorebi/src/stackbar.rs index 71378e3a..5a5d0e16 100644 --- a/komorebi/src/stackbar.rs +++ b/komorebi/src/stackbar.rs @@ -55,8 +55,10 @@ use komorebi_core::Rect; use crate::window::Window; use crate::windows_api::WindowsApi; +use crate::StackbarLabel; use crate::DEFAULT_CONTAINER_PADDING; use crate::STACKBAR_FOCUSED_TEXT_COLOUR; +use crate::STACKBAR_LABEL; use crate::STACKBAR_TAB_BACKGROUND_COLOUR; use crate::STACKBAR_TAB_HEIGHT; use crate::STACKBAR_TAB_WIDTH; @@ -229,9 +231,15 @@ impl Stackbar { WindowsApi::round_rect(hdc, &tab_box, 8); - let exe = window.exe()?; - let exe_trimmed = exe.trim_end_matches(".exe"); - let mut tab_title: Vec = exe_trimmed.encode_utf16().collect(); + let label = match STACKBAR_LABEL.load() { + StackbarLabel::Process => { + let exe = window.exe()?; + exe.trim_end_matches(".exe").to_string() + } + StackbarLabel::Title => window.title()?, + }; + + let mut tab_title: Vec = label.encode_utf16().collect(); tab_box.left_padding(10); tab_box.right_padding(10); diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 3ce2e8ec..b1b33849 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -22,6 +22,7 @@ use crate::MONITOR_INDEX_PREFERENCES; use crate::OBJECT_NAME_CHANGE_ON_LAUNCH; use crate::REGEX_IDENTIFIERS; use crate::STACKBAR_FOCUSED_TEXT_COLOUR; +use crate::STACKBAR_LABEL; use crate::STACKBAR_MODE; use crate::STACKBAR_TAB_BACKGROUND_COLOUR; use crate::STACKBAR_TAB_HEIGHT; @@ -29,6 +30,7 @@ use crate::STACKBAR_TAB_WIDTH; use crate::STACKBAR_UNFOCUSED_TEXT_COLOUR; use crate::TRAY_AND_MULTI_WINDOW_IDENTIFIERS; use crate::WORKSPACE_RULES; +use komorebi_core::StackbarLabel; use komorebi_core::StackbarMode; use color_eyre::Result; @@ -330,11 +332,12 @@ pub struct TabsConfig { /// Tab background colour background: Option, } - #[derive(Debug, Serialize, Deserialize, JsonSchema)] pub struct StackbarConfig { /// Stackbar height pub height: Option, + /// Stackbar height + pub label: Option, /// Stackbar mode pub mode: Option, /// Stackbar tab configuration options @@ -540,9 +543,12 @@ impl StaticConfig { STACKBAR_TAB_HEIGHT.store(*height, Ordering::SeqCst); } + if let Some(label) = &stackbar.label { + STACKBAR_LABEL.store(*label); + } + if let Some(mode) = &stackbar.mode { - let mut stackbar_mode = STACKBAR_MODE.lock(); - *stackbar_mode = *mode; + STACKBAR_MODE.store(*mode); } if let Some(tabs) = &stackbar.tabs { @@ -747,7 +753,7 @@ impl StaticConfig { value.apply_globals()?; - let stackbar_mode = *STACKBAR_MODE.lock(); + let stackbar_mode = STACKBAR_MODE.load(); for m in wm.monitors_mut() { for w in m.workspaces_mut() { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 10baa6bc..06f43135 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -164,7 +164,7 @@ impl Default for GlobalState { border_style: *STYLE.lock(), border_offset: border_manager::BORDER_OFFSET.load(Ordering::SeqCst), border_width: border_manager::BORDER_WIDTH.load(Ordering::SeqCst), - stackbar_mode: *STACKBAR_MODE.lock(), + stackbar_mode: STACKBAR_MODE.load(), stackbar_focused_text_colour: Colour::Rgb(Rgb::from( STACKBAR_FOCUSED_TEXT_COLOUR.load(Ordering::SeqCst), )),