fix: setTitleBarOverlay should be implemented on BaseWindow (#41960)
		
	fix: setTitleBarOverlay should be implemented on BaseWindow
This commit is contained in:
		
					parent
					
						
							
								b95d0f7623
							
						
					
				
			
			
				commit
				
					
						5310b79ffb
					
				
			
		
					 4 changed files with 62 additions and 71 deletions
				
			
		| 
						 | 
				
			
			@ -11,6 +11,7 @@
 | 
			
		|||
 | 
			
		||||
#include "base/containers/contains.h"
 | 
			
		||||
#include "base/task/single_thread_task_runner.h"
 | 
			
		||||
#include "content/public/common/color_parser.h"
 | 
			
		||||
#include "electron/buildflags/buildflags.h"
 | 
			
		||||
#include "gin/dictionary.h"
 | 
			
		||||
#include "shell/browser/api/electron_api_menu.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -36,6 +37,7 @@
 | 
			
		|||
#endif
 | 
			
		||||
 | 
			
		||||
#if BUILDFLAG(IS_WIN)
 | 
			
		||||
#include "shell/browser/ui/views/win_frame_view.h"
 | 
			
		||||
#include "shell/browser/ui/win/taskbar_host.h"
 | 
			
		||||
#include "ui/base/win/shell.h"
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -1039,6 +1041,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<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 anything was updated, invalidate the layout and schedule a paint of the
 | 
			
		||||
  // window's frame view
 | 
			
		||||
  if (updated) {
 | 
			
		||||
    auto* frame_view = static_cast<WinFrameView*>(
 | 
			
		||||
        window->widget()->non_client_view()->frame_view());
 | 
			
		||||
    frame_view->InvalidateCaptionButtons();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int32_t BaseWindow::GetID() const {
 | 
			
		||||
| 
						 | 
				
			
			@ -1227,6 +1286,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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -241,6 +241,8 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
 | 
			
		|||
  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;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,6 @@
 | 
			
		|||
#include "content/browser/web_contents/web_contents_impl.h"  // nogncheck
 | 
			
		||||
#include "content/public/browser/render_process_host.h"
 | 
			
		||||
#include "content/public/browser/render_view_host.h"
 | 
			
		||||
#include "content/public/common/color_parser.h"
 | 
			
		||||
#include "shell/browser/api/electron_api_web_contents_view.h"
 | 
			
		||||
#include "shell/browser/browser.h"
 | 
			
		||||
#include "shell/browser/web_contents_preferences.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -27,10 +26,6 @@
 | 
			
		|||
#include "shell/browser/native_window_views.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if BUILDFLAG(IS_WIN)
 | 
			
		||||
#include "shell/browser/ui/views/win_frame_view.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace electron::api {
 | 
			
		||||
 | 
			
		||||
BrowserWindow::BrowserWindow(gin::Arguments* args,
 | 
			
		||||
| 
						 | 
				
			
			@ -275,65 +270,6 @@ v8::Local<v8::Value> BrowserWindow::GetWebContents(v8::Isolate* isolate) {
 | 
			
		|||
  return v8::Local<v8::Value>::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<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 anything was updated, invalidate the layout and schedule a paint of the
 | 
			
		||||
  // window's frame view
 | 
			
		||||
  if (updated) {
 | 
			
		||||
    auto* frame_view = static_cast<WinFrameView*>(
 | 
			
		||||
        window->widget()->non_client_view()->frame_view());
 | 
			
		||||
    frame_view->InvalidateCaptionButtons();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void BrowserWindow::OnWindowShow() {
 | 
			
		||||
  web_contents()->WasShown();
 | 
			
		||||
  BaseWindow::OnWindowShow();
 | 
			
		||||
| 
						 | 
				
			
			@ -373,9 +309,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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -73,10 +73,6 @@ class BrowserWindow : public BaseWindow,
 | 
			
		|||
  void BlurWebView();
 | 
			
		||||
  bool IsWebViewFocused();
 | 
			
		||||
  v8::Local<v8::Value> GetWebContents(v8::Isolate* isolate);
 | 
			
		||||
#if BUILDFLAG(IS_WIN)
 | 
			
		||||
  void SetTitleBarOverlay(const gin_helper::Dictionary& options,
 | 
			
		||||
                          gin_helper::Arguments* args);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  // Helpers.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue