|
1 | 1 | use iced::{ |
2 | | - button, Align, Application, Background, Button, Color, Column, Command, |
3 | | - Container, Element, HorizontalAlignment, Length, Row, Settings, |
4 | | - Subscription, Text, |
| 2 | + button, Align, Application, Button, Column, Command, Container, Element, |
| 3 | + HorizontalAlignment, Length, Row, Settings, Subscription, Text, |
5 | 4 | }; |
6 | 5 | use std::time::{Duration, Instant}; |
7 | 6 |
|
@@ -98,30 +97,29 @@ impl Application for Stopwatch { |
98 | 97 | )) |
99 | 98 | .size(40); |
100 | 99 |
|
101 | | - let button = |state, label, color: [f32; 3]| { |
| 100 | + let button = |state, label, style| { |
102 | 101 | Button::new( |
103 | 102 | state, |
104 | 103 | Text::new(label) |
105 | | - .color(Color::WHITE) |
106 | 104 | .horizontal_alignment(HorizontalAlignment::Center), |
107 | 105 | ) |
108 | 106 | .min_width(80) |
109 | | - .background(Background::Color(color.into())) |
110 | | - .border_radius(10) |
111 | 107 | .padding(10) |
| 108 | + .style(style) |
112 | 109 | }; |
113 | 110 |
|
114 | 111 | let toggle_button = { |
115 | 112 | let (label, color) = match self.state { |
116 | | - State::Idle => ("Start", [0.11, 0.42, 0.87]), |
117 | | - State::Ticking { .. } => ("Stop", [0.9, 0.4, 0.4]), |
| 113 | + State::Idle => ("Start", style::Button::Primary), |
| 114 | + State::Ticking { .. } => ("Stop", style::Button::Destructive), |
118 | 115 | }; |
119 | 116 |
|
120 | 117 | button(&mut self.toggle, label, color).on_press(Message::Toggle) |
121 | 118 | }; |
122 | 119 |
|
123 | | - let reset_button = button(&mut self.reset, "Reset", [0.7, 0.7, 0.7]) |
124 | | - .on_press(Message::Reset); |
| 120 | + let reset_button = |
| 121 | + button(&mut self.reset, "Reset", style::Button::Secondary) |
| 122 | + .on_press(Message::Reset); |
125 | 123 |
|
126 | 124 | let controls = Row::new() |
127 | 125 | .spacing(20) |
@@ -177,3 +175,29 @@ mod time { |
177 | 175 | } |
178 | 176 | } |
179 | 177 | } |
| 178 | + |
| 179 | +mod style { |
| 180 | + use iced::{button, Background, Color, Vector}; |
| 181 | + |
| 182 | + pub enum Button { |
| 183 | + Primary, |
| 184 | + Secondary, |
| 185 | + Destructive, |
| 186 | + } |
| 187 | + |
| 188 | + impl button::StyleSheet for Button { |
| 189 | + fn active(&self) -> button::Style { |
| 190 | + button::Style { |
| 191 | + background: Some(Background::Color(match self { |
| 192 | + Button::Primary => Color::from_rgb(0.11, 0.42, 0.87), |
| 193 | + Button::Secondary => Color::from_rgb(0.5, 0.5, 0.5), |
| 194 | + Button::Destructive => Color::from_rgb(0.8, 0.2, 0.2), |
| 195 | + })), |
| 196 | + border_radius: 12, |
| 197 | + shadow_offset: Vector::new(1.0, 1.0), |
| 198 | + text_color: Color::WHITE, |
| 199 | + ..button::Style::default() |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments