Skip to content

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

feat: combine `redraw` and `sync_native_focus` into new `platform_sync` command; apply window effects on platform sync

Lars Bergerc4ff02d04f44
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index e400cdfc..ddc135bb 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -347,7 +347,7 @@ impl InvokeCommand {
config,
)?;
- state.containers_to_redraw.push(window.into());
+ state.pending_sync.containers_to_redraw.push(window.into());
Ok(())
}
_ => Ok(()),
@@ -375,7 +375,7 @@ impl InvokeCommand {
config,
)?;
- state.containers_to_redraw.push(window.into());
+ state.pending_sync.containers_to_redraw.push(window.into());
Ok(())
}
_ => Ok(()),
@@ -390,7 +390,7 @@ impl InvokeCommand {
config,
)?;
- state.containers_to_redraw.push(window.into());
+ state.pending_sync.containers_to_redraw.push(window.into());
Ok(())
}
_ => Ok(()),
@@ -485,7 +485,10 @@ impl InvokeCommand {
InvokeCommand::WmExit => todo!(),
InvokeCommand::WmRedraw => {
let root_container = state.root_container.clone();
- state.add_container_to_redraw(root_container.into());
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(root_container.into());
Ok(())
}
InvokeCommand::WmReloadConfig => reload_config(state, config),
diff --git a/packages/wm/src/common/commands/mod.rs b/packages/wm/src/common/commands/mod.rs
index 1fb41d5d..ab26c79c 100644
--- a/packages/wm/src/common/commands/mod.rs
+++ b/packages/wm/src/common/commands/mod.rs
@@ -1,11 +1,11 @@
mod disable_binding_mode;
mod enable_binding_mode;
+mod platform_sync;
mod reload_config;
mod shell_exec;
-mod sync_native_focus;
pub use disable_binding_mode::*;
pub use enable_binding_mode::*;
+pub use platform_sync::*;
pub use reload_config::*;
pub use shell_exec::*;
-pub use sync_native_focus::*;
diff --git a/packages/wm/src/common/commands/platform_sync.rs b/packages/wm/src/common/commands/platform_sync.rs
new file mode 100644
index 00000000..0efc05fe
--- /dev/null
+++ b/packages/wm/src/common/commands/platform_sync.rs
@@ -0,0 +1,173 @@
+use anyhow::Context;
+
+use crate::{
+ common::{platform::Platform, DisplayState},
+ containers::{
+ traits::{CommonGetters, PositionGetters},
+ Container, WindowContainer,
+ },
+ user_config::UserConfig,
+ windows::{traits::WindowGetters, WindowState},
+ wm_event::WmEvent,
+ wm_state::WmState,
+};
+
+pub fn platform_sync(
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ if state.pending_sync.containers_to_redraw.len() > 0 {
+ redraw_containers(state)?;
+ state.pending_sync.containers_to_redraw.clear();
+ }
+
+ let recent_focused_container = state.recent_focused_container.clone();
+ let focused_container =
+ state.focused_container().context("No focused container.")?;
+
+ if state.pending_sync.focus_change {
+ sync_focus(focused_container.clone(), state)?;
+ state.pending_sync.focus_change = false;
+ }
+
+ if let Ok(window) = focused_container.as_window_container() {
+ apply_window_effects(window, true, config);
+ }
+
+ // Get windows that should have the unfocused border applied to them.
+ // For the sake of performance, we only update the border of the
+ // previously focused window. If the `reset_window_effects` flag is
+ // passed, the unfocused border is applied to all unfocused windows.
+ let unfocused_windows = match state.pending_sync.reset_window_effects {
+ true => state.windows(),
+ false => recent_focused_container
+ .and_then(|container| container.as_window_container().ok())
+ .into_iter()
+ .collect(),
+ }
+ .into_iter()
+ .filter(|window| window.id() != focused_container.id());
+
+ for window in unfocused_windows {
+ apply_window_effects(window, false, config);
+ }
+
+ state.pending_sync.reset_window_effects = false;
+
+ Ok(())
+}
+
+pub fn sync_focus(
+ focused_container: Container,
+ state: &mut WmState,
+) -> anyhow::Result<()> {
+ let native_window = match focused_container.as_window_container() {
+ Ok(window) => window.native(),
+ _ => Platform::desktop_window(),
+ };
+
+ // Set focus to the given window handle. If the container is a normal
+ // window, then this will trigger a `PlatformEvent::WindowFocused` event.
+ if Platform::foreground_window() != native_window {
+ let _ = native_window.set_foreground();
+ }
+
+ // TODO: Change z-index of workspace windows that match the focused
+ // container's state. Make sure not to decrease z-index for floating
+ // windows that are always on top.
+
+ state.emit_event(WmEvent::FocusChanged {
+ focused_container: focused_container.to_dto()?,
+ });
+
+ state.recent_focused_container = Some(focused_container);
+
+ Ok(())
+}
+
+fn redraw_containers(state: &mut WmState) -> anyhow::Result<()> {
+ for window in &state.windows_to_redraw() {
+ let workspace =
+ window.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(),
+ },
+ );
+
+ // Restore window if it's minimized/maximized and shouldn't be. This is
+ // needed to be able to move and resize it.
+ match window.state() {
+ // Need to restore window if transitioning from maximized fullscreen
+ // to non-maximized fullscreen.
+ WindowState::Fullscreen(s) => {
+ if !s.maximized && window.native().is_maximized() {
+ let _ = window.native().restore();
+ }
+ }
+ // No need to restore window if it'll be minimized. Transitioning
+ // from maximized to minimized works without restoring.
+ WindowState::Minimized => {}
+ _ => {
+ if window.native().is_minimized() || window.native().is_maximized()
+ {
+ let _ = window.native().restore();
+ }
+ }
+ }
+
+ let rect = window.to_rect()?.apply_delta(&window.border_delta());
+
+ let is_visible = match window.display_state() {
+ DisplayState::Showing | DisplayState::Shown => true,
+ _ => false,
+ };
+
+ let _ =
+ window
+ .native()
+ .set_position(&window.state(), is_visible, &rect);
+
+ // When there's a mismatch between the DPI of the monitor and the
+ // window, the window might be sized incorrectly after the first move.
+ // If we set the position twice, inconsistencies after the first move
+ // are resolved.
+ if window.has_pending_dpi_adjustment() {
+ let _ =
+ window
+ .native()
+ .set_position(&window.state(), is_visible, &rect);
+
+ window.set_has_pending_dpi_adjustment(false);
+ }
+ }
+
+ Ok(())
+}
+
+fn apply_window_effects(
+ window: WindowContainer,
+ is_focused: bool,
+ config: &UserConfig,
+) {
+ let border_color = match is_focused {
+ true => &config.value.window_effects.focused_window.border_color,
+ false => &config.value.window_effects.other_windows.border_color,
+ }
+ .clone();
+
+ // TODO: Be able to add transparency to windows.
+
+ if let Some(border_color) = border_color {
+ _ = window.native().set_border_color(Some(&border_color));
+ }
+}
diff --git a/packages/wm/src/common/commands/reload_config.rs b/packages/wm/src/common/commands/reload_config.rs
index 8a676f63..7838b5ce 100644
--- a/packages/wm/src/common/commands/reload_config.rs
+++ b/packages/wm/src/common/commands/reload_config.rs
@@ -3,7 +3,8 @@ use tracing::{info, warn};
use crate::{
containers::traits::{CommonGetters, TilingSizeGetters},
- user_config::UserConfig,
+ user_config::{ParsedConfig, UserConfig},
+ windows::traits::WindowGetters,
wm_event::WmEvent,
wm_state::WmState,
};
@@ -14,6 +15,9 @@ pub fn reload_config(
) -> anyhow::Result<()> {
info!("Config reloaded.");
+ // Keep reference to old config for comparison.
+ let old_config = config.value.clone();
+
// Re-evaluate user config file and set its values in state.
tokio::task::block_in_place(|| {
let rt = tokio::runtime::Handle::current();
@@ -22,8 +26,43 @@ pub fn reload_config(
// TODO: Run window rules on all windows.
- // Update configs of active workspaces.
+ update_workspace_configs(state, config)?;
+
+ update_container_gaps(state, config);
+
+ update_window_effects(&old_config, state, config)?;
+
+ // Clear active binding modes.
+ state.binding_modes = Vec::new();
+
+ // Redraw full container tree.
+ let root_container = state.root_container.clone();
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(root_container.into());
+
+ // Emit the updated config.
+ state.emit_event(WmEvent::UserConfigChanged {
+ config_path: config
+ .path
+ .to_str()
+ .context("Invalid config path.")?
+ .to_string(),
+ config_string: config.value_str.clone(),
+ parsed_config: config.value.clone(),
+ });
+
+ Ok(())
+}
+
+/// Update configs of active workspaces.
+fn update_workspace_configs(
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
let workspaces = state.workspaces();
+
for workspace in &workspaces {
let workspace_config = config
.value
@@ -54,33 +93,62 @@ pub fn reload_config(
}
}
- // Update inner gaps of tiling containers.
- for container in state
+ Ok(())
+}
+
+/// Updates outer gap of workspaces and inner gaps of tiling containers.
+fn update_container_gaps(state: &mut WmState, config: &UserConfig) {
+ let tiling_containers = state
.root_container
.self_and_descendants()
.into_iter()
- .filter_map(|c| c.as_tiling_container().ok())
- {
+ .filter_map(|container| container.as_tiling_container().ok());
+
+ for container in tiling_containers {
container.set_inner_gap(config.value.gaps.inner_gap.clone());
}
- // Clear active binding modes.
- state.binding_modes = Vec::new();
+ for workspace in state.workspaces() {
+ workspace.set_outer_gap(config.value.gaps.outer_gap.clone());
+ }
+}
- // Redraw full container tree.
- let root_container = state.root_container.clone();
- state.add_container_to_redraw(root_container.into());
+fn update_window_effects(
+ old_config: &ParsedConfig,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let focused_container =
+ state.focused_container().context("No focused container.")?;
- // Emit the updated config.
- state.emit_event(WmEvent::UserConfigChanged {
- config_path: config
- .path
- .to_str()
- .context("Invalid config path.")?
- .to_string(),
- config_string: config.value_str.clone(),
- parsed_config: config.value.clone(),
- });
+ let window_effects = &config.value.window_effects;
+ let old_window_effects = &old_config.window_effects;
+
+ // Window border effects are left as is if set to `null` in the config.
+ // However, when transitioning from a non-null value to `null`, we have
+ // to reset to the system default.
+ if window_effects.focused_window.border_color.is_none()
+ && old_window_effects.focused_window.border_color.is_some()
+ {
+ if let Ok(window) = focused_container.as_window_container() {
+ _ = window.native().set_border_color(None);
+ }
+ }
+
+ if window_effects.other_windows.border_color.is_none()
+ && old_window_effects.other_windows.border_color.is_some()
+ {
+ let unfocused_windows = state
+ .windows()
+ .into_iter()
+ .filter(|window| window.id() != focused_container.id());
+
+ for window in unfocused_windows {
+ _ = window.native().set_border_color(None);
+ }
+ }
+
+ state.pending_sync.reset_window_effects = true;
Ok(())
}
diff --git a/packages/wm/src/common/commands/sync_native_focus.rs b/packages/wm/src/common/commands/sync_native_focus.rs
deleted file mode 100644
index 20e5be8d..00000000
--- a/packages/wm/src/common/commands/sync_native_focus.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-use anyhow::Context;
-
-use crate::{
- common::platform::Platform,
- containers::{traits::CommonGetters, Container},
- windows::traits::WindowGetters,
- wm_event::WmEvent,
- wm_state::WmState,
-};
-
-pub fn sync_native_focus(state: &mut WmState) -> anyhow::Result<()> {
- if !state.has_pending_focus_sync {
- return Ok(());
- }
-
- // Get the container that the WM believes should have focus.
- let focused_container =
- state.focused_container().context("No focused container.")?;
-
- let native_window = match focused_container.clone() {
- Container::TilingWindow(window) => window.native(),
- Container::NonTilingWindow(window) => window.native(),
- _ => Platform::desktop_window(),
- };
-
- // Set focus to the given window handle. If the container is a normal
- // window, then this will trigger a `PlatformEvent::WindowFocused` event.
- if Platform::foreground_window() != native_window {
- let _ = native_window.set_foreground();
- }
-
- // TODO: Change z-index of workspace windows that match the focused
- // container's state. Make sure not to decrease z-index for floating
- // windows that are always on top.
-
- state.emit_event(WmEvent::FocusChanged {
- focused_container: focused_container.to_dto()?,
- });
-
- state.has_pending_focus_sync = false;
-
- Ok(())
-}
diff --git a/packages/wm/src/common/events/handle_display_settings_changed.rs b/packages/wm/src/common/events/handle_display_settings_changed.rs
index 2e2c17db..0c286f2e 100644
--- a/packages/wm/src/common/events/handle_display_settings_changed.rs
+++ b/packages/wm/src/common/events/handle_display_settings_changed.rs
@@ -124,6 +124,7 @@ pub fn handle_display_settings_changed(
// Redraw full container tree.
state
+ .pending_sync
.containers_to_redraw
.push(state.root_container.clone().into());
diff --git a/packages/wm/src/common/events/handle_mouse_move.rs b/packages/wm/src/common/events/handle_mouse_move.rs
index c45bdb45..4a41ed61 100644
--- a/packages/wm/src/common/events/handle_mouse_move.rs
+++ b/packages/wm/src/common/events/handle_mouse_move.rs
@@ -29,7 +29,7 @@ pub fn handle_mouse_move(
if focused_container.id() != window.id() {
set_focused_descendant(window.as_container(), None);
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
}
}
diff --git a/packages/wm/src/common/events/handle_window_focused.rs b/packages/wm/src/common/events/handle_window_focused.rs
index 87313423..8ab81e1f 100644
--- a/packages/wm/src/common/events/handle_window_focused.rs
+++ b/packages/wm/src/common/events/handle_window_focused.rs
@@ -44,7 +44,7 @@ pub fn handle_window_focused(
.unwrap_or(false)
{
info!("Overriding native focus.");
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
return Ok(());
}
diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized.rs b/packages/wm/src/common/events/handle_window_moved_or_resized.rs
index a35cabda..135724a7 100644
--- a/packages/wm/src/common/events/handle_window_moved_or_resized.rs
+++ b/packages/wm/src/common/events/handle_window_moved_or_resized.rs
@@ -30,7 +30,7 @@ pub fn handle_window_moved_or_resized(
// Snap window to its original position if it's the only window in the
// workspace.
if parent.is_workspace() && window.tiling_siblings().count() == 0 {
- state.containers_to_redraw.push(window.into());
+ state.pending_sync.containers_to_redraw.push(window.into());
return Ok(());
}
diff --git a/packages/wm/src/containers/commands/focus_in_direction.rs b/packages/wm/src/containers/commands/focus_in_direction.rs
index bba845d2..27aea80f 100644
--- a/packages/wm/src/containers/commands/focus_in_direction.rs
+++ b/packages/wm/src/containers/commands/focus_in_direction.rs
@@ -47,7 +47,7 @@ pub fn focus_in_direction(
// Set focus to the target container.
if let Some(focus_target) = focus_target {
set_focused_descendant(focus_target, None);
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
}
Ok(())
diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rs
index 6f89997f..f32a149d 100644
--- a/packages/wm/src/containers/commands/mod.rs
+++ b/packages/wm/src/containers/commands/mod.rs
@@ -5,7 +5,6 @@ mod flatten_child_split_containers;
mod flatten_split_container;
mod focus_in_direction;
mod move_container_within_tree;
-mod redraw;
mod replace_container;
mod resize_tiling_container;
mod set_focused_descendant;
@@ -18,7 +17,6 @@ pub use flatten_child_split_containers::*;
pub use flatten_split_container::*;
pub use focus_in_direction::*;
pub use move_container_within_tree::*;
-pub use redraw::*;
pub use replace_container::*;
pub use resize_tiling_container::*;
pub use set_focused_descendant::*;
diff --git a/packages/wm/src/containers/commands/redraw.rs b/packages/wm/src/containers/commands/redraw.rs
deleted file mode 100644
index 965d6ed8..00000000
--- a/packages/wm/src/containers/commands/redraw.rs
+++ /dev/null
@@ -1,79 +0,0 @@
-use anyhow::Context;
-
-use crate::{
- common::DisplayState,
- containers::traits::{CommonGetters, PositionGetters},
- windows::{traits::WindowGetters, WindowState},
- wm_state::WmState,
-};
-
-pub fn redraw(state: &mut WmState) -> anyhow::Result<()> {
- for window in &state.windows_to_redraw() {
- let workspace =
- window.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(),
- },
- );
-
- // Restore window if it's minimized/maximized and shouldn't be. This is
- // needed to be able to move and resize it.
- match window.state() {
- // Need to restore window if transitioning from maximized fullscreen
- // to non-maximized fullscreen.
- WindowState::Fullscreen(s) => {
- if !s.maximized && window.native().is_maximized() {
- let _ = window.native().restore();
- }
- }
- // No need to restore window if it'll be minimized. Transitioning
- // from maximized to minimized works without restoring.
- WindowState::Minimized => {}
- _ => {
- if window.native().is_minimized() || window.native().is_maximized()
- {
- let _ = window.native().restore();
- }
- }
- }
-
- let rect = window.to_rect()?.apply_delta(&window.border_delta());
-
- let is_visible = match window.display_state() {
- DisplayState::Showing | DisplayState::Shown => true,
- _ => false,
- };
-
- let _ =
- window
- .native()
- .set_position(&window.state(), is_visible, &rect);
-
- // When there's a mismatch between the DPI of the monitor and the
- // window, the window might be sized incorrectly after the first move.
- // If we set the position twice, inconsistencies after the first move
- // are resolved.
- if window.has_pending_dpi_adjustment() {
- let _ =
- window
- .native()
- .set_position(&window.state(), is_visible, &rect);
-
- window.set_has_pending_dpi_adjustment(false);
- }
- }
-
- state.clear_containers_to_redraw();
-
- Ok(())
-}
diff --git a/packages/wm/src/windows/commands/apply_window_effects.rs b/packages/wm/src/windows/commands/apply_window_effects.rs
deleted file mode 100644
index 2d485959..00000000
--- a/packages/wm/src/windows/commands/apply_window_effects.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-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/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rs
index 037dfb7c..a829128e 100644
--- a/packages/wm/src/windows/commands/manage_window.rs
+++ b/packages/wm/src/windows/commands/manage_window.rs
@@ -50,10 +50,10 @@ pub fn manage_window(
// OS focus should be set to the newly added window in case it's not
// already focused.
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
// Sibling containers need to be redrawn if the window is tiling.
- state.add_container_to_redraw(
+ state.pending_sync.containers_to_redraw.push(
if window.state() == WindowState::Tiling {
window.parent().context("No parent.")?
} else {
diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rs
index 19828ef7..cd4daf4c 100644
--- a/packages/wm/src/windows/commands/mod.rs
+++ b/packages/wm/src/windows/commands/mod.rs
@@ -1,4 +1,3 @@
-mod apply_window_effects;
mod manage_window;
mod move_window_in_direction;
mod move_window_to_workspace;
@@ -7,7 +6,6 @@ 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/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rs
index a867ef71..7b1fff69 100644
--- a/packages/wm/src/windows/commands/move_window_in_direction.rs
+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs
@@ -149,6 +149,7 @@ fn move_to_sibling_container(
)?;
state
+ .pending_sync
.containers_to_redraw
.extend([sibling_window.into(), window_to_move.into()]);
}
@@ -184,6 +185,7 @@ fn move_to_sibling_container(
// TODO: Only redraw tiling containers.
state
+ .pending_sync
.containers_to_redraw
.extend([target_parent.into(), parent.into()]);
}
@@ -233,6 +235,7 @@ fn move_to_workspace_in_direction(
// TODO: Only redraw tiling containers.
state
+ .pending_sync
.containers_to_redraw
.extend([workspace.into(), parent.into()]);
};
@@ -295,6 +298,7 @@ fn change_workspace_tiling_direction(
resize_tiling_container(&window_to_move.into(), 0.5);
state
+ .pending_sync
.containers_to_redraw
.extend(workspace.tiling_children().map(Into::into));
@@ -405,6 +409,7 @@ fn insert_into_ancestor(
)?;
state
+ .pending_sync
.containers_to_redraw
.extend(target_ancestor.tiling_children().map(Into::into));
@@ -442,7 +447,10 @@ fn move_floating_window(
}
window_to_move.set_floating_placement(position_rect);
- state.containers_to_redraw.push(window_to_move.into());
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(window_to_move.into());
}
Ok(())
diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rs
index ef8abd01..e5bea652 100644
--- a/packages/wm/src/windows/commands/resize_window.rs
+++ b/packages/wm/src/windows/commands/resize_window.rs
@@ -71,6 +71,7 @@ fn resize_tiling_window(
);
state
+ .pending_sync
.containers_to_redraw
.extend(parent.tiling_children().map(Into::into));
}
@@ -154,7 +155,10 @@ fn resize_floating_window(
new_height,
));
- state.add_container_to_redraw(window.clone().into());
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(window.clone().into());
Ok(())
}
diff --git a/packages/wm/src/windows/commands/toggle_window_state.rs b/packages/wm/src/windows/commands/toggle_window_state.rs
index 1e69c569..13faae7e 100644
--- a/packages/wm/src/windows/commands/toggle_window_state.rs
+++ b/packages/wm/src/windows/commands/toggle_window_state.rs
@@ -42,7 +42,7 @@ pub fn toggle_window_state(
// The `update_window_state` call will already add the window for redraw
// on tiling <-> non-tiling state changes. However, on toggle, we always
// want to add the window for redraw.
- state.containers_to_redraw.push(window.into());
+ state.pending_sync.containers_to_redraw.push(window.into());
Ok(())
}
diff --git a/packages/wm/src/windows/commands/unmanage_window.rs b/packages/wm/src/windows/commands/unmanage_window.rs
index a1e68567..2f3b9f93 100644
--- a/packages/wm/src/windows/commands/unmanage_window.rs
+++ b/packages/wm/src/windows/commands/unmanage_window.rs
@@ -42,7 +42,7 @@ pub fn unmanage_window(
// Reassign focus to suitable target.
if let Some(focus_target) = focus_target {
set_focused_descendant(focus_target, None);
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
state.unmanaged_or_minimized_timestamp =
Some(std::time::Instant::now());
}
@@ -55,6 +55,7 @@ pub fn unmanage_window(
.context("No ancestor to redraw.")?;
state
+ .pending_sync
.containers_to_redraw
.extend(ancestor_to_redraw.tiling_children().map(Into::into));
}
diff --git a/packages/wm/src/windows/commands/update_window_state.rs b/packages/wm/src/windows/commands/update_window_state.rs
index d04c85df..e4386f20 100644
--- a/packages/wm/src/windows/commands/update_window_state.rs
+++ b/packages/wm/src/windows/commands/update_window_state.rs
@@ -87,6 +87,7 @@ fn set_tiling(
)?;
state
+ .pending_sync
.containers_to_redraw
.extend(target_parent.tiling_children().map(Into::into))
}
@@ -136,16 +137,19 @@ fn set_non_tiling(
.focus_target_after_removal(&non_tiling_window.clone().into())
{
set_focused_descendant(focus_target, None);
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
state.unmanaged_or_minimized_timestamp =
Some(std::time::Instant::now());
}
}
- state.containers_to_redraw.push(non_tiling_window.into());
+ let changed_containers = std::iter::once(non_tiling_window.into())
+ .chain(workspace.tiling_children().map(Into::into));
+
state
+ .pending_sync
.containers_to_redraw
- .extend(workspace.tiling_children().map(Into::into))
+ .extend(changed_containers)
}
}
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index 9b3ba0af..08c97a8c 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -2,11 +2,11 @@ use anyhow::Context;
use tokio::sync::mpsc::{self};
use uuid::Uuid;
+use crate::common::commands::platform_sync;
use crate::common::events::handle_mouse_move;
use crate::{
app_command::InvokeCommand,
common::{
- commands::sync_native_focus,
events::{
handle_display_settings_changed, handle_window_destroyed,
handle_window_focused, handle_window_hidden,
@@ -16,7 +16,7 @@ use crate::{
},
platform::PlatformEvent,
},
- containers::{commands::redraw, traits::CommonGetters},
+ containers::traits::CommonGetters,
user_config::UserConfig,
wm_event::WmEvent,
wm_state::WmState,
@@ -83,8 +83,7 @@ impl WindowManager {
PlatformEvent::WindowTitleChanged(_) => Ok(()),
}?;
- redraw(state)?;
- sync_native_focus(state)?;
+ platform_sync(state, config)?;
Ok(())
}
@@ -121,8 +120,7 @@ impl WindowManager {
}
}
- redraw(state)?;
- sync_native_focus(state)?;
+ platform_sync(state, config)?;
Ok(subject_container.id())
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index c954a764..e2f02515 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -7,14 +7,13 @@ use uuid::Uuid;
use crate::{
common::{
- commands::sync_native_focus,
+ commands::platform_sync,
platform::{NativeMonitor, NativeWindow, Platform},
Direction,
},
containers::{
- commands::{redraw, set_focused_descendant},
- traits::CommonGetters,
- Container, RootContainer, WindowContainer,
+ commands::set_focused_descendant, traits::CommonGetters, Container,
+ RootContainer, WindowContainer,
},
monitors::{commands::add_monitor, Monitor},
user_config::{BindingModeConfig, UserConfig},
@@ -28,14 +27,7 @@ pub struct WmState {
/// root node, followed by workspaces, then split containers/windows.
pub root_container: RootContainer,
- /// Containers (and their descendants) that have a pending redraw.
- pub containers_to_redraw: Vec<Container>,
-
- /// Whether native focus needs to be reassigned to the WM's focused
- /// container.
- pub has_pending_focus_sync: bool,
-
- pub active_border_window: Option<WindowContainer>,
+ pub pending_sync: PendingSync,
/// Name of the most recently focused workspace.
///
@@ -43,6 +35,11 @@ pub struct WmState {
/// workspace focus.
pub recent_workspace_name: Option<String>,
+ /// Container that most recently had focus synced.
+ ///
+ /// Used for updating window effects on focus change.
+ pub recent_focused_container: Option<Container>,
+
/// Time since a previously focused window was unmanaged or minimized.
///
/// Used to decide whether to override incoming focus events.
@@ -55,13 +52,28 @@ pub struct WmState {
event_tx: mpsc::UnboundedSender<WmEvent>,
}
+pub struct PendingSync {
+ /// Containers (and their descendants) that have a pending redraw.
+ pub containers_to_redraw: Vec<Container>,
+
+ /// Whether native focus should be reassigned to the WM's focused
+ /// container.
+ pub focus_change: bool,
+
+ /// Whether window effects should be reset.
+ pub reset_window_effects: bool,
+}
+
impl WmState {
pub fn new(event_tx: mpsc::UnboundedSender<WmEvent>) -> Self {
Self {
root_container: RootContainer::new(),
- containers_to_redraw: Vec::new(),
- has_pending_focus_sync: false,
- active_border_window: None,
+ pending_sync: PendingSync {
+ containers_to_redraw: Vec::new(),
+ focus_change: false,
+ reset_window_effects: false,
+ },
+ recent_focused_container: None,
recent_workspace_name: None,
unmanaged_or_minimized_timestamp: None,
binding_modes: Vec::new(),
@@ -104,10 +116,10 @@ impl WmState {
.context("Failed to get container to focus.")?;
set_focused_descendant(container_to_focus, None);
- self.has_pending_focus_sync = true;
- redraw(self)?;
- sync_native_focus(self)?;
+ self.pending_sync.focus_change = true;
+ self.pending_sync.reset_window_effects = true;
+ platform_sync(self, config)?;
Ok(())
}
@@ -236,25 +248,17 @@ impl WmState {
/// tiling -> floating), the original detached window might still be
/// queued for a redraw and should be ignored.
pub fn windows_to_redraw(&self) -> Vec<WindowContainer> {
+ // TODO: Get unique windows.
self
+ .pending_sync
.containers_to_redraw
.iter()
.flat_map(|container| container.self_and_descendants())
.filter(|container| !container.is_detached())
.filter_map(|container| container.try_into().ok())
- // .unique()
.collect()
}
- pub fn add_container_to_redraw(&mut self, container: Container) {
- self.containers_to_redraw.push(container);
- }
-
- /// Removes all containers from the redraw queue.
- pub fn clear_containers_to_redraw(&mut self) {
- self.containers_to_redraw.clear();
- }
-
/// Gets the currently focused container. This can either be a window or
/// a workspace without any descendant windows.
pub fn focused_container(&self) -> Option<Container> {
diff --git a/packages/wm/src/workspaces/commands/focus_workspace.rs b/packages/wm/src/workspaces/commands/focus_workspace.rs
index 52d425e7..9450647c 100644
--- a/packages/wm/src/workspaces/commands/focus_workspace.rs
+++ b/packages/wm/src/workspaces/commands/focus_workspace.rs
@@ -59,11 +59,13 @@ pub fn focus_workspace(
.unwrap_or_else(|| workspace_to_focus.clone().into());
set_focused_descendant(container_to_focus, None);
- state.has_pending_focus_sync = true;
+ state.pending_sync.focus_change = true;
// Display the workspace to switch focus to.
- state.containers_to_redraw.push(displayed_workspace.into());
- state.containers_to_redraw.push(workspace_to_focus.into());
+ state
+ .pending_sync
+ .containers_to_redraw
+ .extend([displayed_workspace.into(), workspace_to_focus.into()]);
// Get empty workspace to destroy (if one is found). Cannot destroy
// empty workspaces if they're the only workspace on the monitor.
@@ -81,6 +83,7 @@ pub fn focus_workspace(
// Save the currently focused workspace as recent.
state.recent_workspace_name = Some(focused_workspace.config().name);
}
+
Ok(())
}
diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rs
index 3902af92..ca2f9819 100644
--- a/packages/wm/src/workspaces/workspace.rs
+++ b/packages/wm/src/workspaces/workspace.rs
@@ -31,7 +31,7 @@ struct WorkspaceInner {
child_focus_order: VecDeque<Uuid>,
tiling_direction: TilingDirection,
config: WorkspaceConfig,
- outer_gaps: RectDelta,
+ outer_gap: RectDelta,
}
/// User-friendly representation of a workspace.
@@ -54,7 +54,7 @@ pub struct WorkspaceDto {
impl Workspace {
pub fn new(
config: WorkspaceConfig,
- outer_gaps: RectDelta,
+ outer_gap: RectDelta,
tiling_direction: TilingDirection,
) -> Self {
let workspace = WorkspaceInner {
@@ -64,7 +64,7 @@ impl Workspace {
child_focus_order: VecDeque::new(),
tiling_direction,
config,
- outer_gaps,
+ outer_gap,
};
Self(Rc::new(RefCell::new(workspace)))
@@ -89,8 +89,12 @@ impl Workspace {
.unwrap_or(false)
}
- fn outer_gaps(&self) -> Ref<'_, RectDelta> {
- Ref::map(self.0.borrow(), |inner| &inner.outer_gaps)
+ fn outer_gap(&self) -> Ref<'_, RectDelta> {
+ Ref::map(self.0.borrow(), |inner| &inner.outer_gap)
+ }
+
+ pub fn set_outer_gap(&self, outer_gap: RectDelta) {
+ self.0.borrow_mut().outer_gap = outer_gap;
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
@@ -124,8 +128,8 @@ impl PositionGetters for Workspace {
Ok(
parent.width()?
- - self.outer_gaps().left.to_pixels(parent.width()?)
- - self.outer_gaps().right.to_pixels(parent.width()?),
+ - self.outer_gap().left.to_pixels(parent.width()?)
+ - self.outer_gap().right.to_pixels(parent.width()?),
)
}
@@ -134,20 +138,20 @@ impl PositionGetters for Workspace {
Ok(
parent.height()?
- - self.outer_gaps().top.to_pixels(parent.height()?)
- - self.outer_gaps().bottom.to_pixels(parent.height()?),
+ - self.outer_gap().top.to_pixels(parent.height()?)
+ - self.outer_gap().bottom.to_pixels(parent.height()?),
)
}
fn x(&self) -> anyhow::Result<i32> {
let parent = self.parent().context("Workspace has no parent.")?;
- Ok(parent.x()? + self.outer_gaps().left.to_pixels(parent.width()?))
+ Ok(parent.x()? + self.outer_gap().left.to_pixels(parent.width()?))
}
fn y(&self) -> anyhow::Result<i32> {
let parent = self.parent().context("Workspace has no parent.")?;
- Ok(parent.y()? + self.outer_gaps().top.to_pixels(parent.height()?))
+ Ok(parent.y()? + self.outer_gap().top.to_pixels(parent.height()?))
}
}