feat(widget): Add SpinButtonModel
Enables convenient handling of spin messages, and specifying steppings, minimum, and maximum values
This commit is contained in:
parent
200784b6c1
commit
ef71f7f027
7 changed files with 277 additions and 134 deletions
101
src/widget/spin_button/mod.rs
Normal file
101
src/widget/spin_button/mod.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// Copyright 2022 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
mod model;
|
||||
pub use self::model::SpinButtonModel;
|
||||
|
||||
use crate::{theme, Element};
|
||||
use iced::{
|
||||
alignment::{Horizontal, Vertical},
|
||||
widget::{button, container, row, text},
|
||||
Alignment, Background, Length,
|
||||
};
|
||||
use std::hash::Hash;
|
||||
|
||||
pub struct SpinButton<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
/// A message emitted by the [`SpinButton`] widget.
|
||||
#[derive(Clone, Copy, Debug, Hash)]
|
||||
pub enum SpinMessage {
|
||||
Increment,
|
||||
Decrement,
|
||||
}
|
||||
|
||||
pub fn spin_button<T: 'static + Clone + Hash + ToString>(value: T) -> SpinButton<T> {
|
||||
SpinButton::new(value)
|
||||
}
|
||||
|
||||
impl<T: 'static + Clone + Hash + ToString> SpinButton<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
|
||||
pub fn into_element(self) -> Element<'static, SpinMessage> {
|
||||
let Self { value } = self;
|
||||
Element::from(iced_lazy::lazy(
|
||||
value.clone(),
|
||||
move || -> Element<'static, SpinMessage> {
|
||||
container(
|
||||
row![
|
||||
button(
|
||||
container(text("-").size(26).vertical_alignment(Vertical::Center))
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.align_x(Horizontal::Center)
|
||||
.align_y(Vertical::Center),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.style(theme::Button::Text)
|
||||
.on_press(SpinMessage::Decrement),
|
||||
container(text(value.clone()).vertical_alignment(Vertical::Center))
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.align_x(Horizontal::Center)
|
||||
.align_y(Vertical::Center),
|
||||
button(
|
||||
container(text("+").size(26).vertical_alignment(Vertical::Center))
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.align_x(Horizontal::Center)
|
||||
.align_y(Vertical::Center),
|
||||
)
|
||||
.width(Length::Fill)
|
||||
.height(Length::Fill)
|
||||
.style(theme::Button::Text)
|
||||
.on_press(SpinMessage::Increment),
|
||||
]
|
||||
.width(Length::Fill)
|
||||
.height(Length::Units(32))
|
||||
.align_items(Alignment::Center),
|
||||
)
|
||||
.padding([4, 4])
|
||||
.align_y(Vertical::Center)
|
||||
.width(Length::Units(95))
|
||||
.height(Length::Units(32))
|
||||
.style(theme::Container::Custom(container_style))
|
||||
.into()
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'static + Clone + Hash + ToString> From<SpinButton<T>> for Element<'a, SpinMessage> {
|
||||
fn from(spin_button: SpinButton<T>) -> Self {
|
||||
spin_button.into_element()
|
||||
}
|
||||
}
|
||||
|
||||
fn container_style(theme: &crate::Theme) -> iced_style::container::Appearance {
|
||||
let secondary = &theme.cosmic().secondary;
|
||||
let accent = &theme.cosmic().accent;
|
||||
iced_style::container::Appearance {
|
||||
text_color: None,
|
||||
background: Some(Background::Color(secondary.component.base.into())),
|
||||
border_radius: 24.0,
|
||||
border_width: 0.0,
|
||||
border_color: accent.base.into(),
|
||||
}
|
||||
}
|
||||
150
src/widget/spin_button/model.rs
Normal file
150
src/widget/spin_button/model.rs
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
// Copyright 2022 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use super::{SpinButton, SpinMessage};
|
||||
use crate::Element;
|
||||
use derive_setters::Setters;
|
||||
use std::hash::Hash;
|
||||
use std::ops::{Add, Sub};
|
||||
|
||||
#[derive(Setters)]
|
||||
pub struct SpinButtonModel<T> {
|
||||
/// The current value of the spin button.
|
||||
pub value: T,
|
||||
/// The amount to increment the value.
|
||||
pub step: T,
|
||||
/// The minimum value permitted.
|
||||
pub min: T,
|
||||
/// The maximum value permitted.
|
||||
pub max: T,
|
||||
}
|
||||
|
||||
impl<T: 'static> SpinButtonModel<T>
|
||||
where
|
||||
T: Copy + Hash + ToString + Sub<Output = T> + Add<Output = T> + Ord,
|
||||
{
|
||||
pub fn view(&self) -> Element<'static, SpinMessage> {
|
||||
SpinButton::new(self.value).into_element()
|
||||
}
|
||||
|
||||
pub fn update(&mut self, message: SpinMessage) {
|
||||
self.value = match message {
|
||||
SpinMessage::Increment => {
|
||||
std::cmp::min(std::cmp::max(self.value + self.step, self.min), self.max)
|
||||
}
|
||||
SpinMessage::Decrement => {
|
||||
std::cmp::max(std::cmp::min(self.value - self.step, self.max), self.min)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i8> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: i8::MIN,
|
||||
max: i8::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i16> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: i16::MIN,
|
||||
max: i16::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i32> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: i32::MIN,
|
||||
max: i32::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<isize> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: isize::MIN,
|
||||
max: isize::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u8> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: u8::MIN,
|
||||
max: u8::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u16> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: u16::MIN,
|
||||
max: u16::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u32> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: u32::MIN,
|
||||
max: u32::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<usize> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
step: 1,
|
||||
min: usize::MIN,
|
||||
max: usize::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<f32> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0.0,
|
||||
step: 1.0,
|
||||
min: f32::MIN,
|
||||
max: f32::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<f64> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0.0,
|
||||
step: 1.0,
|
||||
min: f64::MIN,
|
||||
max: f64::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue