Skip to main content

RuntimeStyle

Struct RuntimeStyle 

Source
pub struct RuntimeStyle { /* private fields */ }
Expand description

A stylesheet layered from a base plus an optional runtime override.

Construct the base via either:

  • RuntimeStyle::new — wrap a compile-time &'static Stylesheet (typically from the css! macro), or
  • RuntimeStyle::from_owned — wrap a runtime-parsed Arc<Stylesheet> (e.g. RuntimeStyle::from_owned(Arc::new(Stylesheet::parse(&css)?))), which avoids leaking memory for themes loaded purely at runtime.

Then optionally call RuntimeStyle::load_override (one-shot) or RuntimeStyle::reload_if_changed (mtime-based, tick-friendly) to apply a user-supplied CSS file. The merged sheet is recomputed only when the override changes, so RuntimeStyle::compute stays allocation-free.

Implementations§

Source§

impl RuntimeStyle

Source

pub fn new(embedded: &'static Stylesheet) -> Self

Wrap a compile-time &'static embedded stylesheet with no runtime override. This is the path used by the css! macro and is zero-cost (no allocation, no refcount).

Source

pub fn from_owned(embedded: Arc<Stylesheet>) -> Self

Wrap a runtime-parsed, owned stylesheet with no runtime override.

For apps that load their theme purely at runtime (from disk, config, network, …) there is no compile-time &'static to borrow. This constructor takes an Arc<Stylesheet> so the caller never needs to Box::leak:

let css = "Button { color: red; }";
let style = RuntimeStyle::from_owned(Arc::new(Stylesheet::parse(css).unwrap()));
Source

pub fn load_override(&mut self, path: &Path) -> Result<()>

Load (or reload) a runtime CSS override from path.

If the file exists it is parsed and merged onto the base stylesheet; its rules carry Origin::User and override the base Origin::Theme rules at equal specificity. If the file does not exist, this is not an error — the base stylesheet is used as-is and any previously loaded override is cleared. Other I/O or parse failures are returned as CssError.

This performs a full re-read and re-parse every call. For cheap, mtime-gated reloading in an app tick, see Self::reload_if_changed.

Source

pub fn reload_if_changed(&mut self, path: &Path) -> Result<bool>

Reload the override at path only if its mtime changed since the last load; otherwise do nothing.

Returns true when a reload actually happened (the file changed and was re-parsed), or when an existing override was cleared because the file disappeared (mirroring Self::load_override’s NotFound semantics). Returns false when nothing changed.

Call this from an app’s event-loop tick to get “edit the theme file → see it live” behavior without re-parsing every frame:

// in your tick / poll loop:
if style.reload_if_changed(path).unwrap() {
    // theme was updated — the next compute() reflects the new rules
}

Degradation policy: if the filesystem cannot report a modification time for path (e.g. some network/FUSE mounts), this is treated as a change — the file is reloaded and true is returned — so updates are never silently dropped. NotFound still means “override removed”.

Source

pub fn set_media(&mut self, media: MediaContext) -> &mut Self

Set the active MediaContext used to gate @media rules during compute / compute_with. Returns &mut Self for chaining. Call this once per frame (e.g. after reading the terminal size) before computing node styles, so width-/ color-conditional rules apply.

Source

pub fn with_media(self, media: MediaContext) -> Self

Consuming builder form of set_media.

Source

pub fn media(&self) -> &MediaContext

The currently active MediaContext.

Source

pub fn compute( &self, node: &dyn StyledNode, parent: Option<&ComputedStyle>, ) -> ComputedStyle

Compute the resolved style for node, optionally inheriting from parent. Delegates to the pre-merged sheet, so this is allocation-free.

@media rules are gated against media; set it via set_media / with_media so width-/color-conditional rules apply.

Source

pub fn compute_with( &self, node: &dyn StyledNode, parent: Option<&ComputedStyle>, scratch: &mut ComputeScratch, ) -> ComputedStyle

Compute using a caller-provided ComputeScratch, reused across calls.

Delegates to Stylesheet::compute_with_media on the pre-merged sheet, gating @media rules against media. Use this in the draw loop alongside NodeRef for a fully allocation-free per-frame path.

Source

pub fn embedded(&self) -> &Stylesheet

The base (compile-time or owned) stylesheet.

Source

pub fn runtime(&self) -> Option<&Stylesheet>

The runtime override stylesheet, if one is loaded.

Source

pub fn has_override(&self) -> bool

Whether a runtime override is currently active.

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.