mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-04-04 05:10:44 +02:00
lanes and grids
This commit is contained in:
parent
788dc1ccde
commit
ac865824cc
12 changed files with 213 additions and 112 deletions
|
|
@ -74,7 +74,7 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
|
|||
terminal.draw(|frame|{
|
||||
let area = frame.size();
|
||||
let buffer = frame.buffer_mut();
|
||||
device.lock().unwrap().render(buffer, area)
|
||||
device.lock().unwrap().render(buffer, area).expect("Failed to render content.");
|
||||
}).expect("Failed to render frame");
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
|
|
@ -90,15 +90,19 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
|
|||
}
|
||||
|
||||
pub trait Device: Send + Sync {
|
||||
fn handle (&mut self, _event: &EngineEvent) -> Result<(), Box<dyn Error>> { Ok(()) }
|
||||
fn render (&self, _buffer: &mut Buffer, _area: Rect) {}
|
||||
fn handle (&mut self, _event: &EngineEvent) -> Usually<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn render (&self, _buffer: &mut Buffer, _area: Rect) -> Usually<(u16, u16)> {
|
||||
Ok((0,0))
|
||||
}
|
||||
fn process (&mut self, _client: Client, _scope: ProcessScope) {}
|
||||
}
|
||||
|
||||
pub struct DynamicDevice<T> {
|
||||
pub state: Mutex<T>,
|
||||
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect) + Send>>,
|
||||
pub handle: Mutex<Box<dyn FnMut(&mut T, &EngineEvent)->Result<(), Box<dyn Error>> + Send>>,
|
||||
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<(u16, u16)> + Send>>,
|
||||
pub handle: Mutex<Box<dyn FnMut(&mut T, &EngineEvent)->Usually<()> + Send>>,
|
||||
pub process: Mutex<Box<dyn FnMut(&mut T) + Send>>
|
||||
}
|
||||
|
||||
|
|
@ -109,7 +113,7 @@ impl<T> DynamicDevice<T> {
|
|||
process: P,
|
||||
state: T
|
||||
) -> Self where
|
||||
R: FnMut(&T, &mut Buffer, Rect) + Send + 'static,
|
||||
R: FnMut(&T, &mut Buffer, Rect)->Usually<(u16, u16)> + Send + 'static,
|
||||
H: FnMut(&mut T, &EngineEvent) -> Result<(), Box<dyn Error>> + Send + 'static,
|
||||
P: FnMut(&mut T) + Send + 'static
|
||||
{
|
||||
|
|
@ -126,23 +130,23 @@ impl<T> DynamicDevice<T> {
|
|||
}
|
||||
|
||||
impl<T: Send + Sync> Device for DynamicDevice<T> {
|
||||
fn handle (&mut self, event: &EngineEvent) -> Result<(), Box<dyn Error>> {
|
||||
fn handle (&mut self, event: &EngineEvent) -> Usually<()> {
|
||||
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
|
||||
}
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<(u16, u16)> {
|
||||
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for &dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Device::render(*self, buf, area)
|
||||
Device::render(*self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Device::render(self, buf, area)
|
||||
Device::render(self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue