refactor(windows): Window::focus, maximize and unmaximize call backend()

As on KDE, the shared-looking operations of Window are the backend calls:
Window::focus(mff) -> eyre::Result<()> is backend().focus(..)?,
Window::maximize() / unmaximize() are let _ = backend().maximize(.., bool).
The Windows primitives they used to be are renamed focus_native,
maximize_native and unmaximize_native; WindowsBackend and the slide-in code
in animation/transition.rs (which is inside the focus) call those. The 40
focus call sites and 5 maximize call sites that spelled
backend().x(WindowId::from_hwnd(w.hwnd), ..) are w.focus(..)? / w.maximize()
again, the text KDE has; the two sites that only have a raw handle
(window_manager.rs, focus_manager.rs) use Window::from(hwnd).focus(..). The
result handling (let _ =) is inside the methods. Item 2 (second half) of the
Windows list in core-model.md section 7.

Not a behaviour change: every call ends in the same backend call as before.
Verified: Windows clippy -D warnings via tools/fast-check PASS.
This commit is contained in:
LGUG2Z committed 2026-10-31 11:56:45 -07:00
1 parent f3d4765186
commit de45c2f82a
9 files changed
+70 -78

No files matched your search

+1 -1
View File
@@ -531,7 +531,7 @@ pub fn focus_if_asked(hwnd: isize) {
let asked = FOCUS_LATER.lock().take(hwnd, Instant::now());
if let Some(mouse_follows_focus) = asked
&& let Err(error) = (crate::window::Window { hwnd }).focus(mouse_follows_focus)
&& let Err(error) = (crate::window::Window { hwnd }).focus_native(mouse_follows_focus)
{
tracing::warn!("workspace transition: failed to focus hwnd {hwnd}: {error}");
}
+2 -3
View File
@@ -8,8 +8,7 @@ use std::sync::Arc;
use std::sync::OnceLock;
use crate::WindowManager;
use crate::windows_backend::backend;
use komorebi_backend::WindowId;
use crate::window::Window;
pub struct Notification(isize);
@@ -66,7 +65,7 @@ pub fn handle_notifications(wm: Arc<Mutex<WindowManager>>) -> color_eyre::Result
for notification in receiver {
let mouse_follows_focus = wm.lock().mouse_follows_focus;
let _ = backend().focus(WindowId::from_hwnd(*notification), mouse_follows_focus);
let _ = Window::from(*notification).focus(mouse_follows_focus);
}
Ok(())
+3 -5
View File
@@ -1939,8 +1939,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
}
WorkspaceWindowLocation::Maximized => {
if let Some(window) = &mut self.focused_workspace_mut()?.maximized_window {
backend()
.focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
}
WorkspaceWindowLocation::Container(container_idx, window_idx) => {
@@ -1957,8 +1956,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
.floating_windows_mut()
.get_mut(window_idx)
{
backend()
.focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
}
}
@@ -2390,7 +2388,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
}
};
if let Some(window) = to_focus {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
}
Ok(())
}
+4 -10
View File
@@ -304,7 +304,7 @@ impl WindowManager {
match event {
WindowManagerEvent::Raise(window) => {
backend().focus(WindowId::from_hwnd(window.hwnd), false)?;
window.focus(false)?;
self.has_pending_raise_op = false;
}
WindowManagerEvent::Destroy(_, window) | WindowManagerEvent::Unmanage(window) => {
@@ -438,7 +438,7 @@ impl WindowManager {
if let Some(monocle) = &workspace.monocle_container {
if let Some(window) = monocle.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), false)?;
window.focus(false)?;
}
} else {
workspace.focus_container_by_window(window.hwnd)?;
@@ -624,10 +624,7 @@ impl WindowManager {
// first send the `FocusChange` event and only the `Show` event after
// and we will be focusing the desktop on the `FocusChange` event since
// it is still empty.
backend().focus(
WindowId::from_hwnd(window.hwnd),
self.mouse_follows_focus,
)?;
window.focus(self.mouse_follows_focus)?;
}
}
@@ -818,10 +815,7 @@ impl WindowManager {
self.update_focused_workspace(false, false)?;
// Make sure to give focus to the moved window again
backend().focus(
WindowId::from_hwnd(window.hwnd),
self.mouse_follows_focus,
)?;
window.focus(self.mouse_follows_focus)?;
}
} else if window_management_behaviour.float_override {
workspace.floating_windows_mut().push_back(window);
+1 -3
View File
@@ -375,9 +375,7 @@ impl Stackbar {
// Restore the window corresponding to the tab we have clicked
window.restore_with_border(false);
if let Err(err) =
backend().focus(WindowId::from_hwnd(window.hwnd), false)
{
if let Err(err) = window.focus(false) {
tracing::error!(
"stackbar WMLBUTTONDOWN focus error: hwnd {} ({})",
*window,
+24 -2
View File
@@ -42,8 +42,10 @@ use crate::transparency_manager;
use crate::window_manager_event::WindowManagerEvent;
use crate::windows_api;
use crate::windows_api::WindowsApi;
use crate::windows_backend::backend;
use color_eyre::eyre;
use crossbeam_utils::atomic::AtomicConsume;
use komorebi_backend::WindowId;
pub use komorebi_config::aspect_ratio::AspectRatio;
pub use komorebi_config::aspect_ratio::PredefinedAspectRatio;
use regex::Regex;
@@ -519,7 +521,7 @@ impl Window {
WindowsApi::close_window(self.hwnd)
}
pub fn maximize(self) {
pub fn maximize_native(self) {
let mut programmatically_hidden_hwnds = HIDDEN_HWNDS.lock();
if let Some(idx) = programmatically_hidden_hwnds
.iter()
@@ -531,7 +533,7 @@ impl Window {
WindowsApi::maximize_window(self.hwnd);
}
pub fn unmaximize(self) {
pub fn unmaximize_native(self) {
let mut programmatically_hidden_hwnds = HIDDEN_HWNDS.lock();
if let Some(idx) = programmatically_hidden_hwnds
.iter()
@@ -543,7 +545,27 @@ impl Window {
WindowsApi::unmaximize_window(self.hwnd);
}
// The operations the shared code calls on a window go through the backend, as on KDE
// (`Window::focus`, `maximize` and `unmaximize` there call `backend()` too). The Windows
// primitives they end in are `focus_native`, `maximize_native` and `unmaximize_native`, which
// `WindowsBackend` calls; they stay public for the animation code that is inside the focus.
/// Gives the window the focus (and the pointer, if `mouse_follows_focus`).
pub fn focus(self, mouse_follows_focus: bool) -> eyre::Result<()> {
Ok(backend().focus(WindowId::from_hwnd(self.hwnd), mouse_follows_focus)?)
}
/// Maximizes the window; a window that is gone is left alone.
pub fn maximize(self) {
let _ = backend().maximize(WindowId::from_hwnd(self.hwnd), true);
}
/// Undoes `maximize`; a window that is gone is left alone.
pub fn unmaximize(self) {
let _ = backend().maximize(WindowId::from_hwnd(self.hwnd), false);
}
pub fn focus_native(self, mouse_follows_focus: bool) -> eyre::Result<()> {
// Focusing a window that is hidden shows it, which is not for it to be
// when it is yet to slide in: it is focused when its animation shows it
if transition::is_entering(self.hwnd) {
+20 -27
View File
@@ -1132,17 +1132,17 @@ impl WindowManager {
if follow_focus {
if let Some(window) = self.focused_workspace()?.maximized_window {
if trigger_focus {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
} else if let Some(container) = &self.focused_workspace()?.monocle_container {
if let Some(window) = container.focused_window()
&& trigger_focus
{
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
} else if let Ok(window) = self.focused_window_mut() {
if trigger_focus {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
} else {
let desktop_window = Window::from(WindowsApi::desktop_window()?);
@@ -1182,7 +1182,7 @@ impl WindowManager {
&& let Ok(window) = self.focused_window_mut()
&& trigger_focus
{
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
}
}
@@ -2014,7 +2014,7 @@ impl WindowManager {
if let Some(idx) = target_idx {
focused_workspace.floating_windows.focus(idx);
if let Some(window) = focused_workspace.floating_windows().get(idx) {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
}
return Ok(());
}
@@ -2074,11 +2074,11 @@ impl WindowManager {
if let Ok(focused_workspace) = self.focused_workspace_mut() {
if let Some(window) = focused_workspace.maximized_window {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
cross_monitor_monocle_or_max = true;
} else if let Some(monocle) = &focused_workspace.monocle_container {
if let Some(window) = monocle.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
cross_monitor_monocle_or_max = true;
}
} else if focused_workspace.layer == WorkspaceLayer::Tiling {
@@ -2130,15 +2130,12 @@ impl WindowManager {
}
} else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() {
if let Some(window) = ws.focused_floating_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
} else {
ws.layer = WorkspaceLayer::Tiling;
if let Ok(focused_window) = self.focused_window() {
backend().focus(
WindowId::from_hwnd(focused_window.hwnd),
self.mouse_follows_focus,
)?;
focused_window.focus(self.mouse_follows_focus)?;
}
}
}
@@ -2280,12 +2277,11 @@ impl WindowManager {
if let Ok(focused_workspace) = self.focused_workspace_mut() {
if let Some(window) = focused_workspace.maximized_window {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
cross_monitor_monocle_or_max = true;
} else if let Some(monocle) = &focused_workspace.monocle_container {
if let Some(window) = monocle.focused_window() {
backend()
.focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
cross_monitor_monocle_or_max = true;
}
} else if focused_workspace.layer == WorkspaceLayer::Tiling {
@@ -2343,15 +2339,12 @@ impl WindowManager {
}
} else if ws.layer == WorkspaceLayer::Floating && !ws.floating_windows().is_empty() {
if let Some(window) = ws.focused_floating_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
} else {
ws.layer = WorkspaceLayer::Tiling;
if let Ok(focused_window) = self.focused_window() {
backend().focus(
WindowId::from_hwnd(focused_window.hwnd),
self.mouse_follows_focus,
)?;
focused_window.focus(self.mouse_follows_focus)?;
}
}
}
@@ -2662,7 +2655,7 @@ impl WindowManager {
if let Some(idx) = target_idx
&& let Some(window) = focused_workspace.floating_windows().get(idx)
{
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
}
Ok(())
@@ -2703,7 +2696,7 @@ impl WindowManager {
self.toggle_monocle()?;
} else {
let hwnd = self.focused_window_mut()?.hwnd;
backend().focus(WindowId::from_hwnd(hwnd), self.mouse_follows_focus)?;
Window::from(hwnd).focus(self.mouse_follows_focus)?;
}
Ok(())
@@ -2763,7 +2756,7 @@ impl WindowManager {
container.load_focused_window();
if let Some(window) = container.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
self.update_focused_workspace(self.mouse_follows_focus, true)
@@ -2800,7 +2793,7 @@ impl WindowManager {
container.load_focused_window();
if let Some(window) = container.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
self.update_focused_workspace(self.mouse_follows_focus, true)
@@ -2833,7 +2826,7 @@ impl WindowManager {
container.load_focused_window();
if let Some(window) = container.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
self.update_focused_workspace(self.mouse_follows_focus, true)
@@ -2977,7 +2970,7 @@ impl WindowManager {
if changed_focus && let Some(container) = workspace.focused_container_mut() {
container.load_focused_window();
if let Some(window) = container.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
}
}
@@ -3139,7 +3132,7 @@ impl WindowManager {
if toggle_float_placement.should_center() {
window.center(&work_area, toggle_float_placement.should_resize())?;
}
backend().focus(WindowId::from_hwnd(window.hwnd), self.mouse_follows_focus)?;
window.focus(self.mouse_follows_focus)?;
Ok(())
}
+3 -3
View File
@@ -150,7 +150,7 @@ impl Backend for WindowsBackend {
fn focus(&self, id: WindowId, mouse_follows_focus: bool) -> Result<()> {
live(id)?
.focus(mouse_follows_focus)
.focus_native(mouse_follows_focus)
.map_err(|e| failed(id, e))
}
@@ -203,9 +203,9 @@ impl Backend for WindowsBackend {
fn maximize(&self, id: WindowId, maximize: bool) -> Result<()> {
let window = live(id)?;
if maximize {
window.maximize();
window.maximize_native();
} else {
window.unmaximize();
window.unmaximize_native();
}
Ok(())
}
+12 -24
View File
@@ -557,7 +557,7 @@ impl Workspace {
&& let Some(window) = container.focused_window()
{
container.restore();
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
return self.apply_wallpaper(hmonitor, monitor_wp);
}
@@ -587,30 +587,18 @@ impl Workspace {
// when switching to a workspace
if let Some(window) = to_focus {
if self.maximized_window.is_none() && matches!(self.layer, WorkspaceLayer::Tiling) {
backend().focus(WindowId::from_hwnd(window.hwnd), mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
} else if let Some(maximized_window) = self.maximized_window {
maximized_window.restore_window();
backend().focus(
WindowId::from_hwnd(maximized_window.hwnd),
mouse_follows_focus,
)?;
maximized_window.focus(mouse_follows_focus)?;
} else if let Some(floating_window) = self.focused_floating_window() {
backend().focus(
WindowId::from_hwnd(floating_window.hwnd),
mouse_follows_focus,
)?;
floating_window.focus(mouse_follows_focus)?;
}
} else if let Some(maximized_window) = self.maximized_window {
maximized_window.restore_window();
backend().focus(
WindowId::from_hwnd(maximized_window.hwnd),
mouse_follows_focus,
)?;
maximized_window.focus(mouse_follows_focus)?;
} else if let Some(floating_window) = self.focused_floating_window() {
backend().focus(
WindowId::from_hwnd(floating_window.hwnd),
mouse_follows_focus,
)?;
floating_window.focus(mouse_follows_focus)?;
}
self.apply_wallpaper(hmonitor, monitor_wp)
@@ -731,7 +719,7 @@ impl Workspace {
window.set_position(&adjusted_work_area, true)?;
};
} else if let Some(window) = &mut self.maximized_window {
let _ = backend().maximize(WindowId::from_hwnd(window.hwnd), true);
window.maximize();
} else if !self.containers().is_empty() {
// windows that were not what they were told to be since the last layout, which
// is something that animations only show once they are done
@@ -1202,7 +1190,7 @@ impl Workspace {
if let Some(window) = self.maximized_window
&& window.hwnd == window_id
{
let _ = backend().maximize(WindowId::from_hwnd(window.hwnd), false);
window.unmaximize();
self.maximized_window = None;
self.maximized_window_restore_idx = None;
return Ok(());
@@ -1237,7 +1225,7 @@ impl Workspace {
} else {
container.load_focused_window();
if let Some(window) = container.focused_window() {
backend().focus(WindowId::from_hwnd(window.hwnd), false)?;
window.focus(false)?;
}
}
@@ -1892,7 +1880,7 @@ impl Workspace {
self.maximized_window = floating_window;
self.maximized_window_restore_idx = Option::from(focused_idx);
if let Some(window) = self.maximized_window {
let _ = backend().maximize(WindowId::from_hwnd(window.hwnd), true);
window.maximize();
}
return Ok(());
@@ -1914,7 +1902,7 @@ impl Workspace {
self.maximized_window = Option::from(window);
self.maximized_window_restore_idx = monocle_restore_idx;
if let Some(window) = self.maximized_window {
let _ = backend().maximize(WindowId::from_hwnd(window.hwnd), true);
window.maximize();
}
return Ok(());
@@ -1944,7 +1932,7 @@ impl Workspace {
self.maximized_window_restore_idx = Option::from(focused_idx);
if let Some(window) = self.maximized_window {
let _ = backend().maximize(WindowId::from_hwnd(window.hwnd), true);
window.maximize();
}
self.focus_previous_container();