refactor: add NativeWindowViews::SetTitleBarOverlay() (#47126)

* refactor: move SetTitleBarOverlay() impl to NativeWindowViews

* refactor: make NativeWindowViews::set_overlay_button_color() private

refactor: make NativeWindowViews::set_overlay_symbol_color() private

refactor: make NativeWindow::set_titlebar_overlay_height() protected

* refactor: simplify downcasting in NativeWindowViews::SetTitleBarOverlay()
This commit is contained in:
Charles Kerr 2025-05-19 13:19:03 +00:00 committed by GitHub
commit 687e50b4f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 63 deletions

View file

@ -1093,64 +1093,8 @@ bool BaseWindow::IsSnapped() const {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
void BaseWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options,
gin_helper::Arguments* args) {
// Ensure WCO is already enabled on this window
if (!window_->IsWindowControlsOverlayEnabled()) {
args->ThrowError("Titlebar overlay is not enabled");
return;
}
auto* window = static_cast<NativeWindowViews*>(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 (!updated)
return;
// If anything was updated, ensure the overlay is repainted.
#if BUILDFLAG(IS_WIN)
auto* frame_view = static_cast<WinFrameView*>(
window->widget()->non_client_view()->frame_view());
#else
auto* frame_view = static_cast<OpaqueFrameView*>(
window->widget()->non_client_view()->frame_view());
#endif
frame_view->InvalidateCaptionButtons();
static_cast<NativeWindowViews*>(window_.get())
->SetTitleBarOverlay(options, args);
}
#endif

View file

@ -394,9 +394,6 @@ class NativeWindow : public base::SupportsUserData,
}
int titlebar_overlay_height() const { return titlebar_overlay_height_; }
void set_titlebar_overlay_height(int height) {
titlebar_overlay_height_ = height;
}
bool has_frame() const { return has_frame_; }
@ -431,6 +428,10 @@ class NativeWindow : public base::SupportsUserData,
void UpdateBackgroundThrottlingState();
protected:
void set_titlebar_overlay_height(int height) {
titlebar_overlay_height_ = height;
}
constexpr void set_has_frame(const bool val) { has_frame_ = val; }
[[nodiscard]] constexpr bool is_closed() const { return is_closed_; }

View file

@ -33,6 +33,7 @@
#include "shell/browser/web_view_manager.h"
#include "shell/common/electron_constants.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/arguments.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/options_switches.h"
#include "ui/aura/window_tree_host.h"
@ -457,6 +458,62 @@ NativeWindowViews::~NativeWindowViews() {
window->RemovePreTargetHandler(this);
}
void NativeWindowViews::SetTitleBarOverlay(
const gin_helper::Dictionary& options,
gin_helper::Arguments* args) {
// Ensure WCO is already enabled on this window
if (!IsWindowControlsOverlayEnabled()) {
args->ThrowError("Titlebar overlay is not enabled");
return;
}
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
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
set_overlay_symbol_color(color);
updated = true;
}
// Check and update the height
int height = 0;
if (options.Get(options::kOverlayHeight, &height)) {
set_titlebar_overlay_height(height);
updated = true;
}
// If anything was updated, ensure the overlay is repainted.
if (updated) {
auto* frame_view =
static_cast<FramelessView*>(widget()->non_client_view()->frame_view());
frame_view->InvalidateCaptionButtons();
}
}
void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) {
#if BUILDFLAG(IS_LINUX)
if (x11_util::IsX11()) {

View file

@ -24,6 +24,10 @@
#include "shell/browser/ui/win/taskbar_host.h"
#endif
namespace gin_helper {
class Arguments;
} // namespace gin_helper
namespace electron {
#if BUILDFLAG(IS_LINUX)
@ -150,6 +154,9 @@ class NativeWindowViews : public NativeWindow,
void IncrementChildModals();
void DecrementChildModals();
void SetTitleBarOverlay(const gin_helper::Dictionary& options,
gin_helper::Arguments* args);
#if BUILDFLAG(IS_WIN)
// Catch-all message handling and filtering. Called before
// HWNDMessageHandler's built-in handling, which may preempt some
@ -172,15 +179,16 @@ class NativeWindowViews : public NativeWindow,
#endif
SkColor overlay_button_color() const { return overlay_button_color_; }
SkColor overlay_symbol_color() const { return overlay_symbol_color_; }
private:
void set_overlay_button_color(SkColor color) {
overlay_button_color_ = color;
}
SkColor overlay_symbol_color() const { return overlay_symbol_color_; }
void set_overlay_symbol_color(SkColor color) {
overlay_symbol_color_ = color;
}
private:
// views::WidgetObserver:
void OnWidgetActivationChanged(views::Widget* widget, bool active) override;
void OnWidgetBoundsChanged(views::Widget* widget,