-
Notifications
You must be signed in to change notification settings - Fork 139
Add macOS menu bar status item (NSStatusItem) with system tray icon #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "config.h" | ||
| #include "AppIndicator.h" | ||
|
|
||
| #include <glib/gi18n.h> | ||
| #include <gtk/gtk.h> | ||
|
|
||
| #import <Cocoa/Cocoa.h> | ||
|
|
||
| // Objective-C helper class for NSStatusItem callbacks. | ||
| // Must be at global scope (Objective-C declarations cannot appear inside a C++ | ||
| // namespace). | ||
| @interface IptuxStatusItemHelper : NSObject { | ||
| GActionGroup* actionGroup_; | ||
| iptux::IptuxAppIndicator* owner_; | ||
| } | ||
| - (instancetype)initWithActionGroup:(GActionGroup*)actionGroup | ||
| owner:(iptux::IptuxAppIndicator*)owner; | ||
| - (void)openMainWindow:(id)sender; | ||
| - (void)openPreferences:(id)sender; | ||
| - (void)quit:(id)sender; | ||
| - (void)statusItemClicked:(id)sender; | ||
| @end | ||
|
|
||
| @implementation IptuxStatusItemHelper | ||
| - (instancetype)initWithActionGroup:(GActionGroup*)actionGroup | ||
| owner:(iptux::IptuxAppIndicator*)owner { | ||
| self = [super init]; | ||
| if (self) { | ||
| actionGroup_ = actionGroup; | ||
| owner_ = owner; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)openMainWindow:(id)sender { | ||
| (void)sender; | ||
| g_action_group_activate_action(actionGroup_, "open_main_window", NULL); | ||
| } | ||
|
|
||
| - (void)openPreferences:(id)sender { | ||
| (void)sender; | ||
| g_action_group_activate_action(actionGroup_, "preferences", NULL); | ||
| } | ||
|
|
||
| - (void)quit:(id)sender { | ||
| (void)sender; | ||
| g_action_group_activate_action(actionGroup_, "quit", NULL); | ||
| } | ||
|
|
||
| - (void)statusItemClicked:(id)sender { | ||
| (void)sender; | ||
| owner_->sigActivateMainWindow.emit(); | ||
| } | ||
| @end | ||
|
|
||
| static NSImage* pixbufToNSImage(GdkPixbuf* pixbuf) { | ||
| int width = gdk_pixbuf_get_width(pixbuf); | ||
| int height = gdk_pixbuf_get_height(pixbuf); | ||
| int stride = gdk_pixbuf_get_rowstride(pixbuf); | ||
| gboolean hasAlpha = gdk_pixbuf_get_has_alpha(pixbuf); | ||
| const guchar* pixels = gdk_pixbuf_get_pixels(pixbuf); | ||
|
|
||
| int bitsPerSample = 8; | ||
| int channels = hasAlpha ? 4 : 3; | ||
|
|
||
| NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] | ||
| initWithBitmapDataPlanes:NULL | ||
| pixelsWide:width | ||
| pixelsHigh:height | ||
| bitsPerSample:bitsPerSample | ||
| samplesPerPixel:channels | ||
| hasAlpha:hasAlpha | ||
| isPlanar:NO | ||
| colorSpaceName:NSDeviceRGBColorSpace | ||
| bytesPerRow:stride | ||
| bitsPerPixel:bitsPerSample * channels]; | ||
|
|
||
| memcpy([rep bitmapData], pixels, height * stride); | ||
|
|
||
| NSImage* image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; | ||
| [image addRepresentation:rep]; | ||
| [rep release]; | ||
|
|
||
| return [image autorelease]; | ||
| } | ||
|
|
||
| static NSImage* loadIcon(const char* iconName, int size) { | ||
| auto theme = gtk_icon_theme_get_default(); | ||
| GError* error = nullptr; | ||
| auto pixbuf = gtk_icon_theme_load_icon(theme, iconName, size, | ||
| GtkIconLookupFlags(0), &error); | ||
| if (!pixbuf) { | ||
| g_warning("Couldn't load icon %s: %s", iconName, error->message); | ||
| g_error_free(error); | ||
| return nil; | ||
| } | ||
|
lidaobing marked this conversation as resolved.
|
||
| NSImage* image = pixbufToNSImage(pixbuf); | ||
| [image retain]; | ||
| g_object_unref(pixbuf); | ||
|
|
||
| // Resize for menu bar (typically 18x18 points) | ||
| [image setSize:NSMakeSize(18, 18)]; | ||
| return image; | ||
| } | ||
|
|
||
| namespace iptux { | ||
|
|
||
| class IptuxAppIndicatorPrivate { | ||
| public: | ||
| IptuxAppIndicatorPrivate() {} | ||
| ~IptuxAppIndicatorPrivate() { | ||
| if (statusItem) { | ||
| [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; | ||
| [statusItem release]; | ||
| } | ||
| if (helper) { | ||
| [helper release]; | ||
| } | ||
| if (normalIcon) { | ||
| [normalIcon release]; | ||
| } | ||
| if (attentionIcon) { | ||
| [attentionIcon release]; | ||
| } | ||
| } | ||
|
|
||
| NSStatusItem* statusItem = nil; | ||
| IptuxStatusItemHelper* helper = nil; | ||
| NSImage* normalIcon = nil; | ||
| NSImage* attentionIcon = nil; | ||
| }; | ||
|
|
||
| IptuxAppIndicator::IptuxAppIndicator(GActionGroup* action_group) { | ||
| this->priv = std::make_shared<IptuxAppIndicatorPrivate>(); | ||
|
|
||
| priv->helper = [[IptuxStatusItemHelper alloc] initWithActionGroup:action_group | ||
| owner:this]; | ||
|
|
||
| // Load icons | ||
| priv->normalIcon = loadIcon("iptux-icon", 64); | ||
| priv->attentionIcon = loadIcon("iptux-attention", 64); | ||
|
|
||
| // Create status item | ||
| priv->statusItem = | ||
|
Comment on lines
+144
to
+146
|
||
| [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; | ||
|
lidaobing marked this conversation as resolved.
|
||
|
|
||
| if (priv->normalIcon) { | ||
| priv->statusItem.button.image = priv->normalIcon; | ||
| } | ||
| priv->statusItem.button.toolTip = @"Iptux"; | ||
|
|
||
| // Build the menu | ||
| NSMenu* menu = [[NSMenu alloc] init]; | ||
|
|
||
| NSMenuItem* openItem = | ||
| [[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:_("Open Iptux")] | ||
| action:@selector(openMainWindow:) | ||
| keyEquivalent:@""]; | ||
| openItem.target = priv->helper; | ||
| [menu addItem:openItem]; | ||
| [openItem release]; | ||
|
|
||
| [menu addItem:[NSMenuItem separatorItem]]; | ||
|
|
||
| NSMenuItem* prefsItem = | ||
| [[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:_("Preferences")] | ||
| action:@selector(openPreferences:) | ||
| keyEquivalent:@""]; | ||
| prefsItem.target = priv->helper; | ||
| [menu addItem:prefsItem]; | ||
| [prefsItem release]; | ||
|
|
||
| NSMenuItem* quitItem = | ||
| [[NSMenuItem alloc] initWithTitle:[NSString stringWithUTF8String:_("Quit")] | ||
| action:@selector(quit:) | ||
| keyEquivalent:@""]; | ||
| quitItem.target = priv->helper; | ||
| [menu addItem:quitItem]; | ||
| [quitItem release]; | ||
|
|
||
| priv->statusItem.menu = menu; | ||
| [menu release]; | ||
| } | ||
|
|
||
| void IptuxAppIndicator::SetUnreadCount(int count) { | ||
| if (!priv->statusItem) return; | ||
|
|
||
| if (count > 0 && priv->attentionIcon) { | ||
| priv->statusItem.button.image = priv->attentionIcon; | ||
| } else if (priv->normalIcon) { | ||
| priv->statusItem.button.image = priv->normalIcon; | ||
| } | ||
| } | ||
|
|
||
| } // namespace iptux | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
statusItemClicked:is currently dead code: it isn’t wired to theNSStatusItembutton (no target/action assignment), so this callback (and theowner_field it depends on) will never be invoked. Either hook this up via the status item button’s target/action if you want click-to-open behavior, or remove the unused method/field to avoid confusion.