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