Files
Marcos Paulo 3999db21f3
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
ci / ci (macos-latest) (push) Has been cancelled
feat(whiteboard): edge handles, rotation snapping, flip when a resize crosses the opposite side, right-click menu (flip, order, group, lock, duplicate, delete); colour popover fits without scrolling, its chevron toggles and it fades out
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 18:54:52 -03:00

115 lines
4.6 KiB
Rust

//! Renders the whiteboard widget offscreen for visual review.
use notescraft_ui_egui::{
theme::Tokens,
whiteboard_view::{PaletteTab, WhiteboardTool, WhiteboardView, show_whiteboard},
};
use notescraft_whiteboard::{Element, IdGen, Whiteboard};
fn main() {
let mut args = std::env::args().skip(1);
let output = args.next().unwrap_or_else(|| "/tmp/notescraft-whiteboard.png".to_owned());
// `--theme <name>` picks the built-in theme (default: Catppuccin Mocha).
let mut theme_name = "catppuccin-mocha".to_owned();
let mut layers_open = false;
let mut tool = WhiteboardTool::Select;
let mut selection = Vec::new();
let mut popover = None;
let mut board_file = None;
let mut size = egui::vec2(1000.0, 720.0);
let mut scale = 1.0_f32;
while let Some(flag) = args.next() {
match flag.as_str() {
"--theme" => {
if let Some(name) = args.next() {
theme_name = name;
}
}
"--layers" => layers_open = true,
"--board" => board_file = args.next(),
"--wb-select" => selection = args.next().unwrap_or_default().split(',').map(str::to_owned).collect(),
"--wb-tool" => {
let name = args.next().unwrap_or_default();
tool = WhiteboardTool::ALL
.into_iter()
.find(|tool| tool.label().eq_ignore_ascii_case(&name) || (*tool == WhiteboardTool::Freedraw && name == "freehand"))
.unwrap_or_default();
}
"--wb-popover" => {
popover = match args.next().as_deref() {
Some("fill") => Some(PaletteTab::Fill),
Some("stroke") => Some(PaletteTab::Stroke),
Some("canvas") => Some(PaletteTab::Canvas),
_ => None,
}
}
"--size" => {
if let Some(value) = args.next()
&& let Some((width, height)) = value.split_once('x')
&& let (Ok(width), Ok(height)) = (width.parse::<f32>(), height.parse::<f32>())
{
size = egui::vec2(width, height);
}
}
"--scale" => scale = args.next().and_then(|value| value.parse::<f32>().ok()).filter(|value| *value > 0.0).unwrap_or(1.0),
_ => {}
}
}
let Some(theme) = notescraft_themes::builtin(&theme_name) else {
eprintln!("missing built-in theme {theme_name}");
std::process::exit(1);
};
let mut ids = IdGen::new();
let mut board = Whiteboard::new();
let mut rectangle = Element::rectangle(&mut ids, 140.0, 150.0, 220.0, 140.0);
"#89b4fa".clone_into(&mut rectangle.stroke_color);
board.add(rectangle);
board.add(Element::diamond(&mut ids, 450.0, 210.0, 140.0, 110.0));
board.add(Element::text(&mut ids, 180.0, 330.0, "Sketch your ideas"));
if let Some(path) = board_file {
board = if let Some(board) = std::fs::read_to_string(&path).ok().and_then(|source| Whiteboard::from_str(&source).ok()) {
board
} else {
eprintln!("could not read board {path}");
std::process::exit(1);
};
}
let mut view = WhiteboardView::default();
view.tool = tool;
view.selected_ids = selection;
view.palette_open = popover;
view.layers_open = layers_open;
let state = (Tokens::new(theme), view, board, false);
let mut harness = egui_kittest::Harness::builder().with_size(size).with_pixels_per_point(scale).with_max_steps(32).wgpu().build_ui_state(
|ui, state: &mut (Tokens, WhiteboardView, Whiteboard, bool)| {
// Named font families exist from the pass after they are installed.
if !state.3 {
notescraft_ui_egui::fonts::install(ui.ctx());
state.3 = true;
ui.ctx().request_repaint();
return;
}
// The tool icons are SVG textures; without the image loaders the
// harness paints the broken-image placeholder instead.
egui_extras::install_image_loaders(ui.ctx());
let _ = show_whiteboard(ui, &state.0, &mut state.1, &mut state.2);
},
state,
);
harness.run();
match harness.render() {
Ok(image) => match image.save(&output) {
Ok(()) => println!("wrote {output}"),
Err(error) => {
eprintln!("could not save {output}: {error}");
std::process::exit(1);
}
},
Err(error) => {
eprintln!("render failed: {error}");
std::process::exit(1);
}
}
}