Skip to content

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

feat: wip redraw command

Lars Berger1090c2f2c845
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index e2e21262..80673290 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -1,6 +1,5 @@
use std::sync::{Arc, RwLock};
-use anyhow::Result;
use windows::{
core::PWSTR,
Win32::{
@@ -18,9 +17,10 @@ use windows::{
WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetWindow, GetWindowLongPtrW,
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId,
- IsWindowVisible, SetForegroundWindow, GWL_EXSTYLE, GWL_STYLE,
- GW_OWNER, WINDOWPLACEMENT, WINDOW_EX_STYLE, WINDOW_STYLE,
- WS_CAPTION, WS_CHILD, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
+ IsWindowVisible, SetForegroundWindow, ShowWindowAsync,
+ GWL_EXSTYLE, GWL_STYLE, GW_OWNER, SW_RESTORE, WINDOWPLACEMENT,
+ WINDOW_EX_STYLE, WINDOW_STYLE, WS_CAPTION, WS_CHILD,
+ WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
},
},
},
@@ -68,7 +68,7 @@ impl NativeWindow {
/// Gets the process name associated with the window.
///
/// This value is lazily retrieved and is cached after first retrieval.
- pub fn process_name(&self) -> Result<String> {
+ pub fn process_name(&self) -> anyhow::Result<String> {
let process_name_guard = self.process_name.read().unwrap();
match *process_name_guard {
Some(ref process_name) => Ok(process_name.clone()),
@@ -105,7 +105,7 @@ impl NativeWindow {
/// Gets the class name of the window.
///
/// This value is lazily retrieved and is cached after first retrieval.
- pub fn class_name(&self) -> Result<String> {
+ pub fn class_name(&self) -> anyhow::Result<String> {
let class_name_guard = self.class_name.read().unwrap();
match *class_name_guard {
Some(ref class_name) => Ok(class_name.clone()),
@@ -238,6 +238,11 @@ impl NativeWindow {
(current_style & style.0 as isize) != 0
}
}
+
+ fn restore(&self) -> anyhow::Result<()> {
+ unsafe { ShowWindowAsync(self.handle, SW_RESTORE).ok() }?;
+ Ok(())
+ }
}
impl PartialEq for NativeWindow {
@@ -248,7 +253,7 @@ impl PartialEq for NativeWindow {
impl Eq for NativeWindow {}
-pub fn available_windows() -> Result<Vec<NativeWindow>> {
+pub fn available_windows() -> anyhow::Result<Vec<NativeWindow>> {
available_window_handles()?
.into_iter()
.map(|handle| Ok(NativeWindow::new(handle)))
diff --git a/packages/wm/src/containers/commands/redraw_command.rs b/packages/wm/src/containers/commands/redraw_command.rs
index 051ac0b9..2b9091ed 100644
--- a/packages/wm/src/containers/commands/redraw_command.rs
+++ b/packages/wm/src/containers/commands/redraw_command.rs
@@ -1,7 +1,154 @@
-use crate::wm_state::WmState;
+use std::ptr::null_mut;
+
+use crate::{
+ user_config::UserConfig, windows::WindowState, wm_state::WmState,
+};
pub struct RedrawCommand;
-pub fn redraw_handler(cmd: RedrawCommand, state: &mut WmState) {
- todo!()
+pub fn redraw_handler(
+ command: RedrawCommand,
+ state: &mut WmState,
+ user_config: UserConfig,
+) -> anyhow::Result<&mut WmState> {
+ let windows_to_redraw = state.windows_to_redraw();
+
+ // Get windows that are minimized/maximized and shouldn't be.
+ let windows_to_restore = windows_to_redraw
+ .iter()
+ .filter(|window| {
+ window.state == WindowState::Minimized
+ && !window.native().is_minimized()
+ || window.state == WindowState::Maximized
+ && window.native().is_maximized()
+ })
+ .collect::<Vec<_>>();
+
+ // Restore minimized and maximized windows. Needed to be able to move and
+ // resize them.
+ for window in &windows_to_restore {
+ let _ = window.native().restore();
+ }
+
+ // Get z-order to set for floating windows.
+ let should_show_on_top = self
+ .user_config_service
+ .general_config()
+ .show_floating_on_top;
+
+ for window in &windows_to_redraw {
+ let workspace = window
+ .ancestors()
+ .find_map(|ancestor| ancestor.as_workspace())
+ .context("Window has no workspace.")?;
+
+ // Transition display state depending on whether window will be
+ // shown or hidden.
+ window.set_display_state(
+ match (window.display_state(), workspace.is_displayed()) {
+ (DisplayState::Hidden | DisplayState::Hiding, true) => {
+ DisplayState::Showing
+ }
+ (DisplayState::Shown | DisplayState::Showing, false) => {
+ DisplayState::Hiding
+ }
+ _ => window.display_state(),
+ },
+ );
+
+ window.native().set_position(position);
+
+ // When there's a mismatch between the DPI of the monitor and the window,
+ // `SetWindowPos` might size the window incorrectly. By calling `SetWindowPos`
+ // twice, inconsistencies after the first move are resolved.
+ if window.has_pending_dpi_adjustment() {
+ window.native().set_position(position);
+ window.set_has_pending_dpi_adjustment(false);
+ }
+ }
+
+ state.clear_containers_to_redraw();
+ Ok(state)
+}
+
+fn set_window_position(&self, window: &Window) {
+ let default_flags = SetWindowPosFlags::FRAME_CHANGED
+ | SetWindowPosFlags::NO_ACTIVATE
+ | SetWindowPosFlags::NO_COPY_BITS
+ | SetWindowPosFlags::NO_SEND_CHANGING
+ | SetWindowPosFlags::ASYNC_WINDOW_POS;
+
+ let workspace = window
+ .ancestors()
+ .find_map(|ancestor| ancestor.as_workspace())
+ .context("Window has no workspace.")?;
+
+ let is_workspace_displayed = workspace.is_displayed();
+
+ // Show or hide the window depending on whether the workspace is displayed.
+ let default_flags = if is_workspace_displayed {
+ default_flags | SetWindowPosFlags::SHOW_WINDOW
+ } else {
+ default_flags | SetWindowPosFlags::HIDE_WINDOW
+ };
+
+ let default_flags = if window.is::<MaximizedWindow>() {
+ default_flags | SetWindowPosFlags::NO_SIZE | SetWindowPosFlags::NO_MOVE
+ } else {
+ default_flags
+ };
+
+ // Transition display state depending on whether window will be shown/hidden.
+ window.set_display_state(
+ match (window.display_state(), is_workspace_displayed) {
+ (DisplayState::Hidden | DisplayState::Hiding, true) => {
+ DisplayState::Showing
+ }
+ (DisplayState::Shown | DisplayState::Showing, false) => {
+ DisplayState::Hiding
+ }
+ _ => window.display_state(),
+ },
+ );
+
+ if window.is::<TilingWindow>() {
+ set_window_pos(
+ window.handle(),
+ null_mut(),
+ window.x() - window.border_delta().left,
+ window.y() - window.border_delta().top,
+ window.width()
+ + window.border_delta().left
+ + window.border_delta().right,
+ window.height()
+ + window.border_delta().top
+ + window.border_delta().bottom,
+ default_flags,
+ );
+ return;
+ }
+
+ // Get z-order to set for floating windows.
+ let should_show_on_top = self
+ .user_config_service
+ .general_config()
+ .show_floating_on_top;
+
+ let floating_z_order = if should_show_on_top {
+ ZOrderFlags::TopMost
+ } else {
+ ZOrderFlags::NoTopMost
+ };
+
+ // Avoid adjusting the borders of floating windows. Otherwise the window will
+ // increase in size from its original placement.
+ set_window_pos(
+ window.handle(),
+ floating_z_order as _,
+ window.x(),
+ window.y(),
+ window.width(),
+ window.height(),
+ default_flags,
+ );
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index cddbff35..34404779 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -119,6 +119,27 @@ impl WmState {
todo!()
}
+ /// Gets windows that should be redrawn.
+ ///
+ /// When redrawing after a keybinding that changes a window's type
+ /// (eg. tiling -> floating), the original detached window might still
+ /// be queued for a redraw and should be ignored.
+ pub fn windows_to_redraw(&self) -> Vec<TilingWindow> {
+ self
+ .containers_to_redraw
+ .iter()
+ .flat_map(|container| container.self_and_descendants())
+ .filter_map(|container| container.as_window())
+ .unique()
+ .filter(|window| !window.is_detached())
+ .collect()
+ }
+
+ /// Removes all containers from the redraw queue.
+ pub fn clear_containers_to_redraw(&mut self) {
+ self.containers_to_redraw.clear();
+ }
+
// Get the currently focused container. This can either be a `Window` or
// a `Workspace` without any descendant windows.
// pub fn focused_container(&self) -> Arc<Container> {