|
| 1 | +#include <iostream> |
| 2 | +#include "tray.h" |
| 3 | + |
| 4 | +// Import Cocoa headers |
| 5 | +#import <Cocoa/Cocoa.h> |
| 6 | + |
| 7 | +namespace nativeapi { |
| 8 | + |
| 9 | +// Private implementation class |
| 10 | +class Tray::Impl { |
| 11 | + public: |
| 12 | + Impl(NSStatusItem* tray) : ns_status_item_(tray) {} |
| 13 | + NSStatusItem* ns_status_item_; |
| 14 | +}; |
| 15 | + |
| 16 | +Tray::Tray() : pimpl_(new Impl(nil)) { |
| 17 | + id = -1; |
| 18 | +} |
| 19 | + |
| 20 | +Tray::Tray(void* tray) : pimpl_(new Impl((__bridge NSStatusItem*)tray)) { |
| 21 | + id = 0; |
| 22 | +} |
| 23 | + |
| 24 | +Tray::~Tray() { |
| 25 | + delete pimpl_; |
| 26 | +} |
| 27 | + |
| 28 | +void Tray::SetIcon(std::string icon) { |
| 29 | + // Check if the icon is a base64 string |
| 30 | + if (icon.find("data:image") != std::string::npos) { |
| 31 | + // Extract the base64 part |
| 32 | + size_t pos = icon.find("base64,"); |
| 33 | + if (pos != std::string::npos) { |
| 34 | + std::string base64Icon = icon.substr(pos + 7); |
| 35 | + |
| 36 | + // Convert base64 to NSData |
| 37 | + NSData* imageData = [[NSData alloc] |
| 38 | + initWithBase64EncodedString:[NSString stringWithUTF8String:base64Icon.c_str()] |
| 39 | + options:NSDataBase64DecodingIgnoreUnknownCharacters]; |
| 40 | + |
| 41 | + // Create image from data |
| 42 | + NSImage* image = [[NSImage alloc] initWithData:imageData]; |
| 43 | + |
| 44 | + // Set image size and template property |
| 45 | + [image setSize:NSMakeSize(20, 20)]; // Default icon size |
| 46 | + [image setTemplate:YES]; |
| 47 | + |
| 48 | + // Set the image to the button |
| 49 | + [pimpl_->ns_status_item_.button setImage:image]; |
| 50 | + } |
| 51 | + } else { |
| 52 | + // Use the icon as a file path or named image |
| 53 | + [pimpl_->ns_status_item_.button |
| 54 | + setImage:[NSImage imageNamed:[NSString stringWithUTF8String:icon.c_str()]]]; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void Tray::SetTitle(std::string title) { |
| 59 | + if (pimpl_->ns_status_item_.button) { |
| 60 | + [pimpl_->ns_status_item_.button setTitle:[NSString stringWithUTF8String:title.c_str()]]; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +std::string Tray::GetTitle() { |
| 65 | + return [pimpl_->ns_status_item_.title UTF8String]; |
| 66 | +} |
| 67 | + |
| 68 | +void Tray::SetTooltip(std::string tooltip) { |
| 69 | + [pimpl_->ns_status_item_.button setToolTip:[NSString stringWithUTF8String:tooltip.c_str()]]; |
| 70 | +} |
| 71 | + |
| 72 | +std::string Tray::GetTooltip() { |
| 73 | + return [[pimpl_->ns_status_item_.button toolTip] UTF8String]; |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace nativeapi |
0 commit comments