Resolved vertical vs vertical_spin_button function API

This commit is contained in:
Bryan Hyland 2024-11-18 11:16:31 -08:00
parent 6e0736dbfc
commit f173996f12
3 changed files with 13 additions and 17 deletions

View file

@ -1,4 +1,4 @@
use cosmic::widget::{button, container, text, spin_button, vertical_spin_button, /*spin_button::Orientation*/}; use cosmic::widget::{button, container, text, spin_button, vertical};
use cosmic::{ use cosmic::{
app::{Core, Task}, app::{Core, Task},
iced::{ iced::{
@ -85,7 +85,7 @@ impl Application for SpinButtonExamplApp {
fn view(&self) -> Element<Self::Message> { fn view(&self) -> Element<Self::Message> {
let vert_spinner_row = row![ let vert_spinner_row = row![
vertical_spin_button( vertical(
"i8", // label: displayed above the widget no matter the orientation "i8", // label: displayed above the widget no matter the orientation
1, // step: how much to increment/decrement by 1, // step: how much to increment/decrement by
self.i8_num, // current value, this is also what's displayed in the center of the widget self.i8_num, // current value, this is also what's displayed in the center of the widget
@ -93,7 +93,7 @@ impl Application for SpinButtonExamplApp {
5, // maximum value, if incremented above this the widget's current value rolls to the min value 5, // maximum value, if incremented above this the widget's current value rolls to the min value
SpinBtnMessages::UpdateI8Num // message to send to the application's update function SpinBtnMessages::UpdateI8Num // message to send to the application's update function
), ),
vertical_spin_button( vertical(
"i16", "i16",
1, 1,
self.i16_num, self.i16_num,
@ -101,7 +101,7 @@ impl Application for SpinButtonExamplApp {
10, 10,
SpinBtnMessages::UpdateI16Num SpinBtnMessages::UpdateI16Num
), ),
vertical_spin_button( vertical(
"i32", "i32",
1, 1,
self.i32_num, self.i32_num,
@ -109,7 +109,7 @@ impl Application for SpinButtonExamplApp {
12, 12,
SpinBtnMessages::UpdateI32Num SpinBtnMessages::UpdateI32Num
), ),
vertical_spin_button( vertical(
"i64", "i64",
10, 10,
self.i64_num, self.i64_num,

View file

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

View file

@ -48,8 +48,8 @@ where
/// The maximum value permitted. /// The maximum value permitted.
/// If the value is incremented above this value the current value will rollover to the min value. /// If the value is incremented above this value the current value will rollover to the min value.
max: T, max: T,
/// The direction that the spin button is laid out; Horizontal (default) or Vertical /// The direction that the spin button is laid out; Orientation::Horizontal or Orientation::Vertical
orientation: Option<Orientation>, orientation: Orientation,
/// The message that the spin button emits to the application's update function. /// The message that the spin button emits to the application's update function.
on_press: Box<dyn Fn(T) -> M>, on_press: Box<dyn Fn(T) -> M>,
phantom_data: PhantomData<&'a M>, phantom_data: PhantomData<&'a M>,
@ -66,7 +66,7 @@ where
value: T, value: T,
min: T, min: T,
max: T, max: T,
orientation: Option<Orientation>, orientation: Orientation,
on_press: impl Fn(T) -> M + 'static, on_press: impl Fn(T) -> M + 'static,
) -> Self { ) -> Self {
Self { Self {
@ -94,11 +94,11 @@ pub fn spin_button<'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, None, 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
pub fn vertical_spin_button<'a, T, M>( pub fn vertical<'a, T, M>(
label: impl Into<String>, label: impl Into<String>,
step: T, step: T,
value: T, value: T,
@ -109,7 +109,7 @@ pub fn vertical_spin_button<'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, Some(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
@ -145,12 +145,8 @@ where
// 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 {
Some(orien) => match orien {
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),
}
// Default behavior is to create a horizontal spin button widget
None => create_horizontal_spin_button(&this),
} }
} }
} }