diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index cf0d26a962..cd8c7e77e7 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -207,7 +207,7 @@ It creates a new `BrowserWindow` with native properties as set by the `options`. * `opacity` Number (optional) - Set the initial opacity of the window, between 0.0 (fully transparent) and 1.0 (fully opaque). This is only implemented on Windows and macOS. * `darkTheme` Boolean (optional) - Forces using dark theme for the window, only works on - some GTK desktop environments. Default is [`nativeTheme.shouldUseDarkColors`](native-theme.md). + some GTK+3 desktop environments. Default is `false`. * `transparent` Boolean (optional) - Makes the window [transparent](frameless-window.md#transparent-window). Default is `false`. On Windows, does not work unless the window is frameless. * `type` String (optional) - The type of window, default is normal window. See more about diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index 731e1d8ca9..e2b1422675 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -914,6 +914,10 @@ void BaseWindow::CloseFilePreview() { window_->CloseFilePreview(); } +void BaseWindow::SetGTKDarkThemeEnabled(bool use_dark_theme) { + window_->SetGTKDarkThemeEnabled(use_dark_theme); +} + v8::Local BaseWindow::GetContentView() const { if (content_view_.IsEmpty()) return v8::Null(isolate()); diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index 056e32188b..bf2f1540e4 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -206,6 +206,7 @@ class BaseWindow : public gin_helper::TrackableObject, void SetAspectRatio(double aspect_ratio, gin_helper::Arguments* args); void PreviewFile(const std::string& path, gin_helper::Arguments* args); void CloseFilePreview(); + void SetGTKDarkThemeEnabled(bool use_dark_theme); // Public getters of NativeWindow. v8::Local GetContentView() const; diff --git a/shell/browser/api/electron_api_native_theme.cc b/shell/browser/api/electron_api_native_theme.cc index 8f0ef00ec4..a95187e9f0 100644 --- a/shell/browser/api/electron_api_native_theme.cc +++ b/shell/browser/api/electron_api_native_theme.cc @@ -10,8 +10,6 @@ #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "gin/handle.h" -#include "shell/browser/native_window_views.h" -#include "shell/browser/window_list.h" #include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/object_template_builder.h" @@ -53,13 +51,8 @@ void NativeTheme::SetThemeSource(ui::NativeTheme::ThemeSource override) { // Update the macOS appearance setting for this new override value UpdateMacOSAppearanceForOverrideValue(override); #endif -#if defined(USE_X11) - const bool dark_enabled = ShouldUseDarkColors(); - for (auto* window : WindowList::GetWindows()) { - static_cast(window)->SetGTKDarkThemeEnabled( - dark_enabled); - } -#endif + // TODO(MarshallOfSound): Update all existing browsers windows to use GTK dark + // theme } ui::NativeTheme::ThemeSource NativeTheme::GetThemeSource() const { diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 87b8318468..a1e16926ee 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -234,6 +234,8 @@ class NativeWindow : public base::SupportsUserData, const std::string& display_name); virtual void CloseFilePreview(); + virtual void SetGTKDarkThemeEnabled(bool use_dark_theme) = 0; + // Converts between content bounds and window bounds. virtual gfx::Rect ContentBoundsToWindowBounds( const gfx::Rect& bounds) const = 0; diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index aa1e549ef7..1a41fc2276 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -138,6 +138,7 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver { std::vector items) override; void RefreshTouchBarItem(const std::string& item_id) override; void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override; + void SetGTKDarkThemeEnabled(bool use_dark_theme) override {} gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override; gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override; diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 1fb181a6b4..df88eaa1ed 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -34,7 +34,6 @@ #include "ui/base/hit_test.h" #include "ui/gfx/image/image.h" #include "ui/gfx/native_widget_types.h" -#include "ui/native_theme/native_theme.h" #include "ui/views/background.h" #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" #include "ui/views/controls/webview/webview.h" @@ -215,10 +214,10 @@ NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options, window_state_watcher_ = std::make_unique(this); // Set _GTK_THEME_VARIANT to dark if we have "dark-theme" option set. - bool use_dark_theme = - ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors(); - options.Get(options::kDarkTheme, &use_dark_theme); - SetGTKDarkThemeEnabled(use_dark_theme); + bool use_dark_theme = false; + if (options.Get(options::kDarkTheme, &use_dark_theme) && use_dark_theme) { + SetGTKDarkThemeEnabled(use_dark_theme); + } // Before the window is mapped the SetWMSpecState can not work, so we have // to manually set the _NET_WM_STATE. @@ -329,6 +328,20 @@ NativeWindowViews::~NativeWindowViews() { #endif } +void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) { +#if defined(USE_X11) + if (use_dark_theme) { + ui::SetStringProperty(static_cast(GetAcceleratedWidget()), + gfx::GetAtom("_GTK_THEME_VARIANT"), + gfx::GetAtom("UTF8_STRING"), "dark"); + } else { + ui::SetStringProperty(static_cast(GetAcceleratedWidget()), + gfx::GetAtom("_GTK_THEME_VARIANT"), + gfx::GetAtom("UTF8_STRING"), "light"); + } +#endif +} + void NativeWindowViews::SetContentView(views::View* view) { if (content_view()) { root_view_->RemoveChildView(content_view()); @@ -1291,20 +1304,6 @@ void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) { } #endif -#if defined(USE_X11) -void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) { - if (use_dark_theme) { - ui::SetStringProperty(static_cast(GetAcceleratedWidget()), - gfx::GetAtom("_GTK_THEME_VARIANT"), - gfx::GetAtom("UTF8_STRING"), "dark"); - } else { - ui::SetStringProperty(static_cast(GetAcceleratedWidget()), - gfx::GetAtom("_GTK_THEME_VARIANT"), - gfx::GetAtom("UTF8_STRING"), "light"); - } -} -#endif - void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget, bool active) { if (changed_widget != widget()) diff --git a/shell/browser/native_window_views.h b/shell/browser/native_window_views.h index 98d10807c2..b05aa7bce2 100644 --- a/shell/browser/native_window_views.h +++ b/shell/browser/native_window_views.h @@ -128,6 +128,8 @@ class NativeWindowViews : public NativeWindow, bool IsVisibleOnAllWorkspaces() override; + void SetGTKDarkThemeEnabled(bool use_dark_theme) override; + content::DesktopMediaID GetDesktopMediaID() const override; gfx::AcceleratedWidget GetAcceleratedWidget() const override; NativeWindowHandle GetNativeWindowHandle() const override; @@ -156,10 +158,6 @@ class NativeWindowViews : public NativeWindow, void SetIcon(const gfx::ImageSkia& icon); #endif -#if defined(USE_X11) - void SetGTKDarkThemeEnabled(bool use_dark_theme); -#endif - SkRegion* draggable_region() const { return draggable_region_.get(); } #if defined(OS_WIN)