Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a VerticalSlider widget. #1596

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/slider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "slider"
version = "0.1.0"
authors = ["Casper Rogild Storm<casper@rogildstorm.com>"]
edition = "2021"
publish = false

[dependencies]
iced = { path = "../.." }
14 changes: 14 additions & 0 deletions examples/slider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Slider

A `Slider` is a bar and a handle that selects a single value from a range of values.
There exists both `Slider` and `VerticalSlider` depending on which orientation you need.

<div align="center">
<img src="sliders.gif">
</div>

You can run it with `cargo run`:

```
cargo run --package slider
```
Binary file added examples/slider/sliders.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions examples/slider/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use iced::widget::{column, container, slider, text, vertical_slider};
use iced::{Element, Length, Sandbox, Settings};

pub fn main() -> iced::Result {
Slider::run(Settings::default())
}

#[derive(Debug, Clone)]
pub enum Message {
SliderChanged(u8),
}

pub struct Slider {
slider_value: u8,
}

impl Sandbox for Slider {
type Message = Message;

fn new() -> Slider {
Slider { slider_value: 50 }
}

fn title(&self) -> String {
String::from("Slider - Iced")
}

fn update(&mut self, message: Message) {
match message {
Message::SliderChanged(value) => {
self.slider_value = value;
}
}
}

fn view(&self) -> Element<Message> {
let value = self.slider_value;

let h_slider =
container(slider(0..=100, value, Message::SliderChanged))
.width(Length::Units(250));

let v_slider =
container(vertical_slider(0..=100, value, Message::SliderChanged))
.height(Length::Units(200));

let text = text(format!("{value}"));

container(
column![
container(v_slider).width(Length::Fill).center_x(),
container(h_slider).width(Length::Fill).center_x(),
container(text).width(Length::Fill).center_x(),
]
.spacing(25),
)
.height(Length::Fill)
.width(Length::Fill)
.center_x()
.center_y()
.into()
}
}
3 changes: 3 additions & 0 deletions native/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod text_input;
pub mod toggler;
pub mod tooltip;
pub mod tree;
pub mod vertical_slider;

mod action;
mod id;
Expand Down Expand Up @@ -79,6 +80,8 @@ pub use toggler::Toggler;
pub use tooltip::Tooltip;
#[doc(no_inline)]
pub use tree::Tree;
#[doc(no_inline)]
pub use vertical_slider::VerticalSlider;

pub use action::Action;
pub use id::Id;
Expand Down
17 changes: 17 additions & 0 deletions native/src/widget/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ where
widget::Slider::new(range, value, on_change)
}

/// Creates a new [`VerticalSlider`].
///
/// [`VerticalSlider`]: widget::VerticalSlider
pub fn vertical_slider<'a, T, Message, Renderer>(
range: std::ops::RangeInclusive<T>,
value: T,
on_change: impl Fn(T) -> Message + 'a,
) -> widget::VerticalSlider<'a, T, Message, Renderer>
where
T: Copy + From<u8> + std::cmp::PartialOrd,
Message: Clone,
Renderer: crate::Renderer,
Renderer::Theme: widget::slider::StyleSheet,
{
widget::VerticalSlider::new(range, value, on_change)
}

/// Creates a new [`PickList`].
///
/// [`PickList`]: widget::PickList
Expand Down
Loading