mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
use TuiInput in handlers
This commit is contained in:
parent
d9535b707f
commit
5d00e9f284
11 changed files with 37 additions and 61 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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}")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue