From 87ae8868142a0b7b75ec1c6fc76f83a9f2e3c73f Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 14 Oct 2023 01:22:24 +0900 Subject: [PATCH 1/3] fix(linux): Fix `Window::theme` may return incorrect theme Signed-off-by: rhysd --- .changes/get-window-theme.md | 5 +++++ src/platform_impl/linux/window.rs | 18 ++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 .changes/get-window-theme.md diff --git a/.changes/get-window-theme.md b/.changes/get-window-theme.md new file mode 100644 index 000000000..7a15d0543 --- /dev/null +++ b/.changes/get-window-theme.md @@ -0,0 +1,5 @@ +--- +"tao": "patch" +--- + +Fix `Window::theme` may return a theme different from the actual window's theme on Linux. diff --git a/src/platform_impl/linux/window.rs b/src/platform_impl/linux/window.rs index e55d6038c..4f0bc4118 100644 --- a/src/platform_impl/linux/window.rs +++ b/src/platform_impl/linux/window.rs @@ -67,6 +67,7 @@ pub struct Window { inner_size_constraints: RefCell, /// Draw event Sender draw_tx: crossbeam_channel::Sender, + theme: Theme, } impl Window { @@ -185,10 +186,14 @@ impl Window { let settings = Settings::default(); + let mut theme = Theme::Light; if let Some(settings) = settings { if let Some(preferred_theme) = attributes.preferred_theme { match preferred_theme { - Theme::Dark => settings.set_gtk_application_prefer_dark_theme(true), + Theme::Dark => { + settings.set_gtk_application_prefer_dark_theme(true); + theme = Theme::Dark; + } Theme::Light => { let theme_name = settings.gtk_theme_name().map(|t| t.as_str().to_owned()); if let Some(theme) = theme_name { @@ -303,6 +308,7 @@ impl Window { minimized, fullscreen: RefCell::new(attributes.fullscreen), inner_size_constraints: RefCell::new(attributes.inner_size_constraints), + theme, }; win.set_skip_taskbar(pl_attribs.skip_taskbar); @@ -773,15 +779,7 @@ impl Window { } pub fn theme(&self) -> Theme { - if let Some(settings) = Settings::default() { - let theme_name = settings.gtk_theme_name().map(|s| s.as_str().to_owned()); - if let Some(theme) = theme_name { - if GTK_THEME_SUFFIX_LIST.iter().any(|t| theme.ends_with(t)) { - return Theme::Dark; - } - } - } - return Theme::Light; + self.theme } } From 123693345d40161e532d84bc55090a1d52773bd6 Mon Sep 17 00:00:00 2001 From: rhysd Date: Tue, 17 Oct 2023 00:20:18 +0900 Subject: [PATCH 2/3] Return preferred theme on `Window::theme` if available Signed-off-by: rhysd --- src/platform_impl/linux/window.rs | 34 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/platform_impl/linux/window.rs b/src/platform_impl/linux/window.rs index 4f0bc4118..51ea4bc1d 100644 --- a/src/platform_impl/linux/window.rs +++ b/src/platform_impl/linux/window.rs @@ -67,7 +67,7 @@ pub struct Window { inner_size_constraints: RefCell, /// Draw event Sender draw_tx: crossbeam_channel::Sender, - theme: Theme, + preferred_theme: Option, } impl Window { @@ -184,16 +184,10 @@ impl Window { window.set_icon(Some(&icon.inner.into())); } - let settings = Settings::default(); - - let mut theme = Theme::Light; - if let Some(settings) = settings { + let preferred_theme = if let Some(settings) = Settings::default() { if let Some(preferred_theme) = attributes.preferred_theme { match preferred_theme { - Theme::Dark => { - settings.set_gtk_application_prefer_dark_theme(true); - theme = Theme::Dark; - } + Theme::Dark => settings.set_gtk_application_prefer_dark_theme(true), Theme::Light => { let theme_name = settings.gtk_theme_name().map(|t| t.as_str().to_owned()); if let Some(theme) = theme_name { @@ -209,7 +203,10 @@ impl Window { } } } - } + attributes.preferred_theme + } else { + None + }; if attributes.visible { window.show_all(); @@ -308,7 +305,7 @@ impl Window { minimized, fullscreen: RefCell::new(attributes.fullscreen), inner_size_constraints: RefCell::new(attributes.inner_size_constraints), - theme, + preferred_theme, }; win.set_skip_taskbar(pl_attribs.skip_taskbar); @@ -779,7 +776,20 @@ impl Window { } pub fn theme(&self) -> Theme { - self.theme + if let Some(theme) = self.preferred_theme { + return theme; + } + + if let Some(settings) = Settings::default() { + let theme_name = settings.gtk_theme_name().map(|s| s.as_str().to_owned()); + if let Some(theme) = theme_name { + if GTK_THEME_SUFFIX_LIST.iter().any(|t| theme.ends_with(t)) { + return Theme::Dark; + } + } + } + + Theme::Light } } From 94bf32546ab00b6b95d3926071dda88dffa53528 Mon Sep 17 00:00:00 2001 From: rhysd Date: Tue, 17 Oct 2023 00:47:48 +0900 Subject: [PATCH 3/3] Remove some redundant clones Signed-off-by: rhysd --- src/platform_impl/linux/window.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/platform_impl/linux/window.rs b/src/platform_impl/linux/window.rs index 51ea4bc1d..75232ff39 100644 --- a/src/platform_impl/linux/window.rs +++ b/src/platform_impl/linux/window.rs @@ -189,8 +189,8 @@ impl Window { match preferred_theme { Theme::Dark => settings.set_gtk_application_prefer_dark_theme(true), Theme::Light => { - let theme_name = settings.gtk_theme_name().map(|t| t.as_str().to_owned()); - if let Some(theme) = theme_name { + if let Some(theme) = settings.gtk_theme_name() { + let theme = theme.as_str(); // Remove dark variant. if let Some(theme) = GTK_THEME_SUFFIX_LIST .iter() @@ -780,12 +780,10 @@ impl Window { return theme; } - if let Some(settings) = Settings::default() { - let theme_name = settings.gtk_theme_name().map(|s| s.as_str().to_owned()); - if let Some(theme) = theme_name { - if GTK_THEME_SUFFIX_LIST.iter().any(|t| theme.ends_with(t)) { - return Theme::Dark; - } + if let Some(theme) = Settings::default().and_then(|s| s.gtk_theme_name()) { + let theme = theme.as_str(); + if GTK_THEME_SUFFIX_LIST.iter().any(|t| theme.ends_with(t)) { + return Theme::Dark; } }