feat!(spin_button): refactor and support vertical widget variant

This commit is contained in:
Bryan Hyland 2024-11-19 08:17:40 -08:00 committed by Michael Murphy
parent bc89a8aede
commit b14fde9033
7 changed files with 474 additions and 266 deletions

View file

@ -311,7 +311,7 @@ pub mod settings;
pub mod spin_button;
#[doc(inline)]
pub use spin_button::{spin_button, SpinButton};
pub use spin_button::{spin_button, vertical as vertical_spin_button, SpinButton};
pub mod tab_bar;

260
src/widget/spin_button.rs Normal file
View file

@ -0,0 +1,260 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! A control for incremental adjustments of a value.
use crate::{
theme,
widget::{button, column, container, icon, row, text},
Element,
};
use apply::Apply;
use derive_setters::Setters;
use iced::{alignment::Horizontal, Border, Shadow};
use iced::{Alignment, Length};
use std::marker::PhantomData;
use std::ops::{Add, Sub};
use std::{borrow::Cow, fmt::Display};
/// Horizontal spin button widget.
pub fn spin_button<'a, T, M>(
label: impl Into<Cow<'a, str>>,
value: T,
step: T,
min: T,
max: T,
on_press: impl Fn(T) -> M + 'static,
) -> SpinButton<'a, T, M>
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
SpinButton::new(
label,
value,
step,
min,
max,
Orientation::Horizontal,
on_press,
)
}
/// Vertical spin button widget.
pub fn vertical<'a, T, M>(
label: impl Into<Cow<'a, str>>,
value: T,
step: T,
min: T,
max: T,
on_press: impl Fn(T) -> M + 'static,
) -> SpinButton<'a, T, M>
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
SpinButton::new(
label,
value,
step,
min,
max,
Orientation::Vertical,
on_press,
)
}
#[derive(Clone, Copy)]
enum Orientation {
Horizontal,
Vertical,
}
pub struct SpinButton<'a, T, M>
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
/// The formatted value of the spin button.
label: Cow<'a, str>,
/// The current value of the spin button.
value: T,
/// The amount to increment or decrement the value.
step: T,
/// The minimum value permitted.
min: T,
/// The maximum value permitted.
max: T,
orientation: Orientation,
on_press: Box<dyn Fn(T) -> M>,
}
impl<'a, T, M> SpinButton<'a, T, M>
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
/// Create a new new button
fn new(
label: impl Into<Cow<'a, str>>,
value: T,
step: T,
min: T,
max: T,
orientation: Orientation,
on_press: impl Fn(T) -> M + 'static,
) -> Self {
Self {
label: label.into(),
step,
value: if value < min {
min
} else if value > max {
max
} else {
value
},
min,
max,
orientation,
on_press: Box::from(on_press),
}
}
}
fn increment<T>(value: T, step: T, min: T, max: T) -> T
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
if value > max - step {
max
} else {
value + step
}
}
fn decrement<T>(value: T, step: T, min: T, max: T) -> T
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
if value < min + step {
min
} else {
value - step
}
}
impl<'a, T, Message> From<SpinButton<'a, T, Message>> for Element<'a, Message>
where
Message: Clone + 'static,
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
fn from(this: SpinButton<'a, T, Message>) -> Self {
match this.orientation {
Orientation::Horizontal => horizontal_variant(this),
Orientation::Vertical => vertical_variant(this),
}
}
}
fn horizontal_variant<'a, T, Message>(
spin_button: SpinButton<'a, T, Message>,
) -> Element<'a, Message>
where
Message: Clone + 'static,
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
let decrement_button = icon::from_name("list-remove-symbolic")
.apply(button::icon)
.on_press((spin_button.on_press)(decrement::<T>(
spin_button.value,
spin_button.step,
spin_button.min,
spin_button.max,
)));
let increment_button = icon::from_name("list-add-symbolic")
.apply(button::icon)
.on_press((spin_button.on_press)(increment::<T>(
spin_button.value,
spin_button.step,
spin_button.min,
spin_button.max,
)));
let label = text::title4(spin_button.label)
.apply(container)
.center_x(Length::Fixed(48.0))
.align_y(Alignment::Center);
row::with_capacity(3)
.push(decrement_button)
.push(label)
.push(increment_button)
.align_y(Alignment::Center)
.apply(container)
.class(theme::Container::custom(container_style))
.into()
}
fn vertical_variant<'a, T, Message>(spin_button: SpinButton<'a, T, Message>) -> Element<'a, Message>
where
Message: Clone + 'static,
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
let decrement_button = icon::from_name("list-remove-symbolic")
.apply(button::icon)
.on_press((spin_button.on_press)(decrement::<T>(
spin_button.value,
spin_button.step,
spin_button.min,
spin_button.max,
)));
let increment_button = icon::from_name("list-add-symbolic")
.apply(button::icon)
.on_press((spin_button.on_press)(increment::<T>(
spin_button.value,
spin_button.step,
spin_button.min,
spin_button.max,
)));
let label = text::title4(spin_button.label)
.apply(container)
.center_x(Length::Fixed(48.0))
.align_y(Alignment::Center);
column::with_capacity(3)
.push(increment_button)
.push(label)
.push(decrement_button)
.align_x(Alignment::Center)
.apply(container)
.class(theme::Container::custom(container_style))
.into()
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn container_style(theme: &crate::Theme) -> iced_widget::container::Style {
let cosmic_theme = &theme.cosmic();
let mut neutral_10 = cosmic_theme.palette.neutral_10;
neutral_10.alpha = 0.1;
let accent = &cosmic_theme.accent;
let corners = &cosmic_theme.corner_radii;
iced_widget::container::Style {
icon_color: Some(accent.base.into()),
text_color: Some(cosmic_theme.palette.neutral_10.into()),
background: None,
border: Border {
radius: corners.radius_s.into(),
width: 0.0,
color: accent.base.into(),
},
shadow: Shadow::default(),
}
}
#[cfg(test)]
mod tests {
#[test]
fn decrement() {
assert_eq!(super::decrement(0i32, 10, 15, 35), 15);
}
}

View file

@ -1,108 +0,0 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! A control for incremental adjustments of a value.
mod model;
use std::borrow::Cow;
pub use self::model::{Message, Model};
use crate::widget::{button, container, icon, row, text};
use crate::{theme, Element};
use apply::Apply;
use iced::{Alignment, Length};
use iced_core::{Border, Shadow};
pub struct SpinButton<'a, Message> {
label: Cow<'a, str>,
on_change: Box<dyn Fn(model::Message) -> Message + 'static>,
}
/// A control for incremental adjustments of a value.
pub fn spin_button<'a, Message: 'static>(
label: impl Into<Cow<'a, str>>,
on_change: impl Fn(model::Message) -> Message + 'static,
) -> SpinButton<'a, Message> {
SpinButton::new(label, on_change)
}
impl<'a, Message: 'static> SpinButton<'a, Message> {
pub fn new(
label: impl Into<Cow<'a, str>>,
on_change: impl Fn(model::Message) -> Message + 'static,
) -> Self {
Self {
on_change: Box::from(on_change),
label: label.into(),
}
}
#[must_use]
pub fn into_element(self) -> Element<'a, Message> {
let Self { on_change, label } = self;
container(
row::with_children(vec![
icon::from_name("list-remove-symbolic")
.size(16)
.apply(container)
.center(Length::Fixed(32.0))
.apply(button::custom)
.width(Length::Fixed(32.0))
.height(Length::Fixed(32.0))
.class(theme::Button::Text)
.on_press(model::Message::Decrement)
.into(),
text::title4(label)
.apply(container)
.center_x(Length::Fixed(48.0))
.align_y(Alignment::Center)
.into(),
icon::from_name("list-add-symbolic")
.size(16)
.apply(container)
.center(Length::Fixed(32.0))
.apply(button::custom)
.width(Length::Fixed(32.0))
.height(Length::Fixed(32.0))
.class(theme::Button::Text)
.on_press(model::Message::Increment)
.into(),
])
.width(Length::Shrink)
.height(Length::Fixed(32.0))
.align_y(Alignment::Center),
)
.width(Length::Shrink)
.center_y(Length::Fixed(32.0))
.class(theme::Container::custom(container_style))
.apply(Element::from)
.map(on_change)
}
}
impl<'a, Message: 'static> From<SpinButton<'a, Message>> for Element<'a, Message> {
fn from(spin_button: SpinButton<'a, Message>) -> Self {
spin_button.into_element()
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn container_style(theme: &crate::Theme) -> iced_widget::container::Style {
let basic = &theme.cosmic();
let mut neutral_10 = basic.palette.neutral_10;
neutral_10.alpha = 0.1;
let accent = &basic.accent;
let corners = &basic.corner_radii;
iced_widget::container::Style {
icon_color: Some(basic.palette.neutral_10.into()),
text_color: Some(basic.palette.neutral_10.into()),
background: None,
border: Border {
radius: corners.radius_s.into(),
width: 0.0,
color: accent.base.into(),
},
shadow: Shadow::default(),
}
}

View file

@ -1,156 +0,0 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use derive_setters::Setters;
use fraction::{Bounded, Decimal};
use std::hash::Hash;
use std::ops::{Add, Sub};
/// A message emitted by the [`SpinButton`](super) widget.
#[derive(Clone, Copy, Debug, Hash)]
pub enum Message {
Increment,
Decrement,
}
#[derive(Setters)]
pub struct Model<T> {
/// The current value of the spin button.
#[setters(into)]
pub value: T,
/// The amount to increment the value.
#[setters(into)]
pub step: T,
/// The minimum value permitted.
#[setters(into)]
pub min: T,
/// The maximum value permitted.
#[setters(into)]
pub max: T,
}
impl<T: 'static> Model<T>
where
T: Copy + Hash + Sub<Output = T> + Add<Output = T> + Ord,
{
pub fn update(&mut self, message: Message) {
self.value = match message {
Message::Increment => {
std::cmp::min(std::cmp::max(self.value + self.step, self.min), self.max)
}
Message::Decrement => {
std::cmp::max(std::cmp::min(self.value - self.step, self.max), self.min)
}
}
}
}
impl Default for Model<i8> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: i8::MIN,
max: i8::MAX,
}
}
}
impl Default for Model<i16> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: i16::MIN,
max: i16::MAX,
}
}
}
impl Default for Model<i32> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: i32::MIN,
max: i32::MAX,
}
}
}
impl Default for Model<isize> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: isize::MIN,
max: isize::MAX,
}
}
}
impl Default for Model<u8> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: u8::MIN,
max: u8::MAX,
}
}
}
impl Default for Model<u16> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: u16::MIN,
max: u16::MAX,
}
}
}
impl Default for Model<u32> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: u32::MIN,
max: u32::MAX,
}
}
}
impl Default for Model<usize> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: usize::MIN,
max: usize::MAX,
}
}
}
impl Default for Model<u64> {
fn default() -> Self {
Self {
value: 0,
step: 1,
min: u64::MIN,
max: u64::MAX,
}
}
}
impl Default for Model<Decimal> {
fn default() -> Self {
Self {
value: Decimal::from(0.0),
step: Decimal::from(0.0),
min: Decimal::min_positive_value(),
max: Decimal::max_value(),
}
}
}