feat(ui): implement heartbeat on Windows

This commit is contained in:
Federico Terzi 2021-08-06 19:26:22 +02:00
parent 36400afab4
commit f494d46fee
4 changed files with 26 additions and 3 deletions

View File

@ -21,4 +21,5 @@
pub enum UIEvent {
TrayIconClick,
ContextMenuClick(u32),
Heartbeat,
}

View File

@ -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<RawUIEvent> for Option<UIEvent> {
UI_EVENT_TYPE_CONTEXT_MENU_CLICK => {
return Some(UIEvent::ContextMenuClick(raw.context_menu_id));
}
UI_EVENT_TYPE_HEARTBEAT => {
return Some(UIEvent::Heartbeat);
}
_ => {}
}

View File

@ -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
{

View File

@ -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;