From 4a4bc5de1a010a2d4934deebbaa1ce4d4c94c8a1 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Tue, 14 Nov 2023 11:44:32 -0800 Subject: [PATCH] Added slidable enum demo Also moved into_button to MakeWidget --- examples/animation.rs | 2 +- examples/basic-button.rs | 2 +- examples/containers.rs | 2 +- examples/counter.rs | 2 +- examples/slider.rs | 48 ++++++++++++++++++++++++++++++++---- examples/stack-align-test.rs | 1 - examples/switcher.rs | 2 +- src/value.rs | 7 +----- src/widget.rs | 9 ++++++- src/window.rs | 12 +-------- 10 files changed, 58 insertions(+), 29 deletions(-) diff --git a/examples/animation.rs b/examples/animation.rs index 4e7a1a2..a7eb101 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -1,7 +1,7 @@ use std::time::Duration; use gooey::animation::{AnimationHandle, AnimationTarget, IntoAnimate, Spawn}; -use gooey::value::{Dynamic, StringValue}; +use gooey::value::Dynamic; use gooey::widget::MakeWidget; use gooey::{Run, WithClone}; diff --git a/examples/basic-button.rs b/examples/basic-button.rs index a78f524..34ba73c 100644 --- a/examples/basic-button.rs +++ b/examples/basic-button.rs @@ -1,4 +1,4 @@ -use gooey::value::{Dynamic, StringValue}; +use gooey::value::Dynamic; use gooey::widget::MakeWidget; use gooey::Run; diff --git a/examples/containers.rs b/examples/containers.rs index 98216fd..f67e098 100644 --- a/examples/containers.rs +++ b/examples/containers.rs @@ -1,4 +1,4 @@ -use gooey::value::{Dynamic, StringValue}; +use gooey::value::Dynamic; use gooey::widget::{MakeWidget, WidgetInstance}; use gooey::window::ThemeMode; use gooey::Run; diff --git a/examples/counter.rs b/examples/counter.rs index 99bffea..8f9fabd 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -1,6 +1,6 @@ use std::string::ToString; -use gooey::value::{Dynamic, StringValue}; +use gooey::value::Dynamic; use gooey::widget::MakeWidget; use gooey::Run; use kludgine::figures::units::Lp; diff --git a/examples/slider.rs b/examples/slider.rs index 5672c9f..2c6d832 100644 --- a/examples/slider.rs +++ b/examples/slider.rs @@ -1,10 +1,23 @@ +use gooey::animation::{LinearInterpolate, PercentBetween}; use gooey::value::{Dynamic, StringValue}; use gooey::widget::MakeWidget; use gooey::widgets::slider::Slidable; use gooey::Run; use kludgine::figures::units::Lp; +use kludgine::figures::Ranged; fn main() -> gooey::Result { + u8_slider() + .and(enum_slider()) + .into_rows() + .expand_horizontally() + .width(..Lp::points(800)) + .centered() + .expand() + .run() +} + +fn u8_slider() -> impl MakeWidget { let min_text = Dynamic::new(u8::MIN.to_string()); let min = min_text.map_each(|min| min.parse().unwrap_or(u8::MIN)); let max_text = Dynamic::new(u8::MAX.to_string()); @@ -21,9 +34,34 @@ fn main() -> gooey::Result { .and(value.slider_between(min, max)) .and(value_text.centered()) .into_rows() - .expand_horizontally() - .width(..Lp::points(800)) - .centered() - .expand() - .run() +} + +#[derive(LinearInterpolate, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] +enum SlidableEnum { + A, + B, + C, +} + +impl PercentBetween for SlidableEnum { + fn percent_between(&self, min: &Self, max: &Self) -> gooey::animation::ZeroToOne { + let min = *min as u8; + let max = *max as u8; + let value = *self as u8; + value.percent_between(&min, &max) + } +} + +impl Ranged for SlidableEnum { + const MAX: Self = Self::C; + const MIN: Self = Self::A; +} + +fn enum_slider() -> impl MakeWidget { + let enum_value = Dynamic::new(SlidableEnum::A); + let enum_text = enum_value.map_each(|value| format!("{value:?}")); + "Custom Enum" + .and(enum_value.slider()) + .and(enum_text) + .into_rows() } diff --git a/examples/stack-align-test.rs b/examples/stack-align-test.rs index fa27f5d..f7453a2 100644 --- a/examples/stack-align-test.rs +++ b/examples/stack-align-test.rs @@ -1,4 +1,3 @@ -use gooey::value::StringValue; use gooey::widget::MakeWidget; use gooey::Run; diff --git a/examples/switcher.rs b/examples/switcher.rs index d34a7bc..f0e970b 100644 --- a/examples/switcher.rs +++ b/examples/switcher.rs @@ -1,4 +1,4 @@ -use gooey::value::{Dynamic, StringValue}; +use gooey::value::Dynamic; use gooey::widget::{MakeWidget, WidgetInstance}; use gooey::widgets::Switcher; use gooey::Run; diff --git a/src/value.rs b/src/value.rs index cf8f484..930ee32 100644 --- a/src/value.rs +++ b/src/value.rs @@ -16,7 +16,7 @@ use crate::animation::{DynamicTransition, LinearInterpolate}; use crate::context::{WidgetContext, WindowHandle}; use crate::utils::{IgnorePoison, WithClone}; use crate::widget::WidgetId; -use crate::widgets::{Button, Input}; +use crate::widgets::Input; /// An instance of a value that provides APIs to observe and react to its /// contents. @@ -1259,11 +1259,6 @@ pub trait StringValue: IntoValue + Sized { fn into_input(self) -> Input { Input::new(self.into_value()) } - - /// Returns this string as a clickable button. - fn into_button(self) -> Button { - Button::new(self.into_value()) - } } impl StringValue for T where T: IntoValue {} diff --git a/src/widget.rs b/src/widget.rs index 577b64f..45cb12d 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -24,7 +24,9 @@ use crate::styles::{ use crate::tree::Tree; use crate::utils::IgnorePoison; use crate::value::{IntoValue, Value}; -use crate::widgets::{Align, Container, Expand, Resize, Scroll, Stack, Style, Themed, ThemedMode}; +use crate::widgets::{ + Align, Button, Container, Expand, Resize, Scroll, Stack, Style, Themed, ThemedMode, +}; use crate::window::{RunningWindow, ThemeMode, Window, WindowBehavior}; use crate::{ConstraintLimit, Run}; @@ -643,6 +645,11 @@ pub trait MakeWidget: Sized { Resize::from_height(height, self) } + /// Returns this string as a clickable button. + fn into_button(self) -> Button { + Button::new(self) + } + /// Aligns `self` to the center vertically and horizontally. #[must_use] fn centered(self) -> Align { diff --git a/src/window.rs b/src/window.rs index 64f4aa1..8a0173f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1093,7 +1093,7 @@ pub(crate) mod sealed { } /// Controls whether the light or dark theme is applied. -#[derive(Default, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Default, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, LinearInterpolate)] pub enum ThemeMode { /// Applies the light theme Light, @@ -1144,16 +1144,6 @@ impl From for window::Theme { } } -impl LinearInterpolate for ThemeMode { - fn lerp(&self, target: &Self, percent: f32) -> Self { - if percent >= 0.5 { - *target - } else { - *self - } - } -} - impl PercentBetween for ThemeMode { fn percent_between(&self, min: &Self, max: &Self) -> ZeroToOne { if *min == *max || *self == *min {