From 4094ba3e3d5068f54892282e9bbc51f7acceacf7 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 20 Aug 2022 23:17:42 +0200 Subject: [PATCH] Add popup widget builder --- src/ui/widgets.rs | 1 + src/ui/widgets/popup.rs | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/ui/widgets/popup.rs diff --git a/src/ui/widgets.rs b/src/ui/widgets.rs index 960cf8e..e98f62b 100644 --- a/src/ui/widgets.rs +++ b/src/ui/widgets.rs @@ -12,6 +12,7 @@ pub mod join; pub mod layer; pub mod list; pub mod padding; +pub mod popup; pub mod resize; pub mod rules; pub mod text; diff --git a/src/ui/widgets/popup.rs b/src/ui/widgets/popup.rs new file mode 100644 index 0000000..a319130 --- /dev/null +++ b/src/ui/widgets/popup.rs @@ -0,0 +1,76 @@ +use crossterm::style::ContentStyle; +use toss::styled::Styled; + +use super::background::Background; +use super::border::Border; +use super::float::Float; +use super::layer::Layer; +use super::padding::Padding; +use super::text::Text; +use super::BoxedWidget; + +pub struct Popup { + inner: BoxedWidget, + inner_padding: bool, + title: Option, + border_style: ContentStyle, + bg_style: ContentStyle, +} + +impl Popup { + pub fn new>(inner: W) -> Self { + Self { + inner: inner.into(), + inner_padding: true, + title: None, + border_style: ContentStyle::default(), + bg_style: ContentStyle::default(), + } + } + + pub fn inner_padding(mut self, active: bool) -> Self { + self.inner_padding = active; + self + } + + pub fn title>(mut self, title: S) -> Self { + self.title = Some(title.into()); + self + } + + pub fn border(mut self, style: ContentStyle) -> Self { + self.border_style = style; + self + } + + pub fn background(mut self, style: ContentStyle) -> Self { + self.bg_style = style; + self + } + + pub fn build(self) -> BoxedWidget { + let inner = if self.inner_padding { + Padding::new(self.inner).horizontal(1).into() + } else { + self.inner + }; + let window = + Border::new(Background::new(inner).style(self.bg_style)).style(self.border_style); + + let widget: BoxedWidget = if let Some(title) = self.title { + let title = Float::new( + Padding::new( + Background::new(Padding::new(Text::new(title)).horizontal(1)) + .style(self.border_style), + ) + .horizontal(2), + ) + .horizontal(0.5); + Layer::new(vec![window.into(), title.into()]).into() + } else { + window.into() + }; + + Float::new(widget).vertical(0.5).horizontal(0.5).into() + } +}