From 19471036267c0102b8fc9f51e39a5b6e1e16c62a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 23 Apr 2024 23:59:33 -0400 Subject: [PATCH] fix: setTitleBarOverlay should be implemented on BaseWindow --- shell/browser/api/electron_api_base_window.cc | 58 +++++++++++++++++ shell/browser/api/electron_api_base_window.h | 2 + .../api/electron_api_browser_window.cc | 62 ------------------- .../browser/api/electron_api_browser_window.h | 4 -- 4 files changed, 60 insertions(+), 66 deletions(-) diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index d8c1f8063f66b..9d4d5ac7f8b16 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -1039,6 +1039,63 @@ void BaseWindow::SetAppDetails(const gin_helper::Dictionary& options) { relaunch_command, relaunch_display_name, window_->GetAcceleratedWidget()); } + +void BaseWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options, + gin_helper::Arguments* args) { + // Ensure WCO is already enabled on this window + if (!window_->titlebar_overlay_enabled()) { + args->ThrowError("Titlebar overlay is not enabled"); + return; + } + + auto* window = static_cast(window_.get()); + bool updated = false; + + // Check and update the button color + std::string btn_color; + if (options.Get(options::kOverlayButtonColor, &btn_color)) { + // Parse the string as a CSS color + SkColor color; + if (!content::ParseCssColorString(btn_color, &color)) { + args->ThrowError("Could not parse color as CSS color"); + return; + } + + // Update the view + window->set_overlay_button_color(color); + updated = true; + } + + // Check and update the symbol color + std::string symbol_color; + if (options.Get(options::kOverlaySymbolColor, &symbol_color)) { + // Parse the string as a CSS color + SkColor color; + if (!content::ParseCssColorString(symbol_color, &color)) { + args->ThrowError("Could not parse symbol color as CSS color"); + return; + } + + // Update the view + window->set_overlay_symbol_color(color); + updated = true; + } + + // Check and update the height + int height = 0; + if (options.Get(options::kOverlayHeight, &height)) { + window->set_titlebar_overlay_height(height); + updated = true; + } + + // If anything was updated, invalidate the layout and schedule a paint of the + // window's frame view + if (updated) { + auto* frame_view = static_cast( + window->widget()->non_client_view()->frame_view()); + frame_view->InvalidateCaptionButtons(); + } +} #endif int32_t BaseWindow::GetID() const { @@ -1227,6 +1284,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate, .SetMethod("setThumbnailClip", &BaseWindow::SetThumbnailClip) .SetMethod("setThumbnailToolTip", &BaseWindow::SetThumbnailToolTip) .SetMethod("setAppDetails", &BaseWindow::SetAppDetails) + .SetMethod("setTitleBarOverlay", &BaseWindow::SetTitleBarOverlay) #endif .SetProperty("id", &BaseWindow::GetID); } diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index 7bff94b776f94..46043f284f5e9 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -241,6 +241,8 @@ class BaseWindow : public gin_helper::TrackableObject, bool SetThumbnailClip(const gfx::Rect& region); bool SetThumbnailToolTip(const std::string& tooltip); void SetAppDetails(const gin_helper::Dictionary& options); + void SetTitleBarOverlay(const gin_helper::Dictionary& options, + gin_helper::Arguments* args); #endif int32_t GetID() const; diff --git a/shell/browser/api/electron_api_browser_window.cc b/shell/browser/api/electron_api_browser_window.cc index 234dedb643ea0..95e9ed62741d7 100644 --- a/shell/browser/api/electron_api_browser_window.cc +++ b/shell/browser/api/electron_api_browser_window.cc @@ -275,65 +275,6 @@ v8::Local BrowserWindow::GetWebContents(v8::Isolate* isolate) { return v8::Local::New(isolate, web_contents_); } -#if BUILDFLAG(IS_WIN) -void BrowserWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options, - gin_helper::Arguments* args) { - // Ensure WCO is already enabled on this window - if (!window_->titlebar_overlay_enabled()) { - args->ThrowError("Titlebar overlay is not enabled"); - return; - } - - auto* window = static_cast(window_.get()); - bool updated = false; - - // Check and update the button color - std::string btn_color; - if (options.Get(options::kOverlayButtonColor, &btn_color)) { - // Parse the string as a CSS color - SkColor color; - if (!content::ParseCssColorString(btn_color, &color)) { - args->ThrowError("Could not parse color as CSS color"); - return; - } - - // Update the view - window->set_overlay_button_color(color); - updated = true; - } - - // Check and update the symbol color - std::string symbol_color; - if (options.Get(options::kOverlaySymbolColor, &symbol_color)) { - // Parse the string as a CSS color - SkColor color; - if (!content::ParseCssColorString(symbol_color, &color)) { - args->ThrowError("Could not parse symbol color as CSS color"); - return; - } - - // Update the view - window->set_overlay_symbol_color(color); - updated = true; - } - - // Check and update the height - int height = 0; - if (options.Get(options::kOverlayHeight, &height)) { - window->set_titlebar_overlay_height(height); - updated = true; - } - - // If anything was updated, invalidate the layout and schedule a paint of the - // window's frame view - if (updated) { - auto* frame_view = static_cast( - window->widget()->non_client_view()->frame_view()); - frame_view->InvalidateCaptionButtons(); - } -} -#endif - void BrowserWindow::OnWindowShow() { web_contents()->WasShown(); BaseWindow::OnWindowShow(); @@ -373,9 +314,6 @@ void BrowserWindow::BuildPrototype(v8::Isolate* isolate, .SetMethod("focusOnWebView", &BrowserWindow::FocusOnWebView) .SetMethod("blurWebView", &BrowserWindow::BlurWebView) .SetMethod("isWebViewFocused", &BrowserWindow::IsWebViewFocused) -#if BUILDFLAG(IS_WIN) - .SetMethod("setTitleBarOverlay", &BrowserWindow::SetTitleBarOverlay) -#endif .SetProperty("webContents", &BrowserWindow::GetWebContents); } diff --git a/shell/browser/api/electron_api_browser_window.h b/shell/browser/api/electron_api_browser_window.h index c9de1334a40c7..e752d1ed595d0 100644 --- a/shell/browser/api/electron_api_browser_window.h +++ b/shell/browser/api/electron_api_browser_window.h @@ -73,10 +73,6 @@ class BrowserWindow : public BaseWindow, void BlurWebView(); bool IsWebViewFocused(); v8::Local GetWebContents(v8::Isolate* isolate); -#if BUILDFLAG(IS_WIN) - void SetTitleBarOverlay(const gin_helper::Dictionary& options, - gin_helper::Arguments* args); -#endif private: // Helpers.