Skip to main content

NativeTheme

Struct NativeTheme 

Source
#[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 preserved

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§name: String

Theme 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

Source

pub fn new(name: impl Into<String>) -> Self

Create a new theme with the given name and no variants.

Source

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.
Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if the theme has no variants set.

Source

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());
Source

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");
Source

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();
Source

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);
Source

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

Source§

fn clone(&self) -> NativeTheme

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NativeTheme

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NativeTheme

Source§

fn default() -> NativeTheme

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for NativeTheme

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for NativeTheme

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,