8000 fix: pass button callback in constructor by zcbenz · Pull Request #27545 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: pass button callback in constructor #27545

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions shell/browser/ui/views/menu_bar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/ui/views/menu_bar.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions shell/browser/ui/views/submenu_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion shell/browser/ui/views/submenu_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
0