From 133311bbe219adf713a01352d401dc2bca4bdc20 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 4 Jun 2024 15:54:16 -0700 Subject: [PATCH] refactor(wm): use from trait to construct windows --- komorebi/src/process_command.rs | 12 +++--------- komorebi/src/window.rs | 6 ++++++ komorebi/src/window_manager.rs | 18 +++++++----------- komorebi/src/windows_callbacks.rs | 6 +++--- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index d6a5dcb5..1d138238 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -219,16 +219,10 @@ impl WindowManager { WindowsApi::left_click(); } SocketMessage::Close => { - Window { - hwnd: WindowsApi::foreground_window()?, - } - .close()?; + Window::from(WindowsApi::foreground_window()?).close()?; } SocketMessage::Minimize => { - Window { - hwnd: WindowsApi::foreground_window()?, - } - .minimize(); + Window::from(WindowsApi::foreground_window()?).minimize(); } SocketMessage::ToggleFloat => self.toggle_float()?, SocketMessage::ToggleMonocle => self.toggle_monocle()?, @@ -1342,7 +1336,7 @@ impl WindowManager { self.update_focused_workspace(false, false)?; } SocketMessage::DebugWindow(hwnd) => { - let window = Window { hwnd }; + let window = Window::from(hwnd); let mut rule_debug = RuleDebug::default(); let _ = window.should_manage(None, &mut rule_debug); let schema = serde_json::to_string_pretty(&rule_debug)?; diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index d91fcb67..d56464f1 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -50,6 +50,12 @@ impl From for Window { } } +impl From for Window { + fn from(value: HWND) -> Self { + Self { hwnd: value.0 } + } +} + #[allow(clippy::module_name_repetitions)] #[derive(Debug, Clone, Serialize, JsonSchema)] pub struct WindowDetails { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index da7d69a8..601a7a54 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -520,7 +520,7 @@ impl WindowManager { // Hide the window we are about to remove if it is on the currently focused workspace if op.is_origin(focused_monitor_idx, focused_workspace_idx) { - Window { hwnd: op.hwnd }.hide(); + Window::from(op.hwnd).hide(); should_update_focused_workspace = true; } @@ -550,7 +550,7 @@ impl WindowManager { .get_mut(op.target_workspace_idx) .ok_or_else(|| anyhow!("there is no workspace with that index"))?; - target_workspace.new_container_for_window(Window { hwnd: op.hwnd }); + target_workspace.new_container_for_window(Window::from(op.hwnd)); } // Only re-tile the focused workspace if we need to @@ -598,14 +598,14 @@ impl WindowManager { #[tracing::instrument(skip(self))] pub fn manage_focused_window(&mut self) -> Result<()> { let hwnd = WindowsApi::foreground_window()?; - let event = WindowManagerEvent::Manage(Window { hwnd }); + let event = WindowManagerEvent::Manage(Window::from(hwnd)); Ok(winevent_listener::event_tx().send(event)?) } #[tracing::instrument(skip(self))] pub fn unmanage_focused_window(&mut self) -> Result<()> { let hwnd = WindowsApi::foreground_window()?; - let event = WindowManagerEvent::Unmanage(Window { hwnd }); + let event = WindowManagerEvent::Unmanage(Window::from(hwnd)); Ok(winevent_listener::event_tx().send(event)?) } @@ -633,15 +633,13 @@ impl WindowManager { return Ok(()); } - let event = WindowManagerEvent::Raise(Window { hwnd }); + let event = WindowManagerEvent::Raise(Window::from(hwnd)); self.has_pending_raise_op = true; winevent_listener::event_tx().send(event)?; } else { tracing::debug!( "not raising unknown window: {}", - Window { - hwnd: WindowsApi::window_at_cursor_pos()? - } + Window::from(WindowsApi::window_at_cursor_pos()?) ); } @@ -765,9 +763,7 @@ impl WindowManager { window.focus(self.mouse_follows_focus)?; } } else { - let desktop_window = Window { - hwnd: WindowsApi::desktop_window()?, - }; + let desktop_window = Window::from(WindowsApi::desktop_window()?); let rect = self.focused_monitor_size()?; WindowsApi::center_cursor_in_rect(&rect)?; diff --git a/komorebi/src/windows_callbacks.rs b/komorebi/src/windows_callbacks.rs index de2e1950..14c8e5a3 100644 --- a/komorebi/src/windows_callbacks.rs +++ b/komorebi/src/windows_callbacks.rs @@ -22,7 +22,7 @@ pub extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { let is_maximized = WindowsApi::is_zoomed(hwnd); if is_visible && is_window && !is_minimized { - let window = Window { hwnd: hwnd.0 }; + let window = Window::from(hwnd); if let Ok(should_manage) = window.should_manage(None, &mut RuleDebug::default()) { if should_manage { @@ -48,7 +48,7 @@ pub extern "system" fn alt_tab_windows(hwnd: HWND, lparam: LPARAM) -> BOOL { let is_minimized = WindowsApi::is_iconic(hwnd); if is_visible && is_window && !is_minimized { - let window = Window { hwnd: hwnd.0 }; + let window = Window::from(hwnd); if let Ok(should_manage) = window.should_manage(None, &mut RuleDebug::default()) { if should_manage { @@ -74,7 +74,7 @@ pub extern "system" fn win_event_hook( return; } - let window = Window { hwnd: hwnd.0 }; + let window = Window::from(hwnd); let winevent = match WinEvent::try_from(event) { Ok(event) => event,