Simplify theming for ProgressBar widget
This commit is contained in:
parent
87d16a090b
commit
5824ceb1fe
6 changed files with 89 additions and 106 deletions
|
|
@ -5,7 +5,8 @@ use iced::widget::{
|
||||||
scrollable, slider, text, vertical_space, Scrollable,
|
scrollable, slider, text, vertical_space, Scrollable,
|
||||||
};
|
};
|
||||||
use iced::{
|
use iced::{
|
||||||
Alignment, Application, Color, Command, Element, Length, Settings, Theme,
|
Alignment, Application, Border, Color, Command, Element, Length, Settings,
|
||||||
|
Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
@ -348,6 +349,6 @@ fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Appearance {
|
||||||
progress_bar::Appearance {
|
progress_bar::Appearance {
|
||||||
background: theme.extended_palette().background.strong.color.into(),
|
background: theme.extended_palette().background.strong.color.into(),
|
||||||
bar: Color::from_rgb8(250, 85, 134).into(),
|
bar: Color::from_rgb8(250, 85, 134).into(),
|
||||||
border_radius: 0.0.into(),
|
border: Border::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ pub mod application;
|
||||||
pub mod menu;
|
pub mod menu;
|
||||||
pub mod pane_grid;
|
pub mod pane_grid;
|
||||||
pub mod pick_list;
|
pub mod pick_list;
|
||||||
pub mod progress_bar;
|
|
||||||
pub mod rule;
|
pub mod rule;
|
||||||
pub mod svg;
|
pub mod svg;
|
||||||
pub mod text_editor;
|
pub mod text_editor;
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
//! Change the appearance of a progress bar.
|
|
||||||
use crate::core::border;
|
|
||||||
use crate::core::Background;
|
|
||||||
|
|
||||||
/// The appearance of a progress bar.
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct Appearance {
|
|
||||||
/// The [`Background`] of the progress bar.
|
|
||||||
pub background: Background,
|
|
||||||
/// The [`Background`] of the bar of the progress bar.
|
|
||||||
pub bar: Background,
|
|
||||||
/// The border radius of the progress bar.
|
|
||||||
pub border_radius: border::Radius,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A set of rules that dictate the style of a progress bar.
|
|
||||||
pub trait StyleSheet {
|
|
||||||
/// The supported style of the [`StyleSheet`].
|
|
||||||
type Style: Default;
|
|
||||||
|
|
||||||
/// Produces the [`Appearance`] of the progress bar.
|
|
||||||
fn appearance(&self, style: &Self::Style) -> Appearance;
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,6 @@ use crate::core::widget::text;
|
||||||
use crate::menu;
|
use crate::menu;
|
||||||
use crate::pane_grid;
|
use crate::pane_grid;
|
||||||
use crate::pick_list;
|
use crate::pick_list;
|
||||||
use crate::progress_bar;
|
|
||||||
use crate::rule;
|
use crate::rule;
|
||||||
use crate::svg;
|
use crate::svg;
|
||||||
use crate::text_editor;
|
use crate::text_editor;
|
||||||
|
|
@ -512,61 +511,6 @@ impl pane_grid::StyleSheet for Theme {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a progress bar.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub enum ProgressBar {
|
|
||||||
/// The primary style.
|
|
||||||
#[default]
|
|
||||||
Primary,
|
|
||||||
/// The success style.
|
|
||||||
Success,
|
|
||||||
/// The danger style.
|
|
||||||
Danger,
|
|
||||||
/// A custom style.
|
|
||||||
Custom(Box<dyn progress_bar::StyleSheet<Style = Theme>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Fn(&Theme) -> progress_bar::Appearance + 'static> From<T>
|
|
||||||
for ProgressBar
|
|
||||||
{
|
|
||||||
fn from(f: T) -> Self {
|
|
||||||
Self::Custom(Box::new(f))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl progress_bar::StyleSheet for Theme {
|
|
||||||
type Style = ProgressBar;
|
|
||||||
|
|
||||||
fn appearance(&self, style: &Self::Style) -> progress_bar::Appearance {
|
|
||||||
if let ProgressBar::Custom(custom) = style {
|
|
||||||
return custom.appearance(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
let palette = self.extended_palette();
|
|
||||||
|
|
||||||
let from_palette = |bar: Color| progress_bar::Appearance {
|
|
||||||
background: palette.background.strong.color.into(),
|
|
||||||
bar: bar.into(),
|
|
||||||
border_radius: 2.0.into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
match style {
|
|
||||||
ProgressBar::Primary => from_palette(palette.primary.base.color),
|
|
||||||
ProgressBar::Success => from_palette(palette.success.base.color),
|
|
||||||
ProgressBar::Danger => from_palette(palette.danger.base.color),
|
|
||||||
ProgressBar::Custom(custom) => custom.appearance(self),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Fn(&Theme) -> progress_bar::Appearance> progress_bar::StyleSheet for T {
|
|
||||||
type Style = Theme;
|
|
||||||
|
|
||||||
fn appearance(&self, style: &Self::Style) -> progress_bar::Appearance {
|
|
||||||
(self)(style)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The style of a rule.
|
/// The style of a rule.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub enum Rule {
|
pub enum Rule {
|
||||||
|
|
|
||||||
|
|
@ -351,7 +351,7 @@ pub fn progress_bar<Theme>(
|
||||||
value: f32,
|
value: f32,
|
||||||
) -> ProgressBar<Theme>
|
) -> ProgressBar<Theme>
|
||||||
where
|
where
|
||||||
Theme: progress_bar::StyleSheet,
|
Theme: progress_bar::Style,
|
||||||
{
|
{
|
||||||
ProgressBar::new(range, value)
|
ProgressBar::new(range, value)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,13 @@ use crate::core::layout;
|
||||||
use crate::core::mouse;
|
use crate::core::mouse;
|
||||||
use crate::core::renderer;
|
use crate::core::renderer;
|
||||||
use crate::core::widget::Tree;
|
use crate::core::widget::Tree;
|
||||||
use crate::core::{Border, Element, Layout, Length, Rectangle, Size, Widget};
|
use crate::core::{
|
||||||
|
Background, Border, Element, Layout, Length, Rectangle, Size, Widget,
|
||||||
|
};
|
||||||
|
use crate::style::Theme;
|
||||||
|
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
pub use iced_style::progress_bar::{Appearance, StyleSheet};
|
|
||||||
|
|
||||||
/// A bar that displays progress.
|
/// A bar that displays progress.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
|
|
@ -22,21 +23,15 @@ pub use iced_style::progress_bar::{Appearance, StyleSheet};
|
||||||
///
|
///
|
||||||
/// 
|
/// 
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct ProgressBar<Theme = crate::Theme>
|
pub struct ProgressBar<Theme = crate::Theme> {
|
||||||
where
|
|
||||||
Theme: StyleSheet,
|
|
||||||
{
|
|
||||||
range: RangeInclusive<f32>,
|
range: RangeInclusive<f32>,
|
||||||
value: f32,
|
value: f32,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Option<Length>,
|
height: Option<Length>,
|
||||||
style: Theme::Style,
|
style: fn(&Theme) -> Appearance,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Theme> ProgressBar<Theme>
|
impl<Theme> ProgressBar<Theme> {
|
||||||
where
|
|
||||||
Theme: StyleSheet,
|
|
||||||
{
|
|
||||||
/// The default height of a [`ProgressBar`].
|
/// The default height of a [`ProgressBar`].
|
||||||
pub const DEFAULT_HEIGHT: f32 = 30.0;
|
pub const DEFAULT_HEIGHT: f32 = 30.0;
|
||||||
|
|
||||||
|
|
@ -45,13 +40,16 @@ where
|
||||||
/// It expects:
|
/// It expects:
|
||||||
/// * an inclusive range of possible values
|
/// * an inclusive range of possible values
|
||||||
/// * the current value of the [`ProgressBar`]
|
/// * the current value of the [`ProgressBar`]
|
||||||
pub fn new(range: RangeInclusive<f32>, value: f32) -> Self {
|
pub fn new(range: RangeInclusive<f32>, value: f32) -> Self
|
||||||
|
where
|
||||||
|
Theme: Style,
|
||||||
|
{
|
||||||
ProgressBar {
|
ProgressBar {
|
||||||
value: value.clamp(*range.start(), *range.end()),
|
value: value.clamp(*range.start(), *range.end()),
|
||||||
range,
|
range,
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
height: None,
|
height: None,
|
||||||
style: Default::default(),
|
style: Theme::style(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,8 +66,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`ProgressBar`].
|
/// Sets the style of the [`ProgressBar`].
|
||||||
pub fn style(mut self, style: impl Into<Theme::Style>) -> Self {
|
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self {
|
||||||
self.style = style.into();
|
self.style = style;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +76,6 @@ impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
for ProgressBar<Theme>
|
for ProgressBar<Theme>
|
||||||
where
|
where
|
||||||
Renderer: crate::core::Renderer,
|
Renderer: crate::core::Renderer,
|
||||||
Theme: StyleSheet,
|
|
||||||
{
|
{
|
||||||
fn size(&self) -> Size<Length> {
|
fn size(&self) -> Size<Length> {
|
||||||
Size {
|
Size {
|
||||||
|
|
@ -120,15 +117,15 @@ where
|
||||||
/ (range_end - range_start)
|
/ (range_end - range_start)
|
||||||
};
|
};
|
||||||
|
|
||||||
let style = theme.appearance(&self.style);
|
let appearance = (self.style)(theme);
|
||||||
|
|
||||||
renderer.fill_quad(
|
renderer.fill_quad(
|
||||||
renderer::Quad {
|
renderer::Quad {
|
||||||
bounds: Rectangle { ..bounds },
|
bounds: Rectangle { ..bounds },
|
||||||
border: Border::with_radius(style.border_radius),
|
border: appearance.border,
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.background,
|
appearance.background,
|
||||||
);
|
);
|
||||||
|
|
||||||
if active_progress_width > 0.0 {
|
if active_progress_width > 0.0 {
|
||||||
|
|
@ -138,10 +135,10 @@ where
|
||||||
width: active_progress_width,
|
width: active_progress_width,
|
||||||
..bounds
|
..bounds
|
||||||
},
|
},
|
||||||
border: Border::with_radius(style.border_radius),
|
border: Border::with_radius(appearance.border.radius),
|
||||||
..renderer::Quad::default()
|
..renderer::Quad::default()
|
||||||
},
|
},
|
||||||
style.bar,
|
appearance.bar,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +148,7 @@ impl<'a, Message, Theme, Renderer> From<ProgressBar<Theme>>
|
||||||
for Element<'a, Message, Theme, Renderer>
|
for Element<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Message: 'a,
|
Message: 'a,
|
||||||
Theme: StyleSheet + 'a,
|
Theme: 'a,
|
||||||
Renderer: 'a + crate::core::Renderer,
|
Renderer: 'a + crate::core::Renderer,
|
||||||
{
|
{
|
||||||
fn from(
|
fn from(
|
||||||
|
|
@ -160,3 +157,68 @@ where
|
||||||
Element::new(progress_bar)
|
Element::new(progress_bar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The appearance of a progress bar.
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Appearance {
|
||||||
|
/// The [`Background`] of the progress bar.
|
||||||
|
pub background: Background,
|
||||||
|
/// The [`Background`] of the bar of the progress bar.
|
||||||
|
pub bar: Background,
|
||||||
|
/// The [`Border`] of the progress bar.
|
||||||
|
pub border: Border,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The definiton of the default style of a [`ProgressBar`].
|
||||||
|
pub trait Style {
|
||||||
|
/// Returns the default style of a [`ProgressBar`].
|
||||||
|
fn style() -> fn(&Self) -> Appearance;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Style for Theme {
|
||||||
|
fn style() -> fn(&Self) -> Appearance {
|
||||||
|
primary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The primary style of a [`ProgressBar`].
|
||||||
|
pub fn primary(theme: &Theme) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
styled(palette.background.strong.color, palette.primary.base.color)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The secondary style of a [`ProgressBar`].
|
||||||
|
pub fn secondary(theme: &Theme) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
styled(
|
||||||
|
palette.background.strong.color,
|
||||||
|
palette.secondary.base.color,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The success style of a [`ProgressBar`].
|
||||||
|
pub fn success(theme: &Theme) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
styled(palette.background.strong.color, palette.success.base.color)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The danger style of a [`ProgressBar`].
|
||||||
|
pub fn danger(theme: &Theme) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
styled(palette.background.strong.color, palette.danger.base.color)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn styled(
|
||||||
|
background: impl Into<Background>,
|
||||||
|
bar: impl Into<Background>,
|
||||||
|
) -> Appearance {
|
||||||
|
Appearance {
|
||||||
|
background: background.into(),
|
||||||
|
bar: bar.into(),
|
||||||
|
border: Border::with_radius(2),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue