Skip to main content

hyprforge_ui/
glyph.rs

1//! The marks every Hyprforge app draws — navigation, view modes, the
2//! sidebar toggle, the settings pages, signal strength, a battery, the
3//! eject mark and the star — drawn rather than typed.
4//!
5//! These began in Files and moved here when Settings adopted the same
6//! design, because the reasoning below is about fonts, not about files.
7//!
8//! These were `‹`, `›` and `↑` — characters set in the UI font — and
9//! they looked like what they are: punctuation. `‹` and `›` are
10//! guillemets, quotation marks borrowed from French typesetting; they
11//! are built to sit inside a line of prose at x-height, so in a 26px
12//! button they read as small, thin and slightly high rather than as
13//! chevrons. `↑` is a real arrow but its weight, head angle and length
14//! are whatever the desktop's font happens to think, which is a
15//! different answer on every machine.
16//!
17//! This is the same lesson the file badges already cost: a glyph's shape
18//! and colour belong to a font, and a font is not ours. There the
19//! symptom was colour — a colour emoji ignored the theme entirely.
20//! Here it is proportion. Both end the same way: draw the mark.
21//!
22//! Stroked paths rather than the filled rectangles the badges use,
23//! because a chevron is a line with a corner in it and cannot be built
24//! out of axis-aligned boxes. That is the whole reason this crate asks
25//! for iced's `canvas` feature.
26//!
27//! Every dimension here is a fraction of the button it is drawn in, so
28//! these scale with `FontScale` like everything else rather than
29//! becoming three fixed pixel sizes that drift apart at 125%.
30
31use iced::mouse;
32use iced::widget::canvas;
33use iced::{Element, Length, Point, Rectangle, Renderer, Theme};
34
35/// Which way a chevron points, or that it is the "up a level" arrow.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum Nav {
38    Back,
39    Forward,
40    Up,
41}
42
43/// Stroke weight as a fraction of the button's side.
44///
45/// 1.5px in a 26px button at 100% scale, matching the design's other
46/// drawn outlines — the file badge's edge and the search field's ring —
47/// so every hairline in the window is the same weight.
48const STROKE_FRACTION: f32 = 1.75 / 26.0;
49
50/// Half the mark's height, as a fraction of the button's side.
51///
52/// A chevron is `EXTENT` tall above the midline and `EXTENT` below, so
53/// the mark stands `2 × EXTENT` overall — at 0.22 that is a 11.4px
54/// chevron in a 26px button, which is the proportion the design draws.
55///
56/// This depends on the button having **no padding**: iced buttons carry
57/// a default padding, and with it the canvas was handed about 16px of
58/// the 26 and drew a mark scaled to *that*, which is why the first
59/// attempt came out visibly tiny. See `nav_button`.
60const EXTENT_FRACTION: f32 = 0.22;
61
62/// A drawn navigation mark, sized to `side` and coloured `color`.
63pub fn nav<'a, Message: 'a>(kind: Nav, side: f32, color: iced::Color) -> Element<'a, Message> {
64    canvas(NavGlyph { kind, color })
65        .width(Length::Fixed(side))
66        .height(Length::Fixed(side))
67        .into()
68}
69
70struct NavGlyph {
71    kind: Nav,
72    color: iced::Color,
73}
74
75impl<Message> canvas::Program<Message, Theme, Renderer> for NavGlyph {
76    type State = ();
77
78    fn draw(
79        &self,
80        _state: &Self::State,
81        renderer: &Renderer,
82        _theme: &Theme,
83        bounds: Rectangle,
84        _cursor: mouse::Cursor,
85    ) -> Vec<canvas::Geometry> {
86        let mut frame = canvas::Frame::new(renderer, bounds.size());
87        let side = bounds.width.min(bounds.height);
88        let mid = Point::new(bounds.width / 2.0, bounds.height / 2.0);
89        let extent = side * EXTENT_FRACTION;
90
91        let path = canvas::Path::new(|b| match self.kind {
92            // A chevron: two strokes meeting at a point. Drawn as one
93            // open path rather than two lines so the corner is a single
94            // join — two separate strokes leave a notch at the vertex
95            // that is invisible at 26px and obvious at 200%.
96            Nav::Back => {
97                b.move_to(Point::new(mid.x + extent * 0.5, mid.y - extent));
98                b.line_to(Point::new(mid.x - extent * 0.5, mid.y));
99                b.line_to(Point::new(mid.x + extent * 0.5, mid.y + extent));
100            }
101            Nav::Forward => {
102                b.move_to(Point::new(mid.x - extent * 0.5, mid.y - extent));
103                b.line_to(Point::new(mid.x + extent * 0.5, mid.y));
104                b.line_to(Point::new(mid.x - extent * 0.5, mid.y + extent));
105            }
106            // An arrow: a shaft with a head, drawn as two subpaths. The
107            // head is deliberately wider than a chevron's — at this size
108            // a narrow head reads as a vertical line with a kink.
109            Nav::Up => {
110                let head = extent * 0.72;
111                b.move_to(Point::new(mid.x, mid.y + extent));
112                b.line_to(Point::new(mid.x, mid.y - extent));
113                b.move_to(Point::new(mid.x - head, mid.y - extent + head));
114                b.line_to(Point::new(mid.x, mid.y - extent));
115                b.line_to(Point::new(mid.x + head, mid.y - extent + head));
116            }
117        });
118
119        frame.stroke(
120            &path,
121            canvas::Stroke {
122                style: canvas::Style::Solid(self.color),
123                width: side * STROKE_FRACTION,
124                // Round caps and joins: a chevron with butt caps has
125                // visibly chopped ends at this weight, and the corner
126                // reads as a chip out of the stroke rather than a bend.
127                line_cap: canvas::LineCap::Round,
128                line_join: canvas::LineJoin::Round,
129                ..canvas::Stroke::default()
130            },
131        );
132
133        vec![frame.into_geometry()]
134    }
135}
136
137/// The sidebar toggle's mark: a pane with its left rail picked out.
138///
139/// Drawn rather than typed for the same reason as every mark here — and
140/// the shape says what the control does, which `☰` does not: a hamburger
141/// is a menu everywhere else in the world, and this opens and closes a
142/// panel. The filled rail *is* the sidebar; the outline is the window
143/// around it.
144pub fn sidebar<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
145    canvas(SidebarGlyph { color, right: false }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
146}
147
148/// [`sidebar`], mirrored: a pane with its *right* rail picked out, for a
149/// panel that opens on the far side of the window — Files' preview. The
150/// same shape on the same bar, so the two controls read as a pair, each
151/// sitting at the edge whose panel it shows.
152pub fn side_panel<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
153    canvas(SidebarGlyph { color, right: true }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
154}
155
156struct SidebarGlyph {
157    color: iced::Color,
158    /// The rail on the right rather than the left — see [`side_panel`].
159    right: bool,
160}
161
162impl<Message> canvas::Program<Message, Theme, Renderer> for SidebarGlyph {
163    type State = ();
164
165    fn draw(
166        &self,
167        _state: &Self::State,
168        renderer: &Renderer,
169        _theme: &Theme,
170        bounds: Rectangle,
171        _cursor: mouse::Cursor,
172    ) -> Vec<canvas::Geometry> {
173        let mut frame = canvas::Frame::new(renderer, bounds.size());
174        let side = bounds.width.min(bounds.height);
175        let extent = side * VIEW_EXTENT_FRACTION;
176        let left = bounds.width / 2.0 - extent / 2.0;
177        let top = bounds.height / 2.0 - extent / 2.0;
178        let stroke = (extent * 0.12).max(1.0);
179        let fill = canvas::Fill::from(self.color);
180
181        // Drawn left-railed in a local x, and mirrored on the way out
182        // when the rail belongs on the right — one drawing, so the pair
183        // can never drift apart in weight or proportion.
184        let mirror = |x: f32, width: f32| if self.right { 2.0 * left + extent - x - width } else { x };
185        let mut rect = |x: f32, y: f32, w: f32, h: f32| {
186            frame.fill_rectangle(Point::new(mirror(x, w), y), iced::Size::new(w, h), fill);
187        };
188
189        // The rail, filled: this is the panel the button shows and hides.
190        let rail = extent * 0.34;
191        rect(left, top, rail, extent);
192
193        // The rest of the pane, outlined — four thin rectangles rather
194        // than a stroked path, so the corners meet exactly at any size
195        // instead of depending on the join style.
196        let right = left + extent;
197        let bottom = top + extent;
198        let body_left = left + rail;
199        rect(body_left, top, right - body_left, stroke);
200        rect(body_left, bottom - stroke, right - body_left, stroke);
201        rect(right - stroke, top, stroke, extent);
202
203        vec![frame.into_geometry()]
204    }
205}
206
207/// The view-mode marks: list, grid, columns.
208///
209/// Drawn for the same reason as [`Nav`] — `☰`, `⊞` and `‖` are font
210/// characters, and in a 20px segment they sat off-centre because a
211/// glyph's own bearings decide where it lands, not the box around it.
212/// Three rectangles cannot be off-centre.
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
214pub enum View {
215    List,
216    Grid,
217    Columns,
218}
219
220/// A drawn view-mode mark, sized to fit `side` and filled `color`.
221pub fn view<'a, Message: 'a>(kind: View, side: f32, color: iced::Color) -> Element<'a, Message> {
222    canvas(ViewGlyph { kind, color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
223}
224
225struct ViewGlyph {
226    kind: View,
227    color: iced::Color,
228}
229
230impl<Message> canvas::Program<Message, Theme, Renderer> for ViewGlyph {
231    type State = ();
232
233    fn draw(
234        &self,
235        _state: &Self::State,
236        renderer: &Renderer,
237        _theme: &Theme,
238        bounds: Rectangle,
239        _cursor: mouse::Cursor,
240    ) -> Vec<canvas::Geometry> {
241        let mut frame = canvas::Frame::new(renderer, bounds.size());
242        let side = bounds.width.min(bounds.height);
243        // The mark occupies a square this wide, centred in `bounds` —
244        // so a non-square canvas still centres rather than stretching.
245        let extent = side * VIEW_EXTENT_FRACTION;
246        let left = bounds.width / 2.0 - extent / 2.0;
247        let top = bounds.height / 2.0 - extent / 2.0;
248        let bar = (extent * 0.16).max(1.0);
249
250        let fill = canvas::Fill::from(self.color);
251        match self.kind {
252            // Three full-width bars: rows.
253            View::List => {
254                for i in 0..3 {
255                    let gap = (extent - bar * 3.0) / 2.0;
256                    let y = top + i as f32 * (bar + gap);
257                    frame.fill_rectangle(Point::new(left, y), iced::Size::new(extent, bar), fill);
258                }
259            }
260            // Four squares: cells.
261            View::Grid => {
262                let cell = extent * 0.42;
263                let gap = extent - cell * 2.0;
264                for (cx, cy) in [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)] {
265                    frame.fill_rectangle(
266                        Point::new(left + cx * (cell + gap), top + cy * (cell + gap)),
267                        iced::Size::new(cell, cell),
268                        fill,
269                    );
270                }
271            }
272            // Two full-height bars: panes side by side.
273            View::Columns => {
274                let w = extent * 0.30;
275                let gap = extent - w * 2.0;
276                for i in 0..2 {
277                    frame.fill_rectangle(
278                        Point::new(left + i as f32 * (w + gap), top),
279                        iced::Size::new(w, extent),
280                        fill,
281                    );
282                }
283            }
284        }
285
286        vec![frame.into_geometry()]
287    }
288}
289
290/// How much of the segment the view mark occupies.
291const VIEW_EXTENT_FRACTION: f32 = 0.55;
292
293/// The marks beside a settings page in a sidebar.
294///
295/// Named for what the page is *about*, not for any one app's screen
296/// list, so a page that moves between groups — or a second app with a
297/// sidebar of its own — keeps its mark.
298///
299/// All outline, one weight, one colour. The mockup these came from gave
300/// every page its own hue, and that is the one part of it this suite
301/// does not follow: colour here is reserved for selection and state, so
302/// a mark takes the text colour and turns accent only when its page is
303/// the current one (see `selectable_row_style`).
304#[derive(Debug, Clone, Copy, PartialEq, Eq)]
305pub enum Page {
306    Display,
307    Power,
308    Keyboard,
309    Network,
310    Bluetooth,
311    Windows,
312    WindowRules,
313    Keybinds,
314    Animation,
315    Idle,
316    Session,
317    Advanced,
318    Apps,
319    Appearance,
320    Wallpaper,
321    NightLight,
322    ScreenSharing,
323    Tray,
324}
325
326/// A drawn page mark, sized to `side` and stroked `color`.
327pub fn page<'a, Message: 'a>(kind: Page, side: f32, color: iced::Color) -> Element<'a, Message> {
328    canvas(PageGlyph { kind, color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
329}
330
331struct PageGlyph {
332    kind: Page,
333    color: iced::Color,
334}
335
336/// How much of its box a page mark occupies — a little more than the
337/// view marks, because an outline carries less ink than a filled bar
338/// and reads smaller at the same extent.
339const PAGE_EXTENT_FRACTION: f32 = 0.78;
340
341impl<Message> canvas::Program<Message, Theme, Renderer> for PageGlyph {
342    type State = ();
343
344    fn draw(
345        &self,
346        _state: &Self::State,
347        renderer: &Renderer,
348        _theme: &Theme,
349        bounds: Rectangle,
350        _cursor: mouse::Cursor,
351    ) -> Vec<canvas::Geometry> {
352        use iced::{Radians, Size};
353        use std::f32::consts::PI;
354
355        let mut frame = canvas::Frame::new(renderer, bounds.size());
356        let side = bounds.width.min(bounds.height);
357        let extent = side * PAGE_EXTENT_FRACTION;
358        let left = bounds.width / 2.0 - extent / 2.0;
359        let top = bounds.height / 2.0 - extent / 2.0;
360        // Every coordinate below is in a unit square, so a mark is one
361        // drawing at every size rather than a pixel layout per size.
362        let at = |x: f32, y: f32| Point::new(left + x * extent, top + y * extent);
363        let len = |v: f32| v * extent;
364        let rect = |b: &mut canvas::path::Builder, x: f32, y: f32, w: f32, h: f32, r: f32| {
365            b.rounded_rectangle(at(x, y), Size::new(len(w), len(h)), len(r).into());
366        };
367        let arc = |b: &mut canvas::path::Builder, x: f32, y: f32, r: f32, from: f32, to: f32| {
368            let start = at(x, y) + iced::Vector::new(len(r) * from.cos(), len(r) * from.sin());
369            b.move_to(start);
370            b.arc(canvas::path::Arc {
371                center: at(x, y),
372                radius: len(r),
373                start_angle: Radians(from),
374                end_angle: Radians(to),
375            });
376        };
377        let line = |b: &mut canvas::path::Builder, x0: f32, y0: f32, x1: f32, y1: f32| {
378            b.move_to(at(x0, y0));
379            b.line_to(at(x1, y1));
380        };
381
382        let path = canvas::Path::new(|b| match self.kind {
383            // A screen on a short stand.
384            Page::Display => {
385                rect(b, 0.05, 0.14, 0.9, 0.58, 0.08);
386                line(b, 0.5, 0.72, 0.5, 0.86);
387                line(b, 0.3, 0.86, 0.7, 0.86);
388            }
389            // A battery on its side, with its terminal.
390            Page::Power => {
391                rect(b, 0.04, 0.26, 0.8, 0.48, 0.08);
392                line(b, 0.92, 0.42, 0.92, 0.58);
393            }
394            // A keyboard: the body and a row of keys along it.
395            Page::Keyboard => {
396                rect(b, 0.02, 0.22, 0.96, 0.56, 0.08);
397                for i in 0..4 {
398                    let x = 0.2 + i as f32 * 0.2;
399                    line(b, x, 0.42, x + 0.02, 0.42);
400                }
401                line(b, 0.28, 0.6, 0.72, 0.6);
402            }
403            // Signal: three arcs over a point.
404            Page::Network => {
405                for r in [0.44, 0.3, 0.16] {
406                    arc(b, 0.5, 0.82, r, PI * 1.25, PI * 1.75);
407                }
408                line(b, 0.5, 0.8, 0.5, 0.82);
409            }
410            // The Bluetooth rune: a spine with two bows.
411            Page::Bluetooth => {
412                b.move_to(at(0.26, 0.3));
413                b.line_to(at(0.72, 0.7));
414                b.line_to(at(0.5, 0.9));
415                b.line_to(at(0.5, 0.1));
416                b.line_to(at(0.72, 0.3));
417                b.line_to(at(0.26, 0.7));
418            }
419            // Two tiled windows.
420            Page::Windows => {
421                rect(b, 0.04, 0.12, 0.5, 0.76, 0.07);
422                rect(b, 0.62, 0.12, 0.34, 0.34, 0.07);
423                rect(b, 0.62, 0.54, 0.34, 0.34, 0.07);
424            }
425            // A window and the rule lines that describe it.
426            Page::WindowRules => {
427                rect(b, 0.04, 0.12, 0.92, 0.76, 0.07);
428                line(b, 0.04, 0.32, 0.96, 0.32);
429                line(b, 0.2, 0.52, 0.8, 0.52);
430                line(b, 0.2, 0.68, 0.6, 0.68);
431            }
432            // A single keycap, seen slightly from above.
433            Page::Keybinds => {
434                rect(b, 0.12, 0.12, 0.76, 0.76, 0.14);
435                rect(b, 0.24, 0.2, 0.52, 0.46, 0.08);
436            }
437            // A ball with the lines it left behind.
438            Page::Animation => {
439                b.circle(at(0.64, 0.5), len(0.26));
440                line(b, 0.04, 0.34, 0.26, 0.34);
441                line(b, 0.0, 0.5, 0.28, 0.5);
442                line(b, 0.04, 0.66, 0.26, 0.66);
443            }
444            // A crescent moon: two arcs sharing their tips.
445            Page::Idle => {
446                arc(b, 0.5, 0.5, 0.4, PI * 0.35, PI * 1.65);
447                let tip_top = at(0.5, 0.5)
448                    + iced::Vector::new(len(0.4) * (PI * 1.65).cos(), len(0.4) * (PI * 1.65).sin());
449                let tip_bottom = at(0.5, 0.5)
450                    + iced::Vector::new(len(0.4) * (PI * 0.35).cos(), len(0.4) * (PI * 0.35).sin());
451                b.move_to(tip_top);
452                b.quadratic_curve_to(at(0.34, 0.5), tip_bottom);
453            }
454            // Play: what starts when the session does.
455            Page::Session => {
456                b.move_to(at(0.28, 0.14));
457                b.line_to(at(0.82, 0.5));
458                b.line_to(at(0.28, 0.86));
459                b.close();
460            }
461            // Two slider tracks with their knobs — the knobs of last resort.
462            Page::Advanced => {
463                line(b, 0.04, 0.32, 0.96, 0.32);
464                line(b, 0.04, 0.68, 0.96, 0.68);
465                b.circle(at(0.34, 0.32), len(0.11));
466                b.circle(at(0.66, 0.68), len(0.11));
467            }
468            // A grid of applications.
469            Page::Apps => {
470                for (x, y) in [(0.08, 0.08), (0.56, 0.08), (0.08, 0.56), (0.56, 0.56)] {
471                    rect(b, x, y, 0.36, 0.36, 0.08);
472                }
473            }
474            // Contrast: a circle split down the middle.
475            Page::Appearance => {
476                b.circle(at(0.5, 0.5), len(0.42));
477                line(b, 0.5, 0.08, 0.5, 0.92);
478            }
479            // A picture: its frame and a mountain.
480            Page::Wallpaper => {
481                rect(b, 0.04, 0.14, 0.92, 0.72, 0.07);
482                b.move_to(at(0.14, 0.76));
483                b.line_to(at(0.4, 0.44));
484                b.line_to(at(0.58, 0.64));
485                b.line_to(at(0.7, 0.52));
486                b.line_to(at(0.86, 0.76));
487            }
488            // A low sun: warm light in the evening.
489            Page::NightLight => {
490                arc(b, 0.5, 0.7, 0.26, PI, PI * 2.0);
491                line(b, 0.04, 0.7, 0.96, 0.7);
492                line(b, 0.5, 0.14, 0.5, 0.28);
493                line(b, 0.16, 0.34, 0.25, 0.43);
494                line(b, 0.84, 0.34, 0.75, 0.43);
495            }
496            // A screen with an arrow leaving it.
497            Page::ScreenSharing => {
498                rect(b, 0.04, 0.14, 0.92, 0.64, 0.07);
499                line(b, 0.5, 0.64, 0.5, 0.32);
500                line(b, 0.36, 0.44, 0.5, 0.3);
501                line(b, 0.64, 0.44, 0.5, 0.3);
502            }
503            // A bar along the top with its icons.
504            Page::Tray => {
505                rect(b, 0.04, 0.16, 0.92, 0.68, 0.07);
506                line(b, 0.04, 0.38, 0.96, 0.38);
507                line(b, 0.62, 0.27, 0.64, 0.27);
508                line(b, 0.76, 0.27, 0.78, 0.27);
509            }
510        });
511
512        frame.stroke(
513            &path,
514            canvas::Stroke {
515                style: canvas::Style::Solid(self.color),
516                // Lighter than the nav chevrons: those are one mark in a
517                // button, these are a column of marks beside labels, and
518                // at the chevron weight the column reads louder than the
519                // text it annotates.
520                width: (side * STROKE_FRACTION * 0.85).max(1.0),
521                line_cap: canvas::LineCap::Round,
522                line_join: canvas::LineJoin::Round,
523                ..canvas::Stroke::default()
524            },
525        );
526
527        vec![frame.into_geometry()]
528    }
529}
530
531/// Signal strength as four rising bars, lit up to `strength` percent —
532/// the mark beside a network in a list.
533///
534/// Lit bars take `on`, the rest `off`, so a weak network still shows the
535/// shape of the scale it is low on rather than one lonely bar.
536pub fn signal<'a, Message: 'a>(
537    strength: u8,
538    side: f32,
539    on: iced::Color,
540    off: iced::Color,
541) -> Element<'a, Message> {
542    canvas(SignalGlyph { lit: signal_bars(strength), on, off })
543        .width(Length::Fixed(side))
544        .height(Length::Fixed(side))
545        .into()
546}
547
548/// How many of the four bars a strength lights: at least one for any
549/// signal at all, four only near the top.
550pub fn signal_bars(strength: u8) -> u8 {
551    match strength {
552        0 => 0,
553        1..=29 => 1,
554        30..=54 => 2,
555        55..=79 => 3,
556        _ => 4,
557    }
558}
559
560struct SignalGlyph {
561    lit: u8,
562    on: iced::Color,
563    off: iced::Color,
564}
565
566impl<Message> canvas::Program<Message, Theme, Renderer> for SignalGlyph {
567    type State = ();
568
569    fn draw(
570        &self,
571        _state: &Self::State,
572        renderer: &Renderer,
573        _theme: &Theme,
574        bounds: Rectangle,
575        _cursor: mouse::Cursor,
576    ) -> Vec<canvas::Geometry> {
577        let mut frame = canvas::Frame::new(renderer, bounds.size());
578        let side = bounds.width.min(bounds.height);
579        let extent = side * PAGE_EXTENT_FRACTION;
580        let left = bounds.width / 2.0 - extent / 2.0;
581        let bottom = bounds.height / 2.0 + extent / 2.0;
582        let bar = extent / 5.5;
583        let gap = (extent - bar * 4.0) / 3.0;
584        for i in 0..4u8 {
585            let height = extent * (0.25 + 0.25 * i as f32);
586            let colour = if i < self.lit { self.on } else { self.off };
587            frame.fill_rectangle(
588                Point::new(left + i as f32 * (bar + gap), bottom - height),
589                iced::Size::new(bar, height),
590                canvas::Fill::from(colour),
591            );
592        }
593        vec![frame.into_geometry()]
594    }
595}
596
597/// A battery on its side, filled to `fraction` in `fill` — the mark that
598/// leads a power page. The outline takes `outline`.
599///
600/// `side` is the height; the battery is drawn twice as wide, the shape
601/// people read as a battery before they read the number beside it.
602pub fn battery<'a, Message: 'a>(
603    fraction: f32,
604    side: f32,
605    outline: iced::Color,
606    fill: iced::Color,
607) -> Element<'a, Message> {
608    canvas(BatteryGlyph { fraction: fraction.clamp(0.0, 1.0), outline, fill })
609        .width(Length::Fixed(side * 2.0))
610        .height(Length::Fixed(side))
611        .into()
612}
613
614struct BatteryGlyph {
615    fraction: f32,
616    outline: iced::Color,
617    fill: iced::Color,
618}
619
620impl<Message> canvas::Program<Message, Theme, Renderer> for BatteryGlyph {
621    type State = ();
622
623    fn draw(
624        &self,
625        _state: &Self::State,
626        renderer: &Renderer,
627        _theme: &Theme,
628        bounds: Rectangle,
629        _cursor: mouse::Cursor,
630    ) -> Vec<canvas::Geometry> {
631        let mut frame = canvas::Frame::new(renderer, bounds.size());
632        let h = bounds.height;
633        let stroke = (h * 0.08).max(1.5);
634        let nub = h * 0.12;
635        let body_w = bounds.width - nub - stroke;
636        let body = canvas::Path::rounded_rectangle(
637            Point::new(stroke / 2.0, stroke / 2.0),
638            iced::Size::new(body_w, h - stroke),
639            (h * 0.18).into(),
640        );
641        frame.stroke(
642            &body,
643            canvas::Stroke::default().with_width(stroke).with_color(self.outline),
644        );
645        // The terminal, centred on the right end.
646        frame.fill_rectangle(
647            Point::new(body_w + stroke / 2.0, h * 0.32),
648            iced::Size::new(nub, h * 0.36),
649            canvas::Fill::from(self.outline),
650        );
651        // The charge, inset from the outline so it reads as contents.
652        let inset = stroke * 2.0;
653        let full = body_w - inset * 2.0 + stroke;
654        if self.fraction > 0.0 {
655            frame.fill(
656                &canvas::Path::rounded_rectangle(
657                    Point::new(inset, inset),
658                    iced::Size::new(full * self.fraction, h - inset * 2.0),
659                    (h * 0.08).into(),
660                ),
661                self.fill,
662            );
663        }
664        vec![frame.into_geometry()]
665    }
666}
667
668/// The eject mark: a triangle pointing up over a bar, as on every
669/// drive's button since the cassette deck.
670///
671/// Drawn, because `⏏` is in few fonts and wears whichever weight the one
672/// it falls back to has — and this sits beside drawn folder marks in a
673/// sidebar row, where a typed character would be the one thing out of
674/// line. Files' Devices section puts it on a mounted drive's row.
675pub fn eject<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
676    canvas(EjectGlyph { color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
677}
678
679struct EjectGlyph {
680    color: iced::Color,
681}
682
683impl<Message> canvas::Program<Message, Theme, Renderer> for EjectGlyph {
684    type State = ();
685
686    fn draw(
687        &self,
688        _state: &Self::State,
689        renderer: &Renderer,
690        _theme: &Theme,
691        bounds: Rectangle,
692        _cursor: mouse::Cursor,
693    ) -> Vec<canvas::Geometry> {
694        let mut frame = canvas::Frame::new(renderer, bounds.size());
695        let side = bounds.width.min(bounds.height);
696        let extent = side * VIEW_EXTENT_FRACTION;
697        let left = bounds.width / 2.0 - extent / 2.0;
698        let top = bounds.height / 2.0 - extent / 2.0;
699        let fill = canvas::Fill::from(self.color);
700        // The triangle takes the top three fifths, a gap a fifth, and
701        // the bar the rest — the proportions of the symbol as printed on
702        // hardware, where the bar is as wide as the triangle's base.
703        let triangle = canvas::Path::new(|b| {
704            b.move_to(Point::new(left + extent / 2.0, top));
705            b.line_to(Point::new(left + extent, top + extent * 0.6));
706            b.line_to(Point::new(left, top + extent * 0.6));
707            b.close();
708        });
709        frame.fill(&triangle, fill);
710        frame.fill_rectangle(
711            Point::new(left, top + extent * 0.78),
712            iced::Size::new(extent, extent * 0.22),
713            fill,
714        );
715        vec![frame.into_geometry()]
716    }
717}
718
719/// The star mark: a filled five-pointed star — what a starred file
720/// carries in Files' listing and grid.
721///
722/// Drawn, because `★` is one of the characters whose weight and size
723/// depend entirely on which font the desktop falls back to for it, and
724/// it sits on the corner of a drawn file icon, where a typed character
725/// would sit on its own baseline rather than on the icon.
726pub fn star<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
727    canvas(StarGlyph { color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
728}
729
730struct StarGlyph {
731    color: iced::Color,
732}
733
734impl<Message> canvas::Program<Message, Theme, Renderer> for StarGlyph {
735    type State = ();
736
737    fn draw(
738        &self,
739        _state: &Self::State,
740        renderer: &Renderer,
741        _theme: &Theme,
742        bounds: Rectangle,
743        _cursor: mouse::Cursor,
744    ) -> Vec<canvas::Geometry> {
745        let mut frame = canvas::Frame::new(renderer, bounds.size());
746        let side = bounds.width.min(bounds.height);
747        // The whole box, nearly: at the size it is drawn — a corner of
748        // an icon — any margin is star that is not there.
749        let outer = side * 0.48;
750        // The regular star's inner radius, outer × sin 18° / sin 54°:
751        // straight edges from point to point, not a fat or a spiky star.
752        let inner = outer * 0.382;
753        let centre = Point::new(bounds.width / 2.0, bounds.height / 2.0 + side * 0.03);
754        let star = canvas::Path::new(|b| {
755            for i in 0..10 {
756                let r = if i % 2 == 0 { outer } else { inner };
757                // From straight up, a tenth of a turn at a time.
758                let angle = -std::f32::consts::FRAC_PI_2 + i as f32 * std::f32::consts::PI / 5.0;
759                let point = Point::new(centre.x + r * angle.cos(), centre.y + r * angle.sin());
760                if i == 0 {
761                    b.move_to(point);
762                } else {
763                    b.line_to(point);
764                }
765            }
766            b.close();
767        });
768        frame.fill(&star, canvas::Fill::from(self.color));
769        vec![frame.into_geometry()]
770    }
771}
772
773// Only `signal_bars` is tested below, because it is a decision rather
774// than a shape. For the rest there are no tests, deliberately.
775//
776// What this module decides is a *shape*, and the only instrument that
777// can check a shape is a screenshot. The two things a test could reach —
778// that the three `Nav` variants differ, and that the fractions above are
779// between 0 and 1 — are both true by construction: clippy rejects the
780// second as a constant assertion, which is the correct verdict on a test
781// that cannot fail.
782//
783// What is verified instead, and how: rendered at 26px and looked at
784// beside the design, which is how the typed guillemets this replaced
785// were found wanting in the first place. The property that keeps it
786// honest at other sizes is that every dimension is a fraction of the
787// button rather than a pixel count, and that is visible in the source
788// above rather than assertable below.
789
790#[cfg(test)]
791mod tests {
792    use super::*;
793
794    /// Any signal at all lights a bar — a network that is there but weak
795    /// must not look like one that is gone — and four is kept for strong.
796    #[test]
797    fn any_signal_lights_a_bar_and_only_a_strong_one_lights_four() {
798        assert_eq!(signal_bars(0), 0);
799        assert_eq!(signal_bars(1), 1);
800        assert_eq!(signal_bars(54), 2);
801        assert_eq!(signal_bars(79), 3);
802        assert_eq!(signal_bars(97), 4);
803        assert_eq!(signal_bars(100), 4);
804    }
805}