Added slidable enum demo

Also moved into_button to MakeWidget
This commit is contained in:
Jonathan Johnson 2023-11-14 11:44:32 -08:00
parent 64584c4b14
commit 4a4bc5de1a
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
10 changed files with 58 additions and 29 deletions

View file

@ -1,7 +1,7 @@
use std::time::Duration; use std::time::Duration;
use gooey::animation::{AnimationHandle, AnimationTarget, IntoAnimate, Spawn}; use gooey::animation::{AnimationHandle, AnimationTarget, IntoAnimate, Spawn};
use gooey::value::{Dynamic, StringValue}; use gooey::value::Dynamic;
use gooey::widget::MakeWidget; use gooey::widget::MakeWidget;
use gooey::{Run, WithClone}; use gooey::{Run, WithClone};

View file

@ -1,4 +1,4 @@
use gooey::value::{Dynamic, StringValue}; use gooey::value::Dynamic;
use gooey::widget::MakeWidget; use gooey::widget::MakeWidget;
use gooey::Run; use gooey::Run;

View file

@ -1,4 +1,4 @@
use gooey::value::{Dynamic, StringValue}; use gooey::value::Dynamic;
use gooey::widget::{MakeWidget, WidgetInstance}; use gooey::widget::{MakeWidget, WidgetInstance};
use gooey::window::ThemeMode; use gooey::window::ThemeMode;
use gooey::Run; use gooey::Run;

View file

@ -1,6 +1,6 @@
use std::string::ToString; use std::string::ToString;
use gooey::value::{Dynamic, StringValue}; use gooey::value::Dynamic;
use gooey::widget::MakeWidget; use gooey::widget::MakeWidget;
use gooey::Run; use gooey::Run;
use kludgine::figures::units::Lp; use kludgine::figures::units::Lp;

View file

@ -1,10 +1,23 @@
use gooey::animation::{LinearInterpolate, PercentBetween};
use gooey::value::{Dynamic, StringValue}; use gooey::value::{Dynamic, StringValue};
use gooey::widget::MakeWidget; use gooey::widget::MakeWidget;
use gooey::widgets::slider::Slidable; use gooey::widgets::slider::Slidable;
use gooey::Run; use gooey::Run;
use kludgine::figures::units::Lp; use kludgine::figures::units::Lp;
use kludgine::figures::Ranged;
fn main() -> gooey::Result { 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_text = Dynamic::new(u8::MIN.to_string());
let min = min_text.map_each(|min| min.parse().unwrap_or(u8::MIN)); let min = min_text.map_each(|min| min.parse().unwrap_or(u8::MIN));
let max_text = Dynamic::new(u8::MAX.to_string()); 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.slider_between(min, max))
.and(value_text.centered()) .and(value_text.centered())
.into_rows() .into_rows()
.expand_horizontally() }
.width(..Lp::points(800))
.centered() #[derive(LinearInterpolate, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
.expand() enum SlidableEnum {
.run() 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()
} }

View file

@ -1,4 +1,3 @@
use gooey::value::StringValue;
use gooey::widget::MakeWidget; use gooey::widget::MakeWidget;
use gooey::Run; use gooey::Run;

View file

@ -1,4 +1,4 @@
use gooey::value::{Dynamic, StringValue}; use gooey::value::Dynamic;
use gooey::widget::{MakeWidget, WidgetInstance}; use gooey::widget::{MakeWidget, WidgetInstance};
use gooey::widgets::Switcher; use gooey::widgets::Switcher;
use gooey::Run; use gooey::Run;

View file

@ -16,7 +16,7 @@ use crate::animation::{DynamicTransition, LinearInterpolate};
use crate::context::{WidgetContext, WindowHandle}; use crate::context::{WidgetContext, WindowHandle};
use crate::utils::{IgnorePoison, WithClone}; use crate::utils::{IgnorePoison, WithClone};
use crate::widget::WidgetId; 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 /// An instance of a value that provides APIs to observe and react to its
/// contents. /// contents.
@ -1259,11 +1259,6 @@ pub trait StringValue: IntoValue<String> + Sized {
fn into_input(self) -> Input { fn into_input(self) -> Input {
Input::new(self.into_value()) Input::new(self.into_value())
} }
/// Returns this string as a clickable button.
fn into_button(self) -> Button {
Button::new(self.into_value())
}
} }
impl<T> StringValue for T where T: IntoValue<String> {} impl<T> StringValue for T where T: IntoValue<String> {}

View file

@ -24,7 +24,9 @@ use crate::styles::{
use crate::tree::Tree; use crate::tree::Tree;
use crate::utils::IgnorePoison; use crate::utils::IgnorePoison;
use crate::value::{IntoValue, Value}; 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::window::{RunningWindow, ThemeMode, Window, WindowBehavior};
use crate::{ConstraintLimit, Run}; use crate::{ConstraintLimit, Run};
@ -643,6 +645,11 @@ pub trait MakeWidget: Sized {
Resize::from_height(height, self) 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. /// Aligns `self` to the center vertically and horizontally.
#[must_use] #[must_use]
fn centered(self) -> Align { fn centered(self) -> Align {

View file

@ -1093,7 +1093,7 @@ pub(crate) mod sealed {
} }
/// Controls whether the light or dark theme is applied. /// 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 { pub enum ThemeMode {
/// Applies the light theme /// Applies the light theme
Light, Light,
@ -1144,16 +1144,6 @@ impl From<ThemeMode> 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 { impl PercentBetween for ThemeMode {
fn percent_between(&self, min: &Self, max: &Self) -> ZeroToOne { fn percent_between(&self, min: &Self, max: &Self) -> ZeroToOne {
if *min == *max || *self == *min { if *min == *max || *self == *min {