Moved the vertical spin button widget into spin button's mod.rs
This commit is contained in:
parent
5c29c530a5
commit
3cf473c26b
4 changed files with 117 additions and 125 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use cosmic::widget::{button, container, text, vertical_spinner};
|
use cosmic::widget::{button, container, text, spin_button::vertical_spin_button};
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
app::{Core, Task},
|
app::{Core, Task},
|
||||||
iced::{
|
iced::{
|
||||||
|
|
@ -83,7 +83,7 @@ impl Application for VertSpinnerApp {
|
||||||
|
|
||||||
fn view(&self) -> Element<Self::Message> {
|
fn view(&self) -> Element<Self::Message> {
|
||||||
let spinner_row = row![
|
let spinner_row = row![
|
||||||
vertical_spinner(
|
vertical_spin_button(
|
||||||
"Hours",
|
"Hours",
|
||||||
1,
|
1,
|
||||||
self.hours,
|
self.hours,
|
||||||
|
|
@ -91,7 +91,7 @@ impl Application for VertSpinnerApp {
|
||||||
12,
|
12,
|
||||||
VertSpinnerMessages::UpdateHours
|
VertSpinnerMessages::UpdateHours
|
||||||
),
|
),
|
||||||
vertical_spinner(
|
vertical_spin_button(
|
||||||
"Minutes",
|
"Minutes",
|
||||||
1,
|
1,
|
||||||
self.mins,
|
self.mins,
|
||||||
|
|
@ -99,7 +99,7 @@ impl Application for VertSpinnerApp {
|
||||||
59,
|
59,
|
||||||
VertSpinnerMessages::UpdateMins
|
VertSpinnerMessages::UpdateMins
|
||||||
),
|
),
|
||||||
vertical_spinner(
|
vertical_spin_button(
|
||||||
"Seconds",
|
"Seconds",
|
||||||
1,
|
1,
|
||||||
self.secs,
|
self.secs,
|
||||||
|
|
|
||||||
|
|
@ -358,10 +358,6 @@ pub mod tooltip {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod vertical_spin_button;
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use vertical_spin_button::*;
|
|
||||||
|
|
||||||
pub mod warning;
|
pub mod warning;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use warning::*;
|
pub use warning::*;
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,14 @@ use std::borrow::Cow;
|
||||||
|
|
||||||
pub use self::model::{Message, Model};
|
pub use self::model::{Message, Model};
|
||||||
|
|
||||||
use crate::widget::{button, container, icon, row, text};
|
use crate::widget::{button, column, container, icon, row, text};
|
||||||
use crate::{theme, Element};
|
use crate::{theme, Element};
|
||||||
|
use std::fmt::Display;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::ops::{Add, Sub};
|
||||||
use apply::Apply;
|
use apply::Apply;
|
||||||
use iced::{Alignment, Length};
|
use iced::{Alignment, Length};
|
||||||
|
use iced::alignment::Horizontal;
|
||||||
use iced_core::{Border, Shadow};
|
use iced_core::{Border, Shadow};
|
||||||
|
|
||||||
pub struct SpinButton<'a, Message> {
|
pub struct SpinButton<'a, Message> {
|
||||||
|
|
@ -106,3 +110,111 @@ fn container_style(theme: &crate::Theme) -> iced_widget::container::Style {
|
||||||
shadow: Shadow::default(),
|
shadow: Shadow::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// VerticalSpinButton is the state/model of the vertical spin button widget.
|
||||||
|
/// Restricts T to Add, Sub, Eq, Ord, Display, and Copy so that
|
||||||
|
/// T can only be numerical values.
|
||||||
|
pub struct VerticalSpinButton<'a, T, M>
|
||||||
|
where
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
label: String,
|
||||||
|
step: T,
|
||||||
|
value: T,
|
||||||
|
min: T,
|
||||||
|
max: T,
|
||||||
|
on_select: Box<dyn Fn(T) -> M>,
|
||||||
|
phantom_data: PhantomData<&'a M>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T, M> VerticalSpinButton<'a, T, M>
|
||||||
|
where
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
/// Creates a new vertical spin button widget
|
||||||
|
pub fn new(
|
||||||
|
label: impl Into<String>,
|
||||||
|
step: T,
|
||||||
|
value: T,
|
||||||
|
min: T,
|
||||||
|
max: T,
|
||||||
|
on_select: impl Fn(T) -> M + 'static,
|
||||||
|
) -> Self {
|
||||||
|
VerticalSpinButton {
|
||||||
|
label: label.into(),
|
||||||
|
step,
|
||||||
|
value,
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
on_select: Box::new(on_select),
|
||||||
|
phantom_data: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn vertical_spin_button<T, M>(
|
||||||
|
label: impl Into<String>,
|
||||||
|
step: T,
|
||||||
|
value: T,
|
||||||
|
min: T,
|
||||||
|
max: T,
|
||||||
|
on_select: impl Fn(T) -> M + 'static,
|
||||||
|
) -> VerticalSpinButton<'static, T, M>
|
||||||
|
where
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
VerticalSpinButton::new(label, step, value, min, max, on_select)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn increment<T, Message>(step: T, val: T, min: T, max: T) -> T
|
||||||
|
where
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
std::cmp::min(std::cmp::max(val + step, min), max)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decrement<T, Message>(step: T, val: T, min: T, max: T) -> T
|
||||||
|
where
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
std::cmp::max(std::cmp::min(val - step, max), min)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T, Message> From<VerticalSpinButton<'a, T, Message>> for Element<'a, Message>
|
||||||
|
where
|
||||||
|
Message: Clone + 'static,
|
||||||
|
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
||||||
|
{
|
||||||
|
fn from(this: VerticalSpinButton<T, Message>) -> Self {
|
||||||
|
let val_text = text(format!("{}", this.value)).size(14);
|
||||||
|
let spinner_container = column::with_capacity(3)
|
||||||
|
.push(
|
||||||
|
button::icon(icon::from_name("list-add-symbolic"))
|
||||||
|
.padding([0, 12])
|
||||||
|
.on_press((this.on_select)(increment::<T, Message>(
|
||||||
|
this.step, this.value, this.min, this.max,
|
||||||
|
))),
|
||||||
|
)
|
||||||
|
.push(val_text)
|
||||||
|
.push(
|
||||||
|
button::icon(icon::from_name("list-remove-symbolic"))
|
||||||
|
.padding([0, 12])
|
||||||
|
.on_press((this.on_select)(decrement::<T, Message>(
|
||||||
|
this.step, this.value, this.min, this.max,
|
||||||
|
))),
|
||||||
|
)
|
||||||
|
.align_x(Horizontal::Center);
|
||||||
|
|
||||||
|
let content_list = column::with_children(vec![
|
||||||
|
row::with_capacity(1).push(text(this.label)).into(),
|
||||||
|
row::with_children(vec![Element::from(spinner_container)]).into(),
|
||||||
|
])
|
||||||
|
.width(75)
|
||||||
|
.padding([8, 0])
|
||||||
|
.align_x(Alignment::Center);
|
||||||
|
|
||||||
|
Self::new(content_list)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,116 +0,0 @@
|
||||||
// Spinner for integer selection
|
|
||||||
// Author: Bryan Hyland <bryan.hyland32@gmail.com>
|
|
||||||
|
|
||||||
use crate::iced::alignment::Horizontal;
|
|
||||||
use crate::iced::Alignment;
|
|
||||||
use crate::widget::{button, column, icon, row, text};
|
|
||||||
use crate::Element;
|
|
||||||
use std::fmt::Display;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::ops::{Add, Sub};
|
|
||||||
|
|
||||||
/// Spinner is the state/model of the vertical spinner widget.
|
|
||||||
/// Restricts T to Add, Sub, Eq, Ord, Display, and Copy so that
|
|
||||||
/// T can only be numerical values.
|
|
||||||
pub struct VerticalSpinner<'a, T, M>
|
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
label: String,
|
|
||||||
step: T,
|
|
||||||
value: T,
|
|
||||||
min: T,
|
|
||||||
max: T,
|
|
||||||
on_select: Box<dyn Fn(T) -> M>,
|
|
||||||
phantom_data: PhantomData<&'a M>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T, M> VerticalSpinner<'a, T, M>
|
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
/// Creates a new vertical spinner widget
|
|
||||||
pub fn new(
|
|
||||||
label: impl Into<String>,
|
|
||||||
step: T,
|
|
||||||
value: T,
|
|
||||||
min: T,
|
|
||||||
max: T,
|
|
||||||
on_select: impl Fn(T) -> M + 'static,
|
|
||||||
) -> Self {
|
|
||||||
VerticalSpinner {
|
|
||||||
label: label.into(),
|
|
||||||
step,
|
|
||||||
value,
|
|
||||||
min,
|
|
||||||
max,
|
|
||||||
on_select: Box::new(on_select),
|
|
||||||
phantom_data: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn vertical_spinner<T, M>(
|
|
||||||
label: impl Into<String>,
|
|
||||||
step: T,
|
|
||||||
value: T,
|
|
||||||
min: T,
|
|
||||||
max: T,
|
|
||||||
on_select: impl Fn(T) -> M + 'static,
|
|
||||||
) -> VerticalSpinner<'static, T, M>
|
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
VerticalSpinner::new(label, step, value, min, max, on_select)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn increment<T, Message>(step: T, val: T, min: T, max: T) -> T
|
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
std::cmp::min(std::cmp::max(val + step, min), max)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decrement<T, Message>(step: T, val: T, min: T, max: T) -> T
|
|
||||||
where
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
std::cmp::max(std::cmp::min(val - step, max), min)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T, Message> From<VerticalSpinner<'a, T, Message>> for Element<'a, Message>
|
|
||||||
where
|
|
||||||
Message: Clone + 'static,
|
|
||||||
T: Add<Output = T> + Sub<Output = T> + Eq + Ord + Display + Copy,
|
|
||||||
{
|
|
||||||
fn from(this: VerticalSpinner<T, Message>) -> Self {
|
|
||||||
let val_text = text(format!("{}", this.value)).size(14);
|
|
||||||
let spinner_container = column::with_capacity(3)
|
|
||||||
.push(
|
|
||||||
button::icon(icon::from_name("list-add-symbolic"))
|
|
||||||
.padding([0, 12])
|
|
||||||
.on_press((this.on_select)(increment::<T, Message>(
|
|
||||||
this.step, this.value, this.min, this.max,
|
|
||||||
))),
|
|
||||||
)
|
|
||||||
.push(val_text)
|
|
||||||
.push(
|
|
||||||
button::icon(icon::from_name("list-remove-symbolic"))
|
|
||||||
.padding([0, 12])
|
|
||||||
.on_press((this.on_select)(decrement::<T, Message>(
|
|
||||||
this.step, this.value, this.min, this.max,
|
|
||||||
))),
|
|
||||||
)
|
|
||||||
.align_x(Horizontal::Center);
|
|
||||||
|
|
||||||
let content_list = column::with_children(vec![
|
|
||||||
row::with_capacity(1).push(text(this.label)).into(),
|
|
||||||
row::with_children(vec![Element::from(spinner_container)]).into(),
|
|
||||||
])
|
|
||||||
.width(75)
|
|
||||||
.padding([8, 0])
|
|
||||||
.align_x(Alignment::Center);
|
|
||||||
|
|
||||||
Self::new(content_list)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue