electron/shell/browser/ui/views/root_view.cc
electron-roller[bot] 0dad99561b
chore: bump chromium to 123.0.6296.0 (main) (#41204)
* chore: bump chromium in DEPS to 123.0.6273.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6274.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6276.0

* chore: update patches

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* chore: bump chromium in DEPS to 123.0.6278.0

* chore: bump chromium in DEPS to 123.0.6280.0

* chore: update patches

* chore: use net::CanonicalCookie::SecureAttribute()

renamed from IsSecure()

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

* refactor: handle multiple requested device ids

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

* refactor: trigger View layouts async with View::InvalidateLayout()

Upstream has introduced a PassKey to restrict who can call Layout()
directly. I've opted for calling `InvalidateLayout()` which is the
approach that upstream recommends.

If for some reason this approach doesn't work for us, we could use
`DeprecatedLayoutImmediately()` as a stopgap.

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

Xref: https://chromium.googlesource.com/chromium/src/+/main/ui/views/view.h#809

Xref: https://chromium.googlesource.com/chromium/src/+/main/docs/ui/learn/bestpractices/layout.md?pli=1#don_t-invoke-layout_directly

* chore: bump chromium in DEPS to 123.0.6282.0

* chore: bump chromium in DEPS to 123.0.6284.0

* chore: update patches

* refactor: remove use of blink::MainThreadIsolate() pt 1/3

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

* refactor: remove use of blink::MainThreadIsolate() pt 2/3

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

* refactor: remove use of blink::MainThreadIsolate() pt 3/3

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

* chore: update enum name to ui::AXMode::kPDFPrinting

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

* chore: rebuild filenames.libcxx.gni

* chore: sync with upstream rename of PortProvider.TaskForHandle()

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

* chore: bump chromium in DEPS to 123.0.6286.0

* chore: bump chromium in DEPS to 123.0.6288.0

* WebPreferences: Initialize in declaration.

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

* chore: update webview_fullscreen.patch

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

Simple update to fix patch shear

* chore: update feat_configure_launch_options_for_service_process.patch

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

Simple update to fix patch shear

* chore: add IWC::Delegate::RecordResize() stub to fix FTBFS

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

* chore: add FormControlType::kButtonPopover to the FormControlType converter

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

* chore: e patches all

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

* chore: bump chromium in DEPS to 123.0.6290.0

* chore: bump chromium in DEPS to 123.0.6291.0

* chore: bump chromium in DEPS to 123.0.6292.0

* chore: bump chromium in DEPS to 123.0.6294.0

* chore: update fix_aspect_ratio_with_max_size.patch

Xref: fix_aspect_ratio_with_max_size.patch

note: simple absl::optional -> std::optional conversion

* chore: update feat_filter_out_non-shareable_windows_in_the_current_application_in.patch

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

* chore: update add_maximized_parameter_to_linuxui_getwindowframeprovider.patch

No manual changes; just adjusting line patch offsets

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

* chore: update feat_configure_launch_options_for_service_process.patch

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

* chore: update fix_select_The_first_menu_item_when_opened_via_keyboard.patch

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

note: simple absl::optional -> std::optional conversion

* chore: update feat_allow_code_cache_in_custom_schemes.patch

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

* chore: script/export_all_patches.py

* chore: bump chromium in DEPS to 123.0.6296.0

* chore: update patches

* fixup! chore: update feat_allow_code_cache_in_custom_schemes.patch

* fix: restore MessagePort close event

* spec: fix CORB testing

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

* fix: use sync layout when content view changes

* fixup! chore: update feat_configure_launch_options_for_service_process.patch

* Add remote-cocoa support for context menus.

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

* Rename //net/base/mac directory to //net/base/apple (1/n)

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

* fixup! Add remote-cocoa support for context menus.

* [Clipboard] Don't add meta charset tag for async write() method on Mac.

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

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
2024-02-14 12:33:32 -05:00

219 lines
6.3 KiB
C++

// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/views/root_view.h"
#include <memory>
#include "content/public/common/input/native_web_keyboard_event.h"
#include "shell/browser/native_window.h"
#include "shell/browser/ui/views/menu_bar.h"
namespace electron {
namespace {
// The menu bar height in pixels.
#if BUILDFLAG(IS_WIN)
const int kMenuBarHeight = 20;
#else
const int kMenuBarHeight = 25;
#endif
bool IsAltKey(const content::NativeWebKeyboardEvent& event) {
return event.windows_key_code == ui::VKEY_MENU;
}
bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
typedef content::NativeWebKeyboardEvent::Modifiers Modifiers;
int modifiers = event.GetModifiers();
modifiers &= ~Modifiers::kNumLockOn;
modifiers &= ~Modifiers::kCapsLockOn;
return (modifiers == Modifiers::kAltKey) ||
(modifiers == (Modifiers::kAltKey | Modifiers::kIsLeft)) ||
(modifiers == (Modifiers::kAltKey | Modifiers::kIsRight));
}
} // namespace
RootView::RootView(NativeWindow* window)
: window_(window),
last_focused_view_tracker_(std::make_unique<views::ViewTracker>()) {
set_owned_by_client();
}
RootView::~RootView() = default;
void RootView::SetMenu(ElectronMenuModel* menu_model) {
if (menu_model == nullptr) {
// Remove accelerators
UnregisterAcceleratorsWithFocusManager();
// and menu bar.
SetMenuBarVisibility(false);
menu_bar_.reset();
return;
}
RegisterAcceleratorsWithFocusManager(menu_model);
// Do not show menu bar in frameless window.
if (!window_->has_frame())
return;
if (!menu_bar_) {
menu_bar_ = std::make_unique<MenuBar>(window_, this);
menu_bar_->set_owned_by_client();
if (!menu_bar_autohide_)
SetMenuBarVisibility(true);
}
menu_bar_->SetMenu(menu_model);
InvalidateLayout();
}
bool RootView::HasMenu() const {
return !!menu_bar_;
}
int RootView::GetMenuBarHeight() const {
return kMenuBarHeight;
}
void RootView::SetAutoHideMenuBar(bool auto_hide) {
menu_bar_autohide_ = auto_hide;
}
void RootView::SetMenuBarVisibility(bool visible) {
if (!window_->content_view() || !menu_bar_ || menu_bar_visible_ == visible)
return;
menu_bar_visible_ = visible;
if (visible) {
DCHECK_EQ(children().size(), 1ul);
AddChildView(menu_bar_.get());
} else {
DCHECK_EQ(children().size(), 2ul);
RemoveChildView(menu_bar_.get());
}
InvalidateLayout();
}
void RootView::HandleKeyEvent(const content::NativeWebKeyboardEvent& event) {
if (!menu_bar_)
return;
// Show accelerator when "Alt" is pressed.
if (menu_bar_visible_ && IsAltKey(event))
menu_bar_->SetAcceleratorVisibility(
event.GetType() == blink::WebInputEvent::Type::kRawKeyDown);
// Show the submenu when "Alt+Key" is pressed.
if (event.GetType() == blink::WebInputEvent::Type::kRawKeyDown &&
event.windows_key_code >= ui::VKEY_A &&
event.windows_key_code <= ui::VKEY_Z && IsAltModifier(event) &&
menu_bar_->HasAccelerator(event.windows_key_code)) {
if (!menu_bar_visible_) {
SetMenuBarVisibility(true);
View* focused_view = GetFocusManager()->GetFocusedView();
last_focused_view_tracker_->SetView(focused_view);
menu_bar_->RequestFocus();
}
menu_bar_->ActivateAccelerator(event.windows_key_code);
return;
}
// Toggle the menu bar only when a single Alt is released.
if (event.GetType() == blink::WebInputEvent::Type::kRawKeyDown &&
IsAltKey(event)) {
// When a single Alt is pressed:
menu_bar_alt_pressed_ = true;
} else if (event.GetType() == blink::WebInputEvent::Type::kKeyUp &&
IsAltKey(event) && menu_bar_alt_pressed_) {
// When a single Alt is released right after a Alt is pressed:
menu_bar_alt_pressed_ = false;
if (menu_bar_autohide_)
SetMenuBarVisibility(!menu_bar_visible_);
View* focused_view = GetFocusManager()->GetFocusedView();
last_focused_view_tracker_->SetView(focused_view);
if (menu_bar_visible_) {
menu_bar_->RequestFocus();
// Show accelerators when menu bar is focused
menu_bar_->SetAcceleratorVisibility(true);
}
} else {
// When any other keys except single Alt have been pressed/released:
menu_bar_alt_pressed_ = false;
}
}
void RootView::RestoreFocus() {
View* last_focused_view = last_focused_view_tracker_->view();
if (last_focused_view) {
GetFocusManager()->SetFocusedViewWithReason(
last_focused_view,
views::FocusManager::FocusChangeReason::kFocusRestore);
}
if (menu_bar_autohide_)
SetMenuBarVisibility(false);
}
void RootView::ResetAltState() {
menu_bar_alt_pressed_ = false;
}
void RootView::Layout(PassKey) {
if (!window_->content_view()) // Not ready yet.
return;
const auto menu_bar_bounds =
menu_bar_visible_ ? gfx::Rect(0, 0, size().width(), kMenuBarHeight)
: gfx::Rect();
if (menu_bar_)
menu_bar_->SetBoundsRect(menu_bar_bounds);
window_->content_view()->SetBoundsRect(
gfx::Rect(0, menu_bar_visible_ ? menu_bar_bounds.bottom() : 0,
size().width(), size().height() - menu_bar_bounds.height()));
}
gfx::Size RootView::GetMinimumSize() const {
return window_->GetMinimumSize();
}
gfx::Size RootView::GetMaximumSize() const {
return window_->GetMaximumSize();
}
bool RootView::AcceleratorPressed(const ui::Accelerator& accelerator) {
return accelerator_util::TriggerAcceleratorTableCommand(&accelerator_table_,
accelerator);
}
void RootView::RegisterAcceleratorsWithFocusManager(
ElectronMenuModel* menu_model) {
if (!menu_model)
return;
// Clear previous accelerators.
UnregisterAcceleratorsWithFocusManager();
views::FocusManager* focus_manager = GetFocusManager();
// Register accelerators with focus manager.
accelerator_util::GenerateAcceleratorTable(&accelerator_table_, menu_model);
for (const auto& iter : accelerator_table_) {
focus_manager->RegisterAccelerator(
iter.first, ui::AcceleratorManager::kNormalPriority, this);
}
}
void RootView::UnregisterAcceleratorsWithFocusManager() {
views::FocusManager* focus_manager = GetFocusManager();
accelerator_table_.clear();
focus_manager->UnregisterAccelerators(this);
}
} // namespace electron