Remove AsyncWidget impls replaceable by Desync

This commit is contained in:
Joscha 2023-04-17 19:39:14 +02:00
parent 4179e7f56c
commit 968dbe501f
4 changed files with 53 additions and 144 deletions

View file

@ -1,10 +1,9 @@
use std::iter; use std::iter;
use async_trait::async_trait;
use crossterm::style::Stylize; use crossterm::style::Stylize;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use crate::{AsyncWidget, Frame, Pos, Size, Style, Styled, Widget, WidthDb}; use crate::{Frame, Pos, Size, Style, Styled, Widget, WidthDb};
/// Like [`WidthDb::wrap`] but includes a final break index if the text ends /// Like [`WidthDb::wrap`] but includes a final break index if the text ends
/// with a newline. /// with a newline.
@ -424,24 +423,6 @@ impl Editor<'_> {
text.clone().split_at_indices(indices) text.clone().split_at_indices(indices)
} }
fn size_impl(&self, widthdb: &mut WidthDb, max_width: Option<u16>) -> Size {
let indices = self.indices(widthdb, max_width);
let rows = self.rows(&indices);
let width = rows
.iter()
.map(|row| widthdb.width(row.text()))
.max()
.unwrap_or(0)
// One extra column for cursor
.saturating_add(1);
let height = rows.len();
let width: u16 = width.try_into().unwrap_or(u16::MAX);
let height: u16 = height.try_into().unwrap_or(u16::MAX);
Size::new(width, height)
}
fn cursor(&self, widthdb: &mut WidthDb, width: u16, indices: &[usize], rows: &[Styled]) -> Pos { fn cursor(&self, widthdb: &mut WidthDb, width: u16, indices: &[usize], rows: &[Styled]) -> Pos {
if self.hidden.is_some() { if self.hidden.is_some() {
return Pos::new(0, 0); return Pos::new(0, 0);
@ -457,8 +438,33 @@ impl Editor<'_> {
let cursor_col: i32 = cursor_col.try_into().unwrap_or(i32::MAX); let cursor_col: i32 = cursor_col.try_into().unwrap_or(i32::MAX);
Pos::new(cursor_col, cursor_row) Pos::new(cursor_col, cursor_row)
} }
}
fn draw_impl(&mut self, frame: &mut Frame) { impl<E> Widget<E> for Editor<'_> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
let indices = self.indices(widthdb, max_width);
let rows = self.rows(&indices);
let width = rows
.iter()
.map(|row| widthdb.width(row.text()))
.max()
.unwrap_or(0)
// One extra column for cursor
.saturating_add(1);
let height = rows.len();
let width: u16 = width.try_into().unwrap_or(u16::MAX);
let height: u16 = height.try_into().unwrap_or(u16::MAX);
Ok(Size::new(width, height))
}
fn draw(mut self, frame: &mut Frame) -> Result<(), E> {
let size = frame.size(); let size = frame.size();
let indices = self.indices(frame.widthdb(), Some(size.width)); let indices = self.indices(frame.widthdb(), Some(size.width));
let rows = self.rows(&indices); let rows = self.rows(&indices);
@ -472,39 +478,7 @@ impl Editor<'_> {
frame.set_cursor(Some(cursor)); frame.set_cursor(Some(cursor));
} }
self.state.last_cursor_pos = cursor; self.state.last_cursor_pos = cursor;
}
}
impl<E> Widget<E> for Editor<'_> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size_impl(widthdb, max_width))
}
fn draw(mut self, frame: &mut Frame) -> Result<(), E> {
self.draw_impl(frame);
Ok(())
}
}
#[allow(single_use_lifetimes)]
#[async_trait]
impl<E> AsyncWidget<E> for Editor<'_> {
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size_impl(widthdb, max_width))
}
async fn draw(mut self, frame: &mut Frame) -> Result<(), E> {
self.draw_impl(frame);
Ok(()) Ok(())
} }
} }

View file

@ -1,6 +1,4 @@
use async_trait::async_trait; use crate::{Frame, Size, Widget, WidthDb};
use crate::{AsyncWidget, Frame, Size, Widget, WidthDb};
#[derive(Debug, Default, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub struct Empty { pub struct Empty {
@ -42,19 +40,3 @@ impl<E> Widget<E> for Empty {
Ok(()) Ok(())
} }
} }
#[async_trait]
impl<E> AsyncWidget<E> for Empty {
async fn size(
&self,
_widthdb: &mut WidthDb,
_max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size)
}
async fn draw(self, _frame: &mut Frame) -> Result<(), E> {
Ok(())
}
}

View file

@ -1,7 +1,5 @@
use std::mem; use std::mem;
use async_trait::async_trait;
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::{AsyncWidget, Frame, Pos, Size, Style, Styled, Widget, WidthDb}; use crate::{AsyncWidget, Frame, Pos, Size, Style, Styled, Widget, WidthDb};
@ -45,21 +43,6 @@ impl Predrawn {
pub fn size(&self) -> Size { pub fn size(&self) -> Size {
self.buffer.size() self.buffer.size()
} }
fn draw_impl(&self, frame: &mut Frame) {
for (x, y, cell) in self.buffer.cells() {
let pos = Pos::new(x.into(), y.into());
let style = Style {
content_style: cell.style,
opaque: true,
};
frame.write(pos, Styled::new(&cell.content, style));
}
if let Some(cursor) = self.buffer.cursor() {
frame.set_cursor(Some(cursor));
}
}
} }
impl<E> Widget<E> for Predrawn { impl<E> Widget<E> for Predrawn {
@ -73,24 +56,19 @@ impl<E> Widget<E> for Predrawn {
} }
fn draw(self, frame: &mut Frame) -> Result<(), E> { fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.draw_impl(frame); for (x, y, cell) in self.buffer.cells() {
Ok(()) let pos = Pos::new(x.into(), y.into());
} let style = Style {
} content_style: cell.style,
opaque: true,
#[async_trait] };
impl<E> AsyncWidget<E> for Predrawn { frame.write(pos, Styled::new(&cell.content, style));
async fn size( }
&self,
_widthdb: &mut WidthDb, if let Some(cursor) = self.buffer.cursor() {
_max_width: Option<u16>, frame.set_cursor(Some(cursor));
_max_height: Option<u16>, }
) -> Result<Size, E> {
Ok(self.buffer.size())
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.draw_impl(frame);
Ok(()) Ok(())
} }
} }

View file

@ -1,6 +1,4 @@
use async_trait::async_trait; use crate::{Frame, Pos, Size, Styled, Widget, WidthDb};
use crate::{AsyncWidget, Frame, Pos, Size, Styled, Widget, WidthDb};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Text { pub struct Text {
@ -30,8 +28,15 @@ impl Text {
let indices = widthdb.wrap(self.styled.text(), max_width); let indices = widthdb.wrap(self.styled.text(), max_width);
self.styled.clone().split_at_indices(&indices) self.styled.clone().split_at_indices(&indices)
} }
}
fn size(&self, widthdb: &mut WidthDb, max_width: Option<u16>) -> Size { impl<E> Widget<E> for Text {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
let lines = self.wrapped(widthdb, max_width); let lines = self.wrapped(widthdb, max_width);
let min_width = lines let min_width = lines
@ -43,11 +48,12 @@ impl Text {
let min_width: u16 = min_width.try_into().unwrap_or(u16::MAX); let min_width: u16 = min_width.try_into().unwrap_or(u16::MAX);
let min_height: u16 = min_height.try_into().unwrap_or(u16::MAX); let min_height: u16 = min_height.try_into().unwrap_or(u16::MAX);
Size::new(min_width, min_height) Ok(Size::new(min_width, min_height))
} }
fn draw(self, frame: &mut Frame) { fn draw(self, frame: &mut Frame) -> Result<(), E> {
let size = frame.size(); let size = frame.size();
for (i, line) in self for (i, line) in self
.wrapped(frame.widthdb(), Some(size.width)) .wrapped(frame.widthdb(), Some(size.width))
.into_iter() .into_iter()
@ -56,38 +62,7 @@ impl Text {
let i: i32 = i.try_into().unwrap_or(i32::MAX); let i: i32 = i.try_into().unwrap_or(i32::MAX);
frame.write(Pos::new(0, i), line); frame.write(Pos::new(0, i), line);
} }
}
}
impl<E> Widget<E> for Text {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size(widthdb, max_width))
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.draw(frame);
Ok(())
}
}
#[async_trait]
impl<E> AsyncWidget<E> for Text {
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size(widthdb, max_width))
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.draw(frame);
Ok(()) Ok(())
} }
} }