fix: pass button callback in constructor (#27545)
This commit is contained in:
parent
79b3393768
commit
6edf6c6a95
4 changed files with 22 additions and 12 deletions
|
@ -167,7 +167,7 @@ bool MenuBar::AcceleratorPressed(const ui::Accelerator& accelerator) {
|
||||||
|
|
||||||
if (keycode == accelerator.key_code()) {
|
if (keycode == accelerator.key_code()) {
|
||||||
auto event = accelerator.ToKeyEvent();
|
auto event = accelerator.ToKeyEvent();
|
||||||
ButtonPressed(button, event);
|
ButtonPressed(button->tag(), event);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -254,7 +254,7 @@ const char* MenuBar::GetClassName() const {
|
||||||
return kViewClassName;
|
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.
|
// Hide the accelerator when a submenu is activated.
|
||||||
SetAcceleratorVisibility(false);
|
SetAcceleratorVisibility(false);
|
||||||
|
|
||||||
|
@ -264,13 +264,22 @@ void MenuBar::ButtonPressed(views::Button* source, const ui::Event& event) {
|
||||||
if (!window_->HasFocus())
|
if (!window_->HasFocus())
|
||||||
window_->RequestFocus();
|
window_->RequestFocus();
|
||||||
|
|
||||||
int id = source->tag();
|
|
||||||
ElectronMenuModel::ItemType type = menu_model_->GetTypeAt(id);
|
ElectronMenuModel::ItemType type = menu_model_->GetTypeAt(id);
|
||||||
if (type != ElectronMenuModel::TYPE_SUBMENU) {
|
if (type != ElectronMenuModel::TYPE_SUBMENU) {
|
||||||
menu_model_->ActivatedAt(id, 0);
|
menu_model_->ActivatedAt(id, 0);
|
||||||
return;
|
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
|
// Deleted in MenuDelegate::OnMenuClosed
|
||||||
auto* menu_delegate = new MenuDelegate(this);
|
auto* menu_delegate = new MenuDelegate(this);
|
||||||
menu_delegate->RunMenu(
|
menu_delegate->RunMenu(
|
||||||
|
@ -304,12 +313,10 @@ void MenuBar::OnThemeChanged() {
|
||||||
void MenuBar::RebuildChildren() {
|
void MenuBar::RebuildChildren() {
|
||||||
RemoveAllChildViews(true);
|
RemoveAllChildViews(true);
|
||||||
for (int i = 0, n = GetItemCount(); i < n; ++i) {
|
for (int i = 0, n = GetItemCount(); i < n; ++i) {
|
||||||
auto* button =
|
auto* button = new SubmenuButton(
|
||||||
new SubmenuButton(menu_model_->GetLabelAt(i), background_color_);
|
base::BindRepeating(&MenuBar::ButtonPressed, base::Unretained(this), i),
|
||||||
|
menu_model_->GetLabelAt(i), background_color_);
|
||||||
button->set_tag(i);
|
button->set_tag(i);
|
||||||
button->SetCallback(base::BindRepeating(&MenuBar::ButtonPressed,
|
|
||||||
base::Unretained(this),
|
|
||||||
base::Unretained(button)));
|
|
||||||
AddChildView(button);
|
AddChildView(button);
|
||||||
}
|
}
|
||||||
UpdateViewColors();
|
UpdateViewColors();
|
||||||
|
|
|
@ -80,7 +80,7 @@ class MenuBar : public views::AccessiblePaneView,
|
||||||
// views::View:
|
// views::View:
|
||||||
const char* GetClassName() const override;
|
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 RebuildChildren();
|
||||||
void UpdateViewColors();
|
void UpdateViewColors();
|
||||||
|
|
|
@ -20,9 +20,10 @@
|
||||||
|
|
||||||
namespace electron {
|
namespace electron {
|
||||||
|
|
||||||
SubmenuButton::SubmenuButton(const base::string16& title,
|
SubmenuButton::SubmenuButton(PressedCallback callback,
|
||||||
|
const base::string16& title,
|
||||||
const SkColor& background_color)
|
const SkColor& background_color)
|
||||||
: views::MenuButton(PressedCallback(), gfx::RemoveAccelerator(title)),
|
: views::MenuButton(callback, gfx::RemoveAccelerator(title)),
|
||||||
background_color_(background_color) {
|
background_color_(background_color) {
|
||||||
#if defined(OS_LINUX)
|
#if defined(OS_LINUX)
|
||||||
// Dont' use native style border.
|
// Dont' use native style border.
|
||||||
|
|
|
@ -16,7 +16,9 @@ namespace electron {
|
||||||
// Special button that used by menu bar to show submenus.
|
// Special button that used by menu bar to show submenus.
|
||||||
class SubmenuButton : public views::MenuButton {
|
class SubmenuButton : public views::MenuButton {
|
||||||
public:
|
public:
|
||||||
SubmenuButton(const base::string16& title, const SkColor& background_color);
|
SubmenuButton(PressedCallback callback,
|
||||||
|
const base::string16& title,
|
||||||
|
const SkColor& background_color);
|
||||||
~SubmenuButton() override;
|
~SubmenuButton() override;
|
||||||
|
|
||||||
void SetAcceleratorVisibility(bool visible);
|
void SetAcceleratorVisibility(bool visible);
|
||||||
|
|
Loading…
Reference in a new issue