Clean up tray icon on windows when exiting

This commit is contained in:
Federico Terzi 2019-09-16 11:59:23 +02:00
parent 8e6c536673
commit 7e71baed7e
8 changed files with 33 additions and 3 deletions

View File

@ -57,6 +57,7 @@ HWND nw = NULL;
HWND hwnd_st_u = NULL; HWND hwnd_st_u = NULL;
HBITMAP g_espanso_bmp = NULL; HBITMAP g_espanso_bmp = NULL;
HICON g_espanso_ico = NULL; HICON g_espanso_ico = NULL;
NOTIFYICONDATA nid = {};
// Callbacks // 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); SendMessage(nw, WM_SETICON, ICON_SMALL, (LPARAM)g_espanso_ico);
//Notification //Notification
NOTIFYICONDATA nid = {};
nid.cbSize = sizeof(nid); nid.cbSize = sizeof(nid);
nid.hWnd = nw; nid.hWnd = nw;
nid.uID = 1; nid.uID = 1;
@ -558,6 +558,12 @@ int32_t show_context_menu(MenuItem * items, int32_t count) {
return -1; return -1;
} }
void cleanup_ui() {
Shell_NotifyIcon(NIM_DELETE, &nid);
}
// SYSTEM
int32_t start_daemon_process() { int32_t start_daemon_process() {
wchar_t cmd[MAX_PATH]; wchar_t cmd[MAX_PATH];
swprintf(cmd, MAX_PATH, L"espanso.exe daemon"); swprintf(cmd, MAX_PATH, L"espanso.exe daemon");
@ -586,6 +592,8 @@ int32_t start_daemon_process() {
return 1; return 1;
} }
// CLIPBOARD
int32_t set_clipboard(wchar_t *text) { int32_t set_clipboard(wchar_t *text) {
const size_t len = wcslen(text) + 1; const size_t len = wcslen(text) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len * sizeof(wchar_t)); 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); swprintf(buffer, size, L"%s", hMem);
CloseClipboard(); CloseClipboard();
} }

View File

@ -112,6 +112,11 @@ typedef void (*ContextMenuClickCallback)(void * self, int32_t id);
extern ContextMenuClickCallback context_menu_click_callback; extern ContextMenuClickCallback context_menu_click_callback;
extern "C" void register_context_menu_click_callback(ContextMenuClickCallback callback); extern "C" void register_context_menu_click_callback(ContextMenuClickCallback callback);
/*
* Hide the tray icon
*/
extern "C" void cleanup_ui();
// NOTIFICATION // NOTIFICATION
/* /*

View File

@ -42,6 +42,7 @@ extern {
pub fn show_context_menu(items: *const WindowsMenuItem, count: i32) -> i32; 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_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 register_context_menu_click_callback(cb: extern fn(_self: *mut c_void, id: i32));
pub fn cleanup_ui();
// CLIPBOARD // CLIPBOARD
pub fn get_clipboard(buffer: *mut u16, size: i32) -> i32; pub fn get_clipboard(buffer: *mut u16, size: i32) -> i32;

View File

@ -195,6 +195,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager,
}, },
ActionType::Exit => { ActionType::Exit => {
info!("Terminating espanso."); info!("Terminating espanso.");
self.ui_manager.cleanup();
exit(0); exit(0);
}, },
_ => {} _ => {}

View File

@ -37,6 +37,10 @@ impl super::UIManager for LinuxUIManager {
fn show_menu(&self, _menu: Vec<MenuItem>) { fn show_menu(&self, _menu: Vec<MenuItem>) {
// Not implemented on linux // Not implemented on linux
} }
fn cleanup(&self) {
// Nothing to do here
}
} }
impl LinuxUIManager { impl LinuxUIManager {

View File

@ -76,6 +76,10 @@ impl super::UIManager for MacUIManager {
unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); } unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); }
} }
fn cleanup(&self) {
// Nothing to do here
}
} }
impl MacUIManager { impl MacUIManager {

View File

@ -29,6 +29,7 @@ mod macos;
pub trait UIManager { pub trait UIManager {
fn notify(&self, message: &str); fn notify(&self, message: &str);
fn show_menu(&self, menu: Vec<MenuItem>); fn show_menu(&self, menu: Vec<MenuItem>);
fn cleanup(&self);
} }
pub enum MenuItemType { pub enum MenuItemType {

View File

@ -17,7 +17,7 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 widestring::U16CString;
use std::{thread, time}; use std::{thread, time};
use log::{debug}; 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); } unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); }
} }
fn cleanup(&self) {
unsafe {
cleanup_ui();
}
}
} }
impl WindowsUIManager { impl WindowsUIManager {