diff options
Diffstat (limited to 'src/model/styles.rs')
| -rw-r--r-- | src/model/styles.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/model/styles.rs b/src/model/styles.rs index 9e723171..7db2df42 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -5,6 +5,7 @@ use std::marker::PhantomData; use super::{Barrier, Content, Key, Property, Recipe, Selector, Show, Target}; use crate::diag::TypResult; +use crate::frame::Role; use crate::library::text::{FontFamily, TextNode}; use crate::util::ReadableTypeId; use crate::Context; @@ -36,6 +37,13 @@ impl StyleMap { styles } + /// Create a style map from a single role. + pub fn with_role(role: Role) -> Self { + let mut styles = Self::new(); + styles.push(StyleEntry::Role(role)); + styles + } + /// Set an inner value for a style property. /// /// If the property needs folding and the value is already contained in the @@ -170,6 +178,8 @@ pub enum StyleEntry { Property(Property), /// A show rule recipe. Recipe(Recipe), + /// A semantic role. + Role(Role), /// A barrier for scoped styles. Barrier(Barrier), /// Guards against recursive show rules. @@ -229,6 +239,7 @@ impl Debug for StyleEntry { match self { Self::Property(property) => property.fmt(f)?, Self::Recipe(recipe) => recipe.fmt(f)?, + Self::Role(role) => role.fmt(f)?, Self::Barrier(barrier) => barrier.fmt(f)?, Self::Guard(sel) => write!(f, "Guard against {sel:?}")?, Self::Unguard(sel) => write!(f, "Unguard against {sel:?}")?, @@ -324,8 +335,23 @@ impl<'a> StyleChain<'a> { Ok(realized) } + /// Retrieve the current role + pub fn role(self) -> Option<Role> { + let mut depth = 0; + + for entry in self.entries() { + match *entry { + StyleEntry::Role(role) => return Some(role), + StyleEntry::Barrier(_) if depth == 1 => return None, + StyleEntry::Barrier(_) => depth += 1, + _ => {} + } + } + None + } + /// Whether the recipe identified by the selector is guarded. - fn guarded(&self, sel: Selector) -> bool { + fn guarded(self, sel: Selector) -> bool { for entry in self.entries() { match *entry { StyleEntry::Guard(s) if s == sel => return true, |
