Skip to content
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

Add SetStatusIcon #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions platform/darwin/tray.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#import <Cocoa/Cocoa.h>

NSMenu * appMenu;
NSStatusItem * appStatusItem;
char * clipboardString;

extern void tray_callback(int itemId);
extern BOOL tray_enabled(int itemId);
extern void notification_callback();
extern struct image invert_png_image(struct image img);

void set_status_item_icon(struct image img);

@interface ManageHandler : NSObject<NSUserNotificationCenterDelegate>
- (void)manage:(id)sender;
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
Expand Down Expand Up @@ -112,6 +115,16 @@ int init(const char * title, struct image img) {
[self userNotificationCenter: nil didActivateNotification: launchNotification];
}*/

appStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[appStatusItem setMenu:appMenu];
[appStatusItem setHighlightMode:YES];
[appStatusItem setToolTip:[NSString stringWithUTF8String:title]];
set_status_item_icon(img);

return 0;
}

void set_status_item_icon(struct image img) {
NSSize iconSize = NSMakeSize(16, 16);
NSImage * icon = [[NSImage alloc] initWithSize:iconSize];
NSData * iconData = [NSData dataWithBytes:img.bytes length:img.length];
Expand All @@ -123,14 +136,8 @@ int init(const char * title, struct image img) {
NSData * icon2Data = [NSData dataWithBytes:img.bytes length:img.length];
[icon2 addRepresentation:[NSBitmapImageRep imageRepWithData:icon2Data]];

NSStatusItem * statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setMenu:appMenu];
[statusItem setImage:icon];
[statusItem setAlternateImage:icon2];
[statusItem setHighlightMode:YES];
[statusItem setToolTip:[NSString stringWithUTF8String:title]];

return 0;
[appStatusItem setImage:icon];
[appStatusItem setAlternateImage:icon2];
}

void set_clipboard_string(const char * string) {
Expand Down
3 changes: 3 additions & 0 deletions platform/linux/tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void display_notification(int id, const char* title, const char* body, struct im
// TODO: Implement.
void clear_menu_items() {}

// TODO: Implement.
void set_status_item_icon(struct image img) {}

// TODO: Implement.
struct clipboard_content get_clipboard_content()
{
Expand Down
4 changes: 4 additions & 0 deletions platform/windows/tray.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,7 @@ struct clipboard_content get_clipboard_content() {
void display_notification(int notificationId, const char * title, const char * body, struct image img, double timeout) {
// TODO: Implement.
}

// TODO: Implement.
void set_status_item_icon(struct image img) {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra blank line at the end, it's not needed.

7 changes: 7 additions & 0 deletions trayhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func Initialize(title string, imageData []byte, items []MenuItem) {
}
}

// SetStatusIcon replaces the icon image data.
func SetStatusIcon(imageData []byte) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we already have trayhost.UpdateMenu to modify the menu items, it might make more sense to use more consistent naming.

How about UpdateIcon?

img, freeImg := create_image(Image{Kind: "png", Bytes: imageData})
defer freeImg()
C.set_status_item_icon(img)
}

// EnterLoop enters main loop.
func EnterLoop() {
C.native_loop()
Expand Down
5 changes: 4 additions & 1 deletion trayhost_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func invertPngImage(imageData []byte) []byte {
panic(err)
}

invertImageNrgba(m.(*image.NRGBA))
switch m.(type) {
case *image.NRGBA:
invertImageNrgba(m.(*image.NRGBA))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Can the type be something other than *image.NRGBA? If so, what can it be? Is doing nothing in those cases the best thing to do?


var buf bytes.Buffer
err = png.Encode(&buf, m)
Expand Down