refactor: use raw_ref in RootView (#42114)

* refactor: make RootView::window_ a const raw_ref

The Chromium C++ style guide says "prefer const raw_ref<T> whenever
the held pointer will never be null," so let's do that.

* refactor: make RootView::main_view_ a const raw_ref

> The Chromium C++ style guide says "prefer const raw_ref<T> whenever
> the held pointer will never be null," so let's do that.

* refactor: aggregate RootView::last_focused_view_tracker_

RootView already owns it, so aggregate it
This commit is contained in:
Charles Kerr 2024-05-15 13:45:47 -05:00 committed by GitHub
parent 0b62abf244
commit c67744a127
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 12 deletions

View file

@ -32,15 +32,15 @@ bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
} // namespace
RootView::RootView(NativeWindow* window)
: window_(window),
last_focused_view_tracker_(std::make_unique<views::ViewTracker>()) {
: window_{raw_ref<NativeWindow>::from_ptr(window)},
main_view_{raw_ref<views::View>::from_ptr(
AddChildView(std::make_unique<views::View>()))} {
set_owned_by_client();
views::BoxLayout* layout =
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
main_view_ = AddChildView(std::make_unique<views::View>());
main_view_->SetUseDefaultFillLayout(true);
layout->SetFlexForView(main_view_, 1);
layout->SetFlexForView(&main_view_.get(), 1);
}
RootView::~RootView() = default;
@ -62,7 +62,7 @@ void RootView::SetMenu(ElectronMenuModel* menu_model) {
return;
if (!menu_bar_) {
menu_bar_ = std::make_unique<MenuBar>(window_, this);
menu_bar_ = std::make_unique<MenuBar>(&window_.get(), this);
menu_bar_->set_owned_by_client();
if (!menu_bar_autohide_)
SetMenuBarVisibility(true);
@ -116,7 +116,7 @@ void RootView::HandleKeyEvent(const content::NativeWebKeyboardEvent& event) {
SetMenuBarVisibility(true);
View* focused_view = GetFocusManager()->GetFocusedView();
last_focused_view_tracker_->SetView(focused_view);
last_focused_view_tracker_.SetView(focused_view);
menu_bar_->RequestFocus();
}
@ -137,7 +137,7 @@ void RootView::HandleKeyEvent(const content::NativeWebKeyboardEvent& event) {
SetMenuBarVisibility(!menu_bar_visible_);
View* focused_view = GetFocusManager()->GetFocusedView();
last_focused_view_tracker_->SetView(focused_view);
last_focused_view_tracker_.SetView(focused_view);
if (menu_bar_visible_) {
menu_bar_->RequestFocus();
// Show accelerators when menu bar is focused
@ -150,7 +150,7 @@ void RootView::HandleKeyEvent(const content::NativeWebKeyboardEvent& event) {
}
void RootView::RestoreFocus() {
View* last_focused_view = last_focused_view_tracker_->view();
View* last_focused_view = last_focused_view_tracker_.view();
if (last_focused_view) {
GetFocusManager()->SetFocusedViewWithReason(
last_focused_view,

View file

@ -8,6 +8,7 @@
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "shell/browser/ui/accelerator_util.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/view.h"
@ -46,7 +47,7 @@ class RootView : public views::View {
void RegisterAcceleratorsWithFocusManager(ElectronMenuModel* menu_model);
void UnregisterAcceleratorsWithFocusManager();
views::View* GetMainView() { return main_view_; }
views::View* GetMainView() { return &main_view_.get(); }
// views::View:
gfx::Size GetMinimumSize() const override;
@ -55,7 +56,7 @@ class RootView : public views::View {
private:
// Parent window, weak ref.
raw_ptr<NativeWindow> window_;
const raw_ref<NativeWindow> window_;
// Menu bar.
std::unique_ptr<MenuBar> menu_bar_;
@ -64,12 +65,12 @@ class RootView : public views::View {
bool menu_bar_alt_pressed_ = false;
// Main view area.
raw_ptr<views::View> main_view_;
const raw_ref<views::View> main_view_;
// Map from accelerator to menu item's command id.
accelerator_util::AcceleratorTable accelerator_table_;
std::unique_ptr<views::ViewTracker> last_focused_view_tracker_;
views::ViewTracker last_focused_view_tracker_;
};
} // namespace electron