Skip to main content

demo_common/
page_theme.rs

1//! The iced themes matching the page themes the demos are embedded in, so an
2//! embedded demo follows the page it sits in: rustdoc's three built-in themes
3//! and the project site's phosphor storage-tube theme.
4
5use iced::Theme;
6use iced::theme::Palette;
7
8/// Every page theme name [`page_theme`] accepts. The first three are the names
9/// rustdoc puts in `html[data-theme]`; `phosphor` is the project site's.
10pub const PAGE_THEMES: [&str; 4] = ["light", "dark", "ayu", "phosphor"];
11
12/// The iced theme matching the page theme `name`; `None` for any name not in
13/// [`PAGE_THEMES`].
14///
15/// For rustdoc's themes, background, text, primary and warning come from the
16/// page theme's `--main-background-color`, `--main-color`, `--link-color` and
17/// `--warning-border-color`. Rustdoc has no success or danger colours, so
18/// those are iced's own for the matching brightness. `phosphor` is the site's
19/// storage-tube faceplate: a dark green-grey background with phosphor-green
20/// text and accents.
21pub fn page_theme(name: &str) -> Option<Theme> {
22    let (name, palette) = match name {
23        "light" => (
24            "Rustdoc Light",
25            rustdoc_palette(
26                iced::color!(0xffffff),
27                iced::color!(0x000000),
28                iced::color!(0x3873ad),
29                Palette::LIGHT,
30            ),
31        ),
32        "dark" => (
33            "Rustdoc Dark",
34            rustdoc_palette(
35                iced::color!(0x353535),
36                iced::color!(0xdddddd),
37                iced::color!(0xd2991d),
38                Palette::DARK,
39            ),
40        ),
41        "ayu" => (
42            "Rustdoc Ayu",
43            rustdoc_palette(
44                iced::color!(0x0f1419),
45                iced::color!(0xc5c5c5),
46                iced::color!(0x39afd7),
47                Palette::DARK,
48            ),
49        ),
50        "phosphor" => (
51            "Phosphor",
52            Palette {
53                background: iced::color!(0x0e1712),
54                text: iced::color!(0xa8f5bd),
55                primary: iced::color!(0x5fe38a),
56                success: iced::color!(0x5fe38a),
57                warning: iced::color!(0xe3d36a),
58                danger: iced::color!(0xff8a5c),
59            },
60        ),
61        _ => return None,
62    };
63
64    Some(Theme::custom(name.to_owned(), palette))
65}
66
67/// A rustdoc page theme's palette: its own background, text and primary, the
68/// warning all three rustdoc themes share, and `base` for the rest.
69fn rustdoc_palette(
70    background: iced::Color,
71    text: iced::Color,
72    primary: iced::Color,
73    base: Palette,
74) -> Palette {
75    Palette {
76        background,
77        text,
78        primary,
79        warning: iced::color!(0xff8e00),
80        ..base
81    }
82}