mod accent;
use ashpd::desktop::Color;
use ashpd::desktop::settings::{ColorScheme, Contrast, ReducedMotion};
use crate::model::FontSpec;
use crate::model::font::{FontSize, FontStyle};
const PANGO_WEIGHTS: &[(&str, i32)] = &[
("Thin", 100),
("Ultra-Light", 200),
("Extra-Light", 200),
("Light", 300),
("Semi-Light", 350),
("Demi-Light", 350),
("Book", 380),
("Regular", 400),
("Medium", 500),
("Semi-Bold", 600),
("Demi-Bold", 600),
("Bold", 700),
("Ultra-Bold", 800),
("Extra-Bold", 800),
("Heavy", 900),
("Black", 900),
("Ultra-Heavy", 1000),
("Extra-Heavy", 1000),
("Ultra-Black", 1000),
("Extra-Black", 1000),
];
const PANGO_WEIGHT_NORMAL: i32 = 400;
const PANGO_STYLES: &[(&str, FontStyle)] = &[
("Roman", FontStyle::Normal),
("Oblique", FontStyle::Oblique),
("Italic", FontStyle::Italic),
];
const PANGO_DROPPED_FIELDS: &[(&str, &[(&str, ())])] = &[
(
"width",
&[
("Ultra-Condensed", ()),
("Extra-Condensed", ()),
("Condensed", ()),
("Semi-Condensed", ()),
("Semi-Expanded", ()),
("Expanded", ()),
("Extra-Expanded", ()),
("Ultra-Expanded", ()),
],
),
(
"variant",
&[
("Small-Caps", ()),
("All-Small-Caps", ()),
("Petite-Caps", ()),
("All-Petite-Caps", ()),
("Unicase", ()),
("Title-Caps", ()),
],
),
(
"gravity",
&[
("Not-Rotated", ()),
("South", ()),
("Upside-Down", ()),
("North", ()),
("Rotated-Left", ()),
("East", ()),
("Rotated-Right", ()),
("West", ()),
],
),
("color", &[("With-Color", ()), ("Without-Color", ())]),
];
const PANGO_MAX_SIZE: f64 = 1_000_000.0;
#[cfg(any(feature = "kde", test))]
pub(crate) fn portal_color_to_rgba(color: &Color) -> Option<crate::Rgba> {
let r = color.red();
let g = color.green();
let b = color.blue();
if !(0.0..=1.0).contains(&r) || !(0.0..=1.0).contains(&g) || !(0.0..=1.0).contains(&b) {
return None;
}
Some(crate::Rgba::from_f32(r as f32, g as f32, b as f32, 1.0))
}
fn apply_accent(variant: &mut crate::ThemeMode, accent: &crate::Rgba) {
variant.defaults.accent_color = Some(*accent);
variant.defaults.selection_background = Some(*accent);
variant.defaults.focus_ring_color = Some(*accent);
variant.tab.active_indicator_color = Some(*accent);
}
fn apply_gnome_accent(
variant: &mut crate::ThemeMode,
accent: &crate::Rgba,
dark: bool,
high_contrast: bool,
) {
apply_accent(variant, accent);
variant.defaults.info_color = Some(*accent);
let ring = accent::focus_ring(*accent, dark, high_contrast);
variant.defaults.focus_ring_color = Some(ring);
variant.input.focus_border_color = Some(ring);
}
pub(crate) fn parse_gnome_font_to_fontspec(s: &str) -> Option<FontSpec> {
let mut rest = s.trim().trim_matches('\'');
let (before, word) = pango_last_word(rest, "");
let tail = ['#', '@'].into_iter().find(|mark| word.starts_with(*mark));
if tail.is_some() {
rest = before;
}
let (before, word) = pango_last_word(rest, ",");
if ['#', '@']
.into_iter()
.any(|mark| Some(mark) != tail && word.starts_with(mark))
{
rest = before;
}
let (before, word) = pango_last_word(rest, ",");
let size = pango_size(word)?;
rest = before;
let mut options = PangoStyleOptions {
weight: PANGO_WEIGHT_NORMAL,
style: Some(FontStyle::Normal),
};
loop {
let (before, word) = pango_last_word(rest, ",");
if word.is_empty() || !options.apply(word) {
break;
}
rest = before;
}
let family = rest
.split(',')
.map(|family| family.trim_matches(|c: char| c.is_ascii_whitespace()))
.find(|family| !family.is_empty())?;
Some(FontSpec {
family: Some(family.into()),
size: Some(size),
weight: u16::try_from(options.weight.clamp(100, 900)).ok(),
style: options.style,
..Default::default()
})
}
fn pango_last_word<'a>(s: &'a str, stop: &str) -> (&'a str, &'a str) {
let s = s.trim_end_matches(|c: char| c.is_ascii_whitespace());
let word = s
.rsplit(|c: char| c.is_ascii_whitespace() || stop.contains(c))
.next()
.unwrap_or_default();
(s.strip_suffix(word).unwrap_or_default(), word)
}
fn pango_size(word: &str) -> Option<FontSize> {
let (number, pixels) = match word.strip_suffix("px") {
Some(number) => (number, true),
None => (word, false),
};
let size = number
.parse::<f64>()
.ok()
.filter(|size| *size > 0.0 && *size <= PANGO_MAX_SIZE)? as f32;
Some(if pixels {
FontSize::Px(size)
} else {
FontSize::Pt(size)
})
}
enum PangoField<T> {
Word(T),
Int(i32),
}
fn pango_find_field<T: Copy>(what: &str, map: &[(&str, T)], word: &str) -> Option<PangoField<T>> {
let (word, had_prefix) = match word.strip_prefix(what).and_then(|w| w.strip_prefix('=')) {
Some(value) => (value, true),
None => (word, false),
};
if let Some(&(_, value)) = map.iter().find(|(name, _)| pango_word_matches(name, word)) {
return Some(PangoField::Word(value));
}
if had_prefix {
return word
.parse::<i32>()
.ok()
.filter(|n| *n >= 0)
.map(PangoField::Int);
}
None
}
fn pango_word_matches(name: &str, word: &str) -> bool {
let mut name = name.bytes();
for w in word.bytes() {
loop {
match name.next() {
Some(n) if n.eq_ignore_ascii_case(&w) => break,
Some(b'-') => {}
_ => return false,
}
}
}
name.next().is_none()
}
struct PangoStyleOptions {
weight: i32,
style: Option<FontStyle>,
}
impl PangoStyleOptions {
fn apply(&mut self, word: &str) -> bool {
if pango_word_matches("Normal", word) {
return true;
}
if let Some(weight) = pango_find_field("weight", PANGO_WEIGHTS, word) {
self.weight = match weight {
PangoField::Word(weight) | PangoField::Int(weight) => weight,
};
return true;
}
if let Some(style) = pango_find_field("style", PANGO_STYLES, word) {
self.style = match style {
PangoField::Word(style) => Some(style),
PangoField::Int(0) => Some(FontStyle::Normal),
PangoField::Int(1) => Some(FontStyle::Oblique),
PangoField::Int(2) => Some(FontStyle::Italic),
PangoField::Int(_) => None,
};
return true;
}
PANGO_DROPPED_FIELDS
.iter()
.any(|(what, map)| pango_find_field(what, map, word).is_some())
}
}
const GSETTINGS_DEADLINE: std::time::Duration = crate::detect::SUBPROCESS_TIMEOUT;
fn read_gsetting(schema: &str, key: &str, timeout: std::time::Duration) -> Option<String> {
if timeout.is_zero() {
return None;
}
let raw = crate::detect::gsettings_get(schema, key, timeout)?;
let trimmed = raw.trim_matches('\'').to_string();
if trimmed.is_empty() {
None
} else {
Some(trimmed)
}
}
pub(crate) struct GnomePortalData {
pub(crate) is_dark: bool,
pub(crate) accent_rgb: Option<(f64, f64, f64)>,
pub(crate) high_contrast: bool,
pub(crate) reduce_motion: Option<bool>,
pub(crate) font_name: Option<String>,
pub(crate) monospace_font_name: Option<String>,
pub(crate) titlebar_font: Option<String>,
pub(crate) text_scaling_factor: Option<f32>,
pub(crate) session_type: Option<String>,
pub(crate) overlay_scrolling: Option<bool>,
pub(crate) icon_theme: Option<String>,
pub(crate) gsettings_high_contrast: Option<bool>,
}
fn effective_high_contrast(data: &GnomePortalData) -> bool {
data.high_contrast || data.gsettings_high_contrast == Some(true)
}
fn accessibility_from_gnome_data(data: &GnomePortalData) -> crate::AccessibilityPreferences {
let mut acc = crate::AccessibilityPreferences::default();
let effective_high_contrast = effective_high_contrast(data);
if let Some(tsf) = data.text_scaling_factor {
acc.text_scaling_factor = tsf;
}
if let Some(rm) = data.reduce_motion {
acc.reduce_motion = rm;
}
acc.high_contrast = effective_high_contrast;
acc
}
const CAPTION_FACTOR: f32 = 0.82;
const TITLE_2_FACTOR: f32 = 1.36;
const TITLE_1_FACTOR: f32 = 1.81;
fn scaled_size(body: FontSize, factor: f32) -> FontSize {
match body {
FontSize::Pt(v) => FontSize::Pt(v * factor),
FontSize::Px(v) => FontSize::Px(v * factor),
}
}
fn apply_type_scale(variant: &mut crate::ThemeMode, body: FontSize) {
let entry = |factor| crate::TextScaleEntry {
size: Some(scaled_size(body, factor)),
..Default::default()
};
variant.text_scale.caption = Some(entry(CAPTION_FACTOR));
variant.text_scale.dialog_title = Some(entry(TITLE_2_FACTOR));
variant.text_scale.display = Some(entry(TITLE_1_FACTOR));
variant.dialog.title_font = Some(FontSpec {
size: Some(scaled_size(body, TITLE_2_FACTOR)),
..Default::default()
});
}
fn build_gnome_variant_pure(data: &GnomePortalData) -> crate::ThemeMode {
let mut variant = crate::ThemeMode::default();
if let Some((r, g, b)) = data.accent_rgb
&& (0.0..=1.0).contains(&r)
&& (0.0..=1.0).contains(&g)
&& (0.0..=1.0).contains(&b)
{
let rgba = crate::Rgba::from_f32(r as f32, g as f32, b as f32, 1.0);
apply_gnome_accent(
&mut variant,
&rgba,
data.is_dark,
effective_high_contrast(data),
);
}
if let Some(ref font_str) = data.font_name
&& let Some(fs) = parse_gnome_font_to_fontspec(font_str)
{
if let Some(body) = fs.size {
apply_type_scale(&mut variant, body);
}
variant.defaults.font = fs;
}
if let Some(ref mono_str) = data.monospace_font_name
&& let Some(fs) = parse_gnome_font_to_fontspec(mono_str)
{
variant.defaults.mono_font = fs;
}
if let Some(ref tb_str) = data.titlebar_font
&& let Some(fs) = parse_gnome_font_to_fontspec(tb_str)
{
variant.window.title_bar_font = Some(fs);
}
if let Some(overlay) = data.overlay_scrolling {
variant.scrollbar.overlay_mode = Some(overlay);
}
variant.defaults.icon_theme = data.icon_theme.clone().map(std::borrow::Cow::Owned);
variant
}
fn toolkit_scale_carries_text_scale(session_type: Option<&str>) -> bool {
session_type == Some("x11")
}
fn build_gnome_spec_pure(data: &GnomePortalData) -> crate::ReaderResult {
let mut accessibility = accessibility_from_gnome_data(data);
if toolkit_scale_carries_text_scale(data.session_type.as_deref()) {
accessibility.text_scaling_factor =
crate::AccessibilityPreferences::DEFAULT.text_scaling_factor;
}
crate::ReaderResult {
output: crate::ReaderOutput::Single {
mode: Box::new(build_gnome_variant_pure(data)),
is_dark: data.is_dark,
},
name: "GNOME".into(),
icon_set: None,
layout: crate::LayoutTheme::default(),
font_dpi: Some(crate::detect::GNOME_FONT_DPI),
accessibility,
}
}
pub(crate) fn build_theme(
scheme: Option<ColorScheme>,
accent: Option<Color>,
contrast: Contrast,
reduced_motion: Option<ReducedMotion>,
session_type: Option<String>,
mut gsetting: impl FnMut(&str, &str) -> Option<String>,
) -> crate::ReaderResult {
let portal_dark = scheme.map(|scheme| matches!(scheme, ColorScheme::PreferDark));
let is_dark = crate::detect::color_scheme_is_dark(portal_dark, &mut gsetting).unwrap_or(false);
let reduce_motion = crate::detect::gnome_reduce_motion(
reduced_motion.map(portal_reduces_motion),
&mut gsetting,
);
let flag = |value: Option<String>| value.and_then(|s| s.parse::<bool>().ok());
let data = GnomePortalData {
is_dark,
accent_rgb: accent.map(|c| (c.red(), c.green(), c.blue())),
high_contrast: matches!(contrast, Contrast::High),
reduce_motion,
font_name: gsetting("org.gnome.desktop.interface", "font-name"),
monospace_font_name: gsetting("org.gnome.desktop.interface", "monospace-font-name"),
titlebar_font: gsetting("org.gnome.desktop.wm.preferences", "titlebar-font"),
text_scaling_factor: gsetting("org.gnome.desktop.interface", "text-scaling-factor")
.and_then(|s| s.parse::<f32>().ok()),
session_type,
overlay_scrolling: flag(gsetting("org.gnome.desktop.interface", "overlay-scrolling")),
icon_theme: gsetting("org.gnome.desktop.interface", "icon-theme"),
gsettings_high_contrast: flag(gsetting(
"org.gnome.desktop.a11y.interface",
"high-contrast",
)),
};
build_gnome_spec_pure(&data)
}
fn portal_reduces_motion(reduced_motion: ReducedMotion) -> bool {
matches!(reduced_motion, ReducedMotion::ReducedMotion)
}
pub(crate) async fn portal_reduce_motion() -> Option<bool> {
with_deadline(
async {
let settings = ashpd::desktop::settings::Settings::new().await.ok()?;
settings.reduced_motion().await.ok()
},
PORTAL_DEADLINE,
)
.await
.flatten()
.map(portal_reduces_motion)
}
pub(crate) struct GnomeReader;
#[async_trait::async_trait]
impl crate::reader::ThemeReader for GnomeReader {
async fn read(&self) -> crate::Result<crate::ReaderResult> {
let (scheme, accent, contrast, reduced_motion) = with_deadline(
async {
let settings = ashpd::desktop::settings::Settings::new().await.ok()?;
let scheme = settings.color_scheme().await.ok();
let accent = settings.accent_color().await.ok();
let contrast = settings.contrast().await.unwrap_or_default();
let reduced_motion = settings.reduced_motion().await.ok();
Some((scheme, accent, contrast, reduced_motion))
},
PORTAL_DEADLINE,
)
.await
.flatten()
.unwrap_or_default();
let start = std::time::Instant::now();
Ok(build_theme(
scheme,
accent,
contrast,
reduced_motion,
crate::detect::session_type(),
|schema, key| {
read_gsetting(
schema,
key,
GSETTINGS_DEADLINE.saturating_sub(start.elapsed()),
)
},
))
}
}
#[cfg(feature = "kde")]
pub(crate) struct GnomePortalKdeReader;
#[cfg(feature = "kde")]
#[async_trait::async_trait]
impl crate::reader::ThemeReader for GnomePortalKdeReader {
async fn read(&self) -> crate::Result<crate::ReaderResult> {
let mut result =
<crate::kde::KdeReader as crate::reader::ThemeReader>::read(&crate::kde::KdeReader)
.await?;
let accent = with_deadline(
async {
let settings = ashpd::desktop::settings::Settings::new().await.ok()?;
settings.accent_color().await.ok()
},
PORTAL_DEADLINE,
)
.await
.flatten();
let accent = match accent {
Some(color) => color,
None => return Ok(result),
};
let rgba = match portal_color_to_rgba(&accent) {
Some(r) => r,
None => return Ok(result),
};
match &mut result.output {
crate::ReaderOutput::Single { mode, .. } => {
apply_accent(mode, &rgba);
}
crate::ReaderOutput::Dual { light, dark, .. } => {
apply_accent(light, &rgba);
apply_accent(dark, &rgba);
}
}
Ok(result)
}
}
pub(crate) async fn detect_portal_backend() -> Option<super::LinuxDesktop> {
let (running, activatable) = with_deadline(
async {
let connection = ashpd::zbus::Connection::session().await.ok()?;
let proxy = ashpd::zbus::fdo::DBusProxy::new(&connection).await.ok()?;
let running = proxy.list_names().await.ok()?;
let activatable = proxy.list_activatable_names().await.ok()?;
Some((running, activatable))
},
PORTAL_DEADLINE,
)
.await??;
let running: Vec<&str> = running.iter().map(|n| n.as_str()).collect();
let activatable: Vec<&str> = activatable.iter().map(|n| n.as_str()).collect();
choose_portal_backend(&running, &activatable)
}
const PORTAL_DEADLINE: std::time::Duration = crate::detect::SUBPROCESS_TIMEOUT;
async fn with_deadline<F: std::future::Future>(
fut: F,
deadline: std::time::Duration,
) -> Option<F::Output> {
let mut fut = std::pin::pin!(fut);
let mut timer = async_io::Timer::after(deadline);
std::future::poll_fn(|cx| {
if let std::task::Poll::Ready(out) = fut.as_mut().poll(cx) {
return std::task::Poll::Ready(Some(out));
}
if std::pin::Pin::new(&mut timer).poll(cx).is_ready() {
return std::task::Poll::Ready(None);
}
std::task::Poll::Pending
})
.await
}
fn choose_portal_backend(running: &[&str], activatable: &[&str]) -> Option<super::LinuxDesktop> {
fn backend_in(names: &[&str]) -> Result<Option<super::LinuxDesktop>, ()> {
let kde = names.iter().any(|n| n.contains("portal.desktop.kde"));
let gnome = names.iter().any(|n| n.contains("portal.desktop.gnome"));
match (kde, gnome) {
(true, true) => Err(()),
(true, false) => Ok(Some(super::LinuxDesktop::Kde)),
(false, true) => Ok(Some(super::LinuxDesktop::Gnome)),
(false, false) => Ok(None),
}
}
match backend_in(running) {
Ok(Some(desktop)) => Some(desktop),
Ok(None) => backend_in(activatable).ok().flatten(),
Err(()) => None,
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn fontspec_bold_weight() {
let fs = parse_gnome_font_to_fontspec("Cantarell Bold 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
assert_eq!(fs.weight, Some(700));
}
#[test]
fn fontspec_light_weight() {
let fs = parse_gnome_font_to_fontspec("Inter Light 10").unwrap();
assert_eq!(fs.family.as_deref(), Some("Inter"));
assert_eq!(fs.size, Some(FontSize::Pt(10.0)));
assert_eq!(fs.weight, Some(300));
}
#[test]
fn fontspec_semi_bold_weight() {
let fs = parse_gnome_font_to_fontspec("Noto Sans Semi-Bold 12").unwrap();
assert_eq!(fs.family.as_deref(), Some("Noto Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(12.0)));
assert_eq!(fs.weight, Some(600));
}
#[test]
fn fontspec_no_modifier_defaults_to_regular() {
let fs = parse_gnome_font_to_fontspec("Cantarell 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
assert_eq!(fs.weight, Some(400));
}
#[test]
fn fontspec_medium_weight() {
let fs = parse_gnome_font_to_fontspec("Inter Medium 10.5").unwrap();
assert_eq!(fs.family.as_deref(), Some("Inter"));
assert_eq!(fs.size, Some(FontSize::Pt(10.5)));
assert_eq!(fs.weight, Some(500));
}
#[test]
fn fontspec_heavy_weight() {
let fs = parse_gnome_font_to_fontspec("'Fira Sans Heavy 14'").unwrap();
assert_eq!(fs.family.as_deref(), Some("Fira Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(14.0)));
assert_eq!(fs.weight, Some(900));
}
#[test]
fn fontspec_thin_weight() {
let fs = parse_gnome_font_to_fontspec("Roboto Thin 12").unwrap();
assert_eq!(fs.family.as_deref(), Some("Roboto"));
assert_eq!(fs.size, Some(FontSize::Pt(12.0)));
assert_eq!(fs.weight, Some(100));
}
#[test]
fn fontspec_extra_bold_weight() {
let fs = parse_gnome_font_to_fontspec("Source Sans Extra-Bold 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Source Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
assert_eq!(fs.weight, Some(800));
}
#[test]
fn fontspec_ultra_light_weight() {
let fs = parse_gnome_font_to_fontspec("Noto Sans Ultra-Light 10").unwrap();
assert_eq!(fs.family.as_deref(), Some("Noto Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(10.0)));
assert_eq!(fs.weight, Some(200));
}
#[test]
fn fontspec_empty_returns_none() {
assert!(parse_gnome_font_to_fontspec("").is_none());
}
#[test]
fn fontspec_with_quotes() {
let fs = parse_gnome_font_to_fontspec("'Cantarell Bold 11'").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.weight, Some(700));
}
#[test]
fn fontspec_non_finite_size_is_dropped() {
assert!(parse_gnome_font_to_fontspec("Cantarell NaN").is_none());
assert!(parse_gnome_font_to_fontspec("Cantarell inf").is_none());
assert!(parse_gnome_font_to_fontspec("Cantarell 0").is_none());
assert!(parse_gnome_font_to_fontspec("Cantarell -11").is_none());
}
#[test]
fn fontspec_style_word() {
let fs = parse_gnome_font_to_fontspec("Cantarell Italic 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
assert_eq!(fs.weight, Some(400));
assert_eq!(fs.style, Some(FontStyle::Italic));
}
#[test]
fn fontspec_weight_and_style_words() {
let fs = parse_gnome_font_to_fontspec("Noto Sans Bold Italic 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Noto Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
assert_eq!(fs.weight, Some(700));
assert_eq!(fs.style, Some(FontStyle::Italic));
}
#[test]
fn fontspec_pixel_size() {
let fs = parse_gnome_font_to_fontspec("Cantarell 15px").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Px(15.0)));
}
#[test]
fn fontspec_variations_and_features_are_skipped() {
let fs = parse_gnome_font_to_fontspec("Cantarell 11 @wght=200").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
let fs =
parse_gnome_font_to_fontspec("Cantarell Italic Light 15 @wght=200 #tnum=1").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(15.0)));
assert_eq!(fs.weight, Some(300));
assert_eq!(fs.style, Some(FontStyle::Italic));
}
#[test]
fn fontspec_first_family_of_a_list() {
let fs = parse_gnome_font_to_fontspec("Cantarell, Sans 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.size, Some(FontSize::Pt(11.0)));
let fs = parse_gnome_font_to_fontspec("Cantarell,Bold 11").unwrap();
assert_eq!(fs.family.as_deref(), Some("Cantarell"));
assert_eq!(fs.weight, Some(700));
}
#[test]
fn fontspec_words_ignore_case_and_hyphens() {
let fs = parse_gnome_font_to_fontspec("Inter semibold OBLIQUE 10").unwrap();
assert_eq!(fs.family.as_deref(), Some("Inter"));
assert_eq!(fs.weight, Some(600));
assert_eq!(fs.style, Some(FontStyle::Oblique));
}
#[test]
fn fontspec_pango_only_weights() {
let weight = |s| parse_gnome_font_to_fontspec(s).unwrap().weight;
assert_eq!(weight("Inter Semi-Light 10"), Some(350));
assert_eq!(weight("Inter Book 10"), Some(380));
assert_eq!(weight("Inter Ultra-Black 10"), Some(900));
assert_eq!(weight("Inter weight=650 10"), Some(650));
}
#[test]
fn fontspec_stretch_variant_gravity_words_leave_the_family() {
let fs =
parse_gnome_font_to_fontspec("Noto Sans Condensed Small-Caps Rotated-Left 9").unwrap();
assert_eq!(fs.family.as_deref(), Some("Noto Sans"));
assert_eq!(fs.size, Some(FontSize::Pt(9.0)));
assert_eq!(fs.weight, Some(400));
assert_eq!(fs.style, Some(FontStyle::Normal));
}
#[test]
fn fontspec_family_ends_at_the_first_unknown_word() {
let fs = parse_gnome_font_to_fontspec("DejaVu Sans Mono Book 10").unwrap();
assert_eq!(fs.family.as_deref(), Some("DejaVu Sans Mono"));
assert_eq!(fs.weight, Some(380));
let fs = parse_gnome_font_to_fontspec("Noto Sans Bold Mono 10").unwrap();
assert_eq!(fs.family.as_deref(), Some("Noto Sans Bold Mono"));
assert_eq!(fs.weight, Some(400));
let fs = parse_gnome_font_to_fontspec("Inter Bold Light 10").unwrap();
assert_eq!(fs.weight, Some(700));
}
#[test]
fn non_finite_font_size_keeps_the_presets_font() {
let mut data = default_gnome_data();
data.font_name = Some("Cantarell NaN".to_string());
data.monospace_font_name = Some("Source Code Pro inf".to_string());
let reader = build_gnome_spec_pure(&data);
assert_eq!(reader_mode(&reader).defaults.font, FontSpec::default());
assert_eq!(reader_mode(&reader).defaults.mono_font, FontSpec::default());
assert!(crate::pipeline::run_pipeline(reader, "adwaita").is_ok());
}
#[test]
fn portal_color_valid_converts_to_rgba() {
let color = Color::new(0.2, 0.4, 0.6);
let result = portal_color_to_rgba(&color);
assert!(result.is_some());
let rgba = result.unwrap();
assert_eq!(rgba, crate::Rgba::from_f32(0.2, 0.4, 0.6, 1.0));
}
#[test]
fn portal_color_out_of_range_high_returns_none() {
let color = Color::new(1.5, 0.0, 0.0);
assert!(portal_color_to_rgba(&color).is_none());
}
#[test]
fn portal_color_out_of_range_negative_returns_none() {
let color = Color::new(-0.1, 0.5, 0.5);
assert!(portal_color_to_rgba(&color).is_none());
}
#[test]
fn portal_color_to_rgba_boundary_zero() {
let color = Color::new(0.0, 0.0, 0.0);
let result = portal_color_to_rgba(&color);
assert!(result.is_some());
assert_eq!(result.unwrap(), crate::Rgba::from_f32(0.0, 0.0, 0.0, 1.0));
}
#[test]
fn portal_color_to_rgba_boundary_one() {
let color = Color::new(1.0, 1.0, 1.0);
let result = portal_color_to_rgba(&color);
assert!(result.is_some());
assert_eq!(result.unwrap(), crate::Rgba::from_f32(1.0, 1.0, 1.0, 1.0));
}
fn no_gsettings(_schema: &str, _key: &str) -> Option<String> {
None
}
fn reader_mode(result: &crate::ReaderResult) -> &crate::ThemeMode {
match &result.output {
crate::ReaderOutput::Single { mode, .. } => mode,
crate::ReaderOutput::Dual { light, .. } => light,
}
}
#[test]
fn dark_scheme_produces_dark_variant_only() {
let result = build_theme(
Some(ColorScheme::PreferDark),
None,
Contrast::NoPreference,
None,
None,
no_gsettings,
);
assert_eq!(result.name, "GNOME");
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: true, .. }
));
}
#[test]
fn light_scheme_produces_light_variant_only() {
let result = build_theme(
Some(ColorScheme::PreferLight),
None,
Contrast::NoPreference,
None,
None,
no_gsettings,
);
assert_eq!(result.name, "GNOME");
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: false, .. }
));
}
#[test]
fn no_preference_defaults_to_light() {
let result = build_theme(
Some(ColorScheme::NoPreference),
None,
Contrast::NoPreference,
None,
None,
no_gsettings,
);
assert_eq!(result.name, "GNOME");
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: false, .. }
));
}
#[test]
fn valid_accent_propagates_to_three_fields() {
let accent = Color::new(0.2, 0.4, 0.8);
let result = build_theme(
Some(ColorScheme::NoPreference),
Some(accent),
Contrast::NoPreference,
None,
None,
no_gsettings,
);
let variant = reader_mode(&result);
let expected = crate::Rgba::from_f32(0.2, 0.4, 0.8, 1.0);
assert_eq!(variant.defaults.accent_color, Some(expected));
assert_eq!(variant.defaults.selection_background, Some(expected));
let ring = Some(super::accent::focus_ring(expected, false, false));
assert_eq!(variant.defaults.focus_ring_color, ring);
assert_eq!(variant.input.focus_border_color, ring);
}
#[test]
fn high_contrast_sets_flag_on_theme_variant() {
let result = build_theme(
Some(ColorScheme::NoPreference),
None,
Contrast::High,
None,
None,
no_gsettings,
);
assert!(result.accessibility.high_contrast);
}
#[test]
fn normal_contrast_preserves_adwaita_default() {
let result = build_theme(
Some(ColorScheme::NoPreference),
None,
Contrast::NoPreference,
None,
None,
no_gsettings,
);
assert!(!result.accessibility.high_contrast);
}
#[test]
fn gsettings_high_contrast_key_sets_flag() {
let high_contrast = |schema: &str, key: &str| {
(schema == "org.gnome.desktop.a11y.interface" && key == "high-contrast")
.then(|| "true".to_string())
};
let result = build_theme(
Some(ColorScheme::NoPreference),
None,
Contrast::NoPreference,
None,
None,
high_contrast,
);
assert!(result.accessibility.high_contrast);
}
fn gsettings_color_scheme(value: &'static str) -> impl FnMut(&str, &str) -> Option<String> {
move |_schema, key| (key == "color-scheme").then(|| value.to_string())
}
#[test]
fn no_portal_scheme_reads_gsettings_color_scheme() {
let result = build_theme(
None,
None,
Contrast::NoPreference,
None,
None,
gsettings_color_scheme("prefer-dark"),
);
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: true, .. }
));
let result = build_theme(
None,
None,
Contrast::NoPreference,
None,
None,
gsettings_color_scheme("default"),
);
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: false, .. }
));
}
#[test]
fn portal_scheme_wins_over_gsettings() {
let result = build_theme(
Some(ColorScheme::PreferLight),
None,
Contrast::NoPreference,
None,
None,
gsettings_color_scheme("prefer-dark"),
);
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: false, .. }
));
}
#[test]
fn one_gsettings_read_per_key() {
for (scheme, reads) in [(None, 9), (Some(ColorScheme::PreferDark), 8)] {
let mut keys = Vec::new();
let result = build_theme(
scheme,
None,
Contrast::NoPreference,
None,
None,
|schema, key| {
keys.push(format!("{schema} {key}"));
(key == "icon-theme").then(|| "Papirus".to_string())
},
);
assert_eq!(keys.len(), reads, "{keys:?}");
keys.sort();
keys.dedup();
assert_eq!(keys.len(), reads, "a key read twice");
assert_eq!(
reader_mode(&result).defaults.icon_theme.as_deref(),
Some("Papirus")
);
}
}
#[test]
fn a_spent_deadline_reads_nothing() {
assert_eq!(
read_gsetting(
"org.gnome.desktop.interface",
"font-name",
std::time::Duration::ZERO
),
None
);
}
fn default_gnome_data() -> GnomePortalData {
GnomePortalData {
is_dark: false,
accent_rgb: None,
high_contrast: false,
reduce_motion: None,
font_name: Some("Cantarell 11".to_string()),
monospace_font_name: Some("Source Code Pro 10".to_string()),
titlebar_font: Some("Cantarell Bold 11".to_string()),
text_scaling_factor: Some(1.0),
session_type: None,
overlay_scrolling: Some(true),
icon_theme: Some("Adwaita".to_string()),
gsettings_high_contrast: None,
}
}
#[test]
fn the_focus_ring_and_entry_focus_are_the_accents_standalone_colour_at_half() {
let hex = |s: &str| s.parse::<crate::Rgba>().expect("a hex colour");
for (accent, dark, ring) in [
((0x35, 0x84, 0xe4), false, "#0461be80"),
((0x35, 0x84, 0xe4), true, "#81d0ff80"),
((0x91, 0x41, 0xac), false, "#8939a480"),
((0x91, 0x41, 0xac), true, "#fba7ff80"),
] {
let mut data = default_gnome_data();
data.is_dark = dark;
data.accent_rgb = Some((
f64::from(accent.0) / 255.0,
f64::from(accent.1) / 255.0,
f64::from(accent.2) / 255.0,
));
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
assert_eq!(
variant.defaults.focus_ring_color,
Some(hex(ring)),
"{accent:?} dark {dark}"
);
assert_eq!(
variant.input.focus_border_color,
Some(hex(ring)),
"{accent:?} dark {dark}"
);
}
}
#[test]
fn under_high_contrast_the_focus_ring_is_the_standalone_colour_at_80() {
let hex = |s: &str| s.parse::<crate::Rgba>().expect("a hex colour");
for (dark, portal, gsettings, ring) in [
(false, true, None, "#0461becc"),
(true, true, None, "#81d0ffcc"),
(false, false, Some(true), "#0461becc"),
(false, false, Some(false), "#0461be80"),
] {
let mut data = default_gnome_data();
data.is_dark = dark;
data.high_contrast = portal;
data.gsettings_high_contrast = gsettings;
data.accent_rgb = Some((
f64::from(0x35_u8) / 255.0,
f64::from(0x84_u8) / 255.0,
f64::from(0xe4_u8) / 255.0,
));
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
let at = format!("dark {dark}, portal {portal}, gsettings {gsettings:?}");
assert_eq!(variant.defaults.focus_ring_color, Some(hex(ring)), "{at}");
assert_eq!(variant.input.focus_border_color, Some(hex(ring)), "{at}");
}
}
#[test]
fn text_scale_reaches_the_font_size_once() {
let mut data = default_gnome_data();
data.text_scaling_factor = Some(1.5);
data.session_type = Some("wayland".to_string());
let reader = build_gnome_spec_pure(&data);
assert_eq!(reader.font_dpi, Some(96.0));
let sys = crate::pipeline::run_pipeline(reader, "adwaita").unwrap();
let drawn = sys.accessibility.scaled(sys.light.defaults.font.size);
assert!((drawn - 22.0).abs() < 0.01, "drawn at {drawn} px");
}
#[test]
fn an_x11_session_reports_no_text_scale() {
let mut data = default_gnome_data();
data.text_scaling_factor = Some(1.5);
data.session_type = Some("x11".to_string());
let result = build_gnome_spec_pure(&data);
assert_eq!(result.accessibility.text_scaling_factor, 1.0);
assert_eq!(result.accessibility.text_scale_factor(), 1.0);
}
#[test]
fn a_session_off_x11_reports_the_text_scale() {
for session_type in [Some("wayland"), None] {
let mut data = default_gnome_data();
data.text_scaling_factor = Some(1.5);
data.session_type = session_type.map(str::to_string);
let result = build_gnome_spec_pure(&data);
assert_eq!(
result.accessibility.text_scaling_factor, 1.5,
"{session_type:?}"
);
}
}
#[test]
fn build_theme_reads_the_session_type_it_is_given() {
let scale = |_: &str, key: &str| (key == "text-scaling-factor").then(|| "1.5".to_string());
let factor = |session_type: Option<&str>| {
build_theme(
None,
None,
Contrast::NoPreference,
None,
session_type.map(str::to_string),
scale,
)
.accessibility
.text_scaling_factor
};
assert_eq!(factor(Some("x11")), 1.0);
assert_eq!(factor(Some("wayland")), 1.5);
}
#[test]
fn pure_light_scheme_produces_light_variant() {
let data = default_gnome_data();
let result = build_gnome_spec_pure(&data);
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: false, .. }
));
assert_eq!(result.name, "GNOME");
}
#[test]
fn pure_dark_scheme_produces_dark_variant() {
let mut data = default_gnome_data();
data.is_dark = true;
let result = build_gnome_spec_pure(&data);
assert!(matches!(
result.output,
crate::ReaderOutput::Single { is_dark: true, .. }
));
assert_eq!(result.name, "GNOME");
}
#[test]
fn pure_accent_color_propagates_to_three_fields() {
let mut data = default_gnome_data();
data.accent_rgb = Some((0.2, 0.4, 0.8));
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
let expected = crate::Rgba::from_f32(0.2, 0.4, 0.8, 1.0);
assert_eq!(variant.defaults.accent_color, Some(expected));
assert_eq!(variant.defaults.selection_background, Some(expected));
let ring = Some(super::accent::focus_ring(expected, false, false));
assert_eq!(variant.defaults.focus_ring_color, ring);
assert_eq!(variant.input.focus_border_color, ring);
}
#[test]
fn pure_high_contrast_sets_flag() {
let mut data = default_gnome_data();
data.high_contrast = true;
let result = build_gnome_spec_pure(&data);
assert!(result.accessibility.high_contrast);
}
#[test]
fn a_portal_accent_reaches_the_accent_fills() {
let mut data = default_gnome_data();
data.accent_rgb = Some((0.5, 0.25, 0.75));
let reader = build_gnome_spec_pure(&data);
let accent = reader_mode(&reader).defaults.accent_color;
assert!(accent.is_some());
let sys = crate::pipeline::run_pipeline(reader, "adwaita").unwrap();
let r = &sys.light;
assert_ne!(
Some(r.defaults.accent_color),
crate::Theme::preset("adwaita")
.ok()
.and_then(|t| t.light)
.and_then(|l| l.defaults.accent_color),
"the portal's accent is adwaita's default, so this test proves nothing"
);
let accent = accent.expect("the portal accent was read");
assert_eq!(r.defaults.accent_color, accent);
assert_eq!(r.checkbox.checked_background, accent);
assert_eq!(r.switch.checked_background, accent);
assert_eq!(r.slider.fill_color, accent);
assert_eq!(r.progress_bar.fill_color, accent);
assert_eq!(r.defaults.info_color, accent);
}
fn pt(size: Option<FontSize>) -> Option<f32> {
match size {
Some(FontSize::Pt(v)) => Some(v),
_ => None,
}
}
fn type_scale_over_adwaita(font: &str) -> [Option<FontSize>; 4] {
let mut data = default_gnome_data();
data.font_name = Some(font.to_string());
let reader = build_gnome_spec_pure(&data);
let mut mode = crate::Theme::preset("adwaita")
.ok()
.and_then(|t| t.light)
.unwrap_or_default();
mode.merge(reader_mode(&reader));
let size = |e: &Option<crate::TextScaleEntry>| e.as_ref().and_then(|e| e.size);
[
size(&mode.text_scale.caption),
size(&mode.text_scale.dialog_title),
size(&mode.text_scale.display),
mode.dialog.title_font.as_ref().and_then(|f| f.size),
]
}
#[test]
fn the_default_font_gives_the_presets_type_scale() {
let preset = crate::Theme::preset("adwaita")
.ok()
.and_then(|t| t.light)
.unwrap_or_default();
let size = |e: &Option<crate::TextScaleEntry>| e.as_ref().and_then(|e| e.size);
let stated = [
size(&preset.text_scale.caption),
size(&preset.text_scale.dialog_title),
size(&preset.text_scale.display),
preset.dialog.title_font.as_ref().and_then(|f| f.size),
];
assert_eq!(
stated,
[
Some(FontSize::Pt(9.0)),
Some(FontSize::Pt(15.0)),
Some(FontSize::Pt(20.0)),
Some(FontSize::Pt(15.0)),
]
);
let derived = type_scale_over_adwaita("Adwaita Sans 11");
for (derived, stated) in derived.into_iter().zip(stated) {
assert!(pt(derived).is_some(), "not a point size: {derived:?}");
assert_eq!(
pt(derived).map(f32::round),
pt(stated),
"{derived:?} does not round to {stated:?}"
);
}
}
#[test]
fn another_font_scales_the_type_scale() {
let derived = type_scale_over_adwaita("Adwaita Sans 14");
let expected = [14.0 * 0.82, 14.0 * 1.36, 14.0 * 1.81, 14.0 * 1.36];
for (derived, expected) in derived.into_iter().zip(expected) {
assert!(
pt(derived).is_some_and(|d| (d - expected).abs() < 1e-4),
"{derived:?} is not {expected}pt"
);
}
let mut data = default_gnome_data();
data.font_name = Some("Adwaita Sans 14px".to_string());
let reader = build_gnome_spec_pure(&data);
let caption = reader_mode(&reader)
.text_scale
.caption
.as_ref()
.and_then(|e| e.size);
assert!(
matches!(caption, Some(FontSize::Px(v)) if (v - 14.0 * 0.82).abs() < 1e-4),
"a pixel font scales in pixels: {caption:?}"
);
let mut mode = crate::Theme::preset("adwaita")
.ok()
.and_then(|t| t.light)
.unwrap_or_default();
let section = mode.text_scale.section_heading.clone();
data.font_name = Some("Adwaita Sans 14".to_string());
mode.merge(reader_mode(&build_gnome_spec_pure(&data)));
assert_eq!(mode.text_scale.section_heading, section);
assert_eq!(
mode.text_scale.dialog_title.and_then(|e| e.weight),
Some(800)
);
assert_eq!(mode.text_scale.display.and_then(|e| e.weight), Some(800));
assert_eq!(mode.dialog.title_font.and_then(|f| f.weight), Some(800));
}
#[test]
fn the_text_scaling_factor_does_not_reach_the_type_scale() {
let mut data = default_gnome_data();
data.font_name = Some("Adwaita Sans 11".to_string());
let plain = build_gnome_spec_pure(&data);
data.text_scaling_factor = Some(1.5);
data.session_type = Some("wayland".to_string());
let scaled = build_gnome_spec_pure(&data);
assert_eq!(
reader_mode(&plain).text_scale,
reader_mode(&scaled).text_scale
);
assert_eq!(
reader_mode(&plain).dialog.title_font,
reader_mode(&scaled).dialog.title_font
);
assert_eq!(scaled.accessibility.text_scaling_factor, 1.5);
}
#[test]
fn a_font_without_a_size_leaves_the_type_scale() {
let mut data = default_gnome_data();
data.font_name = Some("Adwaita Sans".to_string());
let reader = build_gnome_spec_pure(&data);
assert_eq!(reader_mode(&reader).text_scale, crate::TextScale::default());
assert_eq!(reader_mode(&reader).dialog.title_font, None);
}
#[test]
fn pure_fonts_parsed_correctly() {
let mut data = default_gnome_data();
data.font_name = Some("Inter Bold 12".to_string());
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
assert_eq!(variant.defaults.font.family.as_deref(), Some("Inter"));
assert_eq!(variant.defaults.font.weight, Some(700));
assert_eq!(variant.defaults.font.size, Some(FontSize::Pt(12.0)));
}
#[test]
fn pure_out_of_range_accent_ignored() {
let baseline_data = default_gnome_data();
let baseline_result = build_gnome_spec_pure(&baseline_data);
let baseline_variant = reader_mode(&baseline_result);
let baseline_accent = baseline_variant.defaults.accent_color;
let mut data = default_gnome_data();
data.accent_rgb = Some((1.5, 0.0, 0.0));
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
assert_eq!(variant.defaults.accent_color, baseline_accent);
}
#[test]
fn pure_gsettings_high_contrast_fallback() {
let mut data = default_gnome_data();
data.high_contrast = false;
data.gsettings_high_contrast = Some(true);
let result = build_gnome_spec_pure(&data);
assert!(result.accessibility.high_contrast);
}
#[test]
fn pure_reduce_motion_from_portal() {
let mut data = default_gnome_data();
data.reduce_motion = Some(true);
let result = build_gnome_spec_pure(&data);
assert!(result.accessibility.reduce_motion);
}
#[test]
fn reduce_motion_is_the_portal_then_gsettings() {
let animations_off =
|_: &str, key: &str| (key == "enable-animations").then(|| "false".to_string());
let read = |portal| {
build_theme(
None,
None,
Contrast::NoPreference,
portal,
None,
animations_off,
)
.accessibility
.reduce_motion
};
assert!(read(None));
assert!(!read(Some(ReducedMotion::NoPreference)));
assert!(read(Some(ReducedMotion::ReducedMotion)));
}
#[test]
fn pure_overlay_scrolling() {
let mut data = default_gnome_data();
data.overlay_scrolling = Some(false);
let result = build_gnome_spec_pure(&data);
let variant = reader_mode(&result);
assert_eq!(variant.scrollbar.overlay_mode, Some(false));
}
#[test]
fn gnome_reader_exists() {
let r: Box<dyn crate::reader::ThemeReader> = Box::new(super::GnomeReader);
let _ = &r;
#[cfg(feature = "kde")]
{
let rp: Box<dyn crate::reader::ThemeReader> = Box::new(super::GnomePortalKdeReader);
let _ = &rp;
}
}
const KDE_BACKEND: &str = "org.freedesktop.impl.portal.desktop.kde";
const GNOME_BACKEND: &str = "org.freedesktop.impl.portal.desktop.gnome";
#[test]
fn portal_backend_running_preferred_over_activatable() {
assert_eq!(
choose_portal_backend(&[KDE_BACKEND], &[GNOME_BACKEND, KDE_BACKEND]),
Some(crate::LinuxDesktop::Kde)
);
}
#[test]
fn portal_backend_both_running_is_none() {
assert_eq!(
choose_portal_backend(&[GNOME_BACKEND, KDE_BACKEND], &[GNOME_BACKEND, KDE_BACKEND]),
None
);
}
#[test]
fn portal_backend_only_activatable_gnome() {
assert_eq!(
choose_portal_backend(&["org.freedesktop.DBus"], &[GNOME_BACKEND]),
Some(crate::LinuxDesktop::Gnome)
);
}
#[test]
fn portal_backend_both_activatable_none_running_is_none() {
assert_eq!(
choose_portal_backend(&[], &[KDE_BACKEND, GNOME_BACKEND]),
None
);
assert_eq!(choose_portal_backend(&[], &[]), None);
}
#[test]
fn deadline_ends_a_future_that_never_completes() {
let deadline = std::time::Duration::from_millis(100);
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let start = std::time::Instant::now();
let out = pollster::block_on(with_deadline(std::future::pending::<()>(), deadline));
let _ = tx.send((out, start.elapsed()));
});
let (out, elapsed) = rx
.recv_timeout(std::time::Duration::from_secs(5))
.expect("with_deadline did not return within 5 s");
assert_eq!(out, None);
assert!(elapsed >= deadline, "returned after {elapsed:?}");
}
#[test]
fn deadline_passes_a_ready_value_through() {
let out = pollster::block_on(with_deadline(
std::future::ready(7),
std::time::Duration::from_secs(5),
));
assert_eq!(out, Some(7));
}
}