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 1 commit
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ members = [
"examples/tour",
"examples/url_handler",
"examples/websocket",
"examples/slider",
]

[dependencies]
Expand Down
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 bar and a handle that selects a single value from a range of values.
Can be oriented both vertical and horizontal.

<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.
69 changes: 69 additions & 0 deletions examples/slider/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use iced::widget::{column, container, slider, text};
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> {
use slider::Orientation::{Horizontal, Vertical};

let value = self.slider_value;

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

let v_slider = container(
slider(0..=100, value, Message::SliderChanged)
.orientation(Vertical),
)
.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()
}
}
Loading