fix: pass button callback in constructor (#27545)

This commit is contained in:
Cheng Zhao 2021-01-30 05:43:51 +09:00 committed by GitHub
parent 79b3393768
commit 6edf6c6a95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 12 deletions

View file

@ -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<SubmenuButton*>(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();

View file

@ -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();

View file

@ -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.

View file

@ -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);