@@ -25,10 +25,10 @@ @interface TrayIconMenuDelegate : NSObject <NSMenuDelegate>
2525// Private implementation class
2626class TrayIcon ::Impl {
2727 public:
28- Impl () : ns_status_item_(nil ), delegate_(nil ), menu_delegate_(nil ), visible_( false ) {}
28+ Impl () : ns_status_item_(nil ), delegate_(nil ), menu_delegate_(nil ) {}
2929
3030 Impl (NSStatusItem * status_item)
31- : ns_status_item_(status_item), delegate_(nil ), menu_delegate_(nil ), visible_( false ) {
31+ : ns_status_item_(status_item), delegate_(nil ), menu_delegate_(nil ) {
3232 if (status_item) {
3333 // Create and set up delegate
3434 delegate_ = [[TrayIconDelegate alloc ] init ];
@@ -82,9 +82,6 @@ @interface TrayIconMenuDelegate : NSObject <NSMenuDelegate>
8282 TrayIconDelegate* delegate_;
8383 TrayIconMenuDelegate* menu_delegate_;
8484 std::shared_ptr<Menu> context_menu_;
85- std::string title_;
86- std::string tooltip_;
87- bool visible_;
8885};
8986
9087TrayIcon::TrayIcon () : pimpl_(std::make_unique<Impl>()) {
@@ -164,30 +161,46 @@ @interface TrayIconMenuDelegate : NSObject <NSMenuDelegate>
164161 }
165162}
166163
167- void TrayIcon::SetTitle (std::string title) {
168- pimpl_->title_ = title;
169-
164+ void TrayIcon::SetTitle (std::optional<std::string> title) {
170165 if (pimpl_->ns_status_item_ && pimpl_->ns_status_item_ .button ) {
171- NSString * titleString = [NSString stringWithUTF8String: title.c_str ()];
172- [pimpl_->ns_status_item_.button setTitle: titleString];
166+ if (title.has_value ()) {
167+ NSString * titleString = [NSString stringWithUTF8String: title.value ().c_str ()];
168+ [pimpl_->ns_status_item_.button setTitle: titleString];
169+ } else {
170+ [pimpl_->ns_status_item_.button setTitle: @" " ];
171+ }
173172 }
174173}
175174
176- std::string TrayIcon::GetTitle () {
177- return pimpl_->title_ ;
175+ std::optional<std::string> TrayIcon::GetTitle () {
176+ if (pimpl_->ns_status_item_ && pimpl_->ns_status_item_ .button ) {
177+ NSString * titleString = [pimpl_->ns_status_item_.button title ];
178+ if (titleString && [titleString length ] > 0 ) {
179+ return std::string ([titleString UTF8String ]);
180+ }
181+ }
182+ return std::nullopt ;
178183}
179184
180- void TrayIcon::SetTooltip (std::string tooltip) {
181- pimpl_->tooltip_ = tooltip;
182-
185+ void TrayIcon::SetTooltip (std::optional<std::string> tooltip) {
183186 if (pimpl_->ns_status_item_ && pimpl_->ns_status_item_ .button ) {
184- NSString * tooltipString = [NSString stringWithUTF8String: tooltip.c_str ()];
185- [pimpl_->ns_status_item_.button setToolTip: tooltipString];
187+ if (tooltip.has_value ()) {
188+ NSString * tooltipString = [NSString stringWithUTF8String: tooltip.value ().c_str ()];
189+ [pimpl_->ns_status_item_.button setToolTip: tooltipString];
190+ } else {
191+ [pimpl_->ns_status_item_.button setToolTip: nil ];
192+ }
186193 }
187194}
188195
189- std::string TrayIcon::GetTooltip () {
190- return pimpl_->tooltip_ ;
196+ std::optional<std::string> TrayIcon::GetTooltip () {
197+ if (pimpl_->ns_status_item_ && pimpl_->ns_status_item_ .button ) {
198+ NSString * tooltipString = [pimpl_->ns_status_item_.button toolTip ];
199+ if (tooltipString && [tooltipString length ] > 0 ) {
200+ return std::string ([tooltipString UTF8String ]);
201+ }
202+ }
203+ return std::nullopt ;
191204}
192205
193206void TrayIcon::SetContextMenu (std::shared_ptr<Menu> menu) {
@@ -224,29 +237,20 @@ @interface TrayIconMenuDelegate : NSObject <NSMenuDelegate>
224237 return bounds;
225238}
226239
227- bool TrayIcon::Show () {
228- if (pimpl_->ns_status_item_ ) {
229- [pimpl_->ns_status_item_ setVisible: YES ];
230- pimpl_->visible_ = true ;
231- return true ;
240+ bool TrayIcon::SetVisible (bool visible) {
241+ if (!pimpl_->ns_status_item_ ) {
242+ return false ;
232243 }
233- return false ;
234- }
235244
236- bool TrayIcon::Hide () {
237- if (pimpl_->ns_status_item_ ) {
238- [pimpl_->ns_status_item_ setVisible: NO ];
239- pimpl_->visible_ = false ;
240- return true ;
241- }
242- return false ;
245+ [pimpl_->ns_status_item_ setVisible: visible ? YES : NO ];
246+ return true ;
243247}
244248
245249bool TrayIcon::IsVisible () {
246250 if (pimpl_->ns_status_item_ ) {
247251 return [pimpl_->ns_status_item_ isVisible ] == YES ;
248252 }
249- return pimpl_-> visible_ ;
253+ return false ;
250254}
251255
252256bool TrayIcon::OpenContextMenu (double x, double y) {
0 commit comments