2022-12-07 03:03:41 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
mod model;
|
|
|
|
|
pub use self::model::SpinButtonModel;
|
|
|
|
|
|
2022-12-07 03:20:52 +01:00
|
|
|
use crate::widget::icon;
|
2022-12-07 03:03:41 +01:00
|
|
|
use crate::{theme, Element};
|
2022-12-07 03:20:52 +01:00
|
|
|
use apply::Apply;
|
2022-12-07 03:03:41 +01:00
|
|
|
use iced::{
|
|
|
|
|
alignment::{Horizontal, Vertical},
|
|
|
|
|
widget::{button, container, row, text},
|
|
|
|
|
Alignment, Background, Length,
|
|
|
|
|
};
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
2022-12-07 03:44:57 +01:00
|
|
|
pub struct SpinButton<T, Message> {
|
2022-12-07 03:03:41 +01:00
|
|
|
value: T,
|
2022-12-07 03:44:57 +01:00
|
|
|
on_change: Box<dyn Fn(SpinMessage) -> Message + 'static>,
|
2022-12-07 03:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A message emitted by the [`SpinButton`] widget.
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash)]
|
|
|
|
|
pub enum SpinMessage {
|
|
|
|
|
Increment,
|
|
|
|
|
Decrement,
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 03:44:57 +01:00
|
|
|
pub fn spin_button<T: 'static + Copy + Hash + ToString, Message: 'static>(
|
|
|
|
|
model: &SpinButtonModel<T>,
|
|
|
|
|
on_change: impl Fn(SpinMessage) -> Message + 'static,
|
|
|
|
|
) -> SpinButton<T, Message> {
|
|
|
|
|
SpinButton::new(model.value, on_change)
|
2022-12-07 03:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 03:44:57 +01:00
|
|
|
impl<T: 'static + Copy + Hash + ToString, Message: 'static> SpinButton<T, Message> {
|
|
|
|
|
pub fn new(value: T, on_change: impl Fn(SpinMessage) -> Message + 'static) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
on_change: Box::from(on_change),
|
|
|
|
|
value,
|
|
|
|
|
}
|
2022-12-07 03:03:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-07 03:44:57 +01:00
|
|
|
pub fn into_element(self) -> Element<'static, Message> {
|
|
|
|
|
let Self { on_change, value } = self;
|
|
|
|
|
|
2022-12-07 03:03:41 +01:00
|
|
|
Element::from(iced_lazy::lazy(
|
2022-12-07 03:44:57 +01:00
|
|
|
value,
|
2022-12-07 03:03:41 +01:00
|
|
|
move || -> Element<'static, SpinMessage> {
|
|
|
|
|
container(
|
|
|
|
|
row![
|
2022-12-07 03:20:52 +01:00
|
|
|
icon("list-remove-symbolic", 24)
|
|
|
|
|
.style(theme::Svg::Symbolic)
|
|
|
|
|
.apply(container)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.align_x(Horizontal::Center)
|
|
|
|
|
.align_y(Vertical::Center)
|
|
|
|
|
.apply(button)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.style(theme::Button::Text)
|
|
|
|
|
.on_press(SpinMessage::Decrement),
|
2022-12-07 03:44:57 +01:00
|
|
|
text(value)
|
2022-12-07 03:20:52 +01:00
|
|
|
.vertical_alignment(Vertical::Center)
|
|
|
|
|
.apply(container)
|
2022-12-07 03:03:41 +01:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.align_x(Horizontal::Center)
|
|
|
|
|
.align_y(Vertical::Center),
|
2022-12-07 03:20:52 +01:00
|
|
|
icon("list-add-symbolic", 24)
|
|
|
|
|
.style(theme::Svg::Symbolic)
|
|
|
|
|
.apply(container)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.align_x(Horizontal::Center)
|
|
|
|
|
.align_y(Vertical::Center)
|
|
|
|
|
.apply(button)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.style(theme::Button::Text)
|
|
|
|
|
.on_press(SpinMessage::Increment),
|
2022-12-07 03:03:41 +01:00
|
|
|
]
|
|
|
|
|
.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()
|
|
|
|
|
},
|
|
|
|
|
))
|
2022-12-07 03:44:57 +01:00
|
|
|
.map(on_change)
|
2022-12-07 03:03:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 03:44:57 +01:00
|
|
|
impl<'a, T: 'static + Copy + Hash + ToString, Message: 'static> From<SpinButton<T, Message>>
|
|
|
|
|
for Element<'a, Message>
|
|
|
|
|
{
|
|
|
|
|
fn from(spin_button: SpinButton<T, Message>) -> Self {
|
2022-12-07 03:03:41 +01:00
|
|
|
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(),
|
|
|
|
|
}
|
|
|
|
|
}
|