#[non_exhaustive]pub struct NativeTheme {
pub name: String,
pub light: Option<ThemeVariant>,
pub dark: Option<ThemeVariant>,
}Expand description
A complete native theme with a name and optional light/dark variants.
This is the top-level type that theme files deserialize into and that platform readers produce.
§Examples
use native_theme::NativeTheme;
// Load a bundled preset
let theme = NativeTheme::preset("dracula").unwrap();
assert_eq!(theme.name, "Dracula");
// Parse from a TOML string
let toml = r##"
name = "Custom"
[light.colors]
accent = "#ff6600"
"##;
let custom = NativeTheme::from_toml(toml).unwrap();
assert_eq!(custom.name, "Custom");
// Merge themes (overlay wins for populated fields)
let mut base = NativeTheme::preset("default").unwrap();
base.merge(&custom);
assert_eq!(base.name, "Default"); // base name is preservedFields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: StringTheme name (e.g., “Breeze”, “Adwaita”, “Windows 11”).
light: Option<ThemeVariant>Light variant of the theme.
dark: Option<ThemeVariant>Dark variant of the theme.
Implementations§
Source§impl NativeTheme
impl NativeTheme
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a new theme with the given name and no variants.
Sourcepub fn merge(&mut self, overlay: &Self)
pub fn merge(&mut self, overlay: &Self)
Merge an overlay theme into this theme.
The base name is kept. For each variant (light/dark):
- If both base and overlay have a variant, they are merged recursively.
- If only the overlay has a variant, it is cloned into the base.
- If only the base has a variant (or neither), no change.
Sourcepub fn pick_variant(&self, is_dark: bool) -> Option<&ThemeVariant>
pub fn pick_variant(&self, is_dark: bool) -> Option<&ThemeVariant>
Pick the appropriate variant for the given mode, with cross-fallback.
When is_dark is true, prefers dark and falls back to light.
When is_dark is false, prefers light and falls back to dark.
Returns None only if the theme has no variants at all.
Sourcepub fn preset(name: &str) -> Result<Self>
pub fn preset(name: &str) -> Result<Self>
Load a bundled theme preset by name.
Returns the preset as a fully populated NativeTheme with both
light and dark variants.
§Errors
Returns crate::Error::Unavailable if the preset name is not recognized.
§Examples
let theme = native_theme::NativeTheme::preset("default").unwrap();
assert!(theme.light.is_some());Sourcepub fn from_toml(toml_str: &str) -> Result<Self>
pub fn from_toml(toml_str: &str) -> Result<Self>
Parse a TOML string into a NativeTheme.
§TOML Format
Theme files use the following structure. All fields are Option<T> –
omit any field you don’t need. Unknown fields are silently ignored.
Hex colors accept #RRGGBB or #RRGGBBAA format.
name = "My Theme"
[light.colors]
# Core (7)
accent = "#4a90d9"
background = "#fafafa"
foreground = "#2e3436"
surface = "#ffffff"
border = "#c0c0c0"
muted = "#929292"
shadow = "#00000018"
# Primary (2)
primary_background = "#4a90d9"
primary_foreground = "#ffffff"
# Secondary (2)
secondary_background = "#6c757d"
secondary_foreground = "#ffffff"
# Status (8) -- each has an optional _foreground variant
danger = "#dc3545"
danger_foreground = "#ffffff"
warning = "#f0ad4e"
warning_foreground = "#ffffff"
success = "#28a745"
success_foreground = "#ffffff"
info = "#4a90d9"
info_foreground = "#ffffff"
# Interactive (4)
selection = "#4a90d9"
selection_foreground = "#ffffff"
link = "#2a6cb6"
focus_ring = "#4a90d9"
# Panel (6) -- each has an optional _foreground variant
sidebar = "#f0f0f0"
sidebar_foreground = "#2e3436"
tooltip = "#2e3436"
tooltip_foreground = "#ffffff"
popover = "#ffffff"
popover_foreground = "#2e3436"
# Component (7) -- button and input have _foreground variants
button = "#e8e8e8"
button_foreground = "#2e3436"
input = "#ffffff"
input_foreground = "#2e3436"
disabled = "#c0c0c0"
separator = "#d0d0d0"
alternate_row = "#f5f5f5"
[light.fonts]
family = "sans-serif"
size = 10.0
mono_family = "monospace"
mono_size = 10.0
[light.geometry]
radius = 6.0
radius_lg = 12.0
frame_width = 1.0
disabled_opacity = 0.5
border_opacity = 0.15
scroll_width = 8.0
[light.spacing]
xxs = 2.0
xs = 4.0
s = 6.0
m = 12.0
l = 18.0
xl = 24.0
xxl = 36.0
# [dark.*] mirrors the same structure as [light.*]§Errors
Returns crate::Error::Format if the TOML is invalid.
§Examples
let toml = r##"
name = "My Theme"
[light.colors]
accent = "#ff0000"
"##;
let theme = native_theme::NativeTheme::from_toml(toml).unwrap();
assert_eq!(theme.name, "My Theme");Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self>
Load a NativeTheme from a TOML file.
§Errors
Returns crate::Error::Unavailable if the file cannot be read.
§Examples
let theme = native_theme::NativeTheme::from_file("my-theme.toml").unwrap();Sourcepub fn list_presets() -> &'static [&'static str]
pub fn list_presets() -> &'static [&'static str]
List all available bundled preset names.
§Examples
let names = native_theme::NativeTheme::list_presets();
assert_eq!(names.len(), 17);Sourcepub fn to_toml(&self) -> Result<String>
pub fn to_toml(&self) -> Result<String>
Serialize this theme to a TOML string.
§Errors
Returns crate::Error::Format if serialization fails.
§Examples
let theme = native_theme::NativeTheme::preset("default").unwrap();
let toml_str = theme.to_toml().unwrap();
assert!(toml_str.contains("name = \"Default\""));Trait Implementations§
Source§impl Clone for NativeTheme
impl Clone for NativeTheme
Source§fn clone(&self) -> NativeTheme
fn clone(&self) -> NativeTheme
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more