From 0573fcec779e090a2a18cdceb93ecc4372cd00dd Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 20 Feb 2023 16:59:17 +0100 Subject: [PATCH] Only provide WidthDb in [Async]Widget::size --- src/widget.rs | 6 ++-- src/widgets/background.rs | 10 +++---- src/widgets/border.rs | 10 +++---- src/widgets/cursor.rs | 10 +++---- src/widgets/editor.rs | 6 ++-- src/widgets/either.rs | 10 +++---- src/widgets/empty.rs | 6 ++-- src/widgets/float.rs | 14 ++++----- src/widgets/join.rs | 60 +++++++++++++++++++++++---------------- src/widgets/layer.rs | 14 ++++----- src/widgets/padding.rs | 10 +++---- src/widgets/resize.rs | 10 +++---- src/widgets/text.rs | 8 +++--- 13 files changed, 91 insertions(+), 83 deletions(-) diff --git a/src/widget.rs b/src/widget.rs index 5d3faa4..dd4859b 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -3,14 +3,14 @@ use async_trait::async_trait; use crate::widgets::{ Background, Border, Either2, Either3, Float, JoinSegment, Layer, Padding, Resize, }; -use crate::{Frame, Size}; +use crate::{Frame, Size, WidthDb}; // TODO Feature-gate these traits pub trait Widget { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result; @@ -22,7 +22,7 @@ pub trait Widget { pub trait AsyncWidget { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result; diff --git a/src/widgets/background.rs b/src/widgets/background.rs index d72add6..d0ba530 100644 --- a/src/widgets/background.rs +++ b/src/widgets/background.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct Background { @@ -37,11 +37,11 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height) + self.inner.size(widthdb, max_width, max_height) } fn draw(self, frame: &mut Frame) -> Result<(), E> { @@ -57,11 +57,11 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height).await + self.inner.size(widthdb, max_width, max_height).await } async fn draw(self, frame: &mut Frame) -> Result<(), E> { diff --git a/src/widgets/border.rs b/src/widgets/border.rs index 3ee2b4a..062cd8f 100644 --- a/src/widgets/border.rs +++ b/src/widgets/border.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct BorderLook { @@ -151,13 +151,13 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { let max_width = max_width.map(|w| w.saturating_sub(2)); let max_height = max_height.map(|h| h.saturating_sub(2)); - let size = self.inner.size(frame, max_width, max_height)?; + let size = self.inner.size(widthdb, max_width, max_height)?; Ok(size + Size::new(2, 2)) } @@ -179,13 +179,13 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { let max_width = max_width.map(|w| w.saturating_sub(2)); let max_height = max_height.map(|h| h.saturating_sub(2)); - let size = self.inner.size(frame, max_width, max_height).await?; + let size = self.inner.size(widthdb, max_width, max_height).await?; Ok(size + Size::new(2, 2)) } diff --git a/src/widgets/cursor.rs b/src/widgets/cursor.rs index 47dca56..2bb8199 100644 --- a/src/widgets/cursor.rs +++ b/src/widgets/cursor.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct Cursor { @@ -32,11 +32,11 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height) + self.inner.size(widthdb, max_width, max_height) } fn draw(self, frame: &mut Frame) -> Result<(), E> { @@ -53,11 +53,11 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height).await + self.inner.size(widthdb, max_width, max_height).await } async fn draw(self, frame: &mut Frame) -> Result<(), E> { diff --git a/src/widgets/editor.rs b/src/widgets/editor.rs index e587a0a..ff95329 100644 --- a/src/widgets/editor.rs +++ b/src/widgets/editor.rs @@ -480,11 +480,10 @@ impl Editor<'_> { impl Widget for Editor<'_> { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, _max_height: Option, ) -> Result { - let widthdb = frame.widthdb(); let indices = self.indices(widthdb, max_width); let rows = self.rows(&indices); Ok(Self::size(widthdb, &rows)) @@ -506,11 +505,10 @@ impl Widget for Editor<'_> { impl AsyncWidget for Editor<'_> { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, _max_height: Option, ) -> Result { - let widthdb = frame.widthdb(); let indices = self.indices(widthdb, max_width); let rows = self.rows(&indices); Ok(Self::size(widthdb, &rows)) diff --git a/src/widgets/either.rs b/src/widgets/either.rs index ea74da4..cb9a55d 100644 --- a/src/widgets/either.rs +++ b/src/widgets/either.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Size, Widget}; +use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; macro_rules! mk_either { ( @@ -19,12 +19,12 @@ macro_rules! mk_either { { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { match self { - $( Self::$constr(w) => w.size(frame, max_width, max_height), )+ + $( Self::$constr(w) => w.size(widthdb, max_width, max_height), )+ } } @@ -42,12 +42,12 @@ macro_rules! mk_either { { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { match self { - $( Self::$constr(w) => w.size(frame, max_width, max_height).await, )+ + $( Self::$constr(w) => w.size(widthdb, max_width, max_height).await, )+ } } diff --git a/src/widgets/empty.rs b/src/widgets/empty.rs index fe6cfad..033ab70 100644 --- a/src/widgets/empty.rs +++ b/src/widgets/empty.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Size, Widget}; +use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; #[derive(Debug, Default, Clone, Copy)] pub struct Empty { @@ -31,7 +31,7 @@ impl Empty { impl Widget for Empty { fn size( &self, - _frame: &mut Frame, + _widthdb: &mut WidthDb, _max_width: Option, _max_height: Option, ) -> Result { @@ -47,7 +47,7 @@ impl Widget for Empty { impl AsyncWidget for Empty { async fn size( &self, - _frame: &mut Frame, + _widthdb: &mut WidthDb, _max_width: Option, _max_height: Option, ) -> Result { diff --git a/src/widgets/float.rs b/src/widgets/float.rs index b22c108..8380538 100644 --- a/src/widgets/float.rs +++ b/src/widgets/float.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct Float { @@ -115,18 +115,18 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height) + self.inner.size(widthdb, max_width, max_height) } fn draw(self, frame: &mut Frame) -> Result<(), E> { let size = frame.size(); let inner_size = self .inner - .size(frame, Some(size.width), Some(size.height))?; + .size(frame.widthdb(), Some(size.width), Some(size.height))?; self.push_inner(frame, size, inner_size); self.inner.draw(frame)?; @@ -143,18 +143,18 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.inner.size(frame, max_width, max_height).await + self.inner.size(widthdb, max_width, max_height).await } async fn draw(self, frame: &mut Frame) -> Result<(), E> { let size = frame.size(); let inner_size = self .inner - .size(frame, Some(size.width), Some(size.height)) + .size(frame.widthdb(), Some(size.width), Some(size.height)) .await?; self.push_inner(frame, size, inner_size); diff --git a/src/widgets/join.rs b/src/widgets/join.rs index fad2549..0778700 100644 --- a/src/widgets/join.rs +++ b/src/widgets/join.rs @@ -2,7 +2,7 @@ use std::cmp::Ordering; use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Widget, WidthDb}; use super::{Either2, Either3, Either4, Either5, Either6, Either7}; @@ -300,14 +300,14 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { if let Some(max_width) = max_width { let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, Some(max_width), max_height)?; + let size = segment.inner.size(widthdb, Some(max_width), max_height)?; balanced_segments.push(Segment::horizontal(size, segment)); } balance(&mut balanced_segments, max_width); @@ -315,7 +315,9 @@ where let mut width = 0_u16; let mut height = 0_u16; for (segment, balanced) in self.segments.iter().zip(balanced_segments.into_iter()) { - let size = segment.inner.size(frame, Some(balanced.size), max_height)?; + let size = segment + .inner + .size(widthdb, Some(balanced.size), max_height)?; width = width.saturating_add(size.width); height = height.max(size.height); } @@ -324,7 +326,7 @@ where let mut width = 0_u16; let mut height = 0_u16; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height)?; + let size = segment.inner.size(widthdb, max_width, max_height)?; width = width.saturating_add(size.width); height = height.max(size.height); } @@ -339,7 +341,7 @@ where let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height)?; + let size = segment.inner.size(frame.widthdb(), max_width, max_height)?; balanced_segments.push(Segment::horizontal(size, segment)); } balance(&mut balanced_segments, size.width); @@ -363,7 +365,7 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { @@ -372,7 +374,7 @@ where for segment in &self.segments { let size = segment .inner - .size(frame, Some(max_width), max_height) + .size(widthdb, Some(max_width), max_height) .await?; balanced_segments.push(Segment::horizontal(size, segment)); } @@ -383,7 +385,7 @@ where for (segment, balanced) in self.segments.iter().zip(balanced_segments.into_iter()) { let size = segment .inner - .size(frame, Some(balanced.size), max_height) + .size(widthdb, Some(balanced.size), max_height) .await?; width = width.saturating_add(size.width); height = height.max(size.height); @@ -393,7 +395,7 @@ where let mut width = 0_u16; let mut height = 0_u16; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height).await?; + let size = segment.inner.size(widthdb, max_width, max_height).await?; width = width.saturating_add(size.width); height = height.max(size.height); } @@ -408,7 +410,10 @@ where let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height).await?; + let size = segment + .inner + .size(frame.widthdb(), max_width, max_height) + .await?; balanced_segments.push(Segment::horizontal(size, segment)); } balance(&mut balanced_segments, size.width); @@ -441,14 +446,14 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { if let Some(max_height) = max_height { let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, Some(max_height))?; + let size = segment.inner.size(widthdb, max_width, Some(max_height))?; balanced_segments.push(Segment::vertical(size, segment)); } balance(&mut balanced_segments, max_height); @@ -456,7 +461,9 @@ where let mut width = 0_u16; let mut height = 0_u16; for (segment, balanced) in self.segments.iter().zip(balanced_segments.into_iter()) { - let size = segment.inner.size(frame, max_width, Some(balanced.size))?; + let size = segment + .inner + .size(widthdb, max_width, Some(balanced.size))?; width = width.max(size.width); height = height.saturating_add(size.height); } @@ -465,7 +472,7 @@ where let mut width = 0_u16; let mut height = 0_u16; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height)?; + let size = segment.inner.size(widthdb, max_width, max_height)?; width = width.max(size.width); height = height.saturating_add(size.height); } @@ -480,7 +487,7 @@ where let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height)?; + let size = segment.inner.size(frame.widthdb(), max_width, max_height)?; balanced_segments.push(Segment::vertical(size, segment)); } balance(&mut balanced_segments, size.height); @@ -504,7 +511,7 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { @@ -513,7 +520,7 @@ where for segment in &self.segments { let size = segment .inner - .size(frame, max_width, Some(max_height)) + .size(widthdb, max_width, Some(max_height)) .await?; balanced_segments.push(Segment::vertical(size, segment)); } @@ -524,7 +531,7 @@ where for (segment, balanced) in self.segments.iter().zip(balanced_segments.into_iter()) { let size = segment .inner - .size(frame, max_width, Some(balanced.size)) + .size(widthdb, max_width, Some(balanced.size)) .await?; width = width.max(size.width); height = height.saturating_add(size.height); @@ -534,7 +541,7 @@ where let mut width = 0_u16; let mut height = 0_u16; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height).await?; + let size = segment.inner.size(widthdb, max_width, max_height).await?; width = width.max(size.width); height = height.saturating_add(size.height); } @@ -549,7 +556,10 @@ where let mut balanced_segments = vec![]; for segment in &self.segments { - let size = segment.inner.size(frame, max_width, max_height).await?; + let size = segment + .inner + .size(frame.widthdb(), max_width, max_height) + .await?; balanced_segments.push(Segment::vertical(size, segment)); } balance(&mut balanced_segments, size.height); @@ -593,11 +603,11 @@ macro_rules! mk_join { { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.0.size(frame, max_width, max_height) + self.0.size(widthdb, max_width, max_height) } fn draw(self, frame: &mut Frame) -> Result<(), E> { @@ -612,11 +622,11 @@ macro_rules! mk_join { { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - self.0.size(frame, max_width, max_height).await + self.0.size(widthdb, max_width, max_height).await } async fn draw(self, frame: &mut Frame) -> Result<(), E> { diff --git a/src/widgets/layer.rs b/src/widgets/layer.rs index f8da993..366f2b5 100644 --- a/src/widgets/layer.rs +++ b/src/widgets/layer.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Size, Widget}; +use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct Layer { @@ -25,12 +25,12 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - let bottom = self.below.size(frame, max_width, max_height)?; - let top = self.above.size(frame, max_width, max_height)?; + let bottom = self.below.size(widthdb, max_width, max_height)?; + let top = self.above.size(widthdb, max_width, max_height)?; Ok(Self::size(bottom, top)) } @@ -49,12 +49,12 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - let bottom = self.below.size(frame, max_width, max_height).await?; - let top = self.above.size(frame, max_width, max_height).await?; + let bottom = self.below.size(widthdb, max_width, max_height).await?; + let top = self.above.size(widthdb, max_width, max_height).await?; Ok(Self::size(bottom, top)) } diff --git a/src/widgets/padding.rs b/src/widgets/padding.rs index 656aec8..473ad02 100644 --- a/src/widgets/padding.rs +++ b/src/widgets/padding.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Pos, Size, Widget}; +use crate::{AsyncWidget, Frame, Pos, Size, Widget, WidthDb}; #[derive(Debug, Clone, Copy)] pub struct Padding { @@ -72,14 +72,14 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { let pad_size = self.pad_size(); let max_width = max_width.map(|w| w.saturating_sub(pad_size.width)); let max_height = max_height.map(|h| h.saturating_sub(pad_size.height)); - let size = self.inner.size(frame, max_width, max_height)?; + let size = self.inner.size(widthdb, max_width, max_height)?; Ok(size + pad_size) } @@ -98,14 +98,14 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { let pad_size = self.pad_size(); let max_width = max_width.map(|w| w.saturating_sub(pad_size.width)); let max_height = max_height.map(|h| h.saturating_sub(pad_size.height)); - let size = self.inner.size(frame, max_width, max_height).await?; + let size = self.inner.size(widthdb, max_width, max_height).await?; Ok(size + pad_size) } diff --git a/src/widgets/resize.rs b/src/widgets/resize.rs index 5adad8a..91bb84a 100644 --- a/src/widgets/resize.rs +++ b/src/widgets/resize.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::{AsyncWidget, Frame, Size, Widget}; +use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; pub struct Resize { pub inner: I, @@ -69,11 +69,11 @@ where { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - let size = self.inner.size(frame, max_width, max_height)?; + let size = self.inner.size(widthdb, max_width, max_height)?; Ok(self.resize(size)) } @@ -89,11 +89,11 @@ where { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, max_height: Option, ) -> Result { - let size = self.inner.size(frame, max_width, max_height).await?; + let size = self.inner.size(widthdb, max_width, max_height).await?; Ok(self.resize(size)) } diff --git a/src/widgets/text.rs b/src/widgets/text.rs index 5acdb8e..3755c8c 100644 --- a/src/widgets/text.rs +++ b/src/widgets/text.rs @@ -62,11 +62,11 @@ impl Text { impl Widget for Text { fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, _max_height: Option, ) -> Result { - Ok(self.size(frame.widthdb(), max_width)) + Ok(self.size(widthdb, max_width)) } fn draw(self, frame: &mut Frame) -> Result<(), E> { @@ -79,11 +79,11 @@ impl Widget for Text { impl AsyncWidget for Text { async fn size( &self, - frame: &mut Frame, + widthdb: &mut WidthDb, max_width: Option, _max_height: Option, ) -> Result { - Ok(self.size(frame.widthdb(), max_width)) + Ok(self.size(widthdb, max_width)) } async fn draw(self, frame: &mut Frame) -> Result<(), E> {