Files
komorebi/komorebi-recorder/src/icon.rs
T
LGUG2Z 8a203f0da7 feat(icon): a watermelon for every window
The recorder window and the viewer carry an icon of their own: a slice of
watermelon, drawn as komorebi-recorder/assets/watermelon.svg. `just icon`
(assets/generate.py, with resvg from the devShell) renders the PNG the windows
set and the ICO that Windows embeds. Until now eframe's default egui icon
showed.

macOS: eframe sets the PNG as the Dock icon while a window is open. The program
is a bare executable and not an app bundle, so Finder is unchanged.

Windows: eframe sets the same PNG as the window and taskbar icon. build.rs
compiles the ICO into both executables with embed-resource (rc.exe on Windows,
llvm-rc when cross-building from a Mac), and the installer shows it in Apps &
features through ARPPRODUCTICON. The taskbar, Explorer and the installer have
not been looked at on Windows: W4 and W5 in docs/dev/windows-checkpoints.md now
say what to check.

embed-resource adds seven crates to the lockfile and moves toml_datetime and
toml_parser up by a patch release.
2026-10-27 22:22:56 -07:00

100 lines
3.8 KiB
Rust

//! The icon of every window the program opens: a slice of watermelon.
//!
//! The picture is `assets/watermelon.svg`. `assets/generate.py` (`just icon`) renders it into `assets/watermelon.png`,
//! which this module embeds, and `assets/watermelon.ico`, which Windows shows for the executables (`build.rs` compiles
//! it into them) and for the installer. What each host does with the icon is in `docs/PLATFORM.md`.
use eframe::egui;
use std::io::Cursor;
/// The watermelon, 256 by 256 pixels of RGBA.
const PNG: &[u8] = include_bytes!("../assets/watermelon.png");
/// Returns the icon of the program's windows: a slice of watermelon on a transparent background, 256 by 256 pixels.
///
/// The recorder window and the viewer pass it to [`egui::ViewportBuilder::with_icon`]. eframe sets it as the window and
/// taskbar icon on Windows and as the Dock icon on macOS.
///
/// The picture is part of the binary, and a test holds that it decodes. If it ever did not, this returns an empty
/// [`egui::IconData`], which the window system takes to mean no icon: the window opens with the system's default.
///
/// # Examples
///
/// ```rust
/// let icon = komorebi_recorder::icon::window_icon();
///
/// assert_eq!((icon.width, icon.height), (256, 256));
/// assert_eq!(icon.rgba.len(), 256 * 256 * 4);
///
/// // the corner of the picture is transparent and its middle is not
/// assert_eq!(icon.rgba[3], 0);
/// assert_eq!(icon.rgba[(128 * 256 + 128) * 4 + 3], 255);
/// ```
pub fn window_icon() -> egui::IconData {
decode().unwrap_or_default()
}
/// The picture as RGBA, or nothing if it is not an RGBA PNG with eight bits a channel, which is what `generate.py`
/// writes.
fn decode() -> Option<egui::IconData> {
let mut reader = png::Decoder::new(Cursor::new(PNG)).read_info().ok()?;
let mut rgba = vec![0; reader.output_buffer_size()?];
let info = reader.next_frame(&mut rgba).ok()?;
(info.color_type == png::ColorType::Rgba && info.bit_depth == png::BitDepth::Eight).then(|| {
rgba.truncate(info.buffer_size());
egui::IconData {
rgba,
width: info.width,
height: info.height,
}
})
}
#[cfg(test)]
mod tests {
use super::*;
const ICO: &[u8] = include_bytes!("../assets/watermelon.ico");
#[test]
fn the_embedded_picture_decodes() {
let icon = decode().expect("watermelon.png is an RGBA PNG with eight bits a channel");
assert_eq!(icon.rgba.len(), (icon.width * icon.height * 4) as usize);
assert_eq!(window_icon(), icon);
}
#[test]
fn the_windows_icon_holds_the_same_picture_at_256_pixels() {
assert_eq!(ICO[..4], [0, 0, 1, 0], "an icon file starts with its type");
let count = usize::from(u16::from_le_bytes([ICO[4], ICO[5]]));
assert!(
count > 1,
"one size is not enough for a taskbar and a window"
);
let mut widths = Vec::new();
for entry in ICO[6..].as_chunks::<16>().0.iter().take(count) {
// a width of 0 means 256
let width = entry[0];
let pixels = if width == 0 { 256 } else { u32::from(width) };
let length = u32::from_le_bytes([entry[8], entry[9], entry[10], entry[11]]) as usize;
let offset = u32::from_le_bytes([entry[12], entry[13], entry[14], entry[15]]) as usize;
let image = &ICO[offset..offset + length];
assert!(
image.starts_with(b"\x89PNG"),
"the {pixels} pixel entry is not a PNG"
);
if width == 0 {
assert_eq!(image, PNG, "the 256 pixel entry is not watermelon.png");
}
widths.push(width);
}
assert!(widths.contains(&0), "no 256 pixel entry in {widths:?}");
assert!(widths.contains(&16), "no 16 pixel entry in {widths:?}");
}
}