chore: audit all components (#2)

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
2026-11-08 06:27:12 +00:00
parent 0ad5c3fe90
commit 11b29dc91a
51 changed files with 2096 additions and 958 deletions
+107 -30
View File
@@ -5,7 +5,7 @@
use std::path::{Path, PathBuf};
use gpui::Hsla;
use gpui::{Hsla, Pixels};
use notify::{RecursiveMode, Watcher};
use super::ThemeSource;
@@ -55,11 +55,12 @@ fn detect(home: &Path, portal_reachable: bool, gsettings_reachable: bool) -> Bac
}
}
/// What `gsettings` states for GNOME sessions without a portal.
/// What `gsettings` states for GNOME sessions.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct GSettingsSnapshot {
variant: Option<ThemeVariant>,
accent: Option<Hsla>,
root_font_size: Option<Pixels>,
}
fn gsettings_value(args: &[&str]) -> Option<String> {
@@ -97,23 +98,31 @@ fn read_gsettings() -> Option<GSettingsSnapshot> {
"prefer-light" => Some(ThemeVariant::Light),
_ => None,
});
let accent = gsettings_value(&["get", "org.gnome.desktop.interface", "accent-color"])
.and_then(|value| accent_rgb(value.trim().trim_matches('\'')));
if variant.is_none() && accent.is_none() {
let root_font_size = gsettings_value(&["get", "org.gnome.desktop.interface", "font-name"])
.and_then(|value| font_root_px(value.trim().trim_matches('\'')));
if variant.is_none() && accent.is_none() && root_font_size.is_none() {
return None;
}
Some(GSettingsSnapshot { variant, accent })
Some(GSettingsSnapshot {
variant,
accent,
root_font_size,
})
}
fn font_root_px(name: &str) -> Option<Pixels> {
let points = name.split_whitespace().last()?.parse::<f32>().ok()?;
(points > 0.0).then(|| gpui::px(points * 96.0 / 72.0))
}
pub(crate) fn platform_source() -> Box<dyn ThemeSource> {
let portal = portal::io::PortalClient::connect();
let gsettings = if portal.is_none() {
read_gsettings()
} else {
None
};
let gsettings = read_gsettings();
let backend = home_dir()
.map(|home| detect(&home, portal.is_some(), gsettings.is_some()))
@@ -129,6 +138,36 @@ pub(crate) fn platform_source() -> Box<dyn ThemeSource> {
}
impl LinuxSource {
fn gnome_snapshot(
&self,
variant: ThemeVariant,
accent: Option<Hsla>,
accessibility: crate::system::AccessibilityPreferences,
) -> SystemTheme {
SystemTheme {
variant,
accent,
palette: PartialPalette::default(),
control_radius: Some(gpui::px(9.)),
overlay_radius: Some(gpui::px(15.)),
sm_radius: Some(gpui::px(6.)),
control_height: Some(gpui::px(34.)),
input_height: Some(gpui::px(34.)),
menu_item_height: Some(gpui::px(32.)),
switch_height: Some(gpui::px(26.)),
switch_width: Some(gpui::px(46.)),
switch_knob: Some(gpui::px(20.)),
titlebar_height: Some(gpui::px(47.)),
root_font_size: Some(
self.gsettings
.and_then(|gsettings| gsettings.root_font_size)
.unwrap_or(gpui::px(14.67)),
),
accessibility,
source: SourceKind::Gnome,
}
}
fn portal_snapshot(
&self,
variant: ThemeVariant,
@@ -136,31 +175,21 @@ impl LinuxSource {
accessibility: crate::system::AccessibilityPreferences,
) -> SystemTheme {
if let Some(appearance) = portal_appearance {
return SystemTheme {
variant: appearance.variant(variant),
accent: appearance.accent,
palette: PartialPalette::default(),
control_radius: None,
input_height: None,
titlebar_height: None,
root_font_size: None,
return self.gnome_snapshot(
appearance.variant(variant),
appearance.accent,
accessibility,
source: SourceKind::Gnome,
};
);
}
if let Some(gsettings) = self.gsettings {
return SystemTheme {
variant: gsettings.variant.unwrap_or(variant),
accent: gsettings.accent,
palette: PartialPalette::default(),
control_radius: None,
input_height: None,
titlebar_height: None,
root_font_size: None,
return self.gnome_snapshot(
gsettings.variant.unwrap_or(variant),
gsettings.accent,
accessibility,
source: SourceKind::Gnome,
};
);
}
SystemTheme::fallback(variant)
}
}
@@ -289,4 +318,52 @@ mod tests {
assert_eq!(detect(&home, true, false), Backend::Kde);
std::fs::remove_dir_all(&home).expect("cleanup");
}
#[test]
fn the_gsettings_font_name_states_the_root_font_size() {
assert_eq!(
font_root_px("Adwaita Sans 11"),
Some(gpui::px(11.0 * 96.0 / 72.0))
);
assert_eq!(
font_root_px("Noto Sans 10"),
Some(gpui::px(10.0 * 96.0 / 72.0))
);
assert_eq!(
font_root_px("Cantarell Bold 12.5"),
Some(gpui::px(12.5 * 96.0 / 72.0))
);
assert_eq!(font_root_px("Adwaita Sans"), None);
assert_eq!(font_root_px("Sans 0"), None);
}
#[test]
fn the_gnome_snapshot_states_the_libadwaita_metrics() {
let source = LinuxSource {
backend: Backend::Portal,
portal: None,
gsettings: None,
file_watcher: None,
_signal: None,
};
let system = source.gnome_snapshot(
ThemeVariant::Light,
None,
crate::system::AccessibilityPreferences::default(),
);
assert_eq!(system.source, SourceKind::Gnome);
assert_eq!(system.control_height, Some(gpui::px(34.)));
assert_eq!(system.input_height, Some(gpui::px(34.)));
assert_eq!(system.menu_item_height, Some(gpui::px(32.)));
assert_eq!(system.control_radius, Some(gpui::px(9.)));
assert_eq!(system.overlay_radius, Some(gpui::px(15.)));
assert_eq!(system.sm_radius, Some(gpui::px(6.)));
assert_eq!(system.switch_height, Some(gpui::px(26.)));
assert_eq!(system.switch_width, Some(gpui::px(46.)));
assert_eq!(system.switch_knob, Some(gpui::px(20.)));
assert_eq!(system.titlebar_height, Some(gpui::px(47.)));
// The default 11pt font when gsettings states none.
assert_eq!(system.root_font_size, Some(gpui::px(14.67)));
}
}