Coverage Report

Created: 2026-12-05 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
super-voice-gpui/src/assets.rs
Line
Count
Source
1
// SPDX-License-Identifier: GPL-3.0-only
2
//! What the app draws that isn't code: the app's icon, the Lucide icons the
3
//! kit doesn't embed by default, and the kit's own.
4
5
use std::borrow::Cow;
6
7
use gpui_kit::assets::icon_assets;
8
use gpui_kit::{
9
    App, AssetSource, ImageSource, ImgResourceLoader, Pixels, Resource, Result, SharedString,
10
    Window,
11
};
12
13
// The Lucide icons the app draws beyond the kit's default set.
14
icon_assets!(
15
    ExtraIcons,
16
    [
17
        AppWindow,
18
        AudioLines,
19
        BellOff,
20
        BookOpenText,
21
        Brain,
22
        Bug,
23
        Cloud,
24
        Download,
25
        FileVolume,
26
        KeyRound,
27
        Keyboard,
28
        LibraryBig,
29
        ListChecks,
30
        ListFilter,
31
        Microchip,
32
        NotebookPen,
33
        PackagePlus,
34
        ServerOff,
35
        ShieldX,
36
        SlidersHorizontal,
37
        Speaker,
38
        Type,
39
        Upload,
40
        UserRoundPen,
41
        Volume2,
42
        WandSparkles,
43
    ]
44
);
45
46
/// The app's icon at each size the desktop gets it, as `brand/icon/linux`
47
/// draws them: each size is drawn for its own pixels, small ones with a
48
/// tighter margin, so none of them is a scaled copy of another.
49
const APP_ICONS: &[(u32, &[u8])] = &[
50
    (
51
        16,
52
        include_bytes!("../resources/icons/hicolor/16x16/apps/super-voice-app.png"),
53
    ),
54
    (
55
        22,
56
        include_bytes!("../resources/icons/hicolor/22x22/apps/super-voice-app.png"),
57
    ),
58
    (
59
        24,
60
        include_bytes!("../resources/icons/hicolor/24x24/apps/super-voice-app.png"),
61
    ),
62
    (
63
        32,
64
        include_bytes!("../resources/icons/hicolor/32x32/apps/super-voice-app.png"),
65
    ),
66
    (
67
        48,
68
        include_bytes!("../resources/icons/hicolor/48x48/apps/super-voice-app.png"),
69
    ),
70
    (
71
        64,
72
        include_bytes!("../resources/icons/hicolor/64x64/apps/super-voice-app.png"),
73
    ),
74
    (
75
        128,
76
        include_bytes!("../resources/icons/hicolor/128x128/apps/super-voice-app.png"),
77
    ),
78
    (
79
        256,
80
        include_bytes!("../resources/icons/hicolor/256x256/apps/super-voice-app.png"),
81
    ),
82
    (
83
        512,
84
        include_bytes!("../resources/icons/hicolor/512x512/apps/super-voice-app.png"),
85
    ),
86
];
87
88
/// The asset path of the app's icon drawn `pixels` wide.
89
54
fn app_icon_path(pixels: u32) -> String {
90
54
    format!("app/super-voice-app-{pixels}.png")
91
54
}
92
93
/// Which of [`APP_ICONS`] to draw for `device_pixels` on screen: the
94
/// smallest that is at least that wide, so it is only ever scaled down,
95
/// and by as little as possible.
96
7
fn app_icon_size(device_pixels: f32) -> u32 {
97
7
    APP_ICONS
98
7
        .iter()
99
        .map(|&(size, _)| size)
100
34
        .find(|&size| f64::from(size) >= f64::from(device_pixels) - 0.5)
101
7
        .unwrap_or(512)
102
7
}
103
104
/// The app's icon, for `img()`, drawn `size` wide.
105
///
106
/// Drawn from the PNG made for that many device pixels, picked when it is
107
/// painted, so it follows the screen's scale. The scalable SVG would be
108
/// rasterised at 1024 px or more and shrunk on the GPU to 16 to 64, which
109
/// blurs it.
110
0
pub fn app_icon(size: Pixels) -> ImageSource {
111
0
    ImageSource::from(move |window: &mut Window, cx: &mut App| {
112
0
        let pixels = app_icon_size(f32::from(size) * window.scale_factor());
113
0
        let path = SharedString::from(app_icon_path(pixels));
114
0
        window.use_asset::<ImgResourceLoader>(&Resource::Embedded(path), cx)
115
0
    })
116
0
}
117
118
/// Every asset the app loads: its own first, then the kit's.
119
#[derive(Clone, Copy, Debug, Default)]
120
pub struct Assets;
121
122
impl AssetSource for Assets {
123
9
    fn load(&self, path: &str) -> Result<Option<Cow<'static, [u8]>>> {
124
9
        if let Some(&(_, png)) = APP_ICONS
125
9
            .iter()
126
45
            .find(|&&(size, _)| app_icon_path(size) == path)
127
        {
128
9
            return Ok(Some(Cow::Borrowed(png)));
129
0
        }
130
0
        if let Some(icon) = ExtraIcons.load(path)? {
131
0
            return Ok(Some(icon));
132
0
        }
133
0
        gpui_kit::assets::Assets.load(path)
134
9
    }
135
136
0
    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
137
0
        let mut all = gpui_kit::assets::Assets.list(path)?;
138
0
        all.extend(ExtraIcons.list(path)?);
139
0
        all.extend(
140
0
            APP_ICONS
141
0
                .iter()
142
0
                .map(|&(size, _)| SharedString::from(app_icon_path(size)))
143
0
                .filter(|name| name.starts_with(path)),
144
        );
145
0
        Ok(all)
146
0
    }
147
}
148
149
#[cfg(test)]
150
mod tests {
151
    use super::*;
152
153
    /// A size the desktop draws gets its own PNG; one between sizes gets the
154
    /// next larger, and anything past the largest gets the largest.
155
    #[test]
156
1
    fn the_icon_drawn_is_the_one_made_for_its_pixels() {
157
1
        assert_eq!(app_icon_size(16.), 16);
158
1
        assert_eq!(app_icon_size(32.), 32);
159
1
        assert_eq!(app_icon_size(64.), 64);
160
1
        assert_eq!(app_icon_size(20.), 22);
161
1
        assert_eq!(app_icon_size(40.), 48);
162
1
        assert_eq!(app_icon_size(80.), 128);
163
1
        assert_eq!(app_icon_size(2000.), 512);
164
1
    }
165
166
    /// Every size loads, as a PNG.
167
    #[test]
168
1
    fn every_icon_size_loads() {
169
1
        for &(size, _) in APP_ICONS {
170
9
            let bytes = Assets
171
9
                .load(&app_icon_path(size))
172
9
                .unwrap()
173
9
                .expect("the icon loads");
174
9
            assert!(bytes.starts_with(b"\x89PNG"), "{size} is a PNG");
175
        }
176
1
    }
177
}