Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

set_active_window_border command

holbyce861adb1ab4
diff --git a/Cargo.lock b/Cargo.lock
index 4f36f839..f0937869 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1277,6 +1277,5 @@ dependencies = [
"tracing-subscriber",
"uuid",
"watcher",
- "winapi",
"windows",
]
diff --git a/packages/wm/Cargo.toml b/packages/wm/Cargo.toml
index a4f0b3a3..c9fcc728 100644
--- a/packages/wm/Cargo.toml
+++ b/packages/wm/Cargo.toml
@@ -25,4 +25,3 @@ tokio-tungstenite = "0.21"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
windows = { workspace = true }
-winapi = { version = "0.3.9", features = ["dwmapi", "windef", "ntdef", "minwindef", "winuser"] }
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rs
index ad75ca20..b0527a08 100644
--- a/packages/wm/src/common/platform/platform.rs
+++ b/packages/wm/src/common/platform/platform.rs
@@ -1,6 +1,7 @@
use std::sync::Arc;
use tokio::sync::Mutex;
+use windows::Win32::UI::WindowsAndMessaging::SetCursorPos;
use windows::Win32::UI::{
HiDpi::{
SetProcessDpiAwarenessContext,
@@ -67,4 +68,12 @@ impl Platform {
Ok(())
}
+
+ pub fn set_cursor_pos(x: i32, y: i32) -> anyhow::Result<()> {
+ unsafe {
+ SetCursorPos(x, y)?;
+ };
+
+ Ok(())
+ }
}
diff --git a/packages/wm/src/containers/commands/center_cursor_on_container.rs b/packages/wm/src/containers/commands/center_cursor_on_container.rs
index eac4b1c6..2ff26e55 100644
--- a/packages/wm/src/containers/commands/center_cursor_on_container.rs
+++ b/packages/wm/src/containers/commands/center_cursor_on_container.rs
@@ -1,29 +1,24 @@
-extern crate winapi;
-
-use winapi::um::winuser::SetCursorPos;
-use anyhow::{Result, Error};
-use crate::containers::{Container, SplitContainer};
+use crate::common::platform::Platform;
use crate::containers::traits::PositionGetters;
+use crate::containers::Container;
+use crate::user_config::UserConfig;
+use anyhow::{Error, Result};
pub fn center_cursor_on_container(
- target: &Container
+ target: &Container,
+ config: &UserConfig,
) -> Result<(), Error> {
- // do i pull from user config here or is that handled elsewhere?
- let is_enabled = true;
+ if !config.value.general.cursor_follows_focus
+ // || target.is_detached()
+ // || target.focus_index() < 0
+ {
+ return Ok(());
+ }
- // target is currently missing is_detached and focus_index
- // if !is_enabled || target.is_detached() || target.focus_index < 0 {
- if !is_enabled {
- return Ok(());
- }
+ let center_x = target.x()? + (target.width()? / 2);
+ let center_y = target.y()? + (target.height()? / 2);
- // Calculate center point of focused window.
- let center_x = target.x()? + (target.width()? / 2);
- let center_y = target.y()? + (target.height()? / 2);
+ Platform::set_cursor_pos(center_x, center_y)?;
- unsafe {
- SetCursorPos(center_x, center_y);
- }
-
- Ok(())
-}
\ No newline at end of file
+ Ok(())
+}
diff --git a/packages/wm/src/containers/commands/set_active_window_border.rs b/packages/wm/src/containers/commands/set_active_window_border.rs
index 4133d723..e8c8543a 100644
--- a/packages/wm/src/containers/commands/set_active_window_border.rs
+++ b/packages/wm/src/containers/commands/set_active_window_border.rs
@@ -1,17 +1,64 @@
-extern crate winapi;
-
+use crate::common::platform::NativeWindow;
+use crate::user_config::UserConfig;
+use crate::wm_state::WmState;
+use anyhow::{Error, Result};
use std::convert::TryInto;
-use std::ptr;
-use winapi::um::dwmapi::DwmSetWindowAttribute;
-use winapi::shared::windef::HWND;
-use winapi::shared::minwindef::DWORD;
-use winapi::shared::ntdef::PVOID;
-use anyhow::{Result, Error};
-use crate::containers::Container;
+use std::os::raw::c_void;
+use windows::Win32::Graphics::Dwm::{
+ DwmSetWindowAttribute, DWMWA_BORDER_COLOR,
+};
pub fn handle_set_active_window_border(
- target: Container
+ state: &mut WmState,
+ target: NativeWindow,
+ config: &UserConfig,
) -> Result<(), Error> {
+ if let Some(last_focused) = &state.active_border_window {
+ // Clear old window border
+ let inactive_color = if config.value.focus_borders.inactive.enabled {
+ rgb_to_uint(config.value.focus_borders.inactive.color.clone())
+ } else {
+ 0xFFFFFFFF
+ };
+ unsafe {
+ let inactive_color: u32 = 0xFFFFFFFF; // replace with your color
+ let pv_attribute: *const c_void =
+ &inactive_color as *const _ as *const c_void;
+ DwmSetWindowAttribute(
+ last_focused.handle,
+ DWMWA_BORDER_COLOR,
+ pv_attribute,
+ std::mem::size_of::<u32>() as u32,
+ )?;
+ }
+ }
+
+ // Set new window border
+ let active_color = if config.value.focus_borders.active.enabled {
+ rgb_to_uint(config.value.focus_borders.active.color.clone())
+ } else {
+ 0xFFFFFFFF
+ };
+ unsafe {
+ let active_color: u32 =
+ rgb_to_uint(config.value.focus_borders.active.color.clone()); // replace with your color
+ let pv_attribute: *const c_void =
+ &active_color as *const _ as *const c_void;
+ DwmSetWindowAttribute(
+ target.handle,
+ DWMWA_BORDER_COLOR,
+ pv_attribute,
+ std::mem::size_of::<u32>() as u32,
+ )?;
+ }
+
+ state.active_border_window = Some(target);
+
+ Ok(())
+}
- Ok(())
-}
\ No newline at end of file
+fn rgb_to_uint(rgb: String) -> u32 {
+ let c = rgb.chars().collect::<Vec<char>>();
+ let bgr = format!("{}{}{}{}{}{}", c[5], c[6], c[3], c[4], c[1], c[2]);
+ u32::from_str_radix(&bgr, 16).unwrap()
+}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 19c68d9c..c4c73f5c 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -10,7 +10,9 @@ use crate::{
FocusMode,
},
containers::{
- commands::{redraw, set_focused_descendant},
+ commands::{
+ handle_set_active_window_border, redraw, set_focused_descendant,
+ },
traits::CommonGetters,
Container, RootContainer, WindowContainer,
},
@@ -33,6 +35,8 @@ pub struct WmState {
/// container.
pub has_pending_focus_sync: bool,
+ pub active_border_window: Option<NativeWindow>,
+
/// Names of any currently enabled binding modes.
pub binding_modes: Vec<String>,
@@ -51,6 +55,7 @@ impl WmState {
root_container: RootContainer::new(),
containers_to_redraw: Vec::new(),
has_pending_focus_sync: false,
+ active_border_window: None,
binding_modes: Vec::new(),
app_bar_windows: Vec::new(),
event_tx,