GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: change `focus_borders` config -> `window_effects`
Lars Berger8ec726d3062b
diff --git a/packages/wm/src/common/color.rs b/packages/wm/src/common/color.rsindex 1a9f2123..34ef3f00 100644--- a/packages/wm/src/common/color.rs+++ b/packages/wm/src/common/color.rs@@ -4,25 +4,21 @@ use anyhow::bail;use serde::{Deserialize, Serialize};#[derive(Debug, Deserialize, Clone, Serialize)]-pub struct ColorRGBA {+pub struct Color {pub r: u8,pub g: u8,pub b: u8,pub a: u8,}-impl ColorRGBA {- pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {- Self { r, g, b, a }- }-- pub fn to_bgr(&self) -> u32 {+impl Color {+ pub fn to_bgr(&self) -> anyhow::Result<u32> {let bgr = format!("{:02x}{:02x}{:02x}", self.b, self.g, self.r);- u32::from_str_radix(&bgr, 16).unwrap()+ Ok(u32::from_str_radix(&bgr, 16)?)}}-impl FromStr for ColorRGBA {+impl FromStr for Color {type Err = anyhow::Error;fn from_str(unparsed: &str) -> anyhow::Result<Self> {@@ -32,18 +28,16 @@ impl FromStr for ColorRGBA {bail!("Color must start with a `#`.");}- println!("r: {}", &unparsed[1..3]);- println!("g: {}", &unparsed[3..5]);- println!("b: {}", &unparsed[5..7]);- println!("{}", u8::from_str_radix("ff", 16).unwrap());-let r = u8::from_str_radix(&unparsed[1..3], 16)?;let g = u8::from_str_radix(&unparsed[3..5], 16)?;let b = u8::from_str_radix(&unparsed[5..7], 16)?;- let a = if unparsed.len() == 9 {- u8::from_str_radix(&unparsed[7..9], 16)?- } else {- 255++ let a = match unparsed.len() {+ 9 => u8::from_str_radix(&unparsed[7..9], 16)?,+ 7 => 255,+ _ => bail!(+ "Expected color to be either a 6 or 8 character long hex value."+ ),};Ok(Self { r, g, b, a })diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rsindex c30806f6..1a54f3e8 100644--- a/packages/wm/src/common/platform/native_window.rs+++ b/packages/wm/src/common/platform/native_window.rs@@ -1,7 +1,4 @@-use std::{- ffi::c_void,- sync::{Arc, RwLock},-};+use std::sync::{Arc, RwLock};use tracing::warn;use windows::{@@ -10,7 +7,7 @@ use windows::{Foundation::{CloseHandle, BOOL, HWND, LPARAM, RECT},Graphics::Dwm::{DwmGetWindowAttribute, DwmSetWindowAttribute, DWMWA_BORDER_COLOR,- DWMWA_CLOAKED, DWMWA_EXTENDED_FRAME_BOUNDS,+ DWMWA_CLOAKED, DWMWA_COLOR_NONE, DWMWA_EXTENDED_FRAME_BOUNDS,},System::Threading::{OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,@@ -39,7 +36,7 @@ use windows::{};use crate::{- common::{LengthValue, Rect, RectDelta},+ common::{Color, LengthValue, Rect, RectDelta},windows::WindowState,};@@ -244,15 +241,24 @@ impl NativeWindow {Ok(())}- pub fn set_border_color(&self, color_rgba: u32) -> anyhow::Result<()> {+ pub fn set_border_color(+ &self,+ color: Option<&Color>,+ ) -> anyhow::Result<()> {+ let bgr = match color {+ Some(color) => color.to_bgr()?,+ None => DWMWA_COLOR_NONE,+ };+unsafe {DwmSetWindowAttribute(HWND(self.handle),DWMWA_BORDER_COLOR,- &color_rgba as *const _ as *const c_void,+ &bgr as *const _ as _,std::mem::size_of::<u32>() as u32,)?;}+Ok(())}diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rsindex 3f546587..6f89997f 100644--- a/packages/wm/src/containers/commands/mod.rs+++ b/packages/wm/src/containers/commands/mod.rs@@ -8,7 +8,6 @@ mod move_container_within_tree;mod redraw;mod replace_container;mod resize_tiling_container;-mod set_active_window_border;mod set_focused_descendant;mod toggle_tiling_direction;@@ -22,6 +21,5 @@ pub use move_container_within_tree::*;pub use redraw::*;pub use replace_container::*;pub use resize_tiling_container::*;-pub use set_active_window_border::*;pub use set_focused_descendant::*;pub use toggle_tiling_direction::*;diff --git a/packages/wm/src/containers/commands/set_active_window_border.rs b/packages/wm/src/containers/commands/set_active_window_border.rsdeleted file mode 100644index 74b60bdf..00000000--- a/packages/wm/src/containers/commands/set_active_window_border.rs+++ /dev/null@@ -1,31 +0,0 @@-use crate::common::platform::NativeWindow;-use crate::user_config::UserConfig;-use crate::wm_state::WmState;-use anyhow::{Error, Result};-use std::convert::TryInto;-use windows::Win32::Graphics::Dwm::{- DWMWA_COLOR_NONE,-};--pub fn handle_set_active_window_border(- state: &mut WmState,- target: NativeWindow,- config: &UserConfig,-) -> Result<(), Error> {- // Clear old window border- let inactive_border_color = if config.value.focus_borders.inactive.enabled {- DWMWA_COLOR_NONE- } else {- config.value.focus_borders.inactive.color.to_bgr().clone()- };- target.set_border_color(inactive_border_color)?;-- // Set new window border- target.set_border_color(- config.value.focus_borders.active.color.to_bgr().clone(),- )?;-- state.active_border_window = Some(target);-- Ok(())-}diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rsindex 9d076f8b..41f0ddf6 100644--- a/packages/wm/src/user_config.rs+++ b/packages/wm/src/user_config.rs@@ -6,7 +6,7 @@ use tokio::fs;use crate::{app_command::InvokeCommand,- common::{ColorRGBA, LengthValue, RectDelta},+ common::{Color, LengthValue, RectDelta},monitors::Monitor,workspaces::Workspace,};@@ -140,7 +140,7 @@ impl UserConfig {#[serde(rename_all(serialize = "camelCase"))]pub struct ParsedConfig {pub binding_modes: Vec<BindingModeConfig>,- pub focus_borders: FocusBordersConfig,+ pub window_effects: WindowEffectsConfig,pub gaps: GapsConfig,pub general: GeneralConfig,pub keybindings: Vec<KeybindingConfig>,@@ -164,23 +164,20 @@ pub struct BindingModeConfig {#[derive(Clone, Debug, Deserialize, Serialize)]#[serde(rename_all(serialize = "camelCase"))]-pub struct FocusBordersConfig {- /// Border of the focused window.- pub active: FocusBorder,+pub struct WindowEffectsConfig {+ /// Visual effects to apply to the focused window.+ pub focused_window: WindowEffectConfig,- /// Border of non-focused windows.- pub inactive: FocusBorder,+ /// Visual effects to apply to non-focused windows.+ pub other_windows: WindowEffectConfig,}#[derive(Clone, Debug, Deserialize, Serialize)]#[serde(rename_all(serialize = "camelCase"))]-pub struct FocusBorder {- /// Whether to use a custom border color.- pub enabled: bool,-- /// Border color of the window.- #[serde(deserialize_with = "deserialize_color")]- pub color: ColorRGBA,+pub struct WindowEffectConfig {+ /// Optional colored border to apply.+ #[serde(deserialize_with = "deserialize_option_color")]+ pub border_color: Option<Color>,}#[derive(Clone, Debug, Deserialize, Serialize)]@@ -281,15 +278,19 @@ const fn default_bool<const V: bool>() -> bool {V}-/// Deserializes `Color` from a string.-fn deserialize_color<'de, D>(+/// Deserializes `Option<Color>` from an optional string.+fn deserialize_option_color<'de, D>(deserializer: D,-) -> Result<ColorRGBA, D::Error>+) -> Result<Option<Color>, D::Error>whereD: Deserializer<'de>,{- let str = String::deserialize(deserializer)?;- ColorRGBA::from_str(&str).map_err(serde::de::Error::custom)+ match Option::<String>::deserialize(deserializer)? {+ Some(str) => Color::from_str(&str)+ .map(Some)+ .map_err(serde::de::Error::custom),+ None => Ok(None),+ }}/// Deserializes `RectDelta` from a string.diff --git a/packages/wm/src/windows/commands/apply_window_effects.rs b/packages/wm/src/windows/commands/apply_window_effects.rsnew file mode 100644index 00000000..2d485959--- /dev/null+++ b/packages/wm/src/windows/commands/apply_window_effects.rs@@ -0,0 +1,32 @@+use crate::{+ containers::WindowContainer, user_config::UserConfig,+ windows::traits::WindowGetters, wm_state::WmState,+};++pub fn apply_window_effects(+ window: WindowContainer,+ state: &mut WmState,+ config: &UserConfig,+) -> anyhow::Result<()> {+ let inactive_border_color =+ config.value.window_effects.other_windows.border_color;++ // Clear old window border.+ if let Some(active_border_window) = &state.active_border_window {+ active_border_window+ .native()+ .set_border_color(inactive_border_color.as_ref())?;+ }++ let active_border_color =+ config.value.window_effects.focused_window.border_color;++ // Set new window border.+ window+ .native()+ .set_border_color(active_border_color.as_ref())?;++ state.active_border_window = Some(window);++ Ok(())+}diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rsindex cd4daf4c..19828ef7 100644--- a/packages/wm/src/windows/commands/mod.rs+++ b/packages/wm/src/windows/commands/mod.rs@@ -1,3 +1,4 @@+mod apply_window_effects;mod manage_window;mod move_window_in_direction;mod move_window_to_workspace;@@ -6,6 +7,7 @@ mod toggle_window_state;mod unmanage_window;mod update_window_state;+pub use apply_window_effects::*;pub use manage_window::*;pub use move_window_in_direction::*;pub use move_window_to_workspace::*;diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex c59e0344..c954a764 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -35,7 +35,7 @@ pub struct WmState {/// container.pub has_pending_focus_sync: bool,- pub active_border_window: Option<NativeWindow>,+ pub active_border_window: Option<WindowContainer>,/// Name of the most recently focused workspace.///diff --git a/resources/sample-config.yaml b/resources/sample-config.yamlindex 23dc26d1..c7d573e6 100644--- a/resources/sample-config.yaml+++ b/resources/sample-config.yaml@@ -16,15 +16,16 @@ gaps:# Gap between windows and the screen edge.outer_gap: '20px'-# Highlight active and inactive windows with a colored border.-# ** Exclusive to Windows 11 due to API limitations.-focus_borders:- active:- enabled: true- color: '#0000ff'- inactive:- enabled: false- color: '#ff0000'+# Visual effects to apply to windows. Separate effects can be configured+# for the focused window vs. other windows.+window_effects:+ focused_window:+ # Highlight the window with a colored border. Exclusive to Windows 11.+ # Set to `null` to disable the effect.+ border_color: '#0000ff'++ other_windows:+ border_color: nullwindow_state_defaults:floating:@@ -83,154 +84,154 @@ binding_modes:keybindings:# Resize focused window by a percentage or pixel amount.- commands: ['resize --width -2%']- bindings: ['H', 'Left']+ bindings: ['h', 'left']- commands: ['resize --width +2%']- bindings: ['L', 'Right']+ bindings: ['l', 'right']- commands: ['resize --height +2%']- bindings: ['K', 'Up']+ bindings: ['k', 'up']- commands: ['resize --height -2%']- bindings: ['J', 'Down']+ bindings: ['j', 'down']# Press enter/escape to return to default keybindings.- commands: ['wm-disable-binding-mode --name resize']- bindings: ['Escape', 'Enter']+ bindings: ['escape', 'enter']keybindings:# Shift focus in a given direction.- commands: ['focus --dir left']- bindings: ['Alt+H', 'Alt+Left']+ bindings: ['alt+h', 'alt+left']- commands: ['focus --dir right']- bindings: ['Alt+L', 'Alt+Right']+ bindings: ['alt+l', 'alt+right']- commands: ['focus --dir up']- bindings: ['Alt+K', 'Alt+Up']+ bindings: ['alt+k', 'alt+up']- commands: ['focus --dir down']- bindings: ['Alt+J', 'Alt+Down']+ bindings: ['alt+j', 'alt+down']# Move focused window in a given direction.- commands: ['move --dir left']- bindings: ['Alt+Shift+H', 'Alt+Shift+Left']+ bindings: ['alt+shift+h', 'alt+shift+left']- commands: ['move --dir right']- bindings: ['Alt+Shift+L', 'Alt+Shift+Right']+ bindings: ['alt+shift+l', 'alt+shift+right']- commands: ['move --dir up']- bindings: ['Alt+Shift+K', 'Alt+Shift+Up']+ bindings: ['alt+shift+k', 'alt+shift+up']- commands: ['move --dir down']- bindings: ['Alt+Shift+J', 'Alt+Shift+Down']+ bindings: ['alt+shift+j', 'alt+shift+down']# Resize focused window by a percentage or pixel amount.- commands: ['resize --width -2%']- bindings: ['Alt+U']+ bindings: ['alt+u']- commands: ['resize --width +2%']- bindings: ['Alt+P']+ bindings: ['alt+p']- commands: ['resize --height +2%']- bindings: ['Alt+O']+ bindings: ['alt+o']- commands: ['resize --height -2%']- bindings: ['Alt+I']+ bindings: ['alt+i']# As an alternative to the resize keybindings above, resize mode enables# resizing via HJKL or arrow keys. The binding mode is defined above with# the name 'resize'.- commands: ['wm-enable-binding-mode --name resize']- bindings: ['Alt+R']+ bindings: ['alt+r']# Change tiling direction. This determines where new tiling windows will# be inserted.- commands: ['toggle-tiling-direction']- bindings: ['Alt+V']+ bindings: ['alt+v']# Change focus between tiling windows and floating/fullscreen windows.- commands: ['wm-toggle-focus-state']- bindings: ['Alt+Space']+ bindings: ['alt+space']# Change the focused window to be floating.- commands: ['toggle-floating --centered']- bindings: ['Alt+Shift+Space']+ bindings: ['alt+shift+space']# Change the focused window to be fullscreen.- commands: ['toggle-fullscreen']- bindings: ['Alt+F']+ bindings: ['alt+f']# Minimize focused window.- commands: ['toggle-minimized']- bindings: ['Alt+M']+ bindings: ['alt+m']# Close focused window.- commands: ['close']- bindings: ['Alt+Shift+Q']+ bindings: ['alt+shift+q']# Kill GlazeWM process safely.- commands: ['wm-exit']- bindings: ['Alt+Shift+E']+ bindings: ['alt+shift+e']# Re-evaluate configuration file.- commands: ['wm-reload-config']- bindings: ['Alt+Shift+R']+ bindings: ['alt+shift+r']# Redraw all windows.- commands: ['wm-redraw']- bindings: ['Alt+Shift+W']+ bindings: ['alt+shift+w']# Launch CMD terminal. Alternatively `shell_exec --command wt` or# `shell_exec --command %ProgramFiles%/Git/git-bash.exe` to start Windows# Terminal and Git Bash respectively.- commands: ['shell-exec --command cmd']- bindings: ['Alt+Enter']+ bindings: ['alt+enter']# Focus the workspace that last had focus.- commands: ['focus --recent-workspace']- bindings: ['Alt+Y']+ bindings: ['alt+y']# Focus the next/previous workspace defined in `workspaces` config.- commands: ['focus --next-workspace']- bindings: ['Alt+T']+ bindings: ['alt+t']- commands: ['focus --prev-workspace']- bindings: ['Alt+Shift+T']+ bindings: ['alt+shift+t']# Change focus to a workspace defined in `workspaces` config.- commands: ['focus --workspace 1']- bindings: ['Alt+1']+ bindings: ['alt+1']- commands: ['focus --workspace 2']- bindings: ['Alt+2']+ bindings: ['alt+2']- commands: ['focus --workspace 3']- bindings: ['Alt+3']+ bindings: ['alt+3']- commands: ['focus --workspace 4']- bindings: ['Alt+4']+ bindings: ['alt+4']- commands: ['focus --workspace 5']- bindings: ['Alt+5']+ bindings: ['alt+5']- commands: ['focus --workspace 6']- bindings: ['Alt+6']+ bindings: ['alt+6']- commands: ['focus --workspace 7']- bindings: ['Alt+7']+ bindings: ['alt+7']- commands: ['focus --workspace 8']- bindings: ['Alt+8']+ bindings: ['alt+8']- commands: ['focus --workspace 9']- bindings: ['Alt+9']+ bindings: ['alt+9']# Move the focused window's parent workspace to a monitor in a given# direction.- commands: ['move-workspace --dir left']- bindings: ['Alt+Shift+A']+ bindings: ['alt+shift+a']- commands: ['move-workspace --dir right']- bindings: ['Alt+Shift+F']+ bindings: ['alt+shift+f']- commands: ['move-workspace --dir up']- bindings: ['Alt+Shift+D']+ bindings: ['alt+shift+d']- commands: ['move-workspace --dir down']- bindings: ['Alt+Shift+S']+ bindings: ['alt+shift+s']# Move focused window to a workspace defined in `workspaces` config.- commands: ['move --workspace 1', 'focus --workspace 1']- bindings: ['Alt+Shift+1']+ bindings: ['alt+shift+1']- commands: ['move --workspace 2', 'focus --workspace 2']- bindings: ['Alt+Shift+2']+ bindings: ['alt+shift+2']- commands: ['move --workspace 3', 'focus --workspace 3']- bindings: ['Alt+Shift+3']+ bindings: ['alt+shift+3']- commands: ['move --workspace 4', 'focus --workspace 4']- bindings: ['Alt+Shift+4']+ bindings: ['alt+shift+4']- commands: ['move --workspace 5', 'focus --workspace 5']- bindings: ['Alt+Shift+5']+ bindings: ['alt+shift+5']- commands: ['move --workspace 6', 'focus --workspace 6']- bindings: ['Alt+Shift+6']+ bindings: ['alt+shift+6']- commands: ['move --workspace 7', 'focus --workspace 7']- bindings: ['Alt+Shift+7']+ bindings: ['alt+shift+7']- commands: ['move --workspace 8', 'focus --workspace 8']- bindings: ['Alt+Shift+8']+ bindings: ['alt+shift+8']- commands: ['move --workspace 9', 'focus --workspace 9']- bindings: ['Alt+Shift+9']+ bindings: ['alt+shift+9']