Use closures for Slider::style and VerticalSlider::style
This commit is contained in:
parent
58a0d5b7ff
commit
afd138dba6
3 changed files with 24 additions and 18 deletions
|
|
@ -240,7 +240,7 @@ pub fn slider<'a, T, Message, Theme>(
|
||||||
where
|
where
|
||||||
T: Copy + From<u8> + std::cmp::PartialOrd,
|
T: Copy + From<u8> + std::cmp::PartialOrd,
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Theme: slider::DefaultStyle,
|
Theme: slider::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Slider::new(range, value, on_change)
|
Slider::new(range, value, on_change)
|
||||||
}
|
}
|
||||||
|
|
@ -256,7 +256,7 @@ pub fn vertical_slider<'a, T, Message, Theme>(
|
||||||
where
|
where
|
||||||
T: Copy + From<u8> + std::cmp::PartialOrd,
|
T: Copy + From<u8> + std::cmp::PartialOrd,
|
||||||
Message: Clone,
|
Message: Clone,
|
||||||
Theme: vertical_slider::DefaultStyle,
|
Theme: vertical_slider::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
VerticalSlider::new(range, value, on_change)
|
VerticalSlider::new(range, value, on_change)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme> {
|
||||||
on_release: Option<Message>,
|
on_release: Option<Message>,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: f32,
|
height: f32,
|
||||||
style: Style<Theme>,
|
style: Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme>
|
impl<'a, T, Message, Theme> Slider<'a, T, Message, Theme>
|
||||||
|
|
@ -70,7 +70,7 @@ where
|
||||||
/// `Message`.
|
/// `Message`.
|
||||||
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
|
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
|
||||||
where
|
where
|
||||||
Theme: DefaultStyle,
|
Theme: DefaultStyle + 'a,
|
||||||
F: 'a + Fn(T) -> Message,
|
F: 'a + Fn(T) -> Message,
|
||||||
{
|
{
|
||||||
let value = if value >= *range.start() {
|
let value = if value >= *range.start() {
|
||||||
|
|
@ -95,7 +95,7 @@ where
|
||||||
on_release: None,
|
on_release: None,
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
height: Self::DEFAULT_HEIGHT,
|
height: Self::DEFAULT_HEIGHT,
|
||||||
style: Theme::default_style(),
|
style: Box::new(Theme::default_style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,8 +131,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Slider`].
|
/// Sets the style of the [`Slider`].
|
||||||
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
|
pub fn style(
|
||||||
self.style = style.into();
|
mut self,
|
||||||
|
style: impl Fn(&Theme, Status) -> Appearance + 'a,
|
||||||
|
) -> Self {
|
||||||
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -547,23 +550,23 @@ pub enum HandleShape {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a [`Slider`].
|
/// The style of a [`Slider`].
|
||||||
pub type Style<Theme> = fn(&Theme, Status) -> Appearance;
|
pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;
|
||||||
|
|
||||||
/// The default style of a [`Slider`].
|
/// The default style of a [`Slider`].
|
||||||
pub trait DefaultStyle {
|
pub trait DefaultStyle {
|
||||||
/// Returns the default style of a [`Slider`].
|
/// Returns the default style of a [`Slider`].
|
||||||
fn default_style() -> Style<Self>;
|
fn default_style(&self, status: Status) -> Appearance;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Theme {
|
impl DefaultStyle for Theme {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, status: Status) -> Appearance {
|
||||||
default
|
default(self, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Appearance {
|
impl DefaultStyle for Appearance {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|appearance, _status| *appearance
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ pub struct VerticalSlider<'a, T, Message, Theme = crate::Theme> {
|
||||||
on_release: Option<Message>,
|
on_release: Option<Message>,
|
||||||
width: f32,
|
width: f32,
|
||||||
height: Length,
|
height: Length,
|
||||||
style: Style<Theme>,
|
style: Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme>
|
impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme>
|
||||||
|
|
@ -72,7 +72,7 @@ where
|
||||||
/// `Message`.
|
/// `Message`.
|
||||||
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
|
pub fn new<F>(range: RangeInclusive<T>, value: T, on_change: F) -> Self
|
||||||
where
|
where
|
||||||
Theme: DefaultStyle,
|
Theme: DefaultStyle + 'a,
|
||||||
F: 'a + Fn(T) -> Message,
|
F: 'a + Fn(T) -> Message,
|
||||||
{
|
{
|
||||||
let value = if value >= *range.start() {
|
let value = if value >= *range.start() {
|
||||||
|
|
@ -97,7 +97,7 @@ where
|
||||||
on_release: None,
|
on_release: None,
|
||||||
width: Self::DEFAULT_WIDTH,
|
width: Self::DEFAULT_WIDTH,
|
||||||
height: Length::Fill,
|
height: Length::Fill,
|
||||||
style: Theme::default_style(),
|
style: Box::new(Theme::default_style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -133,8 +133,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`VerticalSlider`].
|
/// Sets the style of the [`VerticalSlider`].
|
||||||
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
|
pub fn style(
|
||||||
self.style = style.into();
|
mut self,
|
||||||
|
style: impl Fn(&Theme, Status) -> Appearance + 'a,
|
||||||
|
) -> Self {
|
||||||
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue