feat(ui): add method to exit eventloop

This commit is contained in:
Federico Terzi 2021-05-16 18:42:22 +02:00
parent 22ba3a5e03
commit 91046b3c18
4 changed files with 30 additions and 1 deletions

View File

@ -15,10 +15,11 @@ pub mod linux;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub mod mac; pub mod mac;
pub trait UIRemote { pub trait UIRemote: Send {
fn update_tray_icon(&self, icon: TrayIcon); fn update_tray_icon(&self, icon: TrayIcon);
fn show_notification(&self, message: &str); fn show_notification(&self, message: &str);
fn show_context_menu(&self, menu: &menu::Menu); fn show_context_menu(&self, menu: &menu::Menu);
fn exit(&self);
} }
pub type UIEventCallback = Box<dyn Fn(event::UIEvent)>; pub type UIEventCallback = Box<dyn Fn(event::UIEvent)>;

View File

@ -74,6 +74,7 @@ extern "C" {
event_callback: extern "C" fn(_self: *mut Win32EventLoop, event: RawUIEvent), event_callback: extern "C" fn(_self: *mut Win32EventLoop, event: RawUIEvent),
) -> i32; ) -> i32;
pub fn ui_destroy(window_handle: *const c_void) -> i32; pub fn ui_destroy(window_handle: *const c_void) -> i32;
pub fn ui_exit(window_handle: *const c_void) -> i32;
pub fn ui_update_tray_icon(window_handle: *const c_void, index: i32); pub fn ui_update_tray_icon(window_handle: *const c_void, index: i32);
pub fn ui_show_notification(window_handle: *const c_void, message: *const u16); pub fn ui_show_notification(window_handle: *const c_void, message: *const u16);
pub fn ui_show_context_menu(window_handle: *const c_void, payload: *const c_char); pub fn ui_show_context_menu(window_handle: *const c_void, payload: *const c_char);
@ -348,6 +349,16 @@ impl UIRemote for Win32Remote {
} }
} }
} }
fn exit(&self) {
let handle = self.handle.load(Ordering::Acquire);
if handle.is_null() {
error!("Unable to exit eventloop, pointer is null");
return;
}
unsafe { ui_exit(handle) };
}
} }
#[allow(clippy::single_match)] // TODO: remove after another match is used #[allow(clippy::single_match)] // TODO: remove after another match is used

View File

@ -95,6 +95,12 @@ LRESULT CALLBACK ui_window_procedure(HWND window, unsigned int msg, WPARAM wp, L
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(0); PostQuitMessage(0);
// Remove tray icon
if (variables->options.show_icon)
{
Shell_NotifyIcon(NIM_DELETE, &variables->nid);
}
// Free the tray icons // Free the tray icons
for (int i = 0; i < variables->options.icon_paths_count; i++) for (int i = 0; i < variables->options.icon_paths_count; i++)
{ {
@ -316,6 +322,14 @@ int32_t ui_destroy(void *window)
return -1; return -1;
} }
void ui_exit(void *window)
{
if (window)
{
PostMessage((HWND)window, WM_CLOSE, 0, 0);
}
}
void ui_update_tray_icon(void *window, int32_t index) void ui_update_tray_icon(void *window, int32_t index)
{ {
if (window) if (window)

View File

@ -54,6 +54,9 @@ extern "C" int32_t ui_eventloop(void * window, EventCallback callback);
// Destroy the given window. // Destroy the given window.
extern "C" int32_t ui_destroy(void * window); extern "C" int32_t ui_destroy(void * window);
// Send a termination event that exits the event loop
extern "C" void ui_exit(void * window);
// Updates the tray icon to the given one. The method accepts an index that refers to // Updates the tray icon to the given one. The method accepts an index that refers to
// the icon within the UIOptions.icon_paths array. // the icon within the UIOptions.icon_paths array.
extern "C" void ui_update_tray_icon(void * window, int32_t index); extern "C" void ui_update_tray_icon(void * window, int32_t index);