From 02b486dc7a283691dd9e07295306dca34a1f8e28 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Fri, 6 Aug 2021 19:41:59 +0200 Subject: [PATCH] feat(ui): implement Heartbeat on macOS --- espanso-ui/src/mac/AppDelegate.h | 1 + espanso-ui/src/mac/AppDelegate.mm | 15 +++++++++++++++ espanso-ui/src/mac/mod.rs | 4 ++++ espanso-ui/src/mac/native.h | 1 + 4 files changed, 21 insertions(+) diff --git a/espanso-ui/src/mac/AppDelegate.h b/espanso-ui/src/mac/AppDelegate.h index c36ccf3..2a52982 100644 --- a/espanso-ui/src/mac/AppDelegate.h +++ b/espanso-ui/src/mac/AppDelegate.h @@ -35,5 +35,6 @@ - (void) showNotification: (NSString *) message withDelay:(double) delay; - (IBAction) statusIconClick: (id) sender; - (IBAction) contextMenuClick: (id) sender; +- (void) heartbeatHandler: (NSTimer *)timer; @end \ No newline at end of file diff --git a/espanso-ui/src/mac/AppDelegate.mm b/espanso-ui/src/mac/AppDelegate.mm index 16ea228..4de6c19 100644 --- a/espanso-ui/src/mac/AppDelegate.mm +++ b/espanso-ui/src/mac/AppDelegate.mm @@ -33,6 +33,13 @@ void addSubMenu(NSMenu * parent, NSArray * items); } [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; + + // Heartbeat timer setup + [NSTimer scheduledTimerWithTimeInterval:1.0 + target:self + selector:@selector(heartbeatHandler:) + userInfo:nil + repeats:YES]; } - (void) setIcon: (int32_t)iconIndex { @@ -88,6 +95,14 @@ void addSubMenu(NSMenu * parent, NSArray * items); [[NSUserNotificationCenter defaultUserNotificationCenter] performSelector:@selector(removeDeliveredNotification:) withObject:notification afterDelay:delay]; } +- (void) heartbeatHandler: (NSTimer *)timer { + UIEvent event = {}; + event.event_type = UI_EVENT_TYPE_HEARTBEAT; + if (event_callback && rust_instance) { + event_callback(rust_instance, event); + } +} + @end // Menu utility methods diff --git a/espanso-ui/src/mac/mod.rs b/espanso-ui/src/mac/mod.rs index 3eaed31..4570e20 100644 --- a/espanso-ui/src/mac/mod.rs +++ b/espanso-ui/src/mac/mod.rs @@ -32,6 +32,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)] @@ -234,6 +235,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/mac/native.h b/espanso-ui/src/mac/native.h index da7c5fa..2e15900 100644 --- a/espanso-ui/src/mac/native.h +++ b/espanso-ui/src/mac/native.h @@ -28,6 +28,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;