Skip to content

Commit

Permalink
feat(stackbar): make label configurable
Browse files Browse the repository at this point in the history
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<Mutex<T>> to an AtomicCell to match.

resolve #826
  • Loading branch information
LGUG2Z committed May 16, 2024
1 parent 77fc397 commit d5f4f91
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
10 changes: 10 additions & 0 deletions komorebi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
)]
Expand Down
6 changes: 3 additions & 3 deletions komorebi/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Container {
pub fn remove_window_by_idx(&mut self, idx: usize) -> Option<Window> {
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;
Expand All @@ -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()
{
Expand Down
5 changes: 4 additions & 1 deletion komorebi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ 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;
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;
Expand Down Expand Up @@ -201,7 +203,6 @@ lazy_static! {
// eg. Windows Terminal, IntelliJ IDEA, Firefox
static ref NO_TITLEBAR: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));

static ref STACKBAR_MODE: Arc<Mutex<StackbarMode >> = Arc::new(Mutex::new(StackbarMode::Never));
static ref WINDOWS_BY_BAR_HWNDS: Arc<Mutex<HashMap<isize, VecDeque<isize>>>> =
Arc::new(Mutex::new(HashMap::new()));

Expand All @@ -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<StackbarLabel> = AtomicCell::new(StackbarLabel::Process);
pub static STACKBAR_MODE: AtomicCell<StackbarMode> = AtomicCell::new(StackbarMode::Never);

#[must_use]
pub fn current_virtual_desktop() -> Option<Vec<u8>> {
Expand Down
7 changes: 5 additions & 2 deletions komorebi/src/process_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions komorebi/src/stackbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u16> = 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<u16> = label.encode_utf16().collect();

tab_box.left_padding(10);
tab_box.right_padding(10);
Expand Down
14 changes: 10 additions & 4 deletions komorebi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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;
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;
Expand Down Expand Up @@ -330,11 +332,12 @@ pub struct TabsConfig {
/// Tab background colour
background: Option<Colour>,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct StackbarConfig {
/// Stackbar height
pub height: Option<i32>,
/// Stackbar height
pub label: Option<StackbarLabel>,
/// Stackbar mode
pub mode: Option<StackbarMode>,
/// Stackbar tab configuration options
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)),
Expand Down

0 comments on commit d5f4f91

Please sign in to comment.