The window is built in setup with the theme tokens injected by an initialization script and the background colour taken from the theme, so the first frame is already themed. Frontend IPC (engine_execute, engine_list_commands) and the control channel both run commands through the Host, which applies effects such as Quit. ui.screenshot captures the window's client area from the screen because xcap skips windows of its own process. build.rs generates the placeholder icon into the gitignored icons/ directory. Verified on Windows: window opens, ping, listCommands, screenshot, app.quit, and exit status 1 when the control port is taken. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
4.4 KiB
Rust
130 lines
4.4 KiB
Rust
//! Build script of the desktop app.
|
||
//!
|
||
//! Generates the placeholder app icon (an otter-brown rounded square holding a
|
||
//! pale stone) into the gitignored `icons/` directory, then runs the Tauri
|
||
//! build step, which needs those files. The real icon is the owner's to
|
||
//! commission; this one is generated in code and has no asset file.
|
||
|
||
use std::error::Error;
|
||
use std::fs;
|
||
use std::path::Path;
|
||
|
||
/// Edge length of the generated icon in pixels.
|
||
const SIZE: u32 = 256;
|
||
|
||
/// Writes the icons and runs `tauri_build`.
|
||
///
|
||
/// # Errors
|
||
/// Fails if the icons cannot be encoded or written.
|
||
fn main() -> Result<(), Box<dyn Error>> {
|
||
let dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR")?).join("icons");
|
||
fs::create_dir_all(&dir)?;
|
||
let png = encode_png(&draw_icon(), SIZE)?;
|
||
write_if_changed(&dir.join("icon.png"), &png)?;
|
||
write_if_changed(&dir.join("icon.ico"), &ico_from_png(&png))?;
|
||
println!("cargo:rerun-if-changed=build.rs");
|
||
tauri_build::build();
|
||
Ok(())
|
||
}
|
||
|
||
/// Writes a file only if its content differs, so that the Tauri build step
|
||
/// does not see a changed icon on every build.
|
||
///
|
||
/// # Errors
|
||
/// Fails if the file cannot be written.
|
||
fn write_if_changed(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||
if fs::read(path).ok().as_deref() == Some(bytes) {
|
||
return Ok(());
|
||
}
|
||
fs::write(path, bytes)
|
||
}
|
||
|
||
/// Draws the icon as RGBA pixels, row by row, with 4×4 supersampling for
|
||
/// smooth edges.
|
||
fn draw_icon() -> Vec<u8> {
|
||
let body = [0x6b, 0x4a, 0x32];
|
||
let stone = [0xd9, 0xd2, 0xc5];
|
||
let size = f64::from(SIZE);
|
||
let mut pixels = Vec::with_capacity((SIZE * SIZE * 4) as usize);
|
||
for y in 0..SIZE {
|
||
for x in 0..SIZE {
|
||
let (mut body_cover, mut stone_cover) = (0.0, 0.0);
|
||
for sy in 0..4 {
|
||
for sx in 0..4 {
|
||
let px = (f64::from(x) + (f64::from(sx) + 0.5) / 4.0) / size;
|
||
let py = (f64::from(y) + (f64::from(sy) + 0.5) / 4.0) / size;
|
||
if in_rounded_square(px, py) {
|
||
body_cover += 1.0 / 16.0;
|
||
if in_stone(px, py) {
|
||
stone_cover += 1.0 / 16.0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
let mix = |b: u8, s: u8| {
|
||
let b = f64::from(b);
|
||
let s = f64::from(s);
|
||
let t = if body_cover > 0.0 {
|
||
stone_cover / body_cover
|
||
} else {
|
||
0.0
|
||
};
|
||
(b + (s - b) * t).round().clamp(0.0, 255.0) as u8
|
||
};
|
||
pixels.extend_from_slice(&[
|
||
mix(body[0], stone[0]),
|
||
mix(body[1], stone[1]),
|
||
mix(body[2], stone[2]),
|
||
(body_cover * 255.0).round().clamp(0.0, 255.0) as u8,
|
||
]);
|
||
}
|
||
}
|
||
pixels
|
||
}
|
||
|
||
/// Whether a point in unit coordinates lies in the rounded square with a
|
||
/// small margin.
|
||
fn in_rounded_square(x: f64, y: f64) -> bool {
|
||
let (lo, hi, r) = (0.06, 0.94, 0.2);
|
||
if !(lo..=hi).contains(&x) || !(lo..=hi).contains(&y) {
|
||
return false;
|
||
}
|
||
let cx = x.clamp(lo + r, hi - r);
|
||
let cy = y.clamp(lo + r, hi - r);
|
||
(x - cx).powi(2) + (y - cy).powi(2) <= r * r
|
||
}
|
||
|
||
/// Whether a point in unit coordinates lies in the stone, an ellipse in the
|
||
/// lower middle.
|
||
fn in_stone(x: f64, y: f64) -> bool {
|
||
((x - 0.5) / 0.26).powi(2) + ((y - 0.6) / 0.18).powi(2) <= 1.0
|
||
}
|
||
|
||
/// Encodes RGBA pixels as PNG.
|
||
///
|
||
/// # Errors
|
||
/// Fails if the encoder rejects the data.
|
||
fn encode_png(pixels: &[u8], size: u32) -> Result<Vec<u8>, png::EncodingError> {
|
||
let mut out = Vec::new();
|
||
{
|
||
let mut encoder = png::Encoder::new(&mut out, size, size);
|
||
encoder.set_color(png::ColorType::Rgba);
|
||
encoder.set_depth(png::BitDepth::Eight);
|
||
let mut writer = encoder.write_header()?;
|
||
writer.write_image_data(pixels)?;
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
/// Wraps a 256×256 PNG in an ICO container with a single entry, which every
|
||
/// Windows version since Vista reads.
|
||
fn ico_from_png(png: &[u8]) -> Vec<u8> {
|
||
let len = u32::try_from(png.len()).unwrap_or(u32::MAX);
|
||
let mut ico = Vec::with_capacity(22 + png.len());
|
||
ico.extend_from_slice(&[0, 0, 1, 0, 1, 0]);
|
||
ico.extend_from_slice(&[0, 0, 0, 0, 1, 0, 32, 0]);
|
||
ico.extend_from_slice(&len.to_le_bytes());
|
||
ico.extend_from_slice(&22u32.to_le_bytes());
|
||
ico.extend_from_slice(png);
|
||
ico
|
||
}
|