| 1 | //! Icons and battery rings, drawn as vector paths so they take any theme color and stay sharp at |
| 2 | //! any scale. Icons are designed on a 24 × 24 grid. |
| 3 | |
| 4 | use std::f32::consts::{FRAC_PI_2, TAU}; |
| 5 | |
| 6 | use iced::widget::canvas::{self, Frame, LineCap, LineJoin, Path, Stroke, path::Arc}; |
| 7 | use iced::{Color, Element, Point, Radians, Rectangle, Renderer, Size, Theme, mouse}; |
| 8 | |
| 9 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 10 | pub enum Icon { |
| 11 | Overview, |
| 12 | Equalizer, |
| 13 | Gestures, |
| 14 | Earbud, |
| 15 | Settings, |
| 16 | NoiseOff, |
| 17 | Anc, |
| 18 | Transparency, |
| 19 | Game, |
| 20 | Spatial, |
| 21 | Bass, |
| 22 | Wind, |
| 23 | Dual, |
| 24 | Chevron, |
| 25 | Bolt, |
| 26 | Check, |
| 27 | Find, |
| 28 | Info, |
| 29 | Chip, |
| 30 | Keys, |
| 31 | Pencil, |
| 32 | Trash, |
| 33 | Laptop, |
| 34 | Phone, |
| 35 | } |
| 36 | |
| 37 | /// `icon` at `size` pixels, in `color`. |
| 38 | pub fn icon<'a, Message: 'a>(icon: Icon, size: f32, color: Color) -> Element<'a, Message> { |
| 39 | iced::widget::canvas(Glyph { icon, color }) |
| 40 | .width(size) |
| 41 | .height(size) |
| 42 | .into() |
| 43 | } |
| 44 | |
| 45 | struct Glyph { |
| 46 | icon: Icon, |
| 47 | color: Color, |
| 48 | } |
| 49 | |
| 50 | impl<Message> canvas::Program<Message> for Glyph { |
| 51 | type State = (); |
| 52 | |
| 53 | fn draw( |
| 54 | &self, |
| 55 | _state: &(), |
| 56 | renderer: &Renderer, |
| 57 | _theme: &Theme, |
| 58 | bounds: Rectangle, |
| 59 | _cursor: mouse::Cursor, |
| 60 | ) -> Vec<canvas::Geometry> { |
| 61 | let mut frame = Frame::new(renderer, bounds.size()); |
| 62 | let mut pen = Pen::new(&mut frame, Point::ORIGIN, bounds.width, self.color); |
| 63 | draw_icon(&mut pen, self.icon); |
| 64 | vec![frame.into_geometry()] |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Draws on a 24 × 24 grid placed at `origin`, `size` pixels wide. |
| 69 | struct Pen<'f> { |
| 70 | frame: &'f mut Frame, |
| 71 | origin: Point, |
| 72 | scale: f32, |
| 73 | color: Color, |
| 74 | width: f32, |
| 75 | } |
| 76 | |
| 77 | impl<'f> Pen<'f> { |
| 78 | fn new(frame: &'f mut Frame, origin: Point, size: f32, color: Color) -> Self { |
| 79 | let scale = size / 24.0; |
| 80 | Pen { |
| 81 | frame, |
| 82 | origin, |
| 83 | scale, |
| 84 | color, |
| 85 | width: 1.9 * scale, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn at(&self, x: f32, y: f32) -> Point { |
| 90 | Point::new(self.origin.x + x * self.scale, self.origin.y + y * self.scale) |
| 91 | } |
| 92 | |
| 93 | fn stroke(&mut self, path: &Path) { |
| 94 | let stroke = Stroke::default() |
| 95 | .with_color(self.color) |
| 96 | .with_width(self.width) |
| 97 | .with_line_cap(LineCap::Round) |
| 98 | .with_line_join(LineJoin::Round); |
| 99 | self.frame.stroke(path, stroke); |
| 100 | } |
| 101 | |
| 102 | fn polyline(&mut self, points: &[(f32, f32)], closed: bool) { |
| 103 | let path = Path::new(|b| { |
| 104 | for (n, &(x, y)) in points.iter().enumerate() { |
| 105 | if n == 0 { |
| 106 | b.move_to(self.at(x, y)); |
| 107 | } else { |
| 108 | b.line_to(self.at(x, y)); |
| 109 | } |
| 110 | } |
| 111 | if closed { |
| 112 | b.close(); |
| 113 | } |
| 114 | }); |
| 115 | self.stroke(&path); |
| 116 | } |
| 117 | |
| 118 | fn line(&mut self, from: (f32, f32), to: (f32, f32)) { |
| 119 | self.polyline(&[from, to], false); |
| 120 | } |
| 121 | |
| 122 | fn circle(&mut self, x: f32, y: f32, r: f32) { |
| 123 | let path = Path::circle(self.at(x, y), r * self.scale); |
| 124 | self.stroke(&path); |
| 125 | } |
| 126 | |
| 127 | fn dot(&mut self, x: f32, y: f32, r: f32) { |
| 128 | let path = Path::circle(self.at(x, y), r * self.scale); |
| 129 | self.frame.fill(&path, self.color); |
| 130 | } |
| 131 | |
| 132 | /// Degrees, clockwise from three o'clock. `to` may be below `from` to go the other way. |
| 133 | fn arc(&mut self, x: f32, y: f32, r: f32, from: f32, to: f32) { |
| 134 | let path = Path::new(|b| { |
| 135 | b.arc(Arc { |
| 136 | center: self.at(x, y), |
| 137 | radius: r * self.scale, |
| 138 | start_angle: Radians(from.to_radians()), |
| 139 | end_angle: Radians(to.to_radians()), |
| 140 | }); |
| 141 | }); |
| 142 | self.stroke(&path); |
| 143 | } |
| 144 | |
| 145 | fn rect(&mut self, x: f32, y: f32, w: f32, h: f32, r: f32) { |
| 146 | let path = self.rounded(x, y, w, h, r); |
| 147 | self.stroke(&path); |
| 148 | } |
| 149 | |
| 150 | fn fill_rect(&mut self, x: f32, y: f32, w: f32, h: f32, r: f32) { |
| 151 | let path = self.rounded(x, y, w, h, r); |
| 152 | self.frame.fill(&path, self.color); |
| 153 | } |
| 154 | |
| 155 | fn rounded(&self, x: f32, y: f32, w: f32, h: f32, r: f32) -> Path { |
| 156 | Path::rounded_rectangle( |
| 157 | self.at(x, y), |
| 158 | Size::new(w * self.scale, h * self.scale), |
| 159 | (r * self.scale).into(), |
| 160 | ) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn draw_icon(pen: &mut Pen, icon: Icon) { |
| 165 | match icon { |
| 166 | Icon::Overview => { |
| 167 | for (x, y) in [(3.0, 3.0), (13.5, 3.0), (3.0, 13.5), (13.5, 13.5)] { |
| 168 | pen.rect(x, y, 7.5, 7.5, 2.0); |
| 169 | } |
| 170 | } |
| 171 | Icon::Equalizer => { |
| 172 | for (x, knob) in [(6.0, 14.0), (12.0, 8.0), (18.0, 16.0)] { |
| 173 | pen.line((x, 3.5), (x, 20.5)); |
| 174 | pen.line((x - 2.5, knob), (x + 2.5, knob)); |
| 175 | } |
| 176 | } |
| 177 | Icon::Gestures => { |
| 178 | pen.rect(9.5, 11.0, 5.0, 11.0, 2.5); |
| 179 | pen.arc(12.0, 11.5, 4.0, 200.0, 340.0); |
| 180 | pen.arc(12.0, 11.5, 7.5, 210.0, 330.0); |
| 181 | } |
| 182 | Icon::Earbud => earbud(pen, false, None), |
| 183 | Icon::Settings => { |
| 184 | for (y, knob) in [(6.0, 15.0), (12.0, 8.0), (18.0, 13.0)] { |
| 185 | pen.line((3.5, y), (20.5, y)); |
| 186 | pen.dot(knob, y, 2.6); |
| 187 | } |
| 188 | } |
| 189 | Icon::NoiseOff => { |
| 190 | pen.circle(12.0, 12.0, 8.5); |
| 191 | pen.line((6.0, 6.0), (18.0, 18.0)); |
| 192 | } |
| 193 | Icon::Anc => { |
| 194 | pen.circle(12.0, 12.0, 8.5); |
| 195 | pen.circle(12.0, 12.0, 4.5); |
| 196 | pen.dot(12.0, 12.0, 1.4); |
| 197 | } |
| 198 | Icon::Transparency => { |
| 199 | for n in 0..12u8 { |
| 200 | let angle = f32::from(n) * 30f32.to_radians(); |
| 201 | pen.dot(12.0 + 8.5 * angle.cos(), 12.0 + 8.5 * angle.sin(), 1.15); |
| 202 | } |
| 203 | pen.dot(12.0, 12.0, 2.2); |
| 204 | } |
| 205 | Icon::Game | Icon::Bolt => pen.polyline( |
| 206 | &[ |
| 207 | (13.5, 2.5), |
| 208 | (5.0, 13.5), |
| 209 | (11.5, 13.5), |
| 210 | (10.5, 21.5), |
| 211 | (19.0, 10.5), |
| 212 | (12.5, 10.5), |
| 213 | ], |
| 214 | true, |
| 215 | ), |
| 216 | Icon::Spatial => { |
| 217 | pen.dot(12.0, 12.0, 2.4); |
| 218 | for (r, spread) in [(5.5, 40.0), (9.5, 35.0)] { |
| 219 | pen.arc(12.0, 12.0, r, -spread, spread); |
| 220 | pen.arc(12.0, 12.0, r, 180.0 - spread, 180.0 + spread); |
| 221 | } |
| 222 | } |
| 223 | Icon::Bass => { |
| 224 | pen.polyline( |
| 225 | &[ |
| 226 | (3.5, 9.5), |
| 227 | (7.5, 9.5), |
| 228 | (12.5, 5.0), |
| 229 | (12.5, 19.0), |
| 230 | (7.5, 14.5), |
| 231 | (3.5, 14.5), |
| 232 | ], |
| 233 | true, |
| 234 | ); |
| 235 | pen.arc(13.0, 12.0, 4.0, -50.0, 50.0); |
| 236 | pen.arc(13.0, 12.0, 8.0, -50.0, 50.0); |
| 237 | } |
| 238 | Icon::Wind => { |
| 239 | pen.line((3.0, 8.5), (13.0, 8.5)); |
| 240 | pen.arc(13.0, 6.0, 2.5, 90.0, -180.0); |
| 241 | pen.line((3.0, 12.5), (17.5, 12.5)); |
| 242 | pen.arc(17.5, 15.5, 3.0, -90.0, 180.0); |
| 243 | pen.line((3.0, 16.5), (9.0, 16.5)); |
| 244 | } |
| 245 | Icon::Dual => { |
| 246 | pen.rect(2.5, 5.0, 12.5, 9.0, 1.5); |
| 247 | pen.line((1.5, 17.5), (16.0, 17.5)); |
| 248 | pen.rect(17.5, 8.0, 4.5, 11.0, 1.2); |
| 249 | } |
| 250 | Icon::Chevron => pen.polyline(&[(9.0, 5.5), (15.5, 12.0), (9.0, 18.5)], false), |
| 251 | Icon::Find => { |
| 252 | pen.circle(12.0, 12.0, 6.5); |
| 253 | pen.dot(12.0, 12.0, 2.0); |
| 254 | for (from, to) in [ |
| 255 | ((12.0, 2.0), (12.0, 5.0)), |
| 256 | ((12.0, 19.0), (12.0, 22.0)), |
| 257 | ((2.0, 12.0), (5.0, 12.0)), |
| 258 | ((19.0, 12.0), (22.0, 12.0)), |
| 259 | ] { |
| 260 | pen.line(from, to); |
| 261 | } |
| 262 | } |
| 263 | Icon::Pencil => { |
| 264 | pen.polyline(&[(4.5, 19.5), (5.5, 15.0), (15.5, 5.0), (19.0, 8.5), (9.0, 18.5)], true); |
| 265 | pen.line((13.0, 7.5), (16.5, 11.0)); |
| 266 | } |
| 267 | Icon::Trash => { |
| 268 | pen.line((4.0, 6.5), (20.0, 6.5)); |
| 269 | pen.polyline(&[(9.0, 6.5), (9.5, 3.5), (14.5, 3.5), (15.0, 6.5)], false); |
| 270 | pen.polyline(&[(6.0, 6.5), (7.0, 20.5), (17.0, 20.5), (18.0, 6.5)], false); |
| 271 | pen.line((10.0, 10.5), (10.0, 16.5)); |
| 272 | pen.line((14.0, 10.5), (14.0, 16.5)); |
| 273 | } |
| 274 | Icon::Info => { |
| 275 | pen.circle(12.0, 12.0, 9.0); |
| 276 | pen.line((12.0, 11.0), (12.0, 16.5)); |
| 277 | pen.dot(12.0, 7.8, 1.2); |
| 278 | } |
| 279 | Icon::Chip => { |
| 280 | pen.rect(6.5, 6.5, 11.0, 11.0, 2.0); |
| 281 | for at in [9.5, 14.5] { |
| 282 | pen.line((at, 2.5), (at, 6.5)); |
| 283 | pen.line((at, 17.5), (at, 21.5)); |
| 284 | pen.line((2.5, at), (6.5, at)); |
| 285 | pen.line((17.5, at), (21.5, at)); |
| 286 | } |
| 287 | } |
| 288 | Icon::Keys => { |
| 289 | pen.rect(2.5, 6.0, 19.0, 12.0, 2.5); |
| 290 | for x in [6.5, 10.0, 13.5, 17.0] { |
| 291 | pen.dot(x, 10.0, 0.9); |
| 292 | } |
| 293 | pen.line((8.0, 14.0), (16.0, 14.0)); |
| 294 | } |
| 295 | Icon::Check => pen.polyline(&[(4.5, 12.5), (9.5, 17.5), (19.5, 7.0)], false), |
| 296 | Icon::Laptop => { |
| 297 | pen.rect(4.0, 5.0, 16.0, 11.0, 1.5); |
| 298 | pen.line((2.0, 19.0), (22.0, 19.0)); |
| 299 | } |
| 300 | Icon::Phone => { |
| 301 | pen.rect(7.0, 2.5, 10.0, 19.0, 2.0); |
| 302 | pen.line((10.5, 18.5), (13.5, 18.5)); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// A filled earbud: the head, the ear tip pointing in, and the stem going down. The tip is |
| 308 | /// drawn in `tip`, or in the part's color when there is none. |
| 309 | fn earbud(pen: &mut Pen, mirrored: bool, tip: Option<Color>) { |
| 310 | let x = |x: f32, w: f32| if mirrored { 24.0 - x - w } else { x }; |
| 311 | let color = pen.color; |
| 312 | pen.color = tip.unwrap_or(color); |
| 313 | pen.fill_rect(x(14.5, 6.0), 5.0, 6.0, 7.0, 2.8); |
| 314 | pen.color = color; |
| 315 | pen.dot(x(11.0, 0.0), 8.5, 6.3); |
| 316 | pen.fill_rect(x(6.6, 4.4), 10.0, 4.4, 12.5, 2.2); |
| 317 | } |
| 318 | |
| 319 | /// What a battery ring shows in its middle. |
| 320 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 321 | pub enum Part { |
| 322 | Left, |
| 323 | Right, |
| 324 | Case, |
| 325 | } |
| 326 | |
| 327 | /// A battery ring: the track, an arc for the charge (none when unknown) and the part in the |
| 328 | /// middle. |
| 329 | pub fn battery_ring<'a, Message: 'a>( |
| 330 | part: Part, |
| 331 | level: Option<u8>, |
| 332 | size: f32, |
| 333 | colors: RingColors, |
| 334 | ) -> Element<'a, Message> { |
| 335 | iced::widget::canvas(Ring { part, level, colors }) |
| 336 | .width(size) |
| 337 | .height(size) |
| 338 | .into() |
| 339 | } |
| 340 | |
| 341 | #[derive(Debug, Clone, Copy)] |
| 342 | pub struct RingColors { |
| 343 | pub track: Color, |
| 344 | pub charge: Color, |
| 345 | pub part: Color, |
| 346 | /// The earbuds' ear tips. |
| 347 | pub tip: Color, |
| 348 | /// Details cut into the case (the lid line, the light). |
| 349 | pub cutout: Color, |
| 350 | } |
| 351 | |
| 352 | struct Ring { |
| 353 | part: Part, |
| 354 | level: Option<u8>, |
| 355 | colors: RingColors, |
| 356 | } |
| 357 | |
| 358 | impl<Message> canvas::Program<Message> for Ring { |
| 359 | type State = (); |
| 360 | |
| 361 | fn draw( |
| 362 | &self, |
| 363 | _state: &(), |
| 364 | renderer: &Renderer, |
| 365 | _theme: &Theme, |
| 366 | bounds: Rectangle, |
| 367 | _cursor: mouse::Cursor, |
| 368 | ) -> Vec<canvas::Geometry> { |
| 369 | let mut frame = Frame::new(renderer, bounds.size()); |
| 370 | let center = frame.center(); |
| 371 | let width = bounds.width * 0.06; |
| 372 | let radius = bounds.width / 2.0 - width / 2.0 - 1.0; |
| 373 | let ring = |color: Color| { |
| 374 | Stroke::default() |
| 375 | .with_color(color) |
| 376 | .with_width(width) |
| 377 | .with_line_cap(LineCap::Round) |
| 378 | }; |
| 379 | frame.stroke(&Path::circle(center, radius), ring(self.colors.track)); |
| 380 | if let Some(level) = self.level.filter(|&level| level > 0) { |
| 381 | let sweep = f32::from(level.min(100)) / 100.0 * TAU; |
| 382 | let arc = Path::new(|b| { |
| 383 | b.arc(Arc { |
| 384 | center, |
| 385 | radius, |
| 386 | start_angle: Radians(-FRAC_PI_2), |
| 387 | end_angle: Radians(-FRAC_PI_2 + sweep), |
| 388 | }); |
| 389 | }); |
| 390 | frame.stroke(&arc, ring(self.colors.charge)); |
| 391 | } |
| 392 | |
| 393 | let art = bounds.width * 0.44; |
| 394 | let origin = Point::new(center.x - art / 2.0, center.y - art / 2.0); |
| 395 | let mut pen = Pen::new(&mut frame, origin, art, self.colors.part); |
| 396 | match self.part { |
| 397 | Part::Left => earbud(&mut pen, false, Some(self.colors.tip)), |
| 398 | Part::Right => earbud(&mut pen, true, Some(self.colors.tip)), |
| 399 | Part::Case => { |
| 400 | pen.fill_rect(2.0, 5.5, 20.0, 13.5, 6.0); |
| 401 | pen.color = self.colors.cutout; |
| 402 | pen.width = 0.9 * pen.scale; |
| 403 | pen.line((2.0, 10.0), (22.0, 10.0)); |
| 404 | pen.dot(12.0, 14.5, 1.0); |
| 405 | } |
| 406 | } |
| 407 | vec![frame.into_geometry()] |
| 408 | } |
| 409 | } |