From c2e2ee52e85112fd33b1baecfc33f3791e9d7d98 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 21 Jul 2022 14:38:23 +0200 Subject: [PATCH] Add Border widget --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/ui/widgets.rs | 1 + src/ui/widgets/border.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/ui/widgets/border.rs diff --git a/Cargo.lock b/Cargo.lock index fa89f66..6e2c475 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1241,7 +1241,7 @@ dependencies = [ [[package]] name = "toss" version = "0.1.0" -source = "git+https://github.com/Garmelon/toss.git?rev=14aedaf25212cd50924566821ad37645a4cacf28#14aedaf25212cd50924566821ad37645a4cacf28" +source = "git+https://github.com/Garmelon/toss.git?rev=53b2728c827c9f93a743d5860c1b31cb1399875b#53b2728c827c9f93a743d5860c1b31cb1399875b" dependencies = [ "crossterm", "unicode-linebreak", diff --git a/Cargo.toml b/Cargo.toml index 6517aef..5abd4a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,4 +30,4 @@ features = ["rustls-tls-native-roots"] [dependencies.toss] git = "https://github.com/Garmelon/toss.git" -rev = "14aedaf25212cd50924566821ad37645a4cacf28" +rev = "53b2728c827c9f93a743d5860c1b31cb1399875b" diff --git a/src/ui/widgets.rs b/src/ui/widgets.rs index 75fc727..1b93931 100644 --- a/src/ui/widgets.rs +++ b/src/ui/widgets.rs @@ -1,4 +1,5 @@ pub mod background; +pub mod border; pub mod empty; pub mod join; pub mod list; diff --git a/src/ui/widgets/border.rs b/src/ui/widgets/border.rs new file mode 100644 index 0000000..cb18090 --- /dev/null +++ b/src/ui/widgets/border.rs @@ -0,0 +1,49 @@ +use async_trait::async_trait; +use toss::frame::{Frame, Pos, Size}; + +use super::{BoxedWidget, Widget}; + +pub struct Border(BoxedWidget); + +impl Border { + pub fn new>(inner: W) -> Self { + Self(inner.into()) + } +} + +#[async_trait] +impl Widget for Border { + fn size(&self, frame: &mut Frame, max_width: Option, max_height: Option) -> Size { + 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.0.size(frame, max_width, max_height); + size + Size::new(2, 2) + } + + async fn render(self: Box, frame: &mut Frame) { + let mut size = frame.size(); + size.width = size.width.max(2); + size.height = size.height.max(2); + + let right = size.width as i32 - 1; + let bottom = size.height as i32 - 1; + frame.write(Pos::new(0, 0), "┌"); + frame.write(Pos::new(right, 0), "┐"); + frame.write(Pos::new(0, bottom), "└"); + frame.write(Pos::new(right, bottom), "┘"); + + for y in 1..bottom { + frame.write(Pos::new(0, y), "│"); + frame.write(Pos::new(right, y), "│"); + } + + for x in 1..right { + frame.write(Pos::new(x, 0), "─"); + frame.write(Pos::new(x, bottom), "─"); + } + + frame.push(Pos::new(1, 1), size - Size::new(2, 2)); + self.0.render(frame).await; + frame.pop(); + } +}