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 thecss!macro), orRuntimeStyle::from_owned— wrap a runtime-parsedArc<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
impl RuntimeStyle
Sourcepub fn new(embedded: &'static Stylesheet) -> Self
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).
Sourcepub fn from_owned(embedded: Arc<Stylesheet>) -> Self
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()));Sourcepub fn load_override(&mut self, path: &Path) -> Result<()>
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.
Sourcepub fn reload_if_changed(&mut self, path: &Path) -> Result<bool>
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”.
Sourcepub fn set_media(&mut self, media: MediaContext) -> &mut Self
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.
Sourcepub fn with_media(self, media: MediaContext) -> Self
pub fn with_media(self, media: MediaContext) -> Self
Consuming builder form of set_media.
Sourcepub fn media(&self) -> &MediaContext
pub fn media(&self) -> &MediaContext
The currently active MediaContext.
Sourcepub fn compute(
&self,
node: &dyn StyledNode,
parent: Option<&ComputedStyle>,
) -> ComputedStyle
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.
Sourcepub fn compute_with(
&self,
node: &dyn StyledNode,
parent: Option<&ComputedStyle>,
scratch: &mut ComputeScratch,
) -> ComputedStyle
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.
Sourcepub fn embedded(&self) -> &Stylesheet
pub fn embedded(&self) -> &Stylesheet
The base (compile-time or owned) stylesheet.
Sourcepub fn runtime(&self) -> Option<&Stylesheet>
pub fn runtime(&self) -> Option<&Stylesheet>
The runtime override stylesheet, if one is loaded.
Sourcepub fn has_override(&self) -> bool
pub fn has_override(&self) -> bool
Whether a runtime override is currently active.
Auto Trait Implementations§
impl Freeze for RuntimeStyle
impl RefUnwindSafe for RuntimeStyle
impl Send for RuntimeStyle
impl Sync for RuntimeStyle
impl Unpin for RuntimeStyle
impl UnsafeUnpin for RuntimeStyle
impl UnwindSafe for RuntimeStyle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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