Skip to main content

native_theme/resolve/
context.rs

1//! Resolution-time inputs (font DPI, button order, icon theme) captured
2//! once per theme-build and passed by reference through the pipeline.
3//!
4//! Per docs/todo_v0.5.7_gaps.md §G7 / doc 2 §J.2 refinement on B5:
5//! intentionally NO `impl Default` — runtime-detected types must signal
6//! intent at the call site. Use [`ResolutionContext::from_system`] for
7//! production, or [`ResolutionContext::for_tests`] for deterministic
8//! test values.
9
10use std::borrow::Cow;
11
12use crate::model::DialogButtonOrder;
13
14/// Resolution-time inputs captured once per theme-build.
15///
16/// Replaces the `font_dpi: Option<f32>` parameter on
17/// [`ThemeMode::into_resolved`](crate::theme::ThemeMode::into_resolved) and
18/// the corresponding internal `OverlaySource` field.
19///
20/// Accessibility preferences live on
21/// [`SystemTheme`](crate::SystemTheme), NOT here — accessibility is a
22/// render-time concern, not a resolve-time concern. See doc 2 §J.2
23/// refinement on B4 for the rationale.
24///
25/// # Examples
26///
27/// ```
28/// use native_theme::resolve::ResolutionContext;
29///
30/// // Production: auto-detect from OS.
31/// let ctx = ResolutionContext::from_system();
32/// assert!(ctx.font_dpi > 0.0);
33///
34/// // Tests: deterministic values.
35/// let ctx = ResolutionContext::for_tests();
36/// assert_eq!(ctx.font_dpi, 96.0);
37/// assert!(ctx.icon_theme.is_none());
38/// ```
39#[derive(Clone, Debug)]
40pub struct ResolutionContext {
41    /// Font DPI for pt-to-px conversion. 96.0 on Linux/Windows, 72.0 on
42    /// macOS, or reader-supplied for KDE `forceFontDPI`.
43    pub font_dpi: f32,
44    /// Dialog button ordering (`PrimaryLeft` on KDE, `PrimaryRight`
45    /// elsewhere).
46    pub button_order: DialogButtonOrder,
47    /// Fallback icon theme name used when the preset and per-variant
48    /// `icon_theme` fields are both `None`. Three-tier precedence in the
49    /// pipeline: per-variant → `Theme`-level shared → this fallback.
50    pub icon_theme: Option<Cow<'static, str>>,
51}
52
53impl ResolutionContext {
54    /// Build the context by auto-detecting from the current OS.
55    ///
56    /// Calls:
57    /// - `crate::detect::system_font_dpi` for `font_dpi` (private)
58    /// - `crate::resolve::inheritance::platform_button_order` for
59    ///   `button_order`
60    /// - [`crate::model::icons::system_icon_theme`] for `icon_theme`
61    #[must_use]
62    pub fn from_system() -> Self {
63        Self {
64            font_dpi: crate::detect::system_font_dpi(),
65            button_order: crate::resolve::inheritance::platform_button_order(),
66            icon_theme: Some(Cow::Owned(crate::model::icons::system_icon_theme())),
67        }
68    }
69
70    /// Deterministic values for tests: 96 DPI, `PrimaryRight` button
71    /// order, no `icon_theme` fallback.
72    ///
73    /// Tests that need a specific DPI (e.g. 72.0 for Apple point→pixel)
74    /// can construct the struct via a literal:
75    /// `ResolutionContext { font_dpi: 72.0, ..ResolutionContext::for_tests() }`.
76    #[must_use]
77    pub fn for_tests() -> Self {
78        Self {
79            font_dpi: 96.0,
80            button_order: DialogButtonOrder::PrimaryRight,
81            icon_theme: None,
82        }
83    }
84}
85
86// Intentionally NO `impl Default for ResolutionContext`.
87// See module-level doc comment for the signal-intent rationale.