From 8b1e0f1ec2cd95986e0a09b6ec74ebc3891829c2 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Mon, 13 Nov 2023 17:44:30 -0800 Subject: [PATCH] Introduce viewport widget --- crates/yakui-widgets/src/widgets/mod.rs | 2 + crates/yakui-widgets/src/widgets/viewport.rs | 44 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 crates/yakui-widgets/src/widgets/viewport.rs diff --git a/crates/yakui-widgets/src/widgets/mod.rs b/crates/yakui-widgets/src/widgets/mod.rs index f359ea13..a6759927 100644 --- a/crates/yakui-widgets/src/widgets/mod.rs +++ b/crates/yakui-widgets/src/widgets/mod.rs @@ -27,6 +27,7 @@ mod state; mod text; mod textbox; mod unconstrained_box; +mod viewport; mod window; pub use self::align::*; @@ -58,4 +59,5 @@ pub use self::state::*; pub use self::text::*; pub use self::textbox::*; pub use self::unconstrained_box::*; +pub use self::viewport::*; pub use self::window::*; diff --git a/crates/yakui-widgets/src/widgets/viewport.rs b/crates/yakui-widgets/src/widgets/viewport.rs new file mode 100644 index 00000000..fda9d2c7 --- /dev/null +++ b/crates/yakui-widgets/src/widgets/viewport.rs @@ -0,0 +1,44 @@ +use yakui_core::{ + geometry::{Constraints, FlexFit, Vec2}, + widget::{LayoutContext, Widget}, + Response, +}; + +/// Occupies the largest possible space, draws nothing, and consumes no inputs +/// +/// Capture `Viewport.show().id`, then look it up in the final `LayoutDom` to find its +/// dimensions. Useful identifying a viewport for non-GUI rendering. +#[derive(Debug, Default)] +pub struct Viewport; + +impl Viewport { + pub fn show(&self) -> Response { + crate::util::widget::(()) + } +} + +impl Widget for Viewport { + type Props = (); + type Response = (); + + fn new() -> Self { + Self + } + + fn update(&mut self, (): ()) {} + + fn layout(&self, _: LayoutContext<'_>, constraints: Constraints) -> Vec2 { + constraints + .max + .as_ref() + .map(|x| match x.is_infinite() { + true => 0.0, + false => x, + }) + .into() + } + + fn flex(&self) -> (u32, FlexFit) { + (1, FlexFit::Tight) + } +}