fix: don't crash on tray.setContextMenu(null) (#14322)

This commit is contained in:
Milan Burda 2018-08-27 18:58:47 +02:00 committed by Charles Kerr
parent fd27d75129
commit a08ca9defb
4 changed files with 38 additions and 6 deletions

View file

@ -210,7 +210,7 @@ void Tray::PopUpContextMenu(mate::Arguments* args) {
void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) { void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu) {
menu_.Reset(isolate, menu.ToV8()); menu_.Reset(isolate, menu.ToV8());
tray_icon_->SetContextMenu(menu->model()); tray_icon_->SetContextMenu(menu.IsEmpty() ? nullptr : menu->model());
} }
gfx::Rect Tray::GetBounds() { gfx::Rect Tray::GetBounds() {

View file

@ -474,11 +474,18 @@ void TrayIconCocoa::SetContextMenu(AtomMenuModel* menu_model) {
// Substribe to MenuClosed event. // Substribe to MenuClosed event.
if (menu_model_) if (menu_model_)
menu_model_->RemoveObserver(this); menu_model_->RemoveObserver(this);
menu_model->AddObserver(this);
menu_model_ = menu_model;
if (menu_model) {
menu_model->AddObserver(this);
// Create native menu. // Create native menu.
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model menu_.reset([[AtomMenuController alloc] initWithModel:menu_model
useDefaultAccelerator:NO]); useDefaultAccelerator:NO]);
} else {
menu_.reset();
}
[status_item_view_ setMenuController:menu_.get()]; [status_item_view_ setMenuController:menu_.get()];
} }

View file

@ -275,7 +275,7 @@ The `position` is only available on Windows, and it is (0, 0) by default.
#### `tray.setContextMenu(menu)` #### `tray.setContextMenu(menu)`
* `menu` Menu * `menu` Menu | null
Sets the context menu for this icon. Sets the context menu for this icon.

25
spec/api-tray-spec.js Normal file
View file

@ -0,0 +1,25 @@
const {remote} = require('electron')
const {Menu, Tray, nativeImage} = remote
describe('tray module', () => {
describe('tray.setContextMenu', () => {
let tray
beforeEach(() => {
tray = new Tray(nativeImage.createEmpty())
})
afterEach(() => {
tray.destroy()
tray = null
})
it('accepts menu instance', () => {
tray.setContextMenu(new Menu())
})
it('accepts null', () => {
tray.setContextMenu(null)
})
})
})