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    /// Finishing an install: the steps still to do, ticked off.
325    Setup,
326}
327
328/// A drawn page mark, sized to `side` and stroked `color`.
329pub fn page<'a, Message: 'a>(kind: Page, side: f32, color: iced::Color) -> Element<'a, Message> {
330    canvas(PageGlyph { kind, color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
331}
332
333struct PageGlyph {
334    kind: Page,
335    color: iced::Color,
336}
337
338/// How much of its box a page mark occupies — a little more than the
339/// view marks, because an outline carries less ink than a filled bar
340/// and reads smaller at the same extent.
341const PAGE_EXTENT_FRACTION: f32 = 0.78;
342
343impl<Message> canvas::Program<Message, Theme, Renderer> for PageGlyph {
344    type State = ();
345
346    fn draw(
347        &self,
348        _state: &Self::State,
349        renderer: &Renderer,
350        _theme: &Theme,
351        bounds: Rectangle,
352        _cursor: mouse::Cursor,
353    ) -> Vec<canvas::Geometry> {
354        use iced::{Radians, Size};
355        use std::f32::consts::PI;
356
357        let mut frame = canvas::Frame::new(renderer, bounds.size());
358        let side = bounds.width.min(bounds.height);
359        let extent = side * PAGE_EXTENT_FRACTION;
360        let left = bounds.width / 2.0 - extent / 2.0;
361        let top = bounds.height / 2.0 - extent / 2.0;
362        // Every coordinate below is in a unit square, so a mark is one
363        // drawing at every size rather than a pixel layout per size.
364        let at = |x: f32, y: f32| Point::new(left + x * extent, top + y * extent);
365        let len = |v: f32| v * extent;
366        let rect = |b: &mut canvas::path::Builder, x: f32, y: f32, w: f32, h: f32, r: f32| {
367            b.rounded_rectangle(at(x, y), Size::new(len(w), len(h)), len(r).into());
368        };
369        let arc = |b: &mut canvas::path::Builder, x: f32, y: f32, r: f32, from: f32, to: f32| {
370            let start = at(x, y) + iced::Vector::new(len(r) * from.cos(), len(r) * from.sin());
371            b.move_to(start);
372            b.arc(canvas::path::Arc {
373                center: at(x, y),
374                radius: len(r),
375                start_angle: Radians(from),
376                end_angle: Radians(to),
377            });
378        };
379        let line = |b: &mut canvas::path::Builder, x0: f32, y0: f32, x1: f32, y1: f32| {
380            b.move_to(at(x0, y0));
381            b.line_to(at(x1, y1));
382        };
383
384        let path = canvas::Path::new(|b| match self.kind {
385            // A screen on a short stand.
386            Page::Display => {
387                rect(b, 0.05, 0.14, 0.9, 0.58, 0.08);
388                line(b, 0.5, 0.72, 0.5, 0.86);
389                line(b, 0.3, 0.86, 0.7, 0.86);
390            }
391            // A battery on its side, with its terminal.
392            Page::Power => {
393                rect(b, 0.04, 0.26, 0.8, 0.48, 0.08);
394                line(b, 0.92, 0.42, 0.92, 0.58);
395            }
396            // A keyboard: the body and a row of keys along it.
397            Page::Keyboard => {
398                rect(b, 0.02, 0.22, 0.96, 0.56, 0.08);
399                for i in 0..4 {
400                    let x = 0.2 + i as f32 * 0.2;
401                    line(b, x, 0.42, x + 0.02, 0.42);
402                }
403                line(b, 0.28, 0.6, 0.72, 0.6);
404            }
405            // Signal: three arcs over a point.
406            Page::Network => {
407                for r in [0.44, 0.3, 0.16] {
408                    arc(b, 0.5, 0.82, r, PI * 1.25, PI * 1.75);
409                }
410                line(b, 0.5, 0.8, 0.5, 0.82);
411            }
412            // The Bluetooth rune: a spine with two bows.
413            Page::Bluetooth => {
414                b.move_to(at(0.26, 0.3));
415                b.line_to(at(0.72, 0.7));
416                b.line_to(at(0.5, 0.9));
417                b.line_to(at(0.5, 0.1));
418                b.line_to(at(0.72, 0.3));
419                b.line_to(at(0.26, 0.7));
420            }
421            // Two tiled windows.
422            Page::Windows => {
423                rect(b, 0.04, 0.12, 0.5, 0.76, 0.07);
424                rect(b, 0.62, 0.12, 0.34, 0.34, 0.07);
425                rect(b, 0.62, 0.54, 0.34, 0.34, 0.07);
426            }
427            // A window and the rule lines that describe it.
428            Page::WindowRules => {
429                rect(b, 0.04, 0.12, 0.92, 0.76, 0.07);
430                line(b, 0.04, 0.32, 0.96, 0.32);
431                line(b, 0.2, 0.52, 0.8, 0.52);
432                line(b, 0.2, 0.68, 0.6, 0.68);
433            }
434            // A single keycap, seen slightly from above.
435            Page::Keybinds => {
436                rect(b, 0.12, 0.12, 0.76, 0.76, 0.14);
437                rect(b, 0.24, 0.2, 0.52, 0.46, 0.08);
438            }
439            // A ball with the lines it left behind.
440            Page::Animation => {
441                b.circle(at(0.64, 0.5), len(0.26));
442                line(b, 0.04, 0.34, 0.26, 0.34);
443                line(b, 0.0, 0.5, 0.28, 0.5);
444                line(b, 0.04, 0.66, 0.26, 0.66);
445            }
446            // A crescent moon: two arcs sharing their tips.
447            Page::Idle => {
448                arc(b, 0.5, 0.5, 0.4, PI * 0.35, PI * 1.65);
449                let tip_top = at(0.5, 0.5)
450                    + iced::Vector::new(len(0.4) * (PI * 1.65).cos(), len(0.4) * (PI * 1.65).sin());
451                let tip_bottom = at(0.5, 0.5)
452                    + iced::Vector::new(len(0.4) * (PI * 0.35).cos(), len(0.4) * (PI * 0.35).sin());
453                b.move_to(tip_top);
454                b.quadratic_curve_to(at(0.34, 0.5), tip_bottom);
455            }
456            // Play: what starts when the session does.
457            Page::Session => {
458                b.move_to(at(0.28, 0.14));
459                b.line_to(at(0.82, 0.5));
460                b.line_to(at(0.28, 0.86));
461                b.close();
462            }
463            // Two slider tracks with their knobs — the knobs of last resort.
464            Page::Advanced => {
465                line(b, 0.04, 0.32, 0.96, 0.32);
466                line(b, 0.04, 0.68, 0.96, 0.68);
467                b.circle(at(0.34, 0.32), len(0.11));
468                b.circle(at(0.66, 0.68), len(0.11));
469            }
470            // A grid of applications.
471            Page::Apps => {
472                for (x, y) in [(0.08, 0.08), (0.56, 0.08), (0.08, 0.56), (0.56, 0.56)] {
473                    rect(b, x, y, 0.36, 0.36, 0.08);
474                }
475            }
476            // Contrast: a circle split down the middle.
477            Page::Appearance => {
478                b.circle(at(0.5, 0.5), len(0.42));
479                line(b, 0.5, 0.08, 0.5, 0.92);
480            }
481            // A picture: its frame and a mountain.
482            Page::Wallpaper => {
483                rect(b, 0.04, 0.14, 0.92, 0.72, 0.07);
484                b.move_to(at(0.14, 0.76));
485                b.line_to(at(0.4, 0.44));
486                b.line_to(at(0.58, 0.64));
487                b.line_to(at(0.7, 0.52));
488                b.line_to(at(0.86, 0.76));
489            }
490            // A low sun: warm light in the evening.
491            Page::NightLight => {
492                arc(b, 0.5, 0.7, 0.26, PI, PI * 2.0);
493                line(b, 0.04, 0.7, 0.96, 0.7);
494                line(b, 0.5, 0.14, 0.5, 0.28);
495                line(b, 0.16, 0.34, 0.25, 0.43);
496                line(b, 0.84, 0.34, 0.75, 0.43);
497            }
498            // A screen with an arrow leaving it.
499            Page::ScreenSharing => {
500                rect(b, 0.04, 0.14, 0.92, 0.64, 0.07);
501                line(b, 0.5, 0.64, 0.5, 0.32);
502                line(b, 0.36, 0.44, 0.5, 0.3);
503                line(b, 0.64, 0.44, 0.5, 0.3);
504            }
505            // A bar along the top with its icons.
506            Page::Tray => {
507                rect(b, 0.04, 0.16, 0.92, 0.68, 0.07);
508                line(b, 0.04, 0.38, 0.96, 0.38);
509                line(b, 0.62, 0.27, 0.64, 0.27);
510                line(b, 0.76, 0.27, 0.78, 0.27);
511            }
512            // A checklist: one line ticked, one still to do.
513            Page::Setup => {
514                b.move_to(at(0.04, 0.3));
515                b.line_to(at(0.15, 0.41));
516                b.line_to(at(0.32, 0.18));
517                line(b, 0.46, 0.3, 0.96, 0.3);
518                rect(b, 0.06, 0.6, 0.24, 0.24, 0.05);
519                line(b, 0.46, 0.72, 0.96, 0.72);
520            }
521        });
522
523        frame.stroke(
524            &path,
525            canvas::Stroke {
526                style: canvas::Style::Solid(self.color),
527                // Lighter than the nav chevrons: those are one mark in a
528                // button, these are a column of marks beside labels, and
529                // at the chevron weight the column reads louder than the
530                // text it annotates.
531                width: (side * STROKE_FRACTION * 0.85).max(1.0),
532                line_cap: canvas::LineCap::Round,
533                line_join: canvas::LineJoin::Round,
534                ..canvas::Stroke::default()
535            },
536        );
537
538        vec![frame.into_geometry()]
539    }
540}
541
542/// Signal strength as four rising bars, lit up to `strength` percent —
543/// the mark beside a network in a list.
544///
545/// Lit bars take `on`, the rest `off`, so a weak network still shows the
546/// shape of the scale it is low on rather than one lonely bar.
547pub fn signal<'a, Message: 'a>(
548    strength: u8,
549    side: f32,
550    on: iced::Color,
551    off: iced::Color,
552) -> Element<'a, Message> {
553    canvas(SignalGlyph { lit: signal_bars(strength), on, off })
554        .width(Length::Fixed(side))
555        .height(Length::Fixed(side))
556        .into()
557}
558
559/// How many of the four bars a strength lights: at least one for any
560/// signal at all, four only near the top.
561pub fn signal_bars(strength: u8) -> u8 {
562    match strength {
563        0 => 0,
564        1..=29 => 1,
565        30..=54 => 2,
566        55..=79 => 3,
567        _ => 4,
568    }
569}
570
571struct SignalGlyph {
572    lit: u8,
573    on: iced::Color,
574    off: iced::Color,
575}
576
577impl<Message> canvas::Program<Message, Theme, Renderer> for SignalGlyph {
578    type State = ();
579
580    fn draw(
581        &self,
582        _state: &Self::State,
583        renderer: &Renderer,
584        _theme: &Theme,
585        bounds: Rectangle,
586        _cursor: mouse::Cursor,
587    ) -> Vec<canvas::Geometry> {
588        let mut frame = canvas::Frame::new(renderer, bounds.size());
589        let side = bounds.width.min(bounds.height);
590        let extent = side * PAGE_EXTENT_FRACTION;
591        let left = bounds.width / 2.0 - extent / 2.0;
592        let bottom = bounds.height / 2.0 + extent / 2.0;
593        let bar = extent / 5.5;
594        let gap = (extent - bar * 4.0) / 3.0;
595        for i in 0..4u8 {
596            let height = extent * (0.25 + 0.25 * i as f32);
597            let colour = if i < self.lit { self.on } else { self.off };
598            frame.fill_rectangle(
599                Point::new(left + i as f32 * (bar + gap), bottom - height),
600                iced::Size::new(bar, height),
601                canvas::Fill::from(colour),
602            );
603        }
604        vec![frame.into_geometry()]
605    }
606}
607
608/// A battery on its side, filled to `fraction` in `fill` — the mark that
609/// leads a power page. The outline takes `outline`.
610///
611/// `side` is the height; the battery is drawn twice as wide, the shape
612/// people read as a battery before they read the number beside it.
613pub fn battery<'a, Message: 'a>(
614    fraction: f32,
615    side: f32,
616    outline: iced::Color,
617    fill: iced::Color,
618) -> Element<'a, Message> {
619    canvas(BatteryGlyph { fraction: fraction.clamp(0.0, 1.0), outline, fill })
620        .width(Length::Fixed(side * 2.0))
621        .height(Length::Fixed(side))
622        .into()
623}
624
625struct BatteryGlyph {
626    fraction: f32,
627    outline: iced::Color,
628    fill: iced::Color,
629}
630
631impl<Message> canvas::Program<Message, Theme, Renderer> for BatteryGlyph {
632    type State = ();
633
634    fn draw(
635        &self,
636        _state: &Self::State,
637        renderer: &Renderer,
638        _theme: &Theme,
639        bounds: Rectangle,
640        _cursor: mouse::Cursor,
641    ) -> Vec<canvas::Geometry> {
642        let mut frame = canvas::Frame::new(renderer, bounds.size());
643        let h = bounds.height;
644        let stroke = (h * 0.08).max(1.5);
645        let nub = h * 0.12;
646        let body_w = bounds.width - nub - stroke;
647        let body = canvas::Path::rounded_rectangle(
648            Point::new(stroke / 2.0, stroke / 2.0),
649            iced::Size::new(body_w, h - stroke),
650            (h * 0.18).into(),
651        );
652        frame.stroke(
653            &body,
654            canvas::Stroke::default().with_width(stroke).with_color(self.outline),
655        );
656        // The terminal, centred on the right end.
657        frame.fill_rectangle(
658            Point::new(body_w + stroke / 2.0, h * 0.32),
659            iced::Size::new(nub, h * 0.36),
660            canvas::Fill::from(self.outline),
661        );
662        // The charge, inset from the outline so it reads as contents.
663        let inset = stroke * 2.0;
664        let full = body_w - inset * 2.0 + stroke;
665        if self.fraction > 0.0 {
666            frame.fill(
667                &canvas::Path::rounded_rectangle(
668                    Point::new(inset, inset),
669                    iced::Size::new(full * self.fraction, h - inset * 2.0),
670                    (h * 0.08).into(),
671                ),
672                self.fill,
673            );
674        }
675        vec![frame.into_geometry()]
676    }
677}
678
679/// The eject mark: a triangle pointing up over a bar, as on every
680/// drive's button since the cassette deck.
681///
682/// Drawn, because `⏏` is in few fonts and wears whichever weight the one
683/// it falls back to has — and this sits beside drawn folder marks in a
684/// sidebar row, where a typed character would be the one thing out of
685/// line. Files' Devices section puts it on a mounted drive's row.
686pub fn eject<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
687    canvas(EjectGlyph { color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
688}
689
690struct EjectGlyph {
691    color: iced::Color,
692}
693
694impl<Message> canvas::Program<Message, Theme, Renderer> for EjectGlyph {
695    type State = ();
696
697    fn draw(
698        &self,
699        _state: &Self::State,
700        renderer: &Renderer,
701        _theme: &Theme,
702        bounds: Rectangle,
703        _cursor: mouse::Cursor,
704    ) -> Vec<canvas::Geometry> {
705        let mut frame = canvas::Frame::new(renderer, bounds.size());
706        let side = bounds.width.min(bounds.height);
707        let extent = side * VIEW_EXTENT_FRACTION;
708        let left = bounds.width / 2.0 - extent / 2.0;
709        let top = bounds.height / 2.0 - extent / 2.0;
710        let fill = canvas::Fill::from(self.color);
711        // The triangle takes the top three fifths, a gap a fifth, and
712        // the bar the rest — the proportions of the symbol as printed on
713        // hardware, where the bar is as wide as the triangle's base.
714        let triangle = canvas::Path::new(|b| {
715            b.move_to(Point::new(left + extent / 2.0, top));
716            b.line_to(Point::new(left + extent, top + extent * 0.6));
717            b.line_to(Point::new(left, top + extent * 0.6));
718            b.close();
719        });
720        frame.fill(&triangle, fill);
721        frame.fill_rectangle(
722            Point::new(left, top + extent * 0.78),
723            iced::Size::new(extent, extent * 0.22),
724            fill,
725        );
726        vec![frame.into_geometry()]
727    }
728}
729
730/// The star mark: a filled five-pointed star — what a starred file
731/// carries in Files' listing and grid.
732///
733/// Drawn, because `★` is one of the characters whose weight and size
734/// depend entirely on which font the desktop falls back to for it, and
735/// it sits on the corner of a drawn file icon, where a typed character
736/// would sit on its own baseline rather than on the icon.
737pub fn star<'a, Message: 'a>(side: f32, color: iced::Color) -> Element<'a, Message> {
738    canvas(StarGlyph { color }).width(Length::Fixed(side)).height(Length::Fixed(side)).into()
739}
740
741struct StarGlyph {
742    color: iced::Color,
743}
744
745impl<Message> canvas::Program<Message, Theme, Renderer> for StarGlyph {
746    type State = ();
747
748    fn draw(
749        &self,
750        _state: &Self::State,
751        renderer: &Renderer,
752        _theme: &Theme,
753        bounds: Rectangle,
754        _cursor: mouse::Cursor,
755    ) -> Vec<canvas::Geometry> {
756        let mut frame = canvas::Frame::new(renderer, bounds.size());
757        let side = bounds.width.min(bounds.height);
758        // The whole box, nearly: at the size it is drawn — a corner of
759        // an icon — any margin is star that is not there.
760        let outer = side * 0.48;
761        // The regular star's inner radius, outer × sin 18° / sin 54°:
762        // straight edges from point to point, not a fat or a spiky star.
763        let inner = outer * 0.382;
764        let centre = Point::new(bounds.width / 2.0, bounds.height / 2.0 + side * 0.03);
765        let star = canvas::Path::new(|b| {
766            for i in 0..10 {
767                let r = if i % 2 == 0 { outer } else { inner };
768                // From straight up, a tenth of a turn at a time.
769                let angle = -std::f32::consts::FRAC_PI_2 + i as f32 * std::f32::consts::PI / 5.0;
770                let point = Point::new(centre.x + r * angle.cos(), centre.y + r * angle.sin());
771                if i == 0 {
772                    b.move_to(point);
773                } else {
774                    b.line_to(point);
775                }
776            }
777            b.close();
778        });
779        frame.fill(&star, canvas::Fill::from(self.color));
780        vec![frame.into_geometry()]
781    }
782}
783
784// Only `signal_bars` is tested below, because it is a decision rather
785// than a shape. For the rest there are no tests, deliberately.
786//
787// What this module decides is a *shape*, and the only instrument that
788// can check a shape is a screenshot. The two things a test could reach —
789// that the three `Nav` variants differ, and that the fractions above are
790// between 0 and 1 — are both true by construction: clippy rejects the
791// second as a constant assertion, which is the correct verdict on a test
792// that cannot fail.
793//
794// What is verified instead, and how: rendered at 26px and looked at
795// beside the design, which is how the typed guillemets this replaced
796// were found wanting in the first place. The property that keeps it
797// honest at other sizes is that every dimension is a fraction of the
798// button rather than a pixel count, and that is visible in the source
799// above rather than assertable below.
800
801#[cfg(test)]
802mod tests {
803    use super::*;
804
805    /// Any signal at all lights a bar — a network that is there but weak
806    /// must not look like one that is gone — and four is kept for strong.
807    #[test]
808    fn any_signal_lights_a_bar_and_only_a_strong_one_lights_four() {
809        assert_eq!(signal_bars(0), 0);
810        assert_eq!(signal_bars(1), 1);
811        assert_eq!(signal_bars(54), 2);
812        assert_eq!(signal_bars(79), 3);
813        assert_eq!(signal_bars(97), 4);
814        assert_eq!(signal_bars(100), 4);
815    }
816}