From 4adedbae439d8ab45c8a6cc70c788fb6ec6af82a Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 15 Apr 2020 19:09:13 +0200 Subject: [PATCH] Adding option to hide status icon on MacOS. See #95 --- native/libmacbridge/AppDelegate.mm | 18 ++++++++++-------- native/libmacbridge/bridge.h | 3 ++- native/libmacbridge/bridge.mm | 4 +++- src/bridge/macos.rs | 2 +- src/context/macos.rs | 8 +++++++- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/native/libmacbridge/AppDelegate.mm b/native/libmacbridge/AppDelegate.mm index 29d1213..cd24e8b 100644 --- a/native/libmacbridge/AppDelegate.mm +++ b/native/libmacbridge/AppDelegate.mm @@ -24,16 +24,18 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Setup status icon - myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; + if (show_icon) { + myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; - NSString *nsIconPath = [NSString stringWithUTF8String:icon_path]; - NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:nsIconPath]; - [statusImage setTemplate:YES]; + NSString *nsIconPath = [NSString stringWithUTF8String:icon_path]; + NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:nsIconPath]; + [statusImage setTemplate:YES]; - [myStatusItem.button setImage:statusImage]; - [myStatusItem setHighlightMode:YES]; - [myStatusItem.button setAction:@selector(statusIconClick:)]; - [myStatusItem.button setTarget:self]; + [myStatusItem.button setImage:statusImage]; + [myStatusItem setHighlightMode:YES]; + [myStatusItem.button setAction:@selector(statusIconClick:)]; + [myStatusItem.button setTarget:self]; + } // Setup key listener [NSEvent addGlobalMonitorForEventsMatchingMask:(NSEventMaskKeyDown | NSEventMaskFlagsChanged | NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown) diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index 03bb6bf..4a0f1d8 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -26,11 +26,12 @@ extern "C" { extern void * context_instance; extern char * icon_path; +extern int32_t show_icon; /* * Initialize the AppDelegate and check for accessibility permissions */ -int32_t initialize(void * context, const char * icon_path); +int32_t initialize(void * context, const char * icon_path, int32_t show_icon); /* * Start the event loop indefinitely. Blocking call. diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index 1c59bd6..3e2d7e5 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -33,15 +33,17 @@ extern "C" { void * context_instance; char * icon_path; +int32_t show_icon; AppDelegate * delegate_ptr; KeypressCallback keypress_callback; IconClickCallback icon_click_callback; ContextMenuClickCallback context_menu_click_callback; -int32_t initialize(void * context, const char * _icon_path) { +int32_t initialize(void * context, const char * _icon_path, int32_t _show_icon) { context_instance = context; icon_path = strdup(_icon_path); + show_icon = _show_icon; AppDelegate *delegate = [[AppDelegate alloc] init]; delegate_ptr = delegate; diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 989786b..1cfb204 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -29,7 +29,7 @@ pub struct MacMenuItem { #[allow(improper_ctypes)] #[link(name="macbridge", kind="static")] extern { - pub fn initialize(s: *const c_void, icon_path: *const c_char); + pub fn initialize(s: *const c_void, icon_path: *const c_char, show_icon: i32); pub fn eventloop(); pub fn headless_eventloop(); diff --git a/src/context/macos.rs b/src/context/macos.rs index 7b6ec44..7a71d25 100644 --- a/src/context/macos.rs +++ b/src/context/macos.rs @@ -83,7 +83,13 @@ impl MacContext { register_context_menu_click_callback(context_menu_click_callback); let status_icon_path = CString::new(status_icon_target.to_str().unwrap_or_default()).unwrap_or_default(); - initialize(context_ptr, status_icon_path.as_ptr()); + let show_icon = if config.show_icon { + 1 + }else{ + 0 + }; + + initialize(context_ptr, status_icon_path.as_ptr(), show_icon); } context