Skip to content

Commit

Permalink
fix(linux): Fix Window::theme may return incorrect theme (#800)
Browse files Browse the repository at this point in the history
* fix(linux): Fix `Window::theme` may return incorrect theme

Signed-off-by: rhysd <lin90162@yahoo.co.jp>

* Return preferred theme on `Window::theme` if available

Signed-off-by: rhysd <lin90162@yahoo.co.jp>

* Remove some redundant clones

Signed-off-by: rhysd <lin90162@yahoo.co.jp>

---------

Signed-off-by: rhysd <lin90162@yahoo.co.jp>
  • Loading branch information
rhysd committed Oct 16, 2023
1 parent 32ce759 commit 081ba16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changes/get-window-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Fix `Window::theme` may return a theme different from the actual window's theme on Linux.
32 changes: 19 additions & 13 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Window {
inner_size_constraints: RefCell<WindowSizeConstraints>,
/// Draw event Sender
draw_tx: crossbeam_channel::Sender<WindowId>,
preferred_theme: Option<Theme>,
}

impl Window {
Expand Down Expand Up @@ -183,15 +184,13 @@ impl Window {
window.set_icon(Some(&icon.inner.into()));
}

let settings = Settings::default();

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::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()
Expand All @@ -204,7 +203,10 @@ impl Window {
}
}
}
}
attributes.preferred_theme
} else {
None
};

if attributes.visible {
window.show_all();
Expand Down Expand Up @@ -303,6 +305,7 @@ impl Window {
minimized,
fullscreen: RefCell::new(attributes.fullscreen),
inner_size_constraints: RefCell::new(attributes.inner_size_constraints),
preferred_theme,
};

win.set_skip_taskbar(pl_attribs.skip_taskbar);
Expand Down Expand Up @@ -773,15 +776,18 @@ 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;
}
if let Some(theme) = self.preferred_theme {
return theme;
}

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;
}
}
return Theme::Light;

Theme::Light
}
}

Expand Down

0 comments on commit 081ba16

Please sign in to comment.