Skip to content

Commit 65f4422

Browse files
bhh32mmstick
authored andcommitted
feat!(dropdown): refactor and support vertical widget variant
1 parent 2b7d171 commit 65f4422

File tree

5 files changed

+422
-233
lines changed

5 files changed

+422
-233
lines changed

examples/spin-button/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "spin-button"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies.libcosmic]
7+
features = ["debug", "multi-window", "wayland", "winit", "desktop", "tokio"]
8+
path = "../.."
9+
default-features = false

examples/spin-button/src/main.rs

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
use cosmic::widget::{button, container, spin_button, text};
2+
use cosmic::{
3+
app::{Core, Task},
4+
iced::{
5+
alignment::{Horizontal, Vertical},
6+
widget::{column, row, vertical_space},
7+
Alignment, Size,
8+
},
9+
Application, Element,
10+
};
11+
12+
pub struct SpinButtonExamplApp {
13+
core: Core,
14+
i8_num: i8,
15+
i16_num: i16,
16+
i32_num: i32,
17+
i64_num: i64,
18+
i128_num: i128,
19+
f32_num: f32,
20+
f64_num: f64,
21+
spinner_msg: String,
22+
}
23+
24+
#[derive(Debug, Clone)]
25+
pub enum SpinBtnMessages {
26+
UpdateI8Num(i8),
27+
UpdateI16Num(i16),
28+
UpdateI32Num(i32),
29+
UpdateI64Num(i64),
30+
UpdateI128Num(i128),
31+
UpdateF32Num(f32),
32+
UpdateF64Num(f64),
33+
UpdateSpinnerMsg,
34+
}
35+
36+
impl Application for SpinButtonExamplApp {
37+
type Executor = cosmic::executor::Default;
38+
type Flags = ();
39+
type Message = SpinBtnMessages;
40+
41+
const APP_ID: &'static str = "com.system76.SpinButtonExample";
42+
43+
fn core(&self) -> &Core {
44+
&self.core
45+
}
46+
47+
fn core_mut(&mut self) -> &mut Core {
48+
&mut self.core
49+
}
50+
51+
fn init(core: Core, _flags: Self::Flags) -> (Self, Task<Self::Message>) {
52+
(
53+
Self {
54+
core,
55+
i8_num: 0,
56+
i16_num: 0,
57+
i32_num: 0,
58+
i64_num: 0,
59+
i128_num: 0,
60+
f32_num: 0.,
61+
f64_num: 0.,
62+
spinner_msg: String::new(),
63+
},
64+
Task::none(),
65+
)
66+
}
67+
68+
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
69+
match message {
70+
SpinBtnMessages::UpdateI8Num(new_i8) => self.i8_num = new_i8,
71+
SpinBtnMessages::UpdateI16Num(new_i16) => self.i16_num = new_i16,
72+
SpinBtnMessages::UpdateI32Num(new_i32) => self.i32_num = new_i32,
73+
SpinBtnMessages::UpdateI64Num(new_i64) => self.i64_num = new_i64,
74+
SpinBtnMessages::UpdateI128Num(new_i128) => self.i128_num = new_i128,
75+
SpinBtnMessages::UpdateF32Num(new_f32) => self.f32_num = new_f32,
76+
SpinBtnMessages::UpdateF64Num(new_f64) => self.f64_num = new_f64,
77+
SpinBtnMessages::UpdateSpinnerMsg => {
78+
self.spinner_msg = format!(
79+
"i8: {}, i16: {}, i32: {}, i64: {}, i128: {}\nf32: {}, f64: {}",
80+
self.i8_num,
81+
self.i16_num,
82+
self.i32_num,
83+
self.i64_num,
84+
self.i128_num,
85+
self.f32_num,
86+
self.f64_num
87+
);
88+
}
89+
}
90+
91+
Task::none()
92+
}
93+
94+
fn view(&self) -> Element<Self::Message> {
95+
let vert_spinner_row = row![
96+
spin_button::vertical(
97+
"i8", // label: displayed above the widget no matter the orientation
98+
1, // step: how much to increment/decrement by
99+
self.i8_num, // current value, this is also what's displayed in the center of the widget
100+
-5, // minimum value, if decremented below this the widget's current value rolls to the max value
101+
5, // maximum value, if incremented above this the widget's current value rolls to the min value
102+
SpinBtnMessages::UpdateI8Num // message to send to the application's update function
103+
),
104+
spin_button::vertical("i16", 1, self.i16_num, 0, 10, SpinBtnMessages::UpdateI16Num),
105+
spin_button::vertical("i32", 1, self.i32_num, 0, 12, SpinBtnMessages::UpdateI32Num),
106+
spin_button::vertical(
107+
"i64",
108+
10,
109+
self.i64_num,
110+
15,
111+
35,
112+
SpinBtnMessages::UpdateI64Num
113+
),
114+
]
115+
.align_y(Vertical::Center);
116+
117+
let horiz_spinner_row = column![
118+
row![
119+
// This function can be called instead if a Horizontal Spin Button is needed.
120+
spin_button(
121+
"i128",
122+
100,
123+
self.i128_num,
124+
-1000,
125+
500,
126+
SpinBtnMessages::UpdateI128Num
127+
),
128+
],
129+
vertical_space().height(5),
130+
row![spin_button(
131+
"f32",
132+
1.3,
133+
self.f32_num,
134+
-35.3,
135+
12.3,
136+
SpinBtnMessages::UpdateF32Num
137+
)],
138+
vertical_space().height(5),
139+
row![spin_button(
140+
"f64",
141+
1.3,
142+
self.f64_num,
143+
0.0,
144+
3.0,
145+
SpinBtnMessages::UpdateF64Num
146+
)],
147+
]
148+
.align_x(Alignment::Center);
149+
150+
let status_row = row![text(self.spinner_msg.clone()),];
151+
152+
let final_col = column![
153+
vert_spinner_row,
154+
vertical_space().height(5),
155+
horiz_spinner_row,
156+
button::standard("Show Spinner Values Passed")
157+
.on_press(SpinBtnMessages::UpdateSpinnerMsg),
158+
vertical_space().height(10),
159+
status_row,
160+
]
161+
.align_x(Alignment::Center);
162+
163+
container(final_col)
164+
.align_x(Horizontal::Center)
165+
.align_y(Vertical::Center)
166+
.into()
167+
}
168+
}
169+
170+
fn main() -> Result<(), Box<dyn std::error::Error>> {
171+
let settings = cosmic::app::Settings::default().size(Size::new(550., 1024.));
172+
cosmic::app::run::<SpinButtonExamplApp>(settings, ())?;
173+
174+
Ok(())
175+
}

src/widget/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ pub mod settings;
311311

312312
pub mod spin_button;
313313
#[doc(inline)]
314-
pub use spin_button::{spin_button, SpinButton};
314+
pub use spin_button::{spin_button, vertical as vertical_spin_button, SpinButton};
315315

316316
pub mod tab_bar;
317317

0 commit comments

Comments
 (0)