-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from fwcd/slider
Add slider
- Loading branch information
Showing
10 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#![feature(type_alias_impl_trait, impl_trait_in_assoc_type)] | ||
|
||
use nuit::{prelude::*, Circle, Font, FontDesign, FontSize, Frame, HStack, Slider, Text, VStack, Vec2}; | ||
|
||
#[derive(Bind, Default)] | ||
struct SlidersView { | ||
position: State<Vec2<f64>>, | ||
} | ||
|
||
impl View for SlidersView { | ||
type Body = impl View; | ||
|
||
#[allow(clippy::cast_possible_truncation)] | ||
fn body(&self) -> Self::Body { | ||
let position = self.position.clone(); | ||
let width = 400.0; | ||
let height = 300.0; | ||
let slider_width = 100.0; | ||
|
||
VStack::from(( | ||
Circle::new() | ||
.frame(10) | ||
.offset(position.get()) | ||
.frame((width, height)), | ||
|
||
HStack::from(( | ||
Text::new(format!("X: {:>4}", position.get().x as i32)), | ||
Slider::with_default_step( | ||
position.project(|p| &mut p.x), | ||
-(width / 2.0)..=(width / 2.0) | ||
) | ||
.frame(Frame::with_width(slider_width)), | ||
|
||
Text::new(format!("Y: {:>4}", position.get().y as i32)), | ||
Slider::with_default_step( | ||
position.project(|p| &mut p.y), | ||
-(height / 2.0)..=(height / 2.0) | ||
) | ||
.frame(Frame::with_width(slider_width)), | ||
) | ||
.font(Font::system(FontSize::BODY, FontDesign::Monospaced, None))), | ||
)) | ||
} | ||
} | ||
|
||
fn main() { | ||
nuit::run_app(SlidersView::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod button; | ||
mod picker; | ||
mod slider; | ||
mod text_field; | ||
mod text; | ||
|
||
pub use button::*; | ||
pub use picker::*; | ||
pub use slider::*; | ||
pub use text_field::*; | ||
pub use text::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::ops::RangeInclusive; | ||
|
||
use nuit_derive::Bind; | ||
|
||
use crate::{Access, Binding, Context, Event, IdPath, Node, View}; | ||
|
||
/// A control for selecting numeric values from a bounded range. | ||
#[derive(Debug, Clone, Bind)] | ||
pub struct Slider { | ||
value: Binding<f64>, | ||
range: RangeInclusive<f64>, | ||
step: Option<f64>, | ||
} | ||
|
||
impl Slider { | ||
#[must_use] | ||
pub const fn new(value: Binding<f64>, range: RangeInclusive<f64>, step: Option<f64>) -> Self { | ||
Self { value, range, step } | ||
} | ||
|
||
#[must_use] | ||
pub const fn with_default_step(value: Binding<f64>, range: RangeInclusive<f64>) -> Self { | ||
Self { value, range, step: None } | ||
} | ||
} | ||
|
||
impl View for Slider { | ||
fn fire(&self, event: &Event, event_path: &IdPath, _context: &Context) { | ||
assert!(event_path.is_root()); | ||
if let Event::UpdateSliderValue { value } = event { | ||
self.value.set(*value); | ||
} | ||
} | ||
|
||
fn render(&self, _context: &Context) -> Node { | ||
Node::Slider { | ||
value: self.value.get(), | ||
lower_bound: *self.range.start(), | ||
upper_bound: *self.range.end(), | ||
step: self.step, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters