use TuiInput in handlers

This commit is contained in:
🪞👃🪞 2024-09-15 16:11:26 +03:00
parent d9535b707f
commit 5d00e9f284
11 changed files with 37 additions and 61 deletions

View file

@ -23,7 +23,7 @@ pub trait Engine: Send + Sync + Sized {
pub trait Input<E: Engine> {
type Event;
fn event (&self)
-> Self::Event;
-> &Self::Event;
fn is_done (&self)
-> bool;
fn done (&self);
@ -427,35 +427,6 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
type Engine = E;
fn layout (&self, outer_area: E::Size) -> Perhaps<E::Size> {
self.inner().layout(outer_area)
//Ok(match self {
//Self::Center(_) => self.inner().layout(outer_area)?.map(|inner_area|{
//let [_, _, w, h] = inner_area.xywh();
//let offset_x = (outer_area.w() / 2.into()) - (w / 2.into());
//let offset_y = (outer_area.h() / 2.into()) - (h / 2.into());
//let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h];
//result.into()
//}),
//Self::X(_) => self.inner().layout(outer_area)?.map(|inner_area|{
//let [_, y, w, h] = inner_area.xywh();
//let offset_x = (outer_area.w() - w) / 2.into();
//let result = [outer_area.x() + offset_x, y, w, h];
//result.into(
//}),
//Self::Y(_) => self.inner().layout(outer_area)?.map(|inner_area|{
//let [x, _, w, h] = inner_area.xywh();
//let offset_y = (outer_area.h() / 2.into()) - (h / 2.into());
//let result = [x, outer_area.y() + offset_y, w, h];
//result.into()
//}),
//Self::NW(_) => { todo!() },
//Self::N(_) => { todo!() },
//Self::NE(_) => { todo!() },
//Self::W(_) => { todo!() },
//Self::E(_) => { todo!() },
//Self::SW(_) => { todo!() },
//Self::S(_) => { todo!() },
//Self::SE(_) => { todo!() },
//})
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
let outer_area = to.area();

View file

@ -24,7 +24,7 @@ impl Engine for Tui {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
type Input = Self;
type Input = TuiInput;
type Handled = bool;
type Output = Self;
fn exited (&self) -> bool {
@ -48,18 +48,6 @@ impl Engine for Tui {
disable_raw_mode().map_err(Into::into)
}
}
impl Input<Tui> for Tui {
type Event = TuiEvent;
fn event (&self) -> TuiEvent {
self.event.read().unwrap().clone().unwrap()
}
fn is_done (&self) -> bool {
self.exited.fetch_and(true, Ordering::Relaxed)
}
fn done (&self) {
self.exited.store(true, Ordering::Relaxed);
}
}
impl Output<Tui> for Tui {
#[inline] fn area (&self) -> <Self as Engine>::Area {
self.area
@ -78,6 +66,22 @@ impl Output<Tui> for Tui {
Ok(next)
}
}
pub struct TuiInput {
event: TuiEvent,
exited: Arc<AtomicBool>,
}
impl Input<Tui> for TuiInput {
type Event = TuiEvent;
fn event (&self) -> &TuiEvent {
&self.event
}
fn is_done (&self) -> bool {
self.exited.fetch_and(true, Ordering::Relaxed)
}
fn done (&self) {
self.exited.store(true, Ordering::Relaxed);
}
}
impl Tui {
/// Run the main loop.
@ -106,7 +110,6 @@ impl Tui {
engine: &Arc<RwLock<Self>>, state: &Arc<RwLock<R>>, poll: Duration
) -> JoinHandle<()> {
let exited = engine.read().unwrap().exited.clone();
let engine = engine.clone();
let state = state.clone();
spawn(move || loop {
if exited.fetch_and(true, Ordering::Relaxed) {
@ -119,8 +122,10 @@ impl Tui {
exited.store(true, Ordering::Relaxed);
},
_ => {
*engine.write().unwrap().event.write().unwrap() = Some(event);
if let Err(e) = state.write().unwrap().handle(&*engine.read().unwrap()) {
if let Err(e) = state.write().unwrap().handle(&TuiInput {
event,
exited: exited.clone()
}) {
panic!("{e}")
}
}

View file

@ -1,7 +1,7 @@
use crate::*;
impl Handle<Tui> for Mixer<Tui> {
fn handle (&mut self, engine: &Tui) -> Perhaps<bool> {
fn handle (&mut self, engine: &TuiInput) -> Perhaps<bool> {
if let TuiEvent::Input(crossterm::event::Event::Key(event)) = engine.event() {
match event.code {

View file

@ -1,7 +1,7 @@
use crate::*;
impl Handle<Tui> for Plugin<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Up) => {
self.selected = self.selected.saturating_sub(1);

View file

@ -72,7 +72,7 @@ impl Widget for AddSampleModal {
}
impl Handle<Tui> for AddSampleModal {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if handle_keymap(self, &from.event(), KEYMAP_ADD_SAMPLE)? {
return Ok(Some(true))
}

View file

@ -1,6 +1,6 @@
use crate::*;
impl Handle<Tui> for Sampler {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Up) => {
self.cursor.0 = if self.cursor.0 == 0 {

View file

@ -1,7 +1,7 @@
use crate::*;
impl Handle<Tui> for Track<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
//, NONE, "chain_cursor_up", "move cursor up", || {
key!(KeyCode::Up) => {

View file

@ -219,7 +219,7 @@ impl ArrangerFocus {
}
}
impl Handle<Tui> for Arranger<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(modal) = self.modal.as_mut() {
let result = modal.handle(from)?;
if modal.exited() {
@ -893,7 +893,7 @@ impl Widget for ArrangerRenameModal<Tui> {
}
impl Handle<Tui> for ArrangerRenameModal<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
TuiEvent::Input(Event::Key(k)) => {
match k.code {

View file

@ -93,7 +93,7 @@ impl Content for ArrangerStandalone<Tui> {
}
impl Handle<Tui> for ArrangerStandalone<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {

View file

@ -641,7 +641,7 @@ impl<'a> Widget for SequenceTimer<'a> {
}
impl Handle<Tui> for Sequencer<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
// NONE, "seq_cursor_up", "move cursor up", |sequencer: &mut Sequencer| {
key!(KeyCode::Up) => {

View file

@ -187,7 +187,7 @@ impl<E: Engine> Audio for TransportToolbar<E> {
}
}
impl Handle<Tui> for TransportToolbar<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Left) => {
self.focus_prev();
@ -249,7 +249,7 @@ impl Focusable<Tui> for TransportPlayPauseButton<Tui> {
}
}
impl Handle<Tui> for TransportPlayPauseButton<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Enter) => self.toggle().map(|_|Some(true)),
_ => Ok(None)
@ -293,7 +293,7 @@ impl Focusable<Tui> for TransportBPM<Tui> {
}
}
impl Handle<Tui> for TransportBPM<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Char(',')) => {
self.value += 1.0;
@ -355,7 +355,7 @@ impl Focusable<Tui> for TransportQuantize<Tui> {
}
}
impl Handle<Tui> for TransportQuantize<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Char(',')) => {
self.value = prev_note_length(self.value);
@ -408,7 +408,7 @@ impl Focusable<Tui> for TransportSync<Tui> {
}
}
impl Handle<Tui> for TransportSync<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() {
key!(KeyCode::Char(',')) => {
self.value = prev_note_length(self.value);
@ -464,7 +464,7 @@ impl Focusable<Tui> for TransportClock<Tui> {
}
}
impl Handle<Tui> for TransportClock<Tui> {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
Ok(None)
}
}