|
17 | 17 | #include "ui/gfx/geometry/rect.h"
|
18 | 18 | #include "ui/gfx/image/image.h"
|
19 | 19 | #include "ui/views/controls/menu/menu_runner.h"
|
| 20 | +#include "ui/views/widget/widget.h" |
20 | 21 |
|
21 | 22 | namespace electron {
|
22 | 23 |
|
23 | 24 | NotifyIcon::NotifyIcon(NotifyIconHost* host, UINT id, HWND window, UINT message)
|
24 |
| - : host_(host), icon_id_(id), window_(window), message_id_(message) { |
| 25 | + : host_(host), |
| 26 | + icon_id_(id), |
| 27 | + window_(window), |
| 28 | + message_id_(message), |
| 29 | + weak_factory_(this) { |
25 | 30 | NOTIFYICONDATA icon_data;
|
26 | 31 | InitIconData(&icon_data);
|
27 | 32 | icon_data.uFlags |= NIF_MESSAGE;
|
@@ -155,10 +160,26 @@ void NotifyIcon::PopUpContextMenu(const gfx::Point& pos,
|
155 | 160 | if (pos.IsOrigin())
|
156 | 161 | rect.set_origin(display::Screen::GetScreen()->GetCursorScreenPoint());
|
157 | 162 |
|
158 |
| - menu_runner_.reset( |
159 |
| - new views::MenuRunner(menu_model != nullptr ? menu_model : menu_model_, |
160 |
| - views::MenuRunner::HAS_MNEMONICS)); |
161 |
| - menu_runner_->RunMenuAt(nullptr, nullptr, rect, |
| 163 | + // Create a widget for the menu, otherwise we get no keyboard events, which |
| 164 | + // is required for accessibility. |
| 165 | + widget_.reset(new views::Widget()); |
| 166 | + views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 167 | + params.ownership = |
| 168 | + views::Widget::InitParams::Ownership::WIDGET_OWNS_NATIVE_WIDGET; |
| 169 | + params.bounds = gfx::Rect(0, 0, 0, 0); |
| 170 | + params.force_software_compositing = true; |
| 171 | + params.z_order = ui::ZOrderLevel::kFloatingUIElement; |
| 172 | + |
| 173 | + widget_->Init(std::move(params)); |
| 174 | + |
| 175 | + widget_->Show(); |
| 176 | + widget_->Activate(); |
| 177 | + menu_runner_.reset(new views::MenuRunner( |
| 178 | + menu_model != nullptr ? menu_model : menu_model_, |
| 179 | + views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS, |
| 180 | + base::BindRepeating(&NotifyIcon::OnContextMenuClosed, |
| 181 | + weak_factory_.GetWeakPtr()))); |
| 182 | + menu_runner_->RunMenuAt(widget_.get(), NULL, rect, |
162 | 183 | views::MenuAnchorPosition::kTopLeft,
|
163 | 184 | ui::MENU_SOURCE_MOUSE);
|
164 | 185 | }
|
@@ -186,4 +207,8 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) {
|
186 | 207 | icon_data->uID = icon_id_;
|
187 | 208 | }
|
188 | 209 |
|
| 210 | +void NotifyIcon::OnContextMenuClosed() { |
| 211 | + widget_->Close(); |
| 212 | +} |
| 213 | + |
189 | 214 | } // namespace electron
|
0 commit comments