From 07960142e09659a973c1485e7ea0bed2e4773568 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 26 Feb 2023 22:06:24 +0100 Subject: [PATCH] Add Popup AsyncWidget --- src/ui.rs | 1 + src/ui/widgets2.rs | 3 +++ src/ui/widgets2/popup.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/ui/widgets2.rs create mode 100644 src/ui/widgets2/popup.rs diff --git a/src/ui.rs b/src/ui.rs index b5e8873..33696cc 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -4,6 +4,7 @@ mod input; mod rooms; mod util; mod widgets; +mod widgets2; use std::convert::Infallible; use std::io; diff --git a/src/ui/widgets2.rs b/src/ui/widgets2.rs new file mode 100644 index 0000000..a12a5aa --- /dev/null +++ b/src/ui/widgets2.rs @@ -0,0 +1,3 @@ +mod popup; + +pub use self::popup::*; diff --git a/src/ui/widgets2/popup.rs b/src/ui/widgets2/popup.rs new file mode 100644 index 0000000..d8dae47 --- /dev/null +++ b/src/ui/widgets2/popup.rs @@ -0,0 +1,54 @@ +use async_trait::async_trait; +use toss::widgets::{Background, Border, Float, Layer2, Padding, Text}; +use toss::{AsyncWidget, Frame, Size, Style, Styled, WidgetExt, WidthDb}; + +type Body = Background>>; +type Title = Float>>>; + +pub struct Popup(Float, Title>>); + +impl Popup { + pub fn new>(inner: I, title: S) -> Self { + let title = Text::new(title) + .padding() + .with_horizontal(1) + // The background displaces the border without affecting the style + .background() + .with_style(Style::new()) + .padding() + .with_horizontal(2) + .float() + .with_top() + .with_left(); + + let body = inner.padding().with_horizontal(1).border().background(); + + Self(title.above(body).float().with_center()) + } + + pub fn with_border_style(mut self, style: Style) -> Self { + let border = &mut self.0.inner.first.inner; + border.style = style; + self + } +} + +#[async_trait] +impl AsyncWidget for Popup +where + E: Send, + I: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.0.size(widthdb, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.0.draw(frame).await + } +}