From 235fdc41f6ccea67afa10a423024d337093540fd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 24 Sep 2025 08:39:16 -0500 Subject: [PATCH] refactor: use gin::Arguments in BaseWindow (#48355) * refactor: make api::Clipboard::GetClipboardBuffer() private * refactor: move GetClipboadBuffer() into anonymous namespace * refactor: use gin::Arguments in BaseWindow::MoveAbove() refactor: use gin::Arguments in BaseWindow::SetAlwaysOnTop() refactor: use gin::Arguments in BaseWindow::SetIgnoreMouseEvent() * refactor: use gin::Arguments in BaseWindow::SetProgresBar() * refactor: use gin::Arguments in BaseWindow::SetVisibleOnAllWorkspaces() * refactor: use gin::Arguments in BaseWindow::SetVibrancy() * refactor: use gin::Arguments in BaseWindow::SetAspectRatio() * refactor: use gin::Arguments in BaseWindow::PreviewFile() * refactor: use gin::Arguments in BaseWindow::SetThumbarButtons() * refactor: use gin::Arguments in BaseWindow::SetBounds() * refactor: use gin::Arguments in BaseWindow::SetContentBounds() * refactor: use gin::Arguments in BaseWindow::SetSize() * refactor: use gin::Arguments in BaseWindow::SetContentSize() * refactor: use gin::Arguments in BaseWindow::SetSheetOffset() * refactor: use gin::Arguments in BaseWindow::SetPosition() * refactor: use gin::Arguments in BaseWindow::AddTabbedWindow() * refactor: use gin::Arguments in BaseWindow::SetParentWindow() * refactor: use gin::Arguments in BaseWindow::BaseWindow() * refactor: use gin::Arguments in BaseWindow::SetAccentColor() * refactor: use gin::Arguments in BaseWindow::SetTitleBarOverlay() --- shell/browser/api/electron_api_base_window.cc | 100 ++++++++++-------- shell/browser/api/electron_api_base_window.h | 48 +++++---- shell/browser/native_window_views.cc | 8 +- shell/browser/native_window_views.h | 6 +- 4 files changed, 87 insertions(+), 75 deletions(-) diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index b362c47a22..382a271cd0 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -123,7 +123,7 @@ BaseWindow::BaseWindow(v8::Isolate* isolate, #endif } -BaseWindow::BaseWindow(gin_helper::Arguments* args, +BaseWindow::BaseWindow(gin::Arguments* args, const gin_helper::Dictionary& options) : BaseWindow(args->isolate(), options) { InitWithArgs(args); @@ -444,7 +444,7 @@ bool BaseWindow::IsFullscreen() const { } void BaseWindow::SetBounds(const gfx::Rect& bounds, - gin_helper::Arguments* args) { + gin::Arguments* const args) { bool animate = false; args->GetNext(&animate); window_->SetBounds(bounds, animate); @@ -463,7 +463,7 @@ gfx::Rect BaseWindow::GetNormalBounds() const { } void BaseWindow::SetContentBounds(const gfx::Rect& bounds, - gin_helper::Arguments* args) { + gin::Arguments* const args) { bool animate = false; args->GetNext(&animate); window_->SetContentBounds(bounds, animate); @@ -473,7 +473,7 @@ gfx::Rect BaseWindow::GetContentBounds() const { return window_->GetContentBounds(); } -void BaseWindow::SetSize(int width, int height, gin_helper::Arguments* args) { +void BaseWindow::SetSize(int width, int height, gin::Arguments* args) { bool animate = false; gfx::Size size = window_->GetMinimumSize(); size.SetToMax(gfx::Size(width, height)); @@ -485,12 +485,12 @@ std::array BaseWindow::GetSize() const { return ToArray(window_->GetSize()); } -void BaseWindow::SetContentSize(int width, - int height, - gin_helper::Arguments* args) { +void BaseWindow::SetContentSize(const int width, + const int height, + gin::Arguments* const args) { bool animate = false; args->GetNext(&animate); - window_->SetContentSize(gfx::Size(width, height), animate); + window_->SetContentSize(gfx::Size{width, height}, animate); } std::array BaseWindow::GetContentSize() const { @@ -513,7 +513,8 @@ std::array BaseWindow::GetMaximumSize() const { return ToArray(window_->GetMaximumSize()); } -void BaseWindow::SetSheetOffset(double offsetY, gin_helper::Arguments* args) { +void BaseWindow::SetSheetOffset(const double offsetY, + gin::Arguments* const args) { double offsetX = 0.0; args->GetNext(&offsetX); window_->SetSheetOffset(offsetX, offsetY); @@ -567,7 +568,7 @@ bool BaseWindow::IsClosable() const { return window_->IsClosable(); } -void BaseWindow::SetAlwaysOnTop(bool top, gin_helper::Arguments* args) { +void BaseWindow::SetAlwaysOnTop(bool top, gin::Arguments* args) { std::string level = "floating"; int relative_level = 0; args->GetNext(&level); @@ -586,19 +587,21 @@ void BaseWindow::Center() { window_->Center(); } -void BaseWindow::SetPosition(int x, int y, gin_helper::Arguments* args) { +void BaseWindow::SetPosition(const int x, + const int y, + gin::Arguments* const args) { bool animate = false; args->GetNext(&animate); - window_->SetPosition(gfx::Point(x, y), animate); + window_->SetPosition(gfx::Point{x, y}, animate); } std::array BaseWindow::GetPosition() const { return ToArray(window_->GetPosition()); } void BaseWindow::MoveAbove(const std::string& sourceId, - gin_helper::Arguments* args) { + gin::Arguments* const args) { if (!window_->MoveAbove(sourceId)) - args->ThrowError("Invalid media source id"); + args->ThrowTypeError("Invalid media source id"); } void BaseWindow::MoveTop() { @@ -706,8 +709,7 @@ bool BaseWindow::IsDocumentEdited() const { return window_->IsDocumentEdited(); } -void BaseWindow::SetIgnoreMouseEvents(bool ignore, - gin_helper::Arguments* args) { +void BaseWindow::SetIgnoreMouseEvents(bool ignore, gin::Arguments* const args) { gin_helper::Dictionary options; bool forward = false; args->GetNext(&options) && options.Get("forward", &forward); @@ -759,9 +761,9 @@ void BaseWindow::RemoveMenu() { } void BaseWindow::SetParentWindow(v8::Local value, - gin_helper::Arguments* args) { + gin::Arguments* const args) { if (IsModal()) { - args->ThrowError("Can not be called for modal window"); + args->ThrowTypeError("Can not be called for modal window"); return; } @@ -776,7 +778,7 @@ void BaseWindow::SetParentWindow(v8::Local value, window_->SetParentWindow(parent->window_.get()); parent->child_windows_.Set(isolate(), weak_map_id(), GetWrapper()); } else { - args->ThrowError("Must pass BaseWindow instance or null"); + args->ThrowTypeError("Must pass BaseWindow instance or null"); } } @@ -794,7 +796,7 @@ v8::Local BaseWindow::GetNativeWindowHandle() { } #endif -void BaseWindow::SetProgressBar(double progress, gin_helper::Arguments* args) { +void BaseWindow::SetProgressBar(double progress, gin::Arguments* args) { gin_helper::Dictionary options; std::string mode; args->GetNext(&options) && options.Get("mode", &mode); @@ -818,7 +820,7 @@ void BaseWindow::SetOverlayIcon(const gfx::Image& overlay, } void BaseWindow::SetVisibleOnAllWorkspaces(bool visible, - gin_helper::Arguments* args) { + gin::Arguments* const args) { gin_helper::Dictionary options; bool visibleOnFullScreen = false; bool skipTransformProcessType = false; @@ -838,9 +840,9 @@ void BaseWindow::SetAutoHideCursor(bool auto_hide) { window_->SetAutoHideCursor(auto_hide); } -void BaseWindow::SetVibrancy(v8::Isolate* isolate, +void BaseWindow::SetVibrancy(v8::Isolate* const isolate, v8::Local value, - gin_helper::Arguments* args) { + gin::Arguments* const args) { std::string type = gin::V8ToString(isolate, value); gin_helper::Dictionary options; int animation_duration_ms = 0; @@ -925,10 +927,11 @@ void BaseWindow::ToggleTabBar() { window_->ToggleTabBar(); } -void BaseWindow::AddTabbedWindow(NativeWindow* window, - gin_helper::Arguments* args) { +void BaseWindow::AddTabbedWindow(NativeWindow* const window, + gin::Arguments* const args) { if (!window_->AddTabbedWindow(window)) - args->ThrowError("AddTabbedWindow cannot be called by a window on itself."); + args->ThrowTypeError( + "AddTabbedWindow cannot be called by a window on itself."); } v8::Local BaseWindow::GetTabbingIdentifier() { @@ -955,15 +958,15 @@ bool BaseWindow::IsMenuBarVisible() const { return window_->IsMenuBarVisible(); } -void BaseWindow::SetAspectRatio(double aspect_ratio, - gin_helper::Arguments* args) { +void BaseWindow::SetAspectRatio(const double aspect_ratio, + gin::Arguments* const args) { gfx::Size extra_size; args->GetNext(&extra_size); window_->SetAspectRatio(aspect_ratio, extra_size); } void BaseWindow::PreviewFile(const std::string& path, - gin_helper::Arguments* args) { + gin::Arguments* const args) { std::string display_name; if (!args->GetNext(&display_name)) display_name = path; @@ -1000,7 +1003,7 @@ bool BaseWindow::IsModal() const { return window_->is_modal(); } -bool BaseWindow::SetThumbarButtons(gin_helper::Arguments* args) { +bool BaseWindow::SetThumbarButtons(gin::Arguments* args) { #if BUILDFLAG(IS_WIN) std::vector buttons; if (!args->GetNext(&buttons)) { @@ -1092,22 +1095,29 @@ bool BaseWindow::IsSnapped() const { return window_->IsSnapped(); } -void BaseWindow::SetAccentColor(gin_helper::Arguments* args) { - bool accent_color = false; - std::string accent_color_string; - if (args->GetNext(&accent_color_string)) { - std::optional maybe_color = ParseCSSColor(accent_color_string); - if (maybe_color.has_value()) { - window_->SetAccentColor(maybe_color.value()); +void BaseWindow::SetAccentColor(gin::Arguments* const args) { + v8::Local ac_val; + args->GetNext(&ac_val); + + if (!ac_val.IsEmpty() && ac_val->IsBoolean()) { + const bool ac_flag = ac_val->BooleanValue(args->isolate()); + window_->SetAccentColor(ac_flag); + window_->UpdateWindowAccentColor(window_->IsActive()); + return; + } + + if (!ac_val.IsEmpty() && ac_val->IsString()) { + std::string ac_str; + gin::ConvertFromV8(args->isolate(), ac_val, &ac_str); + if (const std::optional ac_color = ParseCSSColor(ac_str)) { + window_->SetAccentColor(*ac_color); window_->UpdateWindowAccentColor(window_->IsActive()); } - } else if (args->GetNext(&accent_color)) { - window_->SetAccentColor(accent_color); - window_->UpdateWindowAccentColor(window_->IsActive()); - } else { - args->ThrowError( - "Invalid accent color value - must be a string or boolean"); + return; } + + args->ThrowTypeError( + "Invalid accent color value - must be a string or boolean"); } v8::Local BaseWindow::GetAccentColor() const { @@ -1122,7 +1132,7 @@ v8::Local BaseWindow::GetAccentColor() const { #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) void BaseWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options, - gin_helper::Arguments* args) { + gin::Arguments* args) { static_cast(window_.get()) ->SetTitleBarOverlay(options, args); } @@ -1144,7 +1154,7 @@ void BaseWindow::RemoveFromParentChildWindows() { } // static -gin_helper::WrappableBase* BaseWindow::New(gin_helper::Arguments* args) { +gin_helper::WrappableBase* BaseWindow::New(gin::Arguments* const args) { auto options = gin_helper::Dictionary::CreateEmpty(args->isolate()); args->GetNext(&options); diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index 53ddd8cee1..efb148e0cb 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -19,8 +19,11 @@ #include "shell/common/api/electron_api_native_image.h" #include "shell/common/gin_helper/trackable_object.h" -namespace gin_helper { +namespace gin { class Arguments; +} // namespace gin + +namespace gin_helper { class PersistentDictionary; template class Handle; @@ -37,7 +40,7 @@ class View; class BaseWindow : public gin_helper::TrackableObject, private NativeWindowObserver { public: - static gin_helper::WrappableBase* New(gin_helper::Arguments* args); + static gin_helper::WrappableBase* New(gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -49,8 +52,7 @@ class BaseWindow : public gin_helper::TrackableObject, // Common constructor. BaseWindow(v8::Isolate* isolate, const gin_helper::Dictionary& options); // Creating independent BaseWindow instance. - BaseWindow(gin_helper::Arguments* args, - const gin_helper::Dictionary& options); + BaseWindow(gin::Arguments* args, const gin_helper::Dictionary& options); ~BaseWindow() override; // TrackableObject: @@ -118,13 +120,13 @@ class BaseWindow : public gin_helper::TrackableObject, bool IsMinimized() const; void SetFullScreen(bool fullscreen); bool IsFullscreen() const; - void SetBounds(const gfx::Rect& bounds, gin_helper::Arguments* args); + void SetBounds(const gfx::Rect& bounds, gin::Arguments* args); gfx::Rect GetBounds() const; - void SetSize(int width, int height, gin_helper::Arguments* args); + void SetSize(int width, int height, gin::Arguments* args); std::array GetSize() const; - void SetContentSize(int width, int height, gin_helper::Arguments* args); + void SetContentSize(int width, int height, gin::Arguments* args); std::array GetContentSize() const; - void SetContentBounds(const gfx::Rect& bounds, gin_helper::Arguments* args); + void SetContentBounds(const gfx::Rect& bounds, gin::Arguments* args); gfx::Rect GetContentBounds() const; bool IsNormal() const; gfx::Rect GetNormalBounds() const; @@ -132,11 +134,11 @@ class BaseWindow : public gin_helper::TrackableObject, std::array GetMinimumSize() const; void SetMaximumSize(int width, int height); std::array GetMaximumSize() const; - void SetSheetOffset(double offsetY, gin_helper::Arguments* args); + void SetSheetOffset(double offsetY, gin::Arguments* args); void SetResizable(bool resizable); bool IsResizable() const; void SetMovable(bool movable); - void MoveAbove(const std::string& sourceId, gin_helper::Arguments* args); + void MoveAbove(const std::string& sourceId, gin::Arguments* args); void MoveTop(); bool IsMovable() const; void SetMinimizable(bool minimizable); @@ -147,10 +149,10 @@ class BaseWindow : public gin_helper::TrackableObject, bool IsFullScreenable() const; void SetClosable(bool closable); bool IsClosable() const; - void SetAlwaysOnTop(bool top, gin_helper::Arguments* args); + void SetAlwaysOnTop(bool top, gin::Arguments* args); bool IsAlwaysOnTop() const; void Center(); - void SetPosition(int x, int y, gin_helper::Arguments* args); + void SetPosition(int x, int y, gin::Arguments* args); std::array GetPosition() const; void SetTitle(const std::string& title); std::string GetTitle() const; @@ -177,25 +179,25 @@ class BaseWindow : public gin_helper::TrackableObject, std::string GetRepresentedFilename() const; void SetDocumentEdited(bool edited); bool IsDocumentEdited() const; - void SetIgnoreMouseEvents(bool ignore, gin_helper::Arguments* args); + void SetIgnoreMouseEvents(bool ignore, gin::Arguments* args); void SetContentProtection(bool enable); bool IsContentProtected() const; void SetFocusable(bool focusable); bool IsFocusable() const; void SetMenu(v8::Isolate* isolate, v8::Local menu); void RemoveMenu(); - void SetParentWindow(v8::Local value, gin_helper::Arguments* args); + void SetParentWindow(v8::Local value, gin::Arguments* args); std::string GetMediaSourceId() const; v8::Local GetNativeWindowHandle(); - void SetProgressBar(double progress, gin_helper::Arguments* args); + void SetProgressBar(double progress, gin::Arguments* args); void SetOverlayIcon(const gfx::Image& overlay, const std::string& description); - void SetVisibleOnAllWorkspaces(bool visible, gin_helper::Arguments* args); + void SetVisibleOnAllWorkspaces(bool visible, gin::Arguments* args); bool IsVisibleOnAllWorkspaces() const; void SetAutoHideCursor(bool auto_hide); virtual void SetVibrancy(v8::Isolate* isolate, v8::Local value, - gin_helper::Arguments* args); + gin::Arguments* args); virtual void SetBackgroundMaterial(const std::string& material); #if BUILDFLAG(IS_MAC) @@ -218,14 +220,14 @@ class BaseWindow : public gin_helper::TrackableObject, void MergeAllWindows(); void MoveTabToNewWindow(); void ToggleTabBar(); - void AddTabbedWindow(NativeWindow* window, gin_helper::Arguments* args); + void AddTabbedWindow(NativeWindow* window, gin::Arguments* args); v8::Local GetTabbingIdentifier(); void SetAutoHideMenuBar(bool auto_hide); bool IsMenuBarAutoHide() const; void SetMenuBarVisibility(bool visible); bool IsMenuBarVisible() const; - void SetAspectRatio(double aspect_ratio, gin_helper::Arguments* args); - void PreviewFile(const std::string& path, gin_helper::Arguments* args); + void SetAspectRatio(double aspect_ratio, gin::Arguments* args); + void PreviewFile(const std::string& path, gin::Arguments* args); void CloseFilePreview(); void SetGTKDarkThemeEnabled(bool use_dark_theme); @@ -236,7 +238,7 @@ class BaseWindow : public gin_helper::TrackableObject, bool IsModal() const; // Extra APIs added in JS. - bool SetThumbarButtons(gin_helper::Arguments* args); + bool SetThumbarButtons(gin::Arguments* args); #if defined(TOOLKIT_VIEWS) void SetIcon(v8::Isolate* isolate, v8::Local icon); void SetIconImpl(v8::Isolate* isolate, @@ -255,13 +257,13 @@ class BaseWindow : public gin_helper::TrackableObject, bool SetThumbnailToolTip(const std::string& tooltip); void SetAppDetails(const gin_helper::Dictionary& options); bool IsSnapped() const; - void SetAccentColor(gin_helper::Arguments* args); + void SetAccentColor(gin::Arguments* args); v8::Local GetAccentColor() const; #endif #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) void SetTitleBarOverlay(const gin_helper::Dictionary& options, - gin_helper::Arguments* args); + gin::Arguments* args); #endif [[nodiscard]] constexpr int32_t GetID() const { return weak_map_id(); } diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 18d7d6e876..fa447f6af0 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -451,10 +451,10 @@ NativeWindowViews::~NativeWindowViews() { void NativeWindowViews::SetTitleBarOverlay( const gin_helper::Dictionary& options, - gin_helper::Arguments* args) { + gin::Arguments* args) { // Ensure WCO is already enabled on this window if (!IsWindowControlsOverlayEnabled()) { - args->ThrowError("Titlebar overlay is not enabled"); + args->ThrowTypeError("Titlebar overlay is not enabled"); return; } @@ -465,7 +465,7 @@ void NativeWindowViews::SetTitleBarOverlay( // Parse the string as a CSS color SkColor color; if (!content::ParseCssColorString(val, &color)) { - args->ThrowError("Could not parse color as CSS color"); + args->ThrowTypeError("Could not parse color as CSS color"); return; } @@ -479,7 +479,7 @@ void NativeWindowViews::SetTitleBarOverlay( // Parse the string as a CSS color SkColor color; if (!content::ParseCssColorString(val, &color)) { - args->ThrowError("Could not parse symbol color as CSS color"); + args->ThrowTypeError("Could not parse symbol color as CSS color"); return; } diff --git a/shell/browser/native_window_views.h b/shell/browser/native_window_views.h index 69d19ea609..be0cca1c1f 100644 --- a/shell/browser/native_window_views.h +++ b/shell/browser/native_window_views.h @@ -25,9 +25,9 @@ #include "shell/browser/ui/win/taskbar_host.h" #endif -namespace gin_helper { +namespace gin { class Arguments; -} // namespace gin_helper +} // namespace gin namespace electron { @@ -158,7 +158,7 @@ class NativeWindowViews : public NativeWindow, void DecrementChildModals(); void SetTitleBarOverlay(const gin_helper::Dictionary& options, - gin_helper::Arguments* args); + gin::Arguments* args); #if BUILDFLAG(IS_WIN) // Catch-all message handling and filtering. Called before