Skip to content

Commit

Permalink
fix: do not use CONTEXT_MENU flag for tray menu (#23886)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckerr committed Jun 2, 2020
1 parent c8177f8 commit 91d1bd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 deletions.
35 changes: 5 additions & 30 deletions shell/browser/ui/win/notify_icon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"

namespace {

Expand All @@ -44,11 +43,7 @@ UINT ConvertIconType(electron::TrayIcon::IconType type) {
namespace electron {

NotifyIcon::NotifyIcon(NotifyIconHost* host, UINT id, HWND window, UINT message)
: host_(host),
icon_id_(id),
window_(window),
message_id_(message),
weak_factory_(this) {
: host_(host), icon_id_(id), window_(window), message_id_(message) {
NOTIFYICONDATA icon_data;
InitIconData(&icon_data);
icon_data.uFlags |= NIF_MESSAGE;
Expand Down Expand Up @@ -207,26 +202,10 @@ void NotifyIcon::PopUpContextMenu(const gfx::Point& pos,
if (pos.IsOrigin())
rect.set_origin(display::Screen::GetScreen()->GetCursorScreenPoint());

// Create a widget for the menu, otherwise we get no keyboard events, which
// is required for accessibility.
widget_.reset(new views::Widget());
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.ownership =
views::Widget::InitParams::Ownership::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 0, 0);
params.force_software_compositing = true;
params.z_order = ui::ZOrderLevel::kFloatingUIElement;

widget_->Init(std::move(params));

widget_->Show();
widget_->Activate();
menu_runner_.reset(new views::MenuRunner(
menu_model != nullptr ? menu_model : menu_model_,
views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS,
base::BindRepeating(&NotifyIcon::OnContextMenuClosed,
weak_factory_.GetWeakPtr())));
menu_runner_->RunMenuAt(widget_.get(), NULL, rect,
menu_runner_.reset(
new views::MenuRunner(menu_model != nullptr ? menu_model : menu_model_,
views::MenuRunner::HAS_MNEMONICS));
menu_runner_->RunMenuAt(nullptr, nullptr, rect,
views::MenuAnchorPosition::kTopLeft,
ui::MENU_SOURCE_MOUSE);
}
Expand Down Expand Up @@ -254,8 +233,4 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) {
icon_data->uID = icon_id_;
}

void NotifyIcon::OnContextMenuClosed() {
widget_->Close();
}

} // namespace electron
11 changes: 1 addition & 10 deletions shell/browser/ui/win/notify_icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/win/scoped_gdi_object.h"
#include "shell/browser/ui/tray_icon.h"

Expand All @@ -24,8 +23,7 @@ class Point;

namespace views {
class MenuRunner;
class Widget;
} // namespace views
}

namespace electron {

Expand Down Expand Up @@ -68,7 +66,6 @@ class NotifyIcon : public TrayIcon {

private:
void InitIconData(NOTIFYICONDATA* icon_data);
void OnContextMenuClosed();

// The tray that owns us. Weak.
NotifyIconHost* host_;
Expand All @@ -91,12 +88,6 @@ class NotifyIcon : public TrayIcon {
// Context menu associated with this icon (if any).
std::unique_ptr<views::MenuRunner> menu_runner_;

// Temporary widget for the context menu, needed for keyboard event capture.
std::unique_ptr<views::Widget> widget_;

// WeakPtrFactory for CloseClosure safety.
base::WeakPtrFactory<NotifyIcon> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(NotifyIcon);
};

Expand Down

0 comments on commit 91d1bd6

Please sign in to comment.