mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
cmdsys: delegate handling
This commit is contained in:
parent
9b770c468f
commit
c551f83d5c
6 changed files with 72 additions and 57 deletions
|
|
@ -4,6 +4,14 @@ pub trait Command<S>: Sized {
|
|||
fn translate (self, _: &S) -> Self { self }
|
||||
fn execute (self, state: &mut S) -> Perhaps<Self>;
|
||||
}
|
||||
pub fn delegate <B, C: Command<S>, S> (
|
||||
wrap: impl Fn(C)->B,
|
||||
state: &mut S,
|
||||
cmd: C
|
||||
) -> Perhaps<B> {
|
||||
Ok(cmd.execute(state)?.map(|x|wrap(x)))
|
||||
}
|
||||
|
||||
pub trait MatchInput<E: Engine, S>: Sized {
|
||||
fn match_input (state: &S, input: &E::Input) -> Option<Self>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,16 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum FocusCommand {
|
||||
Next,
|
||||
Prev,
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Enter,
|
||||
Exit
|
||||
}
|
||||
|
||||
pub trait FocusGrid<T: Copy + PartialEq> {
|
||||
fn layout (&self) -> &[&[T]];
|
||||
pub trait FocusGrid {
|
||||
type Item: Copy + PartialEq;
|
||||
fn layout (&self) -> &[&[Self::Item]];
|
||||
fn cursor (&self) -> (usize, usize);
|
||||
fn cursor_mut (&mut self) -> &mut (usize, usize);
|
||||
fn update_focus (&mut self) {}
|
||||
fn focused (&self) -> &T {
|
||||
fn focused (&self) -> &Self::Item {
|
||||
let (x, y) = self.cursor();
|
||||
&self.layout()[y][x]
|
||||
}
|
||||
fn focus (&mut self, target: T) {
|
||||
fn focus (&mut self, target: Self::Item) {
|
||||
while self.focused() != &target {
|
||||
self.focus_next()
|
||||
}
|
||||
|
|
@ -90,4 +79,35 @@ pub trait FocusGrid<T: Copy + PartialEq> {
|
|||
}
|
||||
self.update_focus();
|
||||
}
|
||||
fn focus_enter (&mut self) {}
|
||||
fn focus_exit (&mut self) {}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum FocusCommand {
|
||||
Next,
|
||||
Prev,
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Enter,
|
||||
Exit
|
||||
}
|
||||
|
||||
impl<F: FocusGrid> Command<F> for FocusCommand {
|
||||
fn execute (self, state: &mut F) -> Perhaps<FocusCommand> {
|
||||
use FocusCommand::*;
|
||||
match self {
|
||||
Next => { state.focus_next(); },
|
||||
Prev => { state.focus_prev(); },
|
||||
Up => { state.focus_up(); },
|
||||
Down => { state.focus_down(); },
|
||||
Left => { state.focus_left(); },
|
||||
Right => { state.focus_right(); },
|
||||
Enter => { state.focus_enter(); },
|
||||
Exit => { state.focus_exit(); },
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue