summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-01-06 21:05:29 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-01-06 21:05:29 +0100
commit8fc57628a0e95c8154c5000fd7ef92fa3c3cd82d (patch)
tree3ab6ad3a760af058178d6c8b066c67450128e0a0 /examples
parentedbb318cbd0d2f6263976eed40d58ce2c3468c7d (diff)
downloadiced-8fc57628a0e95c8154c5000fd7ef92fa3c3cd82d.tar.gz
iced-8fc57628a0e95c8154c5000fd7ef92fa3c3cd82d.tar.bz2
iced-8fc57628a0e95c8154c5000fd7ef92fa3c3cd82d.zip
Showcase different `button` styles in `styling` example
Diffstat (limited to 'examples')
-rw-r--r--examples/styling/src/main.rs54
1 files changed, 49 insertions, 5 deletions
diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs
index 534f5e32..594be4a7 100644
--- a/examples/styling/src/main.rs
+++ b/examples/styling/src/main.rs
@@ -1,12 +1,14 @@
+use iced::keyboard;
use iced::widget::{
button, center, checkbox, column, horizontal_rule, pick_list, progress_bar,
row, scrollable, slider, text, text_input, toggler, vertical_rule,
vertical_space,
};
-use iced::{Center, Element, Fill, Theme};
+use iced::{Center, Element, Fill, Subscription, Theme};
pub fn main() -> iced::Result {
iced::application("Styling - Iced", Styling::update, Styling::view)
+ .subscription(Styling::subscription)
.theme(Styling::theme)
.run()
}
@@ -28,6 +30,8 @@ enum Message {
SliderChanged(f32),
CheckboxToggled(bool),
TogglerToggled(bool),
+ PreviousTheme,
+ NextTheme,
}
impl Styling {
@@ -41,6 +45,23 @@ impl Styling {
Message::SliderChanged(value) => self.slider_value = value,
Message::CheckboxToggled(value) => self.checkbox_value = value,
Message::TogglerToggled(value) => self.toggler_value = value,
+ Message::PreviousTheme | Message::NextTheme => {
+ if let Some(current) = Theme::ALL
+ .iter()
+ .position(|candidate| &self.theme == candidate)
+ {
+ self.theme = if matches!(message, Message::NextTheme) {
+ Theme::ALL[(current + 1) % Theme::ALL.len()].clone()
+ } else if current == 0 {
+ Theme::ALL
+ .last()
+ .expect("Theme::ALL must not be empty")
+ .clone()
+ } else {
+ Theme::ALL[current - 1].clone()
+ };
+ }
+ }
}
}
@@ -57,9 +78,16 @@ impl Styling {
.padding(10)
.size(20);
- let button = button("Submit")
- .padding(10)
- .on_press(Message::ButtonPressed);
+ let styled_button = |label| {
+ button(text(label).width(Fill).center())
+ .padding(10)
+ .on_press(Message::ButtonPressed)
+ };
+
+ let primary = styled_button("Primary");
+ let success = styled_button("Success").style(button::success);
+ let warning = styled_button("Warning").style(button::warning);
+ let danger = styled_button("Danger").style(button::danger);
let slider =
slider(0.0..=100.0, self.slider_value, Message::SliderChanged);
@@ -85,7 +113,10 @@ impl Styling {
let content = column![
choose_theme,
horizontal_rule(38),
- row![text_input, button].spacing(10).align_y(Center),
+ text_input,
+ row![primary, success, warning, danger]
+ .spacing(10)
+ .align_y(Center),
slider,
progress_bar,
row![
@@ -104,6 +135,19 @@ impl Styling {
center(content).into()
}
+ fn subscription(&self) -> Subscription<Message> {
+ keyboard::on_key_press(|key, _modifiers| match key {
+ keyboard::Key::Named(
+ keyboard::key::Named::ArrowUp | keyboard::key::Named::ArrowLeft,
+ ) => Some(Message::PreviousTheme),
+ keyboard::Key::Named(
+ keyboard::key::Named::ArrowDown
+ | keyboard::key::Named::ArrowRight,
+ ) => Some(Message::NextTheme),
+ _ => None,
+ })
+ }
+
fn theme(&self) -> Theme {
self.theme.clone()
}