From d449c61f2717e11480737e275dee882e7180fda9 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 18 Feb 2023 19:05:27 +0100 Subject: [PATCH] Add JoinH2, JoinH3, JoinV2, JoinV3 --- src/widgets/join.rs | 238 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/src/widgets/join.rs b/src/widgets/join.rs index 90156a2..ebd2733 100644 --- a/src/widgets/join.rs +++ b/src/widgets/join.rs @@ -4,6 +4,8 @@ use async_trait::async_trait; use crate::{AsyncWidget, Frame, Pos, Size, Widget}; +use super::{Either, Either3}; + // The following algorithm has three goals, listed in order of importance: // // 1. Use the available space @@ -508,3 +510,239 @@ where Ok(()) } } + +pub struct JoinH2(JoinH>); + +impl JoinH2 { + pub fn new(left: JoinSegment, right: JoinSegment) -> Self { + Self(JoinH::new(vec![ + JoinSegment { + inner: Either::First(left.inner), + weight: left.weight, + }, + JoinSegment { + inner: Either::Second(right.inner), + weight: right.weight, + }, + ])) + } +} + +impl Widget for JoinH2 +where + I1: Widget, + I2: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame) + } +} + +#[async_trait] +impl AsyncWidget for JoinH2 +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } +} + +pub struct JoinH3(JoinH>); + +impl JoinH3 { + pub fn new(left: JoinSegment, middle: JoinSegment, right: JoinSegment) -> Self { + Self(JoinH::new(vec![ + JoinSegment { + inner: Either3::First(left.inner), + weight: left.weight, + }, + JoinSegment { + inner: Either3::Second(middle.inner), + weight: middle.weight, + }, + JoinSegment { + inner: Either3::Third(right.inner), + weight: right.weight, + }, + ])) + } +} + +impl Widget for JoinH3 +where + I1: Widget, + I2: Widget, + I3: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame) + } +} + +#[async_trait] +impl AsyncWidget for JoinH3 +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, + I3: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } +} + +pub struct JoinV2(JoinV>); + +impl JoinV2 { + pub fn new(top: JoinSegment, bottom: JoinSegment) -> Self { + Self(JoinV::new(vec![ + JoinSegment { + inner: Either::First(top.inner), + weight: top.weight, + }, + JoinSegment { + inner: Either::Second(bottom.inner), + weight: bottom.weight, + }, + ])) + } +} + +impl Widget for JoinV2 +where + I1: Widget, + I2: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame) + } +} + +#[async_trait] +impl AsyncWidget for JoinV2 +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } +} + +pub struct JoinV3(JoinV>); + +impl JoinV3 { + pub fn new(top: JoinSegment, middle: JoinSegment, bottom: JoinSegment) -> Self { + Self(JoinV::new(vec![ + JoinSegment { + inner: Either3::First(top.inner), + weight: top.weight, + }, + JoinSegment { + inner: Either3::Second(middle.inner), + weight: middle.weight, + }, + JoinSegment { + inner: Either3::Third(bottom.inner), + weight: bottom.weight, + }, + ])) + } +} + +impl Widget for JoinV3 +where + I1: Widget, + I2: Widget, + I3: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame) + } +} + +#[async_trait] +impl AsyncWidget for JoinV3 +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, + I3: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } +}