Skip to content

Commit

Permalink
add slider bar
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Nov 20, 2023
1 parent 529199c commit 5f56c7e
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 59 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'slidebar'",
"cargo": {
"args": [
"build",
"--bin=slidebar",
"--package=slidebar"
],
"filter": {
"name": "slidebar",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ default = [
"spinner",
"cupertino",
"segmented_button",
"num-traits"
]

[dependencies]
num-traits = { version = "0.2.16", optional = true }
num-traits = {version = "0.2.16", optional = true}
time = { version = "0.3.23", features = ["local-offset"], optional = true }
chrono = { version = "0.4.26", optional = true }
once_cell = { version = "1.18.0", optional = true }
itertools = { version = "0.11.0", optional = true }


[dependencies.iced_widget]
git = "https://github.com/iced-rs/iced.git"
rev = "b474a2b7a763dcde6a377cb409001a7b5285ee8d"
Expand Down Expand Up @@ -110,6 +112,7 @@ members = [
"examples/context_menu",
#"examples/WidgetIDReturn",
"examples/segmented_button",
"examples/sliderbar",
]

[workspace.dependencies.iced]
Expand All @@ -121,3 +124,4 @@ features = ["advanced", "lazy", "tokio"]
[workspace.dependencies.iced_aw]
path = "./"
default-features = false
features = ["num-traits"]
11 changes: 11 additions & 0 deletions examples/sliderbar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "slidebar"
version = "0.1.0"
authors = ["Andrew Wheeler <[email protected]>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced_aw.workspace = true
iced.workspace = true
57 changes: 57 additions & 0 deletions examples/sliderbar/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use iced::{
widget::{Column, Container, Text},
Element, Length, Sandbox, Settings,
};

use iced_aw::SlideBar;

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

#[derive(Debug, Clone)]
enum Message {
SliderBarChange(u32),
}

struct SlideBarExample {
value: u32,
}

impl Sandbox for SlideBarExample {
type Message = Message;

fn new() -> Self {
SlideBarExample { value: 1 }
}

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

fn update(&mut self, message: Message) {
let Message::SliderBarChange(v) = message;
self.value = v;
}

fn view(&self) -> Element<Message> {
let bar = SlideBar::new(0..=100, self.value, Message::SliderBarChange).width(100.0);

let content_all = Column::new()
.spacing(10)
.push(

Check failure on line 42 in examples/sliderbar/src/main.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced_aw/iced_aw/examples/sliderbar/src/main.rs
Text::new(format!("Value is {}", self.value))
.width(Length::Fill)
.vertical_alignment(iced::alignment::Vertical::Center).horizontal_alignment(iced::alignment::Horizontal::Center),
)
.push(bar)
.align_items(iced::Alignment::Center);

Container::new(content_all)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ mod platform {
#[cfg(feature = "spinner")]
pub use {crate::native::spinner, crate::style::SpinnerStyle, spinner::Spinner};

pub use crate::native::SlideBar;

#[doc(no_inline)]
#[cfg(feature = "context_menu")]
pub use {
Expand Down
62 changes: 62 additions & 0 deletions src/native/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Common types for reuse.
//!
use iced_widget::core::{Padding, Rectangle};

/// Methods for creating inner bounds
#[allow(missing_debug_implementations)]
pub enum InnerBounds {
/// Create inner bounds ratio to the outer bounds
Ratio(f32, f32),
/// Create inner bounds by padding the outer bounds
Padding(Padding),
/// Create square inner bounds
Square(f32),
/// Create inner bounds with a custom function
Custom(Box<dyn Fn(Rectangle) -> Rectangle>),
}
impl InnerBounds {
/// Gets the inner bounds of the Set type.
pub fn get_bounds(&self, outer_bounds: Rectangle) -> Rectangle {

Check failure on line 20 in src/native/common.rs

View workflow job for this annotation

GitHub Actions / all

this method could have a `#[must_use]` attribute
use InnerBounds::{Custom, Padding, Ratio, Square};
match self {
Ratio(w, h) => {
let width = w * outer_bounds.width;
let height = h * outer_bounds.height;
let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
Rectangle {
x,
y,
width,
height,
}
}
Padding(p) => {
let x = outer_bounds.x + p.left;
let y = outer_bounds.y + p.top;
let width = outer_bounds.width - p.horizontal();
let height = outer_bounds.width - p.vertical();
Rectangle {
x,
y,
width,
height,
}
}
Square(l) => {
let width = *l;
let height = *l;
let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
Rectangle {
x,
y,
width,
height,
}
}
Custom(f) => f(outer_bounds),
}
}
}
8 changes: 8 additions & 0 deletions src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub use helpers::*;

pub mod overlay;

pub mod common;

pub use common::InnerBounds;

#[cfg(feature = "badge")]
pub mod badge;
#[cfg(feature = "badge")]
Expand Down Expand Up @@ -133,3 +137,7 @@ pub mod segmented_button;
/// A badge for color highlighting small information.
pub type SegmentedButton<'a, Message, Renderer> =
segmented_button::SegmentedButton<'a, Message, Renderer>;

pub mod slide_bar;

pub use slide_bar::SlideBar;
60 changes: 2 additions & 58 deletions src/native/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,10 @@ use iced_widget::core::{
mouse::Cursor,
renderer,
widget::Tree,
Color, Element, Layout, Length, Padding, Rectangle, Widget,
Color, Element, Layout, Length, Rectangle, Widget,
};

/// Methods for creating inner bounds
#[allow(missing_debug_implementations)]
pub enum InnerBounds {
/// Create inner bounds ratio to the outer bounds
Ratio(f32, f32),
/// Create inner bounds by padding the outer bounds
Padding(Padding),
/// Create square inner bounds
Square(f32),
/// Create inner bounds with a custom function
Custom(Box<dyn Fn(Rectangle) -> Rectangle>),
}
impl InnerBounds {
/// Gets the inner bounds of the Set type.
fn get_bounds(&self, outer_bounds: Rectangle) -> Rectangle {
use InnerBounds::{Custom, Padding, Ratio, Square};
match self {
Ratio(w, h) => {
let width = w * outer_bounds.width;
let height = h * outer_bounds.height;
let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
Rectangle {
x,
y,
width,
height,
}
}
Padding(p) => {
let x = outer_bounds.x + p.left;
let y = outer_bounds.y + p.top;
let width = outer_bounds.width - p.horizontal();
let height = outer_bounds.width - p.vertical();
Rectangle {
x,
y,
width,
height,
}
}
Square(l) => {
let width = *l;
let height = *l;
let x = outer_bounds.x + (outer_bounds.width - width) * 0.5;
let y = outer_bounds.y + (outer_bounds.height - height) * 0.5;
Rectangle {
x,
y,
width,
height,
}
}
Custom(f) => f(outer_bounds),
}
}
}
use crate::native::InnerBounds;

/// A dummy widget that draws a quad
#[allow(missing_debug_implementations)]
Expand Down
Loading

0 comments on commit 5f56c7e

Please sign in to comment.