Updated formatting
This commit is contained in:
parent
e7762e16f1
commit
83a6450776
1 changed files with 114 additions and 93 deletions
|
|
@ -6,23 +6,16 @@
|
||||||
|
|
||||||
//! A control for incremental adjustments of a value.
|
//! A control for incremental adjustments of a value.
|
||||||
|
|
||||||
use std::fmt::Display;
|
use crate::{
|
||||||
use std::ops::{Add, Sub};
|
widget::{button, column, container, icon, row, text},
|
||||||
use std::marker::PhantomData;
|
Element,
|
||||||
|
};
|
||||||
use apply::Apply;
|
use apply::Apply;
|
||||||
use iced::alignment::Horizontal;
|
use iced::alignment::Horizontal;
|
||||||
use iced::{Alignment, Length};
|
use iced::{Alignment, Length};
|
||||||
use crate::{
|
use std::fmt::Display;
|
||||||
Element,
|
use std::marker::PhantomData;
|
||||||
widget::{
|
use std::ops::{Add, Sub};
|
||||||
button,
|
|
||||||
column,
|
|
||||||
container,
|
|
||||||
icon,
|
|
||||||
row,
|
|
||||||
text,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Orientation {
|
enum Orientation {
|
||||||
|
|
@ -32,7 +25,7 @@ enum Orientation {
|
||||||
|
|
||||||
pub struct SpinButton<'a, T, M>
|
pub struct SpinButton<'a, T, M>
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
/// The label that the spin button widget will have.
|
/// The label that the spin button widget will have.
|
||||||
/// It is placed on the top of and centered on the spin button widget itself.
|
/// It is placed on the top of and centered on the spin button widget itself.
|
||||||
|
|
@ -57,7 +50,7 @@ where
|
||||||
|
|
||||||
impl<'a, T, M> SpinButton<'a, T, M>
|
impl<'a, T, M> SpinButton<'a, T, M>
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
/// Create a new new button
|
/// Create a new new button
|
||||||
fn new(
|
fn new(
|
||||||
|
|
@ -92,9 +85,17 @@ pub fn spin_button<'a, T, M>(
|
||||||
on_press: impl Fn(T) -> M + 'static,
|
on_press: impl Fn(T) -> M + 'static,
|
||||||
) -> SpinButton<'a, T, M>
|
) -> SpinButton<'a, T, M>
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
SpinButton::new(label, step, value, min, max, Orientation::Horizontal, on_press)
|
SpinButton::new(
|
||||||
|
label,
|
||||||
|
step,
|
||||||
|
value,
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
Orientation::Horizontal,
|
||||||
|
on_press,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shorthand to create a standard (horizontal) spin button widget
|
/// Shorthand to create a standard (horizontal) spin button widget
|
||||||
|
|
@ -107,14 +108,22 @@ pub fn vertical<'a, T, M>(
|
||||||
on_press: impl Fn(T) -> M + 'static,
|
on_press: impl Fn(T) -> M + 'static,
|
||||||
) -> SpinButton<'a, T, M>
|
) -> SpinButton<'a, T, M>
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
SpinButton::new(label, step, value, min, max, Orientation::Vertical, on_press)
|
SpinButton::new(
|
||||||
|
label,
|
||||||
|
step,
|
||||||
|
value,
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
Orientation::Vertical,
|
||||||
|
on_press,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn increment<T>(step: T, value: T, min: T, max: T) -> T
|
fn increment<T>(step: T, value: T, min: T, max: T) -> T
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
//! Make it roll over back to min if the increase is too high
|
//! Make it roll over back to min if the increase is too high
|
||||||
if value + step > max {
|
if value + step > max {
|
||||||
|
|
@ -126,7 +135,7 @@ where
|
||||||
|
|
||||||
fn decrement<T>(step: T, value: T, min: T, max: T) -> T
|
fn decrement<T>(step: T, value: T, min: T, max: T) -> T
|
||||||
where
|
where
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
//! Make it roll over back to max if the decrese is too low
|
//! Make it roll over back to max if the decrese is too low
|
||||||
if value - step < min {
|
if value - step < min {
|
||||||
|
|
@ -139,14 +148,14 @@ where
|
||||||
impl<'a, T, Message> From<SpinButton<'a, T, Message>> for Element<'a, Message>
|
impl<'a, T, Message> From<SpinButton<'a, T, Message>> for Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: Clone + 'static,
|
Message: Clone + 'static,
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
fn from(this: SpinButton<'a, T, Message>) -> Self {
|
fn from(this: SpinButton<'a, T, Message>) -> Self {
|
||||||
// Matching on the direction enum given by the developer when the
|
// Matching on the direction enum given by the developer when the
|
||||||
// widget is initially created in the application's view function.
|
// widget is initially created in the application's view function.
|
||||||
match this.orientation {
|
match this.orientation {
|
||||||
Orientation::Horizontal => create_horizontal_spin_button(&this),
|
Orientation::Horizontal => create_horizontal_spin_button(&this),
|
||||||
Orientation::Vertical => create_vertical_spin_button(&this),
|
Orientation::Vertical => create_vertical_spin_button(&this),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -154,52 +163,54 @@ where
|
||||||
// Helper Functions
|
// Helper Functions
|
||||||
// Create a horizontal spin button
|
// Create a horizontal spin button
|
||||||
// Implemented to make the creation easier to read in the from function for Element implementation.
|
// Implemented to make the creation easier to read in the from function for Element implementation.
|
||||||
fn create_horizontal_spin_button<'a, T, Message>(spin_btn: &SpinButton<T, Message>) -> Element<'a, Message>
|
fn create_horizontal_spin_button<'a, T, Message>(
|
||||||
|
spin_btn: &SpinButton<T, Message>,
|
||||||
|
) -> Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: Clone + 'static,
|
Message: Clone + 'static,
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
// Create a spinner container variable that contains the row with all of
|
// Create a spinner container variable that contains the row with all of
|
||||||
// the combined widgets that make up the widget.
|
// the combined widgets that make up the widget.
|
||||||
let spinner_container = column::with_capacity(2)
|
let spinner_container = column::with_capacity(2)
|
||||||
|
.push(row::with_children(vec![
|
||||||
|
// Using the title4 variant of text, just like the original spin button did.
|
||||||
|
text::title4(spin_btn.label.clone())
|
||||||
|
.apply(container)
|
||||||
|
.center_x(Length::Fill)
|
||||||
|
.align_y(Alignment::Center)
|
||||||
|
.into(),
|
||||||
|
]))
|
||||||
.push(
|
.push(
|
||||||
row::with_children(
|
row::with_children(vec![
|
||||||
vec![
|
// Using an button instead of an icon for the decrement functionality.
|
||||||
// Using the title4 variant of text, just like the original spin button did.
|
button::icon(icon::from_name("list-remove-symbolic"))
|
||||||
text::title4(spin_btn.label.clone())
|
.padding([0, 12])
|
||||||
|
.on_press((spin_btn.on_press)(decrement::<T>(
|
||||||
|
spin_btn.step,
|
||||||
|
spin_btn.value,
|
||||||
|
spin_btn.min,
|
||||||
|
spin_btn.max,
|
||||||
|
)))
|
||||||
|
.into(),
|
||||||
|
// Using the title4 variant of text for consistency.
|
||||||
|
text::title4(format!("{}", spin_btn.value))
|
||||||
.apply(container)
|
.apply(container)
|
||||||
.center_x(Length::Fill)
|
.center_x(Length::Fixed(48.0))
|
||||||
.align_y(Alignment::Center)
|
.align_y(Alignment::Center)
|
||||||
.into(),
|
.into(),
|
||||||
]
|
// Using another button for the increment functionality.
|
||||||
)
|
button::icon(icon::from_name("list-add-symbolic"))
|
||||||
)
|
.padding([0, 12])
|
||||||
.push(
|
.on_press((spin_btn.on_press)(increment::<T>(
|
||||||
row::with_children(
|
spin_btn.step,
|
||||||
vec![
|
spin_btn.value,
|
||||||
// Using an button instead of an icon for the decrement functionality.
|
spin_btn.min,
|
||||||
button::icon(icon::from_name("list-remove-symbolic"))
|
spin_btn.max,
|
||||||
.padding([0, 12])
|
)))
|
||||||
.on_press((spin_btn.on_press)(decrement::<T>(
|
.into(),
|
||||||
spin_btn.step, spin_btn.value, spin_btn.min, spin_btn.max,
|
])
|
||||||
))
|
.align_y(Alignment::Center),
|
||||||
).into(),
|
|
||||||
// Using the title4 variant of text for consistency.
|
|
||||||
text::title4(format!("{}", spin_btn.value))
|
|
||||||
.apply(container)
|
|
||||||
.center_x(Length::Fixed(48.0))
|
|
||||||
.align_y(Alignment::Center)
|
|
||||||
.into(),
|
|
||||||
// Using another button for the increment functionality.
|
|
||||||
button::icon(icon::from_name("list-add-symbolic"))
|
|
||||||
.padding([0, 12])
|
|
||||||
.on_press((spin_btn.on_press)(increment::<T>(
|
|
||||||
spin_btn.step, spin_btn.value, spin_btn.min, spin_btn.max,
|
|
||||||
))
|
|
||||||
).into(),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
.align_y(Alignment::Center)
|
|
||||||
)
|
)
|
||||||
.align_x(Alignment::Center);
|
.align_x(Alignment::Center);
|
||||||
|
|
||||||
|
|
@ -209,41 +220,51 @@ where
|
||||||
|
|
||||||
// Used to create a vertical spin button widget.
|
// Used to create a vertical spin button widget.
|
||||||
// Implemented to make the creation easier to read in the from function for Element implementation.
|
// Implemented to make the creation easier to read in the from function for Element implementation.
|
||||||
fn create_vertical_spin_button<'a, T, Message>(spin_btn: &SpinButton<T, Message>) -> Element<'a, Message>
|
fn create_vertical_spin_button<'a, T, Message>(
|
||||||
|
spin_btn: &SpinButton<T, Message>,
|
||||||
|
) -> Element<'a, Message>
|
||||||
where
|
where
|
||||||
Message: Clone + 'static,
|
Message: Clone + 'static,
|
||||||
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy
|
T: Add<Output = T> + Sub<Output = T> + PartialEq + PartialOrd + Display + Copy,
|
||||||
{
|
{
|
||||||
// Create a text widget that holds the value
|
// Create a text widget that holds the value
|
||||||
let val_text = text::title4(format!("{}", spin_btn.value));
|
let val_text = text::title4(format!("{}", spin_btn.value));
|
||||||
// Create a spinner container variable that contains the column with all of
|
// Create a spinner container variable that contains the column with all of
|
||||||
// the combined widgets that make up the widget.
|
// the combined widgets that make up the widget.
|
||||||
let spinner_container = column::with_capacity(3)
|
let spinner_container = column::with_capacity(3)
|
||||||
.push(
|
.push(
|
||||||
// Use a button for the increment functionality
|
// Use a button for the increment functionality
|
||||||
button::icon(icon::from_name("list-add-symbolic"))
|
button::icon(icon::from_name("list-add-symbolic"))
|
||||||
.padding([5, 0])
|
.padding([5, 0])
|
||||||
.on_press((spin_btn.on_press)(increment::<T>(
|
.on_press((spin_btn.on_press)(increment::<T>(
|
||||||
spin_btn.step, spin_btn.value, spin_btn.min, spin_btn.max,
|
spin_btn.step,
|
||||||
))),
|
spin_btn.value,
|
||||||
)
|
spin_btn.min,
|
||||||
// Add the text widget that holds the current value
|
spin_btn.max,
|
||||||
.push(val_text)
|
))),
|
||||||
.push(
|
)
|
||||||
// Use a button for the decrement functionality
|
// Add the text widget that holds the current value
|
||||||
button::icon(icon::from_name("list-remove-symbolic"))
|
.push(val_text)
|
||||||
.padding([5, 0])
|
.push(
|
||||||
.on_press((spin_btn.on_press)(decrement::<T>(
|
// Use a button for the decrement functionality
|
||||||
spin_btn.step, spin_btn.value, spin_btn.min, spin_btn.max,
|
button::icon(icon::from_name("list-remove-symbolic"))
|
||||||
))),
|
.padding([5, 0])
|
||||||
)
|
.on_press((spin_btn.on_press)(decrement::<T>(
|
||||||
.align_x(Horizontal::Center);
|
spin_btn.step,
|
||||||
|
spin_btn.value,
|
||||||
|
spin_btn.min,
|
||||||
|
spin_btn.max,
|
||||||
|
))),
|
||||||
|
)
|
||||||
|
.align_x(Horizontal::Center);
|
||||||
|
|
||||||
// Create a column that contains two rows:
|
// Create a column that contains two rows:
|
||||||
// First Row -> The label/title for the spin button.
|
// First Row -> The label/title for the spin button.
|
||||||
// Second Row -> The spin button container from above.
|
// Second Row -> The spin button container from above.
|
||||||
let content_list = column::with_children(vec![
|
let content_list = column::with_children(vec![
|
||||||
row::with_capacity(1).push(text::title4(spin_btn.label.clone())).into(),
|
row::with_capacity(1)
|
||||||
|
.push(text::title4(spin_btn.label.clone()))
|
||||||
|
.into(),
|
||||||
row::with_children(vec![Element::from(spinner_container)]).into(),
|
row::with_children(vec![Element::from(spinner_container)]).into(),
|
||||||
])
|
])
|
||||||
.width(75)
|
.width(75)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue