![trop[bot]](/assets/img/avatar_default.png)
* chore: bump chromium in DEPS to 139.0.7242.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: update render_widget_host_view_mac.patch no code changes; just updating patch context Do a cleanup pass on the history swiper code |6604367
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update mas_avoid_private_macos_api_usage.patch.patch no code changes; just updating patch context [tracing] Delete base/trace_event/base_tracing.h |6624012
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update chore_provide_iswebcontentscreationoverridden_with_full_params.patch no manual changes; just updating patch context [ActorFramework] Refactor Actor Task Management |6618684
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update fix_move_autopipsettingshelper_behind_branding_buildflag.patch [pip] Tuck picture-in-picture windows when a file dialog is open |6449682
Reland "[document pip] Restrict the size that a website can request" |6372104
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update feat_corner_smoothing_css_rule_and_blink_painting.patch Xref: corner-shape: constraint radii based on opposite corner overlap |6592572
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update revert_code_health_clean_up_stale_macwebcontentsocclusion.patch no manual changes; just updating patch context Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update fix_rename_sqlite_win32_exports_to_avoid_conflicts_with_node_js.patch no code changes; just updating patch context Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: e patches all Co-authored-by: Charles Kerr <charles@charleskerr.com> * Plumb Verify2QwacBinding and hook it up in QwacWebContentsObserver6624719
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Remove host delegate OnMainFrameCreatedForBackgroundPage6631123
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Extensions: Rename GetResourceURL to ResolveExtensionURL6625053
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Consolidate NativeFrameViewMac6614239
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * ICWYU Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Remove dead code WidgetAXTreeIDMap6619701
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Reland "extensions: Add `WillPrepareForEvaluation` to setup MojoJS"6630056
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * NavigationThrottleRunner2: Remove MaybeAddThrottle6628079
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Tuck picture-in-picture windows when a file dialog is open6449682
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * build: fix snapshot_blob.bin build error xref: https://issues.chromium.org/issues/416540976 Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: e patches all Co-authored-by: Charles Kerr <charles@charleskerr.com> * build: freeup disk space on macos Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: bump chromium in DEPS to 139.0.7244.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: update printing.patch no manual changes; just updating patch context Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: remove upstreamed ignore_parse_errors_for_resolveshortcutproperties.patch Prevent Windows crash on unexpected shortcut type |6633298
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: e patches all Co-authored-by: Charles Kerr <charles@charleskerr.com> * Revert "Reland "extensions: Add `WillPrepareForEvaluation` to setup MojoJS"" This reverts commit 77c4f967a637f7e8970114f91311f9fddede0f7c. Revert CL for the high confidence crash culprit for http://crash/28f897bb9743dfe0 |6641819
Co-authored-by: Charles Kerr <charles@charleskerr.com> * Fix spec's expected base64-encoded PNG strings to match upstream changes. [rust png] Enable by default. |6085801
Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: bump chromium in DEPS to 139.0.7246.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: e patches all Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: bump chromium in DEPS to 139.0.7248.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: update patches Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * siso: Enable Siso by default for non-Google builds6638830
Disabling for now until we are ready to build siso on all platforms. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Revert "revert Don't use static variable for UseExternalPopupMenus" This reverts commit e91e3894e6c34cc0ffe69ed45417c0ebec882fb1. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Update mac_sdk_min to match minimum required SDK version6493969
(cherry picked from commit 3e7cbe912d8fe1062d68ed06968aaee22013985f) Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Use default window styling on Mac6648665
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * Reland "Force the unintentional renderer process creation check by default"6626905
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * fixup: Reland "Force the unintentional renderer process creation check by default6626905
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: bump chromium in DEPS to 139.0.7249.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * fixup: Reland "Force the unintentional renderer process creation check by default6626905
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: update patches Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: bump chromium in DEPS to 139.0.7250.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: bump chromium in DEPS to 139.0.7252.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: bump chromium in DEPS to 139.0.7254.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * 6638187: browser level TOCTOU check for coordinate target6638187
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: fixup patch indices Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: add missing base/notimplemented includes Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * 6652910: [Frame Cleanup] Push down/hide implementation-specific API6652910
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: bump chromium in DEPS to 139.0.7256.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: fix lint Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * fixup! 6652910: [Frame Cleanup] Push down/hide implementation-specific API Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * fix: move HandleScope location Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * chore: bump chromium in DEPS to 139.0.7258.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * fixup! [NonClientFrameView] Consolidate NativeFrameViewMac Co-authored-by: deepak1556 <hop2deep@gmail.com> * Revert "chore: bump chromium in DEPS to 139.0.7258.0" This reverts commit 264b2e934f4b2705c47d9761010052b95d9dd5de. Co-authored-by: deepak1556 <hop2deep@gmail.com> * chore: update patches --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> 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: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: patchup[bot] <73610968+patchup[bot]@users.noreply.github.com>
311 lines
11 KiB
C++
311 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 <string>
|
|
|
|
#include "base/containers/flat_map.h"
|
|
#include "base/notimplemented.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "chrome/app/chrome_command_ids.h"
|
|
#include "shell/browser/linux/x11_util.h"
|
|
#include "shell/browser/ui/gtk_util.h"
|
|
#include "third_party/skia/include/core/SkBitmap.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,
|
|
const gfx::Image& icon) {
|
|
// GTK4 removed support for menuitem icons.
|
|
#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());
|
|
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(*icon.ToSkBitmap());
|
|
GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf);
|
|
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
|
|
g_object_unref(pixbuf);
|
|
G_GNUC_END_IGNORE_DEPRECATIONS;
|
|
return menu_item;
|
|
#endif
|
|
}
|
|
|
|
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) {
|
|
base::flat_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 (x11_util::IsX11()) {
|
|
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
|