diff --git a/src/widgets/editor.rs b/src/widgets/editor.rs index 5a7da70..693a69f 100644 --- a/src/widgets/editor.rs +++ b/src/widgets/editor.rs @@ -1,10 +1,9 @@ use std::iter; -use async_trait::async_trait; use crossterm::style::Stylize; 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 /// with a newline. @@ -424,24 +423,6 @@ impl Editor<'_> { text.clone().split_at_indices(indices) } - fn size_impl(&self, widthdb: &mut WidthDb, max_width: Option) -> 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 { if self.hidden.is_some() { return Pos::new(0, 0); @@ -457,8 +438,33 @@ impl Editor<'_> { let cursor_col: i32 = cursor_col.try_into().unwrap_or(i32::MAX); Pos::new(cursor_col, cursor_row) } +} - fn draw_impl(&mut self, frame: &mut Frame) { +impl Widget for Editor<'_> { + fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + _max_height: Option, + ) -> Result { + 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 indices = self.indices(frame.widthdb(), Some(size.width)); let rows = self.rows(&indices); @@ -472,39 +478,7 @@ impl Editor<'_> { frame.set_cursor(Some(cursor)); } self.state.last_cursor_pos = cursor; - } -} -impl Widget for Editor<'_> { - fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - _max_height: Option, - ) -> Result { - 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 AsyncWidget for Editor<'_> { - async fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - _max_height: Option, - ) -> Result { - Ok(self.size_impl(widthdb, max_width)) - } - - async fn draw(mut self, frame: &mut Frame) -> Result<(), E> { - self.draw_impl(frame); Ok(()) } } diff --git a/src/widgets/empty.rs b/src/widgets/empty.rs index 033ab70..5de4fdf 100644 --- a/src/widgets/empty.rs +++ b/src/widgets/empty.rs @@ -1,6 +1,4 @@ -use async_trait::async_trait; - -use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; +use crate::{Frame, Size, Widget, WidthDb}; #[derive(Debug, Default, Clone, Copy)] pub struct Empty { @@ -42,19 +40,3 @@ impl Widget for Empty { Ok(()) } } - -#[async_trait] -impl AsyncWidget for Empty { - async fn size( - &self, - _widthdb: &mut WidthDb, - _max_width: Option, - _max_height: Option, - ) -> Result { - Ok(self.size) - } - - async fn draw(self, _frame: &mut Frame) -> Result<(), E> { - Ok(()) - } -} diff --git a/src/widgets/predrawn.rs b/src/widgets/predrawn.rs index a1f7672..8301f1e 100644 --- a/src/widgets/predrawn.rs +++ b/src/widgets/predrawn.rs @@ -1,7 +1,5 @@ use std::mem; -use async_trait::async_trait; - use crate::buffer::Buffer; use crate::{AsyncWidget, Frame, Pos, Size, Style, Styled, Widget, WidthDb}; @@ -45,21 +43,6 @@ impl Predrawn { pub fn size(&self) -> 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 Widget for Predrawn { @@ -73,24 +56,19 @@ impl Widget for Predrawn { } fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.draw_impl(frame); - Ok(()) - } -} - -#[async_trait] -impl AsyncWidget for Predrawn { - async fn size( - &self, - _widthdb: &mut WidthDb, - _max_width: Option, - _max_height: Option, - ) -> Result { - Ok(self.buffer.size()) - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.draw_impl(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)); + } + Ok(()) } } diff --git a/src/widgets/text.rs b/src/widgets/text.rs index 3755c8c..007f4fe 100644 --- a/src/widgets/text.rs +++ b/src/widgets/text.rs @@ -1,6 +1,4 @@ -use async_trait::async_trait; - -use crate::{AsyncWidget, Frame, Pos, Size, Styled, Widget, WidthDb}; +use crate::{Frame, Pos, Size, Styled, Widget, WidthDb}; #[derive(Debug, Clone)] pub struct Text { @@ -30,8 +28,15 @@ impl Text { let indices = widthdb.wrap(self.styled.text(), max_width); self.styled.clone().split_at_indices(&indices) } +} - fn size(&self, widthdb: &mut WidthDb, max_width: Option) -> Size { +impl Widget for Text { + fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + _max_height: Option, + ) -> Result { let lines = self.wrapped(widthdb, max_width); let min_width = lines @@ -43,11 +48,12 @@ impl Text { let min_width: u16 = min_width.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(); + for (i, line) in self .wrapped(frame.widthdb(), Some(size.width)) .into_iter() @@ -56,38 +62,7 @@ impl Text { let i: i32 = i.try_into().unwrap_or(i32::MAX); frame.write(Pos::new(0, i), line); } - } -} -impl Widget for Text { - fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - _max_height: Option, - ) -> Result { - Ok(self.size(widthdb, max_width)) - } - - fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.draw(frame); - Ok(()) - } -} - -#[async_trait] -impl AsyncWidget for Text { - async fn size( - &self, - widthdb: &mut WidthDb, - max_width: Option, - _max_height: Option, - ) -> Result { - Ok(self.size(widthdb, max_width)) - } - - async fn draw(self, frame: &mut Frame) -> Result<(), E> { - self.draw(frame); Ok(()) } }