#![cfg(all(feature = "icons", not(feature = "mini"), not(target_arch = "wasm32")))]
use std::fs;
use std::path::Path;
use rust_widgets::core::{Color, Font, HorizontalAlignment, Point, Rect};
use rust_widgets::render::{PaintBackend, RenderContext, SvgPaintBackend};
use rust_widgets::theme::AppearanceMode;
use rust_widgets::widget::{Draw, Icon, IconName};
const OUTPUT_DIR: &str = "snapshots/svg";
const GENERATED_MARKER: &str = "<!-- GENERATED by examples/export_icon_sheet.rs -->";
const COLUMNS: usize = 8;
const CELL: u32 = 48;
const LABEL_HEIGHT: u32 = 14;
const PADDING: u32 = 8;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let dir = Path::new(OUTPUT_DIR);
fs::create_dir_all(dir)?;
for (appearance, suffix, label) in
[(AppearanceMode::Dark, "", "dark"), (AppearanceMode::Light, ".light", "light")]
{
rust_widgets::theme::global_theme_manager().set_appearance(appearance);
let backdrop = rust_widgets::theme::global_theme_manager()
.current_theme()
.map(|active| active.colors.background)
.unwrap_or(Color::WHITE);
let ink = rust_widgets::theme::global_theme_manager()
.current_theme()
.map(|active| active.colors.foreground)
.unwrap_or(Color::BLACK);
let svg = render_sheet(backdrop, ink);
let document =
format!("{GENERATED_MARKER}\n<!-- icon sheet | appearance: {label} -->\n{svg}\n");
let path = dir.join(format!("icon_sheet{suffix}.svg"));
fs::write(&path, document)?;
println!("wrote {}", path.display());
}
Ok(())
}
fn render_sheet(backdrop: Color, ink: Color) -> String {
let rows = IconName::ALL.len().div_ceil(COLUMNS);
let width = PADDING + COLUMNS as u32 * (CELL + PADDING);
let height = PADDING + rows as u32 * (CELL + LABEL_HEIGHT + PADDING);
let mut backend = SvgPaintBackend::new(rust_widgets::core::Size::new(width, height));
backend.begin_frame(backdrop);
{
let mut ctx = RenderContext::new(&mut backend);
for (index, name) in IconName::ALL.into_iter().enumerate() {
let column = (index % COLUMNS) as u32;
let row = (index / COLUMNS) as u32;
let x = PADDING + column * (CELL + PADDING);
let y = PADDING + row * (CELL + LABEL_HEIGHT + PADDING);
let mut icon = Icon::new(Rect::new(x as i32, y as i32, CELL, CELL));
icon.set_icon_enum(name);
icon.set_size(CELL as f32);
icon.set_color(ink);
icon.draw(&mut ctx);
ctx.draw_text(
Point::new((x + CELL / 2) as i32, (y + CELL + 10) as i32),
name.as_str(),
&Font::simple("sans-serif", 8.0),
ink,
HorizontalAlignment::Center,
);
}
}
backend.end_frame();
backend.finish()
}