Only provide WidthDb in [Async]Widget::size

This commit is contained in:
Joscha 2023-02-20 16:59:17 +01:00
parent 417f33cc24
commit 0573fcec77
13 changed files with 91 additions and 83 deletions

View file

@ -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<E> {
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E>;
@ -22,7 +22,7 @@ pub trait Widget<E> {
pub trait AsyncWidget<E> {
async fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E>;

View file

@ -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<I> {
@ -37,11 +37,11 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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> {

View file

@ -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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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))
}

View file

@ -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<I> {
@ -32,11 +32,11 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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> {

View file

@ -480,11 +480,10 @@ impl Editor<'_> {
impl<E> Widget<E> for Editor<'_> {
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
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<E> Widget<E> for Editor<'_> {
impl<E> AsyncWidget<E> for Editor<'_> {
async fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
let widthdb = frame.widthdb();
let indices = self.indices(widthdb, max_width);
let rows = self.rows(&indices);
Ok(Self::size(widthdb, &rows))

View file

@ -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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
match self {
$( Self::$constr(w) => w.size(frame, max_width, max_height).await, )+
$( Self::$constr(w) => w.size(widthdb, max_width, max_height).await, )+
}
}

View file

@ -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<E> Widget<E> for Empty {
fn size(
&self,
_frame: &mut Frame,
_widthdb: &mut WidthDb,
_max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
@ -47,7 +47,7 @@ impl<E> Widget<E> for Empty {
impl<E> AsyncWidget<E> for Empty {
async fn size(
&self,
_frame: &mut Frame,
_widthdb: &mut WidthDb,
_max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {

View file

@ -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<I> {
@ -115,18 +115,18 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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);

View file

@ -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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
@ -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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
@ -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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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> {

View file

@ -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<I1, I2> {
@ -25,12 +25,12 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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))
}

View file

@ -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<I> {
@ -72,14 +72,14 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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)
}

View file

@ -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<I> {
pub inner: I,
@ -69,11 +69,11 @@ where
{
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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))
}

View file

@ -62,11 +62,11 @@ impl Text {
impl<E> Widget<E> for Text {
fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
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<E> Widget<E> for Text {
impl<E> AsyncWidget<E> for Text {
async fn size(
&self,
frame: &mut Frame,
widthdb: &mut WidthDb,
max_width: Option<u16>,
_max_height: Option<u16>,
) -> Result<Size, E> {
Ok(self.size(frame.widthdb(), max_width))
Ok(self.size(widthdb, max_width))
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {