diff --git a/shell/browser/ui/views/menu_bar.cc b/shell/browser/ui/views/menu_bar.cc index 0ccf916f964f..fe4ed9f7ac1e 100644 --- a/shell/browser/ui/views/menu_bar.cc +++ b/shell/browser/ui/views/menu_bar.cc @@ -167,7 +167,7 @@ bool MenuBar::AcceleratorPressed(const ui::Accelerator& accelerator) { if (keycode == accelerator.key_code()) { auto event = accelerator.ToKeyEvent(); - ButtonPressed(button, event); + ButtonPressed(button->tag(), event); return true; } } @@ -254,7 +254,7 @@ const char* MenuBar::GetClassName() const { return kViewClassName; } -void MenuBar::ButtonPressed(views::Button* source, const ui::Event& event) { +void MenuBar::ButtonPressed(int id, const ui::Event& event) { // Hide the accelerator when a submenu is activated. SetAcceleratorVisibility(false); @@ -264,13 +264,22 @@ void MenuBar::ButtonPressed(views::Button* source, const ui::Event& event) { if (!window_->HasFocus()) window_->RequestFocus(); - int id = source->tag(); ElectronMenuModel::ItemType type = menu_model_->GetTypeAt(id); if (type != ElectronMenuModel::TYPE_SUBMENU) { menu_model_->ActivatedAt(id, 0); return; } + SubmenuButton* source = nullptr; + for (auto* child : children()) { + auto* button = static_cast(child); + if (button->tag() == id) { + source = button; + break; + } + } + DCHECK(source); + // Deleted in MenuDelegate::OnMenuClosed auto* menu_delegate = new MenuDelegate(this); menu_delegate->RunMenu( @@ -304,12 +313,10 @@ void MenuBar::OnThemeChanged() { void MenuBar::RebuildChildren() { RemoveAllChildViews(true); for (int i = 0, n = GetItemCount(); i < n; ++i) { - auto* button = - new SubmenuButton(menu_model_->GetLabelAt(i), background_color_); + auto* button = new SubmenuButton( + base::BindRepeating(&MenuBar::ButtonPressed, base::Unretained(this), i), + menu_model_->GetLabelAt(i), background_color_); button->set_tag(i); - button->SetCallback(base::BindRepeating(&MenuBar::ButtonPressed, - base::Unretained(this), - base::Unretained(button))); AddChildView(button); } UpdateViewColors(); diff --git a/shell/browser/ui/views/menu_bar.h b/shell/browser/ui/views/menu_bar.h index b570a0fa3cb2..eb2e2d35ac57 100644 --- a/shell/browser/ui/views/menu_bar.h +++ b/shell/browser/ui/views/menu_bar.h @@ -80,7 +80,7 @@ class MenuBar : public views::AccessiblePaneView, // views::View: const char* GetClassName() const override; - void ButtonPressed(views::Button* source, const ui::Event& event); + void ButtonPressed(int id, const ui::Event& event); void RebuildChildren(); void UpdateViewColors(); diff --git a/shell/browser/ui/views/submenu_button.cc b/shell/browser/ui/views/submenu_button.cc index 9d2ba5d98c21..3e3c70356c25 100644 --- a/shell/browser/ui/views/submenu_button.cc +++ b/shell/browser/ui/views/submenu_button.cc @@ -20,9 +20,10 @@ namespace electron { -SubmenuButton::SubmenuButton(const base::string16& title, +SubmenuButton::SubmenuButton(PressedCallback callback, + const base::string16& title, const SkColor& background_color) - : views::MenuButton(PressedCallback(), gfx::RemoveAccelerator(title)), + : views::MenuButton(callback, gfx::RemoveAccelerator(title)), background_color_(background_color) { #if defined(OS_LINUX) // Dont' use native style border. diff --git a/shell/browser/ui/views/submenu_button.h b/shell/browser/ui/views/submenu_button.h index 171ea8797934..a5685cd1b066 100644 --- a/shell/browser/ui/views/submenu_button.h +++ b/shell/browser/ui/views/submenu_button.h @@ -16,7 +16,9 @@ namespace electron { // Special button that used by menu bar to show submenus. class SubmenuButton : public views::MenuButton { public: - SubmenuButton(const base::string16& title, const SkColor& background_color); + SubmenuButton(PressedCallback callback, + const base::string16& title, + const SkColor& background_color); ~SubmenuButton() override; void SetAcceleratorVisibility(bool visible);