From 91046b3c18e4f7b4c5c0ccded107f272c9309f0e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 16 May 2021 18:42:22 +0200 Subject: [PATCH] feat(ui): add method to exit eventloop --- espanso-ui/src/lib.rs | 3 ++- espanso-ui/src/win32/mod.rs | 11 +++++++++++ espanso-ui/src/win32/native.cpp | 14 ++++++++++++++ espanso-ui/src/win32/native.h | 3 +++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/espanso-ui/src/lib.rs b/espanso-ui/src/lib.rs index 9a9babb..0792975 100644 --- a/espanso-ui/src/lib.rs +++ b/espanso-ui/src/lib.rs @@ -15,10 +15,11 @@ pub mod linux; #[cfg(target_os = "macos")] pub mod mac; -pub trait UIRemote { +pub trait UIRemote: Send { fn update_tray_icon(&self, icon: TrayIcon); fn show_notification(&self, message: &str); fn show_context_menu(&self, menu: &menu::Menu); + fn exit(&self); } pub type UIEventCallback = Box; diff --git a/espanso-ui/src/win32/mod.rs b/espanso-ui/src/win32/mod.rs index ddc3c22..981ef42 100644 --- a/espanso-ui/src/win32/mod.rs +++ b/espanso-ui/src/win32/mod.rs @@ -74,6 +74,7 @@ extern "C" { event_callback: extern "C" fn(_self: *mut Win32EventLoop, event: RawUIEvent), ) -> i32; pub fn ui_destroy(window_handle: *const c_void) -> i32; + pub fn ui_exit(window_handle: *const c_void) -> i32; pub fn ui_update_tray_icon(window_handle: *const c_void, index: i32); pub fn ui_show_notification(window_handle: *const c_void, message: *const u16); pub fn ui_show_context_menu(window_handle: *const c_void, payload: *const c_char); @@ -348,6 +349,16 @@ impl UIRemote for Win32Remote { } } } + + fn exit(&self) { + let handle = self.handle.load(Ordering::Acquire); + if handle.is_null() { + error!("Unable to exit eventloop, pointer is null"); + return; + } + + unsafe { ui_exit(handle) }; + } } #[allow(clippy::single_match)] // TODO: remove after another match is used diff --git a/espanso-ui/src/win32/native.cpp b/espanso-ui/src/win32/native.cpp index e511e30..9422269 100644 --- a/espanso-ui/src/win32/native.cpp +++ b/espanso-ui/src/win32/native.cpp @@ -94,6 +94,12 @@ LRESULT CALLBACK ui_window_procedure(HWND window, unsigned int msg, WPARAM wp, L { case WM_DESTROY: PostQuitMessage(0); + + // Remove tray icon + if (variables->options.show_icon) + { + Shell_NotifyIcon(NIM_DELETE, &variables->nid); + } // Free the tray icons for (int i = 0; i < variables->options.icon_paths_count; i++) @@ -316,6 +322,14 @@ int32_t ui_destroy(void *window) return -1; } +void ui_exit(void *window) +{ + if (window) + { + PostMessage((HWND)window, WM_CLOSE, 0, 0); + } +} + void ui_update_tray_icon(void *window, int32_t index) { if (window) diff --git a/espanso-ui/src/win32/native.h b/espanso-ui/src/win32/native.h index 768f34f..c03c5d1 100644 --- a/espanso-ui/src/win32/native.h +++ b/espanso-ui/src/win32/native.h @@ -54,6 +54,9 @@ extern "C" int32_t ui_eventloop(void * window, EventCallback callback); // Destroy the given window. extern "C" int32_t ui_destroy(void * window); +// Send a termination event that exits the event loop +extern "C" void ui_exit(void * window); + // Updates the tray icon to the given one. The method accepts an index that refers to // the icon within the UIOptions.icon_paths array. extern "C" void ui_update_tray_icon(void * window, int32_t index);