diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-17 11:32:15 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-17 11:45:57 +0100 |
| commit | 312197b276748e1a17258ad21837850f582a467c (patch) | |
| tree | 3fd0c078a2673a98b74bc12b4d654a4c143b4e1f /library/src/shared | |
| parent | e8435df5ec718e8ecc8a2ad48e4eb3ddd1f92a72 (diff) | |
Counters
Diffstat (limited to 'library/src/shared')
| -rw-r--r-- | library/src/shared/behave.rs | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/library/src/shared/behave.rs b/library/src/shared/behave.rs index 74c4d151..eff41c0b 100644 --- a/library/src/shared/behave.rs +++ b/library/src/shared/behave.rs @@ -1,38 +1,9 @@ //! Node interaction. -use typst::model::{Content, StyleChain, StyleVec, StyleVecBuilder}; - -/// How a node interacts with other nodes. -pub trait Behave { - /// The node's interaction behaviour. - fn behaviour(&self) -> Behaviour; - - /// Whether this weak node is larger than a previous one and thus picked as - /// the maximum when the levels are the same. - #[allow(unused_variables)] - fn larger(&self, prev: &Content) -> bool { - false - } -} - -/// How a node interacts with other nodes in a stream. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum Behaviour { - /// A weak node which only survives when a supportive node is before and - /// after it. Furthermore, per consecutive run of weak nodes, only one - /// survives: The one with the lowest weakness level (or the larger one if - /// there is a tie). - Weak(usize), - /// A node that enables adjacent weak nodes to exist. The default. - Supportive, - /// A node that destroys adjacent weak nodes. - Destructive, - /// A node that does not interact at all with other nodes, having the - /// same effect as if it didn't exist. - Ignorant, -} +use typst::model::{Behave, Behaviour, Content, StyleChain, StyleVec, StyleVecBuilder}; /// A wrapper around a [`StyleVecBuilder`] that allows items to interact. +#[derive(Debug)] pub struct BehavedBuilder<'a> { /// The internal builder. builder: StyleVecBuilder<'a, Content>, @@ -53,11 +24,21 @@ impl<'a> BehavedBuilder<'a> { } } - /// Whether the builder is empty. + /// Whether the builder is totally empty. pub fn is_empty(&self) -> bool { self.builder.is_empty() && self.staged.is_empty() } + /// Whether the builder is empty except for some weak items that will + /// probably collapse. + pub fn is_basically_empty(&self) -> bool { + self.builder.is_empty() + && self + .staged + .iter() + .all(|(_, behaviour, _)| matches!(behaviour, Behaviour::Weak(_))) + } + /// Push an item into the sequence. pub fn push(&mut self, item: Content, styles: StyleChain<'a>) { let interaction = item |
