electron/shell/browser/ui/gtk/menu_util.cc
electron-roller[bot] 8c89137ecd
chore: bump chromium to 122.0.6194.0 (main) (#40750)
* chore: bump chromium in DEPS to 122.0.6180.0

* chore: update render_widget_host_view_base.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5079532

minor manual changes to match upstream changes

* chore: update printing.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5094777

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5054299

Update patch to upstream changes in print_view_manager_base.cc.
CC @codebytere for 👀

* chore: update feat_add_onclose_to_messageport.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5083156

no manual changes; patch applied with fuzz 1

* chore: update build_do_not_depend_on_packed_resource_integrity.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5087072

minor manual changes to match upstream changes

* chore: update port_autofill_colors_to_the_color_pipeline.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4960481

no manual changes; patch applied with fuzz 2

* chore: update revert_api_dcheck-fail_when_we_reenter_v8_while_terminating.patch

Xref: https://chromium-review.googlesource.com/c/v8/v8/+/5076902
Xref: https://chromium-review.googlesource.com/c/v8/v8/+/5096253

The `bailout_value` variable, which allowed us to fail gracefully in
these macros, has been removed upstream. This raises the temperature
on https://github.com/electron/electron/issues/40741 ...

Since we don't have a bailout value, return a default-constructed
instance of whatever the return type is of the function that invokes
the macro.

* Remove performActionOnRemotePage

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5068017

It appears it is not used by anything within Chromium or outside.

* perf: in SubmenuButton ctor, move() the PressedCallback

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5066872

The copy construtor is more expensive and has been removed upstream

* chore: update constructor call for ElectronExtensionsRendererClient::dispatcher_

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5055146

* chore: handle CAPTURED_SURFACE_CONTROL in Converter<blink::PermissionType>::ToV8()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5096614

* chore: e patches all

* chore: bump chromium to 122.0.6181.0

* chore: e patches all

* refactor: Add lookup for per-RenderFrame SpellCheckProvider

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5075094

Previously we could pass a routing ID into the spellcheck methods.
This ID arg has been replaced with a SpellCheckHost& arg.

We can get the SpellCheckHost from an accessor in SpellCheckProvider,
which was already being created on a per-render-frame basis in
RenderClientBase.

This commit adds a RenderFrame-to-SpellCheckProvider accessor
in RenderClientBase so that it can be used to get that needed
SpellCheckHost.

* chore: make lint happy

* chore: restore the will_cause_resize arg to WebContents::ExitFullscreen

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5074445

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5031312

upstream reverted this change

* chore: bump chromium in DEPS to 122.0.6182.0

* chore: update patches

* chore: fix virtual function names that were renamed upstream

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5115241

* chore: handle new blink::PermissionType::SMART_CARD in PermissionType converter

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5049989

* chore: node script/gen-libc++-filenames.js

* fixup! chore: update build_do_not_depend_on_packed_resource_integrity.patch

* chore: remove revert_api_dcheck-fail_when_we_reenter_v8_while_terminating.patch

The previous experiment of removing the DCHECK in 35093783 was a huge
failure. We either need to either:

A) use upstream as-is and fix the DCHECK failure, or
B) revert https://chromium-review.googlesource.com/c/v8/v8/+/5076902 too
C) find some other workaround

This commit is the first half of option A, removing the patch to use
upstream as-is.

* chore: bump chromium in DEPS to 122.0.6184.0

* chore: bump chromium in DEPS to 122.0.6186.0

* chore; update mas_avoid_private_macos_api_usage.patch.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5085619

minor manual changes to match upstream changes

* chore: update build_do_not_depend_on_packed_resource_integrity.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5120922

minor manual changes to match upstream changes

* chore: update fix_move_autopipsettingshelper_behind_branding_buildflag.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5041595

no manual changes; patch applied with fuzz 1

* chore: e patches all

* chore: add refactor_update_node-platform_to_match_v8-platform_api_changes.patch

* chore: bump chromium in DEPS to 122.0.6188.0

* chore: bump chromium in DEPS to 122.0.6190.0

* chore: bump chromium in DEPS to 122.0.6192.0

* chore: bump chromium in DEPS to 122.0.6194.0

* Remove an unnecessary DCHECK which causes Lacros to crash.

https://chromium-review.googlesource.com/c/chromium/src/+/5124880

* [FixIt Week] Add shared regular profile function that includes ash

https://chromium-review.googlesource.com/c/chromium/src/+/5100709

* Simplify the PrintManagerHost::ScriptedPrint() call

https://chromium-review.googlesource.com/c/chromium/src/+/5019703

* [local compile hints] Add tests

https://chromium-review.googlesource.com/c/chromium/src/+/5024173

* chore: update patches

* fixup! chore: add refactor_update_node-platform_to_match_v8-platform_api_changes.patch

* chore: node script/gen-libc++-filenames.js

* Remove //ui/ozone/buildflags.h

https://chromium-review.googlesource.com/c/chromium/src/+/5119048

* fixup! Remove //ui/ozone/buildflags.h

* fixup! Remove //ui/ozone/buildflags.h

* chore: update patches

* fixup! refactor: Add lookup for per-RenderFrame SpellCheckProvider

* fixup! Remove //ui/ozone/buildflags.h

* Add permission policy verification in PermissionControllerImpl

https://chromium-review.googlesource.com/c/chromium/src/+/5059112

* fix: ensure spellcheck interface is registered for frame

* [exceptions] Unify pending and scheduled exceptions

https://chromium-review.googlesource.com/c/v8/v8/+/5050065

* fix: dchecks in node.js tests

* fix: try adding termination check

* chore: document breaking iframe change

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2024-01-03 09:52:49 +01:00

318 lines
11 KiB
C++

// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shell/browser/ui/gtk/menu_util.h"
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <map>
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "shell/browser/ui/gtk_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkUnPreMultiply.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/menu_label_accelerator_util_linux.h"
#include "ui/base/models/image_model.h"
#include "ui/base/models/menu_model.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/ozone/public/ozone_platform.h"
namespace electron::gtkui {
namespace {
int EventFlagsFromGdkState(guint state) {
int flags = ui::EF_NONE;
flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_ON : ui::EF_NONE;
flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE;
flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE;
flags |=
(state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE;
flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE;
return flags;
}
guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) {
// The second parameter is false because accelerator keys are expressed in
// terms of the non-shift-modified key.
return XKeysymForWindowsKeyCode(accelerator.key_code(), false);
}
GdkModifierType GetGdkModifierForAccelerator(
const ui::Accelerator& accelerator) {
int event_flag = accelerator.modifiers();
int modifier = 0;
if (event_flag & ui::EF_SHIFT_DOWN)
modifier |= GDK_SHIFT_MASK;
if (event_flag & ui::EF_CONTROL_DOWN)
modifier |= GDK_CONTROL_MASK;
if (event_flag & ui::EF_ALT_DOWN)
modifier |= GDK_MOD1_MASK;
return static_cast<GdkModifierType>(modifier);
}
} // namespace
GtkWidget* BuildMenuItemWithImage(const std::string& label, GtkWidget* image) {
// GTK4 removed support for image menu items.
#if GTK_CHECK_VERSION(3, 90, 0)
return gtk_menu_item_new_with_mnemonic(label.c_str());
#else
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
GtkWidget* menu_item = gtk_image_menu_item_new_with_mnemonic(label.c_str());
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
G_GNUC_END_IGNORE_DEPRECATIONS;
return menu_item;
#endif
}
GtkWidget* BuildMenuItemWithImage(const std::string& label,
const gfx::Image& icon) {
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(*icon.ToSkBitmap());
GtkWidget* menu_item =
BuildMenuItemWithImage(label, gtk_image_new_from_pixbuf(pixbuf));
g_object_unref(pixbuf);
return menu_item;
}
GtkWidget* BuildMenuItemWithLabel(const std::string& label) {
return gtk_menu_item_new_with_mnemonic(label.c_str());
}
ui::MenuModel* ModelForMenuItem(GtkMenuItem* menu_item) {
return reinterpret_cast<ui::MenuModel*>(
g_object_get_data(G_OBJECT(menu_item), "model"));
}
GtkWidget* AppendMenuItemToMenu(int index,
ui::MenuModel* model,
GtkWidget* menu_item,
GtkWidget* menu,
bool connect_to_activate,
MenuActivatedCallback item_activated_cb,
std::vector<ScopedGSignal>* signals) {
// Set the ID of a menu item.
// Add 1 to the menu_id to avoid setting zero (null) to "menu-id".
g_object_set_data(G_OBJECT(menu_item), "menu-id", GINT_TO_POINTER(index + 1));
// Native menu items do their own thing, so only selectively listen for the
// activate signal.
if (connect_to_activate) {
signals->emplace_back(menu_item, "activate", item_activated_cb);
}
// AppendMenuItemToMenu is used both internally when we control menu creation
// from a model (where the model can choose to hide certain menu items), and
// with immediate commands which don't provide the option.
if (model) {
if (model->IsVisibleAt(index))
gtk_widget_show(menu_item);
} else {
gtk_widget_show(menu_item);
}
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
return menu_item;
}
bool GetMenuItemID(GtkWidget* menu_item, int* menu_id) {
gpointer id_ptr = g_object_get_data(G_OBJECT(menu_item), "menu-id");
if (id_ptr != nullptr) {
*menu_id = GPOINTER_TO_INT(id_ptr) - 1;
return true;
}
return false;
}
void ExecuteCommand(ui::MenuModel* model, int id) {
GdkEvent* event = gtk_get_current_event();
int event_flags = 0;
if (event && event->type == GDK_BUTTON_RELEASE)
event_flags = EventFlagsFromGdkState(event->button.state);
model->ActivatedAt(static_cast<int>(id), event_flags);
if (event)
gdk_event_free(event);
}
void BuildSubmenuFromModel(ui::MenuModel* model,
GtkWidget* menu,
MenuActivatedCallback item_activated_cb,
bool* block_activation,
std::vector<ScopedGSignal>* signals) {
std::map<int, GtkWidget*> radio_groups;
GtkWidget* menu_item = nullptr;
for (size_t i = 0; i < model->GetItemCount(); ++i) {
std::string label = ui::ConvertAcceleratorsFromWindowsStyle(
base::UTF16ToUTF8(model->GetLabelAt(i)));
bool connect_to_activate = true;
switch (model->GetTypeAt(i)) {
case ui::MenuModel::TYPE_SEPARATOR:
menu_item = gtk_separator_menu_item_new();
break;
case ui::MenuModel::TYPE_CHECK:
menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str());
break;
case ui::MenuModel::TYPE_RADIO: {
auto iter = radio_groups.find(model->GetGroupIdAt(i));
if (iter == radio_groups.end()) {
menu_item =
gtk_radio_menu_item_new_with_mnemonic(nullptr, label.c_str());
radio_groups[model->GetGroupIdAt(i)] = menu_item;
} else {
menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
GTK_RADIO_MENU_ITEM(iter->second), label.c_str());
}
break;
}
case ui::MenuModel::TYPE_BUTTON_ITEM: {
NOTIMPLEMENTED();
break;
}
case ui::MenuModel::TYPE_SUBMENU:
case ui::MenuModel::TYPE_COMMAND: {
auto icon_model = model->GetIconAt(i);
if (!icon_model.IsEmpty())
menu_item = BuildMenuItemWithImage(label, icon_model.GetImage());
else
menu_item = BuildMenuItemWithLabel(label);
#if !GTK_CHECK_VERSION(3, 90, 0)
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
if (GTK_IS_IMAGE_MENU_ITEM(menu_item)) {
gtk_image_menu_item_set_always_show_image(
GTK_IMAGE_MENU_ITEM(menu_item), TRUE);
}
G_GNUC_END_IGNORE_DEPRECATIONS;
#endif
break;
}
default:
NOTREACHED();
}
if (model->GetTypeAt(i) == ui::MenuModel::TYPE_SUBMENU) {
GtkWidget* submenu = gtk_menu_new();
ui::MenuModel* submenu_model = model->GetSubmenuModelAt(i);
BuildSubmenuFromModel(submenu_model, submenu, item_activated_cb,
block_activation, signals);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), submenu);
// Update all the menu item info in the newly-generated menu.
gtk_container_foreach(GTK_CONTAINER(submenu), SetMenuItemInfo,
block_activation);
submenu_model->MenuWillShow();
connect_to_activate = false;
}
if (ui::OzonePlatform::GetInstance()
->GetPlatformProperties()
.electron_can_call_x11) {
ui::Accelerator accelerator;
if (model->GetAcceleratorAt(i, &accelerator)) {
gtk_widget_add_accelerator(menu_item, "activate", nullptr,
GetGdkKeyCodeForAccelerator(accelerator),
GetGdkModifierForAccelerator(accelerator),
GTK_ACCEL_VISIBLE);
}
}
g_object_set_data(G_OBJECT(menu_item), "model", model);
AppendMenuItemToMenu(i, model, menu_item, menu, connect_to_activate,
item_activated_cb, signals);
menu_item = nullptr;
}
}
void SetMenuItemInfo(GtkWidget* widget, void* block_activation_ptr) {
if (GTK_IS_SEPARATOR_MENU_ITEM(widget)) {
// We need to explicitly handle this case because otherwise we'll ask the
// menu delegate about something with an invalid id.
return;
}
int id;
if (!GetMenuItemID(widget, &id))
return;
ui::MenuModel* model = ModelForMenuItem(GTK_MENU_ITEM(widget));
if (!model) {
// If we're not providing the sub menu, then there's no model. For
// example, the IME submenu doesn't have a model.
return;
}
bool* block_activation = static_cast<bool*>(block_activation_ptr);
if (GTK_IS_CHECK_MENU_ITEM(widget)) {
GtkCheckMenuItem* item = GTK_CHECK_MENU_ITEM(widget);
// gtk_check_menu_item_set_active() will send the activate signal. Touching
// the underlying "active" property will also call the "activate" handler
// for this menu item. So we prevent the "activate" handler from
// being called while we set the checkbox.
// Why not use one of the glib signal-blocking functions? Because when we
// toggle a radio button, it will deactivate one of the other radio buttons,
// which we don't have a pointer to.
*block_activation = true;
gtk_check_menu_item_set_active(item, model->IsItemCheckedAt(id));
*block_activation = false;
}
if (GTK_IS_MENU_ITEM(widget)) {
gtk_widget_set_sensitive(widget, model->IsEnabledAt(id));
if (model->IsVisibleAt(id)) {
// Update the menu item label if it is dynamic.
if (model->IsItemDynamicAt(id)) {
std::string label = ui::ConvertAcceleratorsFromWindowsStyle(
base::UTF16ToUTF8(model->GetLabelAt(id)));
gtk_menu_item_set_label(GTK_MENU_ITEM(widget), label.c_str());
#if !GTK_CHECK_VERSION(3, 90, 0)
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
if (GTK_IS_IMAGE_MENU_ITEM(widget)) {
auto icon_model = model->GetIconAt(id);
if (!icon_model.IsEmpty()) {
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(
*icon_model.GetImage().ToSkBitmap());
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget),
gtk_image_new_from_pixbuf(pixbuf));
g_object_unref(pixbuf);
} else {
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(widget), nullptr);
}
}
G_GNUC_END_IGNORE_DEPRECATIONS;
#endif
}
gtk_widget_show(widget);
} else {
gtk_widget_hide(widget);
}
GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widget));
if (submenu) {
gtk_container_foreach(GTK_CONTAINER(submenu), &SetMenuItemInfo,
block_activation_ptr);
}
}
}
} // namespace electron::gtkui