Skip to main content

egui_pop_viewport/
tokens.rs

1//! The viewport's component tokens (`viewport.*` in the theme files),
2//! each an alias of a system token.
3//!
4//! <div class="warning">Experimental: may change in any release
5//! (docs/stability.md).</div>
6
7use egui::{Color32, Context};
8use egui_pop_core::theme::Tokens;
9
10/// The viewport's colours, from the installed theme.
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct ViewportTokens {
13    /// Around the image.
14    pub background: Color32,
15    /// Under the image where no tile has arrived yet.
16    pub backdrop: Color32,
17    /// The image's outline.
18    pub frame: Color32,
19    /// The pixel grid at high zoom.
20    pub grid: Color32,
21    /// Overlay shapes and guides.
22    pub guide: Color32,
23    /// A handle at rest.
24    pub handle: Color32,
25    /// A handle under the pointer or dragged.
26    pub handle_hover: Color32,
27    /// The ring around a handle, so it reads on any image.
28    pub handle_outline: Color32,
29    /// Tool cursors' strokes and a brush outline's line.
30    pub cursor_fill: Color32,
31    /// The ring around a cursor's strokes, so it reads on any image.
32    pub cursor_outline: Color32,
33    /// Around the loupe's picture and behind its readout.
34    pub loupe_background: Color32,
35    /// The loupe's outline.
36    pub loupe_frame: Color32,
37    /// The loupe's pixel grid.
38    pub loupe_grid: Color32,
39    /// The square around the loupe's centre pixel.
40    pub loupe_marker: Color32,
41}
42
43impl ViewportTokens {
44    /// The tokens of the theme installed in `ctx`.
45    pub fn of(ctx: &Context) -> ViewportTokens {
46        let t = Tokens::of(ctx);
47        let c = |path: &str, fallback: Color32| t.color(path).unwrap_or(fallback);
48        ViewportTokens {
49            background: c("viewport.background", t.surface.canvas),
50            backdrop: c("viewport.backdrop", t.surface.sunken),
51            frame: c("viewport.frame", t.border.default),
52            grid: c("viewport.grid", t.border.default),
53            guide: c("viewport.guide", t.text.strong),
54            handle: c("viewport.handle", t.accent.default),
55            handle_hover: c("viewport.handle-hover", t.accent.hover),
56            handle_outline: c("viewport.handle-outline", t.surface.canvas),
57            cursor_fill: c("cursor.fill", t.text.strong),
58            cursor_outline: c("cursor.outline", t.surface.canvas),
59            loupe_background: c("loupe.background", t.surface.overlay),
60            loupe_frame: c("loupe.frame", t.border.strong),
61            loupe_grid: c("loupe.grid", t.border.default),
62            loupe_marker: c("loupe.marker", t.accent.default),
63        }
64    }
65}