X Tutup
Skip to content

Commit f69b59e

Browse files
authored
feat: add WCO title bar style setters (electron#33066)
* feat: add wco title bar style setters * return after throwing
1 parent 4fdf858 commit f69b59e

File tree

8 files changed

+108
-0
lines changed

8 files changed

+108
-0
lines changed

docs/api/browser-window.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,16 @@ with `addBrowserView` or `setBrowserView`.
18421842
**Note:** The BrowserView API is currently experimental and may change or be
18431843
removed in future Electron releases.
18441844

1845+
#### `win.setTitleBarOverlay(options)` _Windows_
1846+
1847+
* `options` Object
1848+
* `color` String (optional) _Windows_ - The CSS color of the Window Controls Overlay when enabled.
1849+
* `symbolColor` String (optional) _Windows_ - The CSS color of the symbols on the Window Controls Overlay when enabled.
1850+
* `height` Integer (optional) _Windows_ - The height of the title bar and Window Controls Overlay in pixels.
1851+
1852+
On a Window with Window Controls Overlay already enabled, this method updates
1853+
the style of the title bar overlay.
1854+
18451855
[runtime-enabled-features]: https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/runtime_enabled_features.json5?l=70
18461856
[page-visibility-api]: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
18471857
[quick-look]: https://en.wikipedia.org/wiki/Quick_Look

shell/browser/api/electron_api_browser_window.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "content/browser/web_contents/web_contents_impl.h" // nogncheck
1111
#include "content/public/browser/render_process_host.h"
1212
#include "content/public/browser/render_view_host.h"
13+
#include "content/public/common/color_parser.h"
1314
#include "shell/browser/api/electron_api_web_contents_view.h"
1415
#include "shell/browser/browser.h"
1516
#include "shell/browser/native_browser_view.h"
@@ -24,6 +25,14 @@
2425
#include "shell/common/options_switches.h"
2526
#include "ui/gl/gpu_switching_manager.h"
2627

28+
#if defined(TOOLKIT_VIEWS)
29+
#include "shell/browser/native_window_views.h"
30+
#endif
31+
32+
#if BUILDFLAG(IS_WIN)
33+
#include "shell/browser/ui/views/win_frame_view.h"
34+
#endif
35+
2736
namespace electron {
2837

2938
namespace api {
@@ -466,6 +475,65 @@ v8::Local<v8::Value> BrowserWindow::GetWebContents(v8::Isolate* isolate) {
466475
return v8::Local<v8::Value>::New(isolate, web_contents_);
467476
}
468477

478+
#if BUILDFLAG(IS_WIN)
479+
void BrowserWindow::SetTitleBarOverlay(const gin_helper::Dictionary& options,
480+
gin_helper::Arguments* args) {
481+
// Ensure WCO is already enabled on this window
482+
if (!window_->titlebar_overlay_enabled()) {
483+
args->ThrowError("Titlebar overlay is not enabled");
484+
return;
485+
}
486+
487+
auto* window = static_cast<NativeWindowViews*>(window_.get());
488+
bool updated = false;
489+
490+
// Check and update the button color
491+
std::string btn_color;
492+
if (options.Get(options::kOverlayButtonColor, &btn_color)) {
493+
// Parse the string as a CSS color
494+
SkColor color;
495+
if (!content::ParseCssColorString(btn_color, &color)) {
496+
args->ThrowError("Could not parse color as CSS color");
497+
return;
498+
}
499+
500+
// Update the view
501+
window->set_overlay_button_color(color);
502+
updated = true;
503+
}
504+
505+
// Check and update the symbol color
506+
std::string symbol_color;
507+
if (options.Get(options::kOverlaySymbolColor, &symbol_color)) {
508+
// Parse the string as a CSS color
509+
SkColor color;
510+
if (!content::ParseCssColorString(symbol_color, &color)) {
511+
args->ThrowError("Could not parse symbol color as CSS color");
512+
return;
513+
}
514+
515+
// Update the view
516+
window->set_overlay_symbol_color(color);
517+
updated = true;
518+
}
519+
520+
// Check and update the height
521+
int height = 0;
522+
if (options.Get(options::kOverlayHeight, &height)) {
523+
window->set_titlebar_overlay_height(height);
524+
updated = true;
525+
}
526+
527+
// If anything was updated, invalidate the layout and schedule a paint of the
528+
// window's frame view
529+
if (updated) {
530+
auto* frame_view = static_cast<WinFrameView*>(
531+
window->widget()->non_client_view()->frame_view());
532+
frame_view->InvalidateCaptionButtons();
533+
}
534+
}
535+
#endif
536+
469537
void BrowserWindow::ScheduleUnresponsiveEvent(int ms) {
470538
if (!window_unresponsive_closure_.IsCancelled())
471539
return;
@@ -524,6 +592,9 @@ void BrowserWindow::BuildPrototype(v8::Isolate* isolate,
524592
.SetMethod("focusOnWebView", &BrowserWindow::FocusOnWebView)
525593
.SetMethod("blurWebView", &BrowserWindow::BlurWebView)
526594
.SetMethod("isWebViewFocused", &BrowserWindow::IsWebViewFocused)
595+
#if BUILDFLAG(IS_WIN)
596+
.SetMethod("setTitleBarOverlay", &BrowserWindow::SetTitleBarOverlay)
597+
#endif
527598
.SetProperty("webContents", &BrowserWindow::GetWebContents);
528599
}
529600

shell/browser/api/electron_api_browser_window.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class BrowserWindow : public BaseWindow,
9999
void BlurWebView();
100100
bool IsWebViewFocused();
101101
v8::Local<v8::Value> GetWebContents(v8::Isolate* isolate);
102+
#if BUILDFLAG(IS_WIN)
103+
void SetTitleBarOverlay(const gin_helper::Dictionary& options,
104+
gin_helper::Arguments* args);
105+
#endif
102106

103107
private:
104108
#if BUILDFLAG(IS_MAC)

shell/browser/native_window.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ class NativeWindow : public base::SupportsUserData,
328328
};
329329
TitleBarStyle title_bar_style() const { return title_bar_style_; }
330330
int titlebar_overlay_height() const { return titlebar_overlay_height_; }
331+
void set_titlebar_overlay_height(int height) {
332+
titlebar_overlay_height_ = height;
333+
}
334+
bool titlebar_overlay_enabled() const { return titlebar_overlay_; }
331335

332336
bool has_frame() const { return has_frame_; }
333337
void set_has_frame(bool has_frame) { has_frame_ = has_frame; }

shell/browser/native_window_views.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,13 @@ class NativeWindowViews : public NativeWindow,
181181
titlebar_overlay_;
182182
}
183183
SkColor overlay_button_color() const { return overlay_button_color_; }
184+
void set_overlay_button_color(SkColor color) {
185+
overlay_button_color_ = color;
186+
}
184187
SkColor overlay_symbol_color() const { return overlay_symbol_color_; }
188+
void set_overlay_symbol_color(SkColor color) {
189+
overlay_symbol_color_ = color;
190+
}
185191
#endif
186192

187193
private:

shell/browser/ui/views/win_caption_button.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void WinCaptionButton::SetSize(gfx::Size size) {
100100
base_width_ = width;
101101
if (height > 0)
102102
height_ = height;
103+
104+
InvalidateLayout();
103105
}
104106

105107
int WinCaptionButton::GetBetweenButtonSpacing() const {

shell/browser/ui/views/win_frame_view.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ SkColor WinFrameView::GetReadableFeatureColor(SkColor background_color) {
6262
: SK_ColorBLACK;
6363
}
6464

65+
void WinFrameView::InvalidateCaptionButtons() {
66+
// Ensure that the caption buttons container exists
67+
DCHECK(caption_button_container_);
68+
69+
caption_button_container_->InvalidateLayout();
70+
caption_button_container_->SchedulePaint();
71+
}
72+
6573
gfx::Rect WinFrameView::GetWindowBoundsForClientBounds(
6674
const gfx::Rect& client_bounds) const {
6775
return views::GetWindowBoundsForClientBounds(

shell/browser/ui/views/win_frame_view.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class WinFrameView : public FramelessView {
3030

3131
SkColor GetReadableFeatureColor(SkColor background_color);
3232

33+
// Tells the NonClientView to invalidate the WinFrameView's caption buttons.
34+
void InvalidateCaptionButtons();
35+
3336
// views::NonClientFrameView:
3437
gfx::Rect GetWindowBoundsForClientBounds(
3538
const gfx::Rect& client_bounds) const override;

0 commit comments

Comments
 (0)
X Tutup