cmdsys: scaffold menu

This commit is contained in:
🪞👃🪞 2024-11-07 02:52:40 +01:00
parent d3718f0b78
commit 1aaad23691
2 changed files with 134 additions and 169 deletions

View file

@ -1,28 +1,7 @@
use crate::*;
pub trait Command<T>: Sized {
fn run (&self, state: &mut T) -> Perhaps<Self>;
}
pub const fn key (code: KeyCode) -> KeyEvent {
KeyEvent {
code,
modifiers: KeyModifiers::NONE,
kind: crossterm::event::KeyEventKind::Press,
state: crossterm::event::KeyEventState::NONE
}
}
pub const fn ctrl (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::CONTROL), ..key }
}
pub const fn alt (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::ALT), ..key }
}
pub const fn shift (key: KeyEvent) -> KeyEvent {
KeyEvent { modifiers: key.modifiers.union(KeyModifiers::SHIFT), ..key }
pub trait Command<S>: Sized {
fn run (&self, state: &mut S) -> Perhaps<Self>;
}
pub trait HandleKey<C: Command<Self> + Clone + 'static>: Sized {
@ -62,3 +41,23 @@ pub trait HandleKey<C: Command<Self> + Clone + 'static>: Sized {
}
}
}
pub struct Menu<E: Engine, S: Handle<E>, C: Command<S>> {
pub items: Vec<MenuItem<E, S, C>>,
pub index: usize,
}
impl<E: Engine, S: Handle<E>, C: Command<S>> Menu<E, S, C> {
pub const fn item (command: C, name: &'static str, key: &'static str) -> MenuItem<E, S, C> {
MenuItem::Command(command, name, key)
}
}
pub enum MenuItem<E: Engine, S: Handle<E>, C: Command<S>> {
/// Unused.
__(PhantomData<E>, PhantomData<S>),
/// A separator. Skip it.
Separator,
/// A menu item with command, description and hotkey.
Command(C, &'static str, &'static str)
}