From 2fb9085e5b9b28b7adf0ab7de720c87461b6689d Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 18 Mar 2019 12:40:34 -0700 Subject: [PATCH] feat: add tray.getTitle() (#17385) * feat: add tray.getTitle * fix spec --- atom/browser/api/atom_api_tray.cc | 11 +++++++++++ atom/browser/api/atom_api_tray.h | 1 + atom/browser/ui/tray_icon.cc | 2 -- atom/browser/ui/tray_icon.h | 11 +++++------ atom/browser/ui/tray_icon_cocoa.h | 1 + atom/browser/ui/tray_icon_cocoa.mm | 8 ++++++++ docs/api/tray.md | 8 +++++++- spec/api-tray-spec.js | 25 ++++++++++++++++++++----- 8 files changed, 53 insertions(+), 14 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 1ccd289f386..881bd5d84e4 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -159,7 +159,17 @@ void Tray::SetToolTip(const std::string& tool_tip) { } void Tray::SetTitle(const std::string& title) { +#if defined(OS_MACOSX) tray_icon_->SetTitle(title); +#endif +} + +std::string Tray::GetTitle() { +#if defined(OS_MACOSX) + return tray_icon_->GetTitle(); +#else + return ""; +#endif } void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) { @@ -227,6 +237,7 @@ void Tray::BuildPrototype(v8::Isolate* isolate, .SetMethod("setPressedImage", &Tray::SetPressedImage) .SetMethod("setToolTip", &Tray::SetToolTip) .SetMethod("setTitle", &Tray::SetTitle) + .SetMethod("getTitle", &Tray::GetTitle) .SetMethod("setHighlightMode", &Tray::SetHighlightMode) .SetMethod("setIgnoreDoubleClickEvents", &Tray::SetIgnoreDoubleClickEvents) diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 2ccb836d629..b16b92f47b2 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -69,6 +69,7 @@ class Tray : public mate::TrackableObject, public TrayIconObserver { void SetPressedImage(v8::Isolate* isolate, mate::Handle image); void SetToolTip(const std::string& tool_tip); void SetTitle(const std::string& title); + std::string GetTitle(); void SetHighlightMode(TrayIcon::HighlightMode mode); void SetIgnoreDoubleClickEvents(bool ignore); bool GetIgnoreDoubleClickEvents(); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index b5332ac97b2..14b73d93a28 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -12,8 +12,6 @@ TrayIcon::~TrayIcon() {} void TrayIcon::SetPressedImage(ImageType image) {} -void TrayIcon::SetTitle(const std::string& title) {} - void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) {} void TrayIcon::DisplayBalloon(ImageType icon, diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 76155000e88..ba799780587 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -39,10 +39,6 @@ class TrayIcon { // status icon (e.g. Ubuntu Unity). virtual void SetToolTip(const std::string& tool_tip) = 0; - // Sets the title displayed aside of the status icon in the status bar. This - // only works on macOS. - virtual void SetTitle(const std::string& title); - // Sets the status icon highlight mode. This only works on macOS. enum HighlightMode { ALWAYS, // Always highlight the tray icon @@ -51,11 +47,14 @@ class TrayIcon { }; virtual void SetHighlightMode(HighlightMode mode); - // Setter and getter for the flag which determines whether to ignore double - // click events. These only work on macOS. #if defined(OS_MACOSX) + // Set/Get flag determining whether to ignore double click events. virtual void SetIgnoreDoubleClickEvents(bool ignore) = 0; virtual bool GetIgnoreDoubleClickEvents() = 0; + + // Set/Get title displayed next to status icon in the status bar. + virtual void SetTitle(const std::string& title) = 0; + virtual std::string GetTitle() = 0; #endif // Displays a notification balloon with the specified contents. diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 34ace850a79..10db5c55323 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -26,6 +26,7 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer { void SetPressedImage(const gfx::Image& image) override; void SetToolTip(const std::string& tool_tip) override; void SetTitle(const std::string& title) override; + std::string GetTitle() override; void SetHighlightMode(TrayIcon::HighlightMode mode) override; void SetIgnoreDoubleClickEvents(bool ignore) override; bool GetIgnoreDoubleClickEvents() override; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index e466a86c35d..71d10c93d32 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -235,6 +235,10 @@ const CGFloat kVerticalTitleMargin = 2; [self updateDimensions]; } +- (NSString*)title { + return title_; +} + - (void)updateAttributedTitle { NSDictionary* attributes = @{NSFontAttributeName : [NSFont menuBarFontOfSize:0]}; @@ -459,6 +463,10 @@ void TrayIconCocoa::SetTitle(const std::string& title) { [status_item_view_ setTitle:base::SysUTF8ToNSString(title)]; } +std::string TrayIconCocoa::GetTitle() { + return base::SysNSStringToUTF8([status_item_view_ title]); +} + void TrayIconCocoa::SetHighlightMode(TrayIcon::HighlightMode mode) { [status_item_view_ setHighlight:mode]; } diff --git a/docs/api/tray.md b/docs/api/tray.md index 8457e3b1d12..c6725bf3017 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -184,7 +184,13 @@ Sets the hover text for this tray icon. * `title` String -Sets the title displayed aside of the tray icon in the status bar (Support ANSI colors). +Sets the title displayed next to the tray icon in the status bar (Support ANSI colors). + +#### `tray.getTitle()` _macOS_ + +* `title` String + +Returns `String` - the title displayed next to the tray icon in the status bar #### `tray.setHighlightMode(mode)` _macOS_ diff --git a/spec/api-tray-spec.js b/spec/api-tray-spec.js index ed6ebb2cc59..7c42fb563ef 100644 --- a/spec/api-tray-spec.js +++ b/spec/api-tray-spec.js @@ -1,4 +1,5 @@ const { remote } = require('electron') +const { expect } = require('chai') const { Menu, Tray, nativeImage } = remote describe('tray module', () => { @@ -35,13 +36,27 @@ describe('tray module', () => { }) }) - describe('tray.setTitle', () => { - it('accepts non-empty string', () => { - tray.setTitle('Hello World!') + describe('tray title get/set', () => { + before(function () { + if (process.platform !== 'darwin') { + this.skip() + } }) - it('accepts empty string', () => { - tray.setTitle('') + it('sets/gets non-empty title', () => { + const title = 'Hello World!' + tray.setTitle(title) + const newTitle = tray.getTitle() + + expect(newTitle).to.equal(title) + }) + + it('sets/gets empty title', () => { + const title = '' + tray.setTitle(title) + const newTitle = tray.getTitle() + + expect(newTitle).to.equal(title) }) }) })