diff --git a/native/libwinbridge/bridge.cpp b/native/libwinbridge/bridge.cpp index 8230862..1bf3ceb 100644 --- a/native/libwinbridge/bridge.cpp +++ b/native/libwinbridge/bridge.cpp @@ -57,6 +57,7 @@ HWND nw = NULL; HWND hwnd_st_u = NULL; HBITMAP g_espanso_bmp = NULL; HICON g_espanso_ico = NULL; +NOTIFYICONDATA nid = {}; // Callbacks @@ -376,7 +377,6 @@ int32_t initialize(void * self, wchar_t * ico_path, wchar_t * bmp_path) { SendMessage(nw, WM_SETICON, ICON_SMALL, (LPARAM)g_espanso_ico); //Notification - NOTIFYICONDATA nid = {}; nid.cbSize = sizeof(nid); nid.hWnd = nw; nid.uID = 1; @@ -558,6 +558,12 @@ int32_t show_context_menu(MenuItem * items, int32_t count) { return -1; } +void cleanup_ui() { + Shell_NotifyIcon(NIM_DELETE, &nid); +} + +// SYSTEM + int32_t start_daemon_process() { wchar_t cmd[MAX_PATH]; swprintf(cmd, MAX_PATH, L"espanso.exe daemon"); @@ -586,6 +592,8 @@ int32_t start_daemon_process() { return 1; } +// CLIPBOARD + int32_t set_clipboard(wchar_t *text) { const size_t len = wcslen(text) + 1; HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len * sizeof(wchar_t)); @@ -622,4 +630,4 @@ int32_t get_clipboard(wchar_t *buffer, int32_t size) { swprintf(buffer, size, L"%s", hMem); CloseClipboard(); -} \ No newline at end of file +} diff --git a/native/libwinbridge/bridge.h b/native/libwinbridge/bridge.h index 473dac5..ef20f7e 100644 --- a/native/libwinbridge/bridge.h +++ b/native/libwinbridge/bridge.h @@ -112,6 +112,11 @@ typedef void (*ContextMenuClickCallback)(void * self, int32_t id); extern ContextMenuClickCallback context_menu_click_callback; extern "C" void register_context_menu_click_callback(ContextMenuClickCallback callback); +/* + * Hide the tray icon + */ +extern "C" void cleanup_ui(); + // NOTIFICATION /* diff --git a/src/bridge/windows.rs b/src/bridge/windows.rs index fe6803f..40480a5 100644 --- a/src/bridge/windows.rs +++ b/src/bridge/windows.rs @@ -42,6 +42,7 @@ extern { pub fn show_context_menu(items: *const WindowsMenuItem, count: i32) -> i32; pub fn register_icon_click_callback(cb: extern fn(_self: *mut c_void)); pub fn register_context_menu_click_callback(cb: extern fn(_self: *mut c_void, id: i32)); + pub fn cleanup_ui(); // CLIPBOARD pub fn get_clipboard(buffer: *mut u16, size: i32) -> i32; diff --git a/src/engine.rs b/src/engine.rs index 1f47d94..84cb22b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -195,6 +195,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, }, ActionType::Exit => { info!("Terminating espanso."); + self.ui_manager.cleanup(); exit(0); }, _ => {} diff --git a/src/ui/linux.rs b/src/ui/linux.rs index df19705..de8ac81 100644 --- a/src/ui/linux.rs +++ b/src/ui/linux.rs @@ -37,6 +37,10 @@ impl super::UIManager for LinuxUIManager { fn show_menu(&self, _menu: Vec) { // Not implemented on linux } + + fn cleanup(&self) { + // Nothing to do here + } } impl LinuxUIManager { diff --git a/src/ui/macos.rs b/src/ui/macos.rs index 61065de..29e4b50 100644 --- a/src/ui/macos.rs +++ b/src/ui/macos.rs @@ -76,6 +76,10 @@ impl super::UIManager for MacUIManager { unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); } } + + fn cleanup(&self) { + // Nothing to do here + } } impl MacUIManager { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c55283b..1b096c6 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -29,6 +29,7 @@ mod macos; pub trait UIManager { fn notify(&self, message: &str); fn show_menu(&self, menu: Vec); + fn cleanup(&self); } pub enum MenuItemType { diff --git a/src/ui/windows.rs b/src/ui/windows.rs index 367e1f5..4024939 100644 --- a/src/ui/windows.rs +++ b/src/ui/windows.rs @@ -17,7 +17,7 @@ * along with espanso. If not, see . */ -use crate::bridge::windows::{show_notification, close_notification, WindowsMenuItem, show_context_menu}; +use crate::bridge::windows::{show_notification, close_notification, WindowsMenuItem, show_context_menu, cleanup_ui}; use widestring::U16CString; use std::{thread, time}; use log::{debug}; @@ -90,6 +90,12 @@ impl super::UIManager for WindowsUIManager { unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); } } + + fn cleanup(&self) { + unsafe { + cleanup_ui(); + } + } } impl WindowsUIManager {