From 352c0100211679301c7858935cd988c8713ce20d Mon Sep 17 00:00:00 2001 From: Yusuf007R Date: Wed, 12 Oct 2022 12:44:23 -0500 Subject: [PATCH] feat(wm): manage return bool of PostMessage + update ahk library this commit makes a small refactor to the way PostMessageW is used so now be able to handle the returned bool, as well as adding a custom error message to WindowsApi::close_window and updating the ahk generated library #259 --- komorebi/src/process_command.rs | 2 +- komorebi/src/window.rs | 4 ++-- komorebi/src/windows_api.rs | 13 +++++++++++-- komorebic.lib.ahk | 8 ++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index dd00fed5..b8f64e68 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -177,7 +177,7 @@ impl WindowManager { SocketMessage::CycleStack(direction) => { self.cycle_container_window_in_direction(direction)?; } - SocketMessage::Close => self.focused_window()?.close(), + SocketMessage::Close => self.focused_window()?.close()?, SocketMessage::Minimize => self.focused_window()?.minimize(), SocketMessage::ToggleFloat => self.toggle_float()?, SocketMessage::ToggleMonocle => self.toggle_monocle()?, diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 48cf1f3b..a7e3f066 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -155,8 +155,8 @@ impl Window { WindowsApi::minimize_window(self.hwnd()); } - pub fn close(self) { - WindowsApi::close_window(self.hwnd()); + pub fn close(self) -> Result<()> { + WindowsApi::close_window(self.hwnd()) } pub fn restore(self) { diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 3529d779..b88f4a2c 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -339,8 +339,17 @@ impl WindowsApi { Self::show_window(hwnd, SW_MINIMIZE); } - pub fn close_window(hwnd: HWND) { - unsafe { PostMessageW(hwnd, WM_CLOSE, WPARAM(0), LPARAM(0)) }; + fn post_message(hwnd: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> Result<()> { + unsafe { PostMessageW(hwnd, message, wparam, lparam) } + .ok() + .process() + } + + pub fn close_window(hwnd: HWND) -> Result<()> { + match Self::post_message(hwnd, WM_CLOSE, WPARAM(0), LPARAM(0)) { + Ok(_) => Ok(()), + Err(_) => Err(anyhow!("could not close window")), + } } pub fn hide_window(hwnd: HWND) { diff --git a/komorebic.lib.ahk b/komorebic.lib.ahk index 1d210c6d..65f2b029 100644 --- a/komorebic.lib.ahk +++ b/komorebic.lib.ahk @@ -52,6 +52,14 @@ Move(operation_direction) { Run, komorebic.exe move %operation_direction%, , Hide } +Minimize() { + Run, komorebic.exe minimize, , Hide +} + +Close() { + Run, komorebic.exe close, , Hide +} + CycleFocus(cycle_direction) { Run, komorebic.exe cycle-focus %cycle_direction%, , Hide }