From f494d46fee14a4dc6e0b116ef2aa79a599bc0a4e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Fri, 6 Aug 2021 19:26:22 +0200 Subject: [PATCH] feat(ui): implement heartbeat on Windows --- espanso-ui/src/event.rs | 1 + espanso-ui/src/win32/mod.rs | 4 ++++ espanso-ui/src/win32/native.cpp | 23 ++++++++++++++++++++--- espanso-ui/src/win32/native.h | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/espanso-ui/src/event.rs b/espanso-ui/src/event.rs index 9e8ac89..b3eb580 100644 --- a/espanso-ui/src/event.rs +++ b/espanso-ui/src/event.rs @@ -21,4 +21,5 @@ pub enum UIEvent { TrayIconClick, ContextMenuClick(u32), + Heartbeat, } diff --git a/espanso-ui/src/win32/mod.rs b/espanso-ui/src/win32/mod.rs index 427919b..41fb2bd 100644 --- a/espanso-ui/src/win32/mod.rs +++ b/espanso-ui/src/win32/mod.rs @@ -46,6 +46,7 @@ const MAX_ICON_COUNT: usize = 3; const UI_EVENT_TYPE_ICON_CLICK: i32 = 1; const UI_EVENT_TYPE_CONTEXT_MENU_CLICK: i32 = 2; +const UI_EVENT_TYPE_HEARTBEAT: i32 = 3; // Take a look at the native.h header file for an explanation of the fields #[repr(C)] @@ -342,6 +343,9 @@ impl From for Option { UI_EVENT_TYPE_CONTEXT_MENU_CLICK => { return Some(UIEvent::ContextMenuClick(raw.context_menu_id)); } + UI_EVENT_TYPE_HEARTBEAT => { + return Some(UIEvent::Heartbeat); + } _ => {} } diff --git a/espanso-ui/src/win32/native.cpp b/espanso-ui/src/win32/native.cpp index 9c7502d..7455d82 100644 --- a/espanso-ui/src/win32/native.cpp +++ b/espanso-ui/src/win32/native.cpp @@ -50,6 +50,8 @@ using json = nlohmann::json; #define APPWM_SHOW_CONTEXT_MENU (WM_APP + 2) #define APPWM_UPDATE_TRAY_ICON (WM_APP + 3) +#define HEARTBEAT_TIMER_ID 10001 + const wchar_t *const ui_winclass = L"EspansoUI"; typedef struct @@ -78,7 +80,7 @@ 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) { @@ -104,7 +106,7 @@ LRESULT CALLBACK ui_window_procedure(HWND window, unsigned int msg, WPARAM wp, L if (flags == 0) { event.event_type = UI_EVENT_TYPE_CONTEXT_MENU_CLICK; - event.context_menu_id = (uint32_t) idItem; + event.context_menu_id = (uint32_t)idItem; if (variables->event_callback && variables->rust_instance) { variables->event_callback(variables->rust_instance, event); @@ -153,6 +155,18 @@ LRESULT CALLBACK ui_window_procedure(HWND window, unsigned int msg, WPARAM wp, L break; } } + case WM_TIMER: // Regular timer check event + { + if (wp == HEARTBEAT_TIMER_ID) + { + event.event_type = UI_EVENT_TYPE_HEARTBEAT; + if (variables->event_callback && variables->rust_instance) + { + variables->event_callback(variables->rust_instance, event); + } + break; + } + } default: if (msg == WM_TASKBARCREATED) { // Explorer crashed, recreate the icon @@ -227,7 +241,7 @@ void *ui_initialize(void *_self, UIOptions _options, int32_t *error_code) SendMessage(window, WM_SETICON, ICON_BIG, (LPARAM)variables->g_icons[0]); SendMessage(window, WM_SETICON, ICON_SMALL, (LPARAM)variables->g_icons[0]); - // Tray icon + // Tray icon variables->nid.cbSize = sizeof(variables->nid); variables->nid.hWnd = window; variables->nid.uID = 1; @@ -241,6 +255,9 @@ void *ui_initialize(void *_self, UIOptions _options, int32_t *error_code) { Shell_NotifyIcon(NIM_ADD, &variables->nid); } + + // Setup heartbeat timer + SetTimer(window, HEARTBEAT_TIMER_ID, 1000, (TIMERPROC)NULL); } else { diff --git a/espanso-ui/src/win32/native.h b/espanso-ui/src/win32/native.h index fd0d783..c403603 100644 --- a/espanso-ui/src/win32/native.h +++ b/espanso-ui/src/win32/native.h @@ -29,6 +29,7 @@ #define UI_EVENT_TYPE_ICON_CLICK 1 #define UI_EVENT_TYPE_CONTEXT_MENU_CLICK 2 +#define UI_EVENT_TYPE_HEARTBEAT 3 typedef struct { int32_t show_icon;