diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-08 15:31:15 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-08 16:37:07 +0100 |
| commit | 12a59963b08b68cc39dcded4d3d3e6a6631c2732 (patch) | |
| tree | 3d20b014ada1ac06d2e74611a1798d7fb18dca33 /src/model | |
| parent | a7a4cae2948176119e8995bd8e1868f2d0e65029 (diff) | |
Reduce style chain bloat
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/content.rs | 10 | ||||
| -rw-r--r-- | src/model/styles.rs | 13 |
2 files changed, 18 insertions, 5 deletions
diff --git a/src/model/content.rs b/src/model/content.rs index bc25cd79..3851d38d 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -115,10 +115,14 @@ impl Content { pub fn styled_with_entry(mut self, entry: StyleEntry) -> Self { if let Some(styled) = self.try_downcast_mut::<StyledNode>() { styled.map.apply(entry); - return self; + self + } else if let Some(styled) = self.downcast::<StyledNode>() { + let mut map = styled.map.clone(); + map.apply(entry); + StyledNode { sub: styled.sub.clone(), map }.pack() + } else { + StyledNode { sub: self, map: entry.into() }.pack() } - - StyledNode { sub: self, map: entry.into() }.pack() } /// Style this content with a full style map. diff --git a/src/model/styles.rs b/src/model/styles.rs index 8e731942..62e3188f 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -85,6 +85,15 @@ impl StyleMap { /// Like [`chain`](Self::chain) or [`apply_map`](Self::apply_map), but with /// only a entry. pub fn apply(&mut self, entry: StyleEntry) { + if let StyleEntry::Guard(a) = &entry { + if let [StyleEntry::Unguard(b), ..] = self.0.as_slice() { + if a == b { + self.0.remove(0); + return; + } + } + } + self.0.insert(0, entry); } @@ -124,7 +133,7 @@ impl From<StyleEntry> for StyleMap { impl Debug for StyleMap { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - for entry in self.0.iter().rev() { + for entry in self.0.iter() { writeln!(f, "{:?}", entry)?; } Ok(()) @@ -351,7 +360,7 @@ impl<'a> StyleChain<'a> { impl Debug for StyleChain<'_> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - for entry in self.entries() { + for entry in self.entries().collect::<Vec<_>>().into_iter().rev() { writeln!(f, "{:?}", entry)?; } Ok(()) |
