Skip to content

Commit

Permalink
feat: migrate custom macOS tray view to native one (#18981)
Browse files Browse the repository at this point in the history
* restore stash

revert

some things work others dont

tracking area for rescue

manual popup

restore drag n drop

cleanup

* fix: make tray not block main process (#18880)

* fix: make tray not block main process

* make AtomMenuModel refcounted

* add support for ansi codes in title

add remove TODOs

* chore: use ScopedPumpMessagesInPrivateModes in tray (#18977)

* chore: use ScopedPumpMessagesInPrivateModes in tray

* revert refcounting of AtomMenuModel

* Prefer WeakPtr for posting tasks to handle unexpected destruction

* cleanup .h

* cleanup .mm

* add imports

add missing include

* fix: crash when tray popup called twice (#18999)

* remove highlightMode and TODOs

* remove unnecessary copy
  • Loading branch information
Micha Hanselmann authored and MarshallOfSound committed Jul 31, 2019
1 parent cde7950 commit 47a38da
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 357 deletions.
33 changes: 0 additions & 33 deletions docs/api/tray.md
Expand Up @@ -206,39 +206,6 @@ Sets the title displayed next to the tray icon in the status bar (Support ANSI c

Returns `String` - the title displayed next to the tray icon in the status bar

#### `tray.setHighlightMode(mode)` _macOS_

* `mode` String - Highlight mode with one of the following values:
* `selection` - Highlight the tray icon when it is clicked and also when
its context menu is open. This is the default.
* `always` - Always highlight the tray icon.
* `never` - Never highlight the tray icon.

Sets when the tray's icon background becomes highlighted (in blue).

**[Deprecated](breaking-changes.md#tray)**

**Note:** You can use `highlightMode` with a [`BrowserWindow`](browser-window.md)
by toggling between `'never'` and `'always'` modes when the window visibility
changes.

```javascript
const { BrowserWindow, Tray } = require('electron')

const win = new BrowserWindow({ width: 800, height: 600 })
const tray = new Tray('/path/to/my/icon')

tray.on('click', () => {
win.isVisible() ? win.hide() : win.show()
})
win.on('show', () => {
tray.setHighlightMode('always')
})
win.on('hide', () => {
tray.setHighlightMode('never')
})
```

#### `tray.setIgnoreDoubleClickEvents(ignore)` _macOS_

* `ignore` Boolean
Expand Down
3 changes: 0 additions & 3 deletions lib/browser/api/tray.js
Expand Up @@ -6,7 +6,4 @@ const { Tray } = process.electronBinding('tray')

Object.setPrototypeOf(Tray.prototype, EventEmitter.prototype)

// Deprecations
Tray.prototype.setHighlightMode = deprecate.removeFunction(Tray.prototype.setHighlightMode, 'setHighlightMode')

module.exports = Tray
33 changes: 0 additions & 33 deletions shell/browser/api/atom_api_tray.cc
Expand Up @@ -18,34 +18,6 @@
#include "shell/common/node_includes.h"
#include "ui/gfx/image/image.h"

namespace mate {

template <>
struct Converter<electron::TrayIcon::HighlightMode> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::TrayIcon::HighlightMode* out) {
using HighlightMode = electron::TrayIcon::HighlightMode;
std::string mode;
if (ConvertFromV8(isolate, val, &mode)) {
if (mode == "always") {
*out = HighlightMode::ALWAYS;
return true;
}
if (mode == "selection") {
*out = HighlightMode::SELECTION;
return true;
}
if (mode == "never") {
*out = HighlightMode::NEVER;
return true;
}
}
return false;
}
};
} // namespace mate

namespace electron {

namespace api {
Expand Down Expand Up @@ -169,10 +141,6 @@ std::string Tray::GetTitle() {
#endif
}

void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) {
tray_icon_->SetHighlightMode(mode);
}

void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
#if defined(OS_MACOSX)
tray_icon_->SetIgnoreDoubleClickEvents(ignore);
Expand Down Expand Up @@ -235,7 +203,6 @@ void Tray::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setToolTip", &Tray::SetToolTip)
.SetMethod("setTitle", &Tray::SetTitle)
.SetMethod("getTitle", &Tray::GetTitle)
.SetMethod("setHighlightMode", &Tray::SetHighlightMode)
.SetMethod("setIgnoreDoubleClickEvents",
&Tray::SetIgnoreDoubleClickEvents)
.SetMethod("getIgnoreDoubleClickEvents",
Expand Down
1 change: 0 additions & 1 deletion shell/browser/api/atom_api_tray.h
Expand Up @@ -70,7 +70,6 @@ class Tray : public mate::TrackableObject<Tray>, public TrayIconObserver {
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();
void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options);
Expand Down
2 changes: 0 additions & 2 deletions shell/browser/ui/tray_icon.cc
Expand Up @@ -12,8 +12,6 @@ TrayIcon::~TrayIcon() {}

void TrayIcon::SetPressedImage(ImageType image) {}

void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) {}

void TrayIcon::DisplayBalloon(ImageType icon,
const base::string16& title,
const base::string16& contents) {}
Expand Down
8 changes: 0 additions & 8 deletions shell/browser/ui/tray_icon.h
Expand Up @@ -39,14 +39,6 @@ class TrayIcon {
// status icon (e.g. Ubuntu Unity).
virtual void SetToolTip(const std::string& tool_tip) = 0;

// Sets the status icon highlight mode. This only works on macOS.
enum class HighlightMode {
ALWAYS, // Always highlight the tray icon
NEVER, // Never highlight the tray icon
SELECTION // Highlight the tray icon when clicked or the menu is opened
};
virtual void SetHighlightMode(HighlightMode mode);

#if defined(OS_MACOSX)
// Set/Get flag determining whether to ignore double click events.
virtual void SetIgnoreDoubleClickEvents(bool ignore) = 0;
Expand Down
12 changes: 2 additions & 10 deletions shell/browser/ui/tray_icon_cocoa.h
Expand Up @@ -17,7 +17,7 @@

namespace electron {

class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer {
class TrayIconCocoa : public TrayIcon {
public:
TrayIconCocoa();
~TrayIconCocoa() override;
Expand All @@ -27,7 +27,6 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer {
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;
void PopUpOnUI(AtomMenuModel* menu_model);
Expand All @@ -36,20 +35,13 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer {
void SetContextMenu(AtomMenuModel* menu_model) override;
gfx::Rect GetBounds() override;

protected:
// AtomMenuModel::Observer:
void OnMenuWillClose() override;

private:
// Atom custom view for NSStatusItem.
// Electron custom view for NSStatusItem.
base::scoped_nsobject<StatusItemView> status_item_view_;

// Status menu shown when right-clicking the system icon.
base::scoped_nsobject<AtomMenuController> menu_;

// Used for unregistering observer.
AtomMenuModel* menu_model_ = nullptr; // weak ref.

base::WeakPtrFactory<TrayIconCocoa> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(TrayIconCocoa);
Expand Down

0 comments on commit 47a38da

Please sign in to comment.