change(widget): custom coloring for progressbar
This commit is contained in:
parent
d60f3b89a7
commit
bf8f83decc
@ -1,8 +1,17 @@
|
|||||||
use iced::{slider, Column, Element, Sandbox, Settings, Slider};
|
use iced::{
|
||||||
|
settings::Window, slider, Background, Color, Column, Element, Sandbox,
|
||||||
|
Settings, Slider,
|
||||||
|
};
|
||||||
use iced_winit::Progressbar;
|
use iced_winit::Progressbar;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
Progress::run(Settings::default())
|
Progress::run(Settings {
|
||||||
|
window: Window {
|
||||||
|
size: (700, 300),
|
||||||
|
resizable: true,
|
||||||
|
decorations: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@ -29,17 +38,20 @@ impl Sandbox for Progress {
|
|||||||
|
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) {
|
||||||
match message {
|
match message {
|
||||||
Message::SliderChanged(x) => {
|
Message::SliderChanged(x) => self.value = x,
|
||||||
self.value = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<Message> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
Column::new()
|
Column::new()
|
||||||
.padding(20)
|
.padding(20)
|
||||||
.push(Progressbar::new(0.0..=100.0, self.value))
|
.push(
|
||||||
.padding(20)
|
Progressbar::new(0.0..=100.0, self.value)
|
||||||
|
.background(Background::Color(Color::from_rgb(
|
||||||
|
0.6, 0.6, 0.6,
|
||||||
|
)))
|
||||||
|
.active_color(Color::from_rgb(0.0, 0.95, 0.0)),
|
||||||
|
)
|
||||||
.push(Slider::new(
|
.push(Slider::new(
|
||||||
&mut self.progressbar_slider,
|
&mut self.progressbar_slider,
|
||||||
0.0..=100.0,
|
0.0..=100.0,
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
//!
|
//!
|
||||||
//! [`Progressbar`]: struct.Progressbar.html
|
//! [`Progressbar`]: struct.Progressbar.html
|
||||||
use crate::{
|
use crate::{
|
||||||
layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
layout, Background, Color, Element, Hasher, Layout, Length, Point,
|
||||||
|
Rectangle, Size, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{hash::Hash, ops::RangeInclusive};
|
use std::{hash::Hash, ops::RangeInclusive};
|
||||||
@ -14,17 +15,19 @@ use std::{hash::Hash, ops::RangeInclusive};
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use iced_native::Progressbar;
|
/// # use iced_native::Progressbar;
|
||||||
///
|
///
|
||||||
|
/// let value = 50.0;
|
||||||
/// Progressbar::new(0.0..=100.0, value);
|
/// Progressbar::new(0.0..=100.0, value);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// TODO: Get a progressbar render
|
/// 
|
||||||
/// 
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Progressbar {
|
pub struct Progressbar {
|
||||||
range: RangeInclusive<f32>,
|
range: RangeInclusive<f32>,
|
||||||
value: f32,
|
value: f32,
|
||||||
width: Length,
|
width: Length,
|
||||||
|
background: Option<Background>,
|
||||||
|
active_color: Option<Color>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Progressbar {
|
impl Progressbar {
|
||||||
@ -40,6 +43,8 @@ impl Progressbar {
|
|||||||
value: value.max(*range.start()).min(*range.end()),
|
value: value.max(*range.start()).min(*range.end()),
|
||||||
range,
|
range,
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
|
background: None,
|
||||||
|
active_color: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,6 +55,22 @@ impl Progressbar {
|
|||||||
self.width = width;
|
self.width = width;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the background of the [`Progressbar`].
|
||||||
|
///
|
||||||
|
/// [`Progressbar`]: struct.Progressbar.html
|
||||||
|
pub fn background(mut self, background: Background) -> Self {
|
||||||
|
self.background = Some(background);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the active color of the [`Progressbar`].
|
||||||
|
///
|
||||||
|
/// [`Progressbar`]: struct.Progressbar.html
|
||||||
|
pub fn active_color(mut self, active_color: Color) -> Self {
|
||||||
|
self.active_color = Some(active_color);
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Renderer> Widget<Message, Renderer> for Progressbar
|
impl<Message, Renderer> Widget<Message, Renderer> for Progressbar
|
||||||
@ -84,7 +105,13 @@ where
|
|||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
) -> Renderer::Output {
|
) -> Renderer::Output {
|
||||||
renderer.draw(layout.bounds(), self.range.clone(), self.value)
|
renderer.draw(
|
||||||
|
layout.bounds(),
|
||||||
|
self.range.clone(),
|
||||||
|
self.value,
|
||||||
|
self.background,
|
||||||
|
self.active_color,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
@ -112,6 +139,8 @@ pub trait Renderer: crate::Renderer {
|
|||||||
/// * the bounds of the [`Progressbar`]
|
/// * the bounds of the [`Progressbar`]
|
||||||
/// * the range of values of the [`Progressbar`]
|
/// * the range of values of the [`Progressbar`]
|
||||||
/// * the current value of the [`Progressbar`]
|
/// * the current value of the [`Progressbar`]
|
||||||
|
/// * maybe a specific background of the [`Progressbar`]
|
||||||
|
/// * maybe a specific active color of the [`Progressbar`]
|
||||||
///
|
///
|
||||||
/// [`Progressbar`]: struct.Progressbar.html
|
/// [`Progressbar`]: struct.Progressbar.html
|
||||||
/// [`State`]: struct.State.html
|
/// [`State`]: struct.State.html
|
||||||
@ -121,6 +150,8 @@ pub trait Renderer: crate::Renderer {
|
|||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
range: RangeInclusive<f32>,
|
range: RangeInclusive<f32>,
|
||||||
value: f32,
|
value: f32,
|
||||||
|
background: Option<Background>,
|
||||||
|
active_color: Option<Color>,
|
||||||
) -> Self::Output;
|
) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ impl progressbar::Renderer for Renderer {
|
|||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
range: std::ops::RangeInclusive<f32>,
|
range: std::ops::RangeInclusive<f32>,
|
||||||
value: f32,
|
value: f32,
|
||||||
|
background: Option<Background>,
|
||||||
|
active_color: Option<Color>,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
let (range_start, range_end) = range.into_inner();
|
let (range_start, range_end) = range.into_inner();
|
||||||
let active_progress_width = bounds.width
|
let active_progress_width = bounds.width
|
||||||
@ -19,7 +21,9 @@ impl progressbar::Renderer for Renderer {
|
|||||||
let background = Primitive::Group {
|
let background = Primitive::Group {
|
||||||
primitives: vec![Primitive::Quad {
|
primitives: vec![Primitive::Quad {
|
||||||
bounds: Rectangle { ..bounds },
|
bounds: Rectangle { ..bounds },
|
||||||
background: Color::from_rgb(0.6, 0.6, 0.6).into(),
|
background: background
|
||||||
|
.unwrap_or(Background::Color([0.6, 0.6, 0.6].into()))
|
||||||
|
.into(),
|
||||||
border_radius: 5,
|
border_radius: 5,
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
@ -29,8 +33,10 @@ impl progressbar::Renderer for Renderer {
|
|||||||
width: active_progress_width,
|
width: active_progress_width,
|
||||||
..bounds
|
..bounds
|
||||||
},
|
},
|
||||||
background: Background::Color([0.0, 0.95, 0.0].into()),
|
background: Background::Color(
|
||||||
border_radius: 4,
|
active_color.unwrap_or([0.0, 0.95, 0.0].into()),
|
||||||
|
),
|
||||||
|
border_radius: 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user