Use sctk-adwaita 0.5.1 auto theme selection

Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
Alex Butler 2022-08-24 21:43:36 +01:00 committed by GitHub
parent b3b80166ce
commit e91ee811cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View file

@ -212,6 +212,8 @@ pub trait WindowExtUnix {
///
/// You can also use `WINIT_WAYLAND_CSD_THEME` env variable to set the theme.
/// Possible values for env variable are: "dark" and light"
///
/// When unspecified a theme is automatically selected.
#[cfg(feature = "wayland")]
fn wayland_set_csd_theme(&self, config: Theme);
@ -349,6 +351,8 @@ pub trait WindowBuilderExtUnix {
///
/// You can also use `WINIT_WAYLAND_CSD_THEME` env variable to set the theme.
/// Possible values for env variable are: "dark" and light"
///
/// When unspecified a theme is automatically selected.
#[cfg(feature = "wayland")]
fn with_wayland_csd_theme(self, theme: Theme) -> Self;

View file

@ -170,17 +170,14 @@ impl Window {
)
.map_err(|_| os_error!(OsError::WaylandMisc("failed to create window.")))?;
// Set CSD frame config
// Set CSD frame config from theme if specified,
// otherwise use upstream automatic selection.
#[cfg(feature = "sctk-adwaita")]
{
let theme = platform_attributes.csd_theme.unwrap_or_else(|| {
let env = std::env::var(WAYLAND_CSD_THEME_ENV_VAR).unwrap_or_default();
match env.to_lowercase().as_str() {
"dark" => Theme::Dark,
_ => Theme::Light,
}
});
if let Some(theme) = platform_attributes.csd_theme.or_else(|| {
std::env::var(WAYLAND_CSD_THEME_ENV_VAR)
.ok()
.and_then(|s| s.as_str().try_into().ok())
}) {
window.set_frame_config(theme.into());
}
@ -636,3 +633,23 @@ impl From<Theme> for sctk_adwaita::FrameConfig {
}
}
}
impl TryFrom<&str> for Theme {
type Error = ();
/// ```
/// use winit::window::Theme;
///
/// assert_eq!("dark".try_into(), Ok(Theme::Dark));
/// assert_eq!("lIghT".try_into(), Ok(Theme::Light));
/// ```
fn try_from(theme: &str) -> Result<Self, Self::Error> {
if theme.eq_ignore_ascii_case("dark") {
Ok(Self::Dark)
} else if theme.eq_ignore_ascii_case("light") {
Ok(Self::Light)
} else {
Err(())
}
}
}