dsl: InputLayerCond; collect macros

This commit is contained in:
🪞👃🪞 2025-05-03 02:13:22 +03:00
parent 0d4ba4a54e
commit 3df1938626
6 changed files with 121 additions and 110 deletions

View file

@ -70,14 +70,18 @@ impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter
}
}
pub type InputLayerCond<'a, S> = Box<dyn Fn(&S)->bool + Send + Sync + 'a>;
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<'a, S, C: DslCommand<'a, S>, I: DslInput, M: KeyMap<'a, S, C, I> + Send + Sync> {
pub struct InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
__: &'a PhantomData<(S, C, I)>,
pub layers: Vec<(
fn(&S)->bool,
M
)>,
pub layers: Vec<(InputLayerCond<'a, S>, M)>,
}
impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M>
@ -108,15 +112,15 @@ where
self
}
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
self.add_layer_if(|_|true, keymap);
self.add_layer_if(Box::new(|_|true), keymap);
self
}
pub fn layer_if (mut self, condition: fn(&S)->bool, keymap: M) -> Self {
pub fn layer_if (mut self, condition: InputLayerCond<'a, S>, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (&mut self, condition: fn(&S)->bool, keymap: M) -> &mut Self {
self.layers.push((condition, keymap));
pub fn add_layer_if (&mut self, condition: InputLayerCond<'a, S>, keymap: M) -> &mut Self {
self.layers.push((Box::new(condition), keymap));
self
}
}