wip: update core for generic Render

This commit is contained in:
🪞👃🪞 2024-09-03 18:13:10 +03:00
parent fcd2d16de9
commit bf165c6be1
5 changed files with 68 additions and 62 deletions

View file

@ -144,7 +144,9 @@ impl Jack {
})
}
pub fn run <D, T, U> (self, state: impl FnOnce(JackPorts)->Box<D>) -> Usually<JackDevice<T, U>>
where D: Device<T, U> + Process + Sized + 'static
where D: Device<T, U> + Process + Sized + 'static,
T: 'static,
U: 'static
{
let owned_ports = JackPorts {
audio_ins: register_ports(&self.client, self.audio_ins, AudioIn)?,

View file

@ -59,3 +59,14 @@ impl<R, T, U> Render<T, U> for RwLock<R> where R: Render<T, U> {
self.read().unwrap().render(to)
}
}
/// Boxed closures can be rendered.
///
/// Rendering unboxed closures should also be possible;
/// but in practice implementing the trait for an unboxed
/// `Fn` closure causes an impl conflict.
impl<'a, T, U> Render<T, U> for Box<dyn Fn(&mut T) -> Perhaps<U> + Send + Sync + 'a> {
fn render (&self, to: &mut T) -> Perhaps<U> {
(*self)(to)
}
}

View file

@ -28,11 +28,11 @@ pub trait BorderStyle {
Ok(area)
}
#[inline]
fn draw_north (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Usually<Rect> {
fn draw_north (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Perhaps<Rect> {
Self::N.blit(buf, x, y, style)
}
#[inline]
fn draw_south (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Usually<Rect> {
fn draw_south (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) -> Perhaps<Rect> {
Self::S.blit(buf, x, y, style)
}

View file

@ -60,34 +60,37 @@ impl<'a> Render<TuiOutput<'a>, Rect> for Split<'a, TuiOutput<'a>, Rect> {
}
}
impl<'a> Split<'a, TuiOutput<'a>, Rect> {
pub fn render_areas (&self, to: &mut TuiOutput<'a>) -> Usually<(Rect, Vec<Rect>)> {
let Rect { mut x, mut y, mut width, mut height } = to.area;
let mut areas = vec![];
let buffer = &mut to.buffer;
for (index, item) in self.items.0.iter().enumerate() {
if width == 0 || height == 0 {
break
}
let target = TuiOutput { buffer, area: Rect { x, y, width, height } };
if let Some(result) = item.render(&mut target)? {
match self.direction {
Direction::Down => {
y += result.height;
height = height.saturating_sub(result.height);
},
Direction::Right => {
x += result.width;
width = width.saturating_sub(result.width);
},
_ => unimplemented!()
};
areas.push(result);
if self.focus == Some(index) {
Corners(Style::default().green().not_dim()).draw(buffer, result)?;
}
}
}
Ok((to.area, areas))
impl<'a, 'b: 'a> Split<'a, TuiOutput<'a>, Rect> {
pub fn render_areas (&'a self, to: &mut TuiOutput<'a>) -> Usually<(Rect, Vec<Rect>)> {
Ok((Rect::default(), vec![]))
//let Rect { mut x, mut y, mut width, mut height } = to.area;
//let TuiOutput { buffer, area } = to;
//let mut areas = vec![];
//for (index, item) in self.items.0.iter().enumerate() {
//if width == 0 || height == 0 {
//break
//}
//if let Some(result) = item.render(&mut TuiOutput {
//buffer: to.buffer,
//area: Rect { x, y, width, height }
//})? {
//match self.direction {
//Direction::Down => {
//y += result.height;
//height = height.saturating_sub(result.height);
//},
//Direction::Right => {
//x += result.width;
//width = width.saturating_sub(result.width);
//},
//_ => unimplemented!()
//};
//areas.push(result);
//if self.focus == Some(index) {
//Corners(Style::default().green().not_dim()).draw(to.buffer, result)?;
//}
//}
//}
//Ok((to.area, areas))
}
}

View file

@ -8,29 +8,30 @@ pub struct TuiOutput<'a> {
}
/// Main thread render loop
pub fn tui_render_thread <'a, T: Render<TuiOutput<'a>, Rect> + 'static> (
pub fn tui_render_thread <'a: 'static, T> (
exited: &Arc<AtomicBool>, device: &Arc<RwLock<T>>,
) -> Usually<JoinHandle<()>> {
) -> Usually<JoinHandle<()>> where
T: Render<TuiOutput<'a>, Rect> + 'static
{
let exited = exited.clone();
let device = device.clone();
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?;
let sleep = Duration::from_millis(20);
Ok(spawn(move || loop {
if let Ok(device) = device.try_read() {
terminal.draw(|frame|{
let area = frame.size();
let buffer = frame.buffer_mut();
let mut output = TuiOutput { buffer, area };
device.render(&mut output).expect("Failed to render content");
})
.expect("Failed to render frame");
Ok(spawn(move || {
let draw = |frame: &'a mut ratatui::Frame|{
let area = frame.size();
let buffer = frame.buffer_mut();
device.render(&mut TuiOutput { buffer, area }).expect("Failed to render content");
};
loop {
if let Ok(_) = device.try_read() {
terminal.draw(draw).expect("Failed to render frame");
}
if exited.fetch_and(true, Ordering::Relaxed) {
break
}
std::thread::sleep(sleep);
}
if exited.fetch_and(true, Ordering::Relaxed) {
break
}
std::thread::sleep(sleep);
}))
}
@ -76,19 +77,8 @@ impl<T: AsRef<str>> Blit for T {
/// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates.
impl<'a> Render<TuiOutput<'a>, Rect> for () {
fn render (&self, (_, area): (&mut Buffer, Rect)) -> Perhaps<Rect> {
Ok(Some(Rect { x: area.x, y: area.y, width: 0, height: 0 }))
}
}
/// Boxed closures can be rendered.
///
/// Rendering unboxed closures should also be possible;
/// but in practice implementing the trait for an unboxed
/// `Fn` closure causes an impl conflict.
impl<'a, T, U> Render<T, U> for Box<dyn Fn(T) -> Usually<U> + Send + Sync + 'a> {
fn render (&self, to: T) -> Usually<U> {
(*self)(to)
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
Ok(Some(Rect { x: to.area.x, y: to.area.y, width: 0, height: 0 }))
}
}