ce3c3bf2b0
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
66 lines
2.6 KiB
Rust
66 lines
2.6 KiB
Rust
//! Renders a sample Canvas view offscreen into a PNG.
|
|
|
|
use std::error::Error;
|
|
|
|
use notescraft_canvas::{Canvas, Edge, Node, NodeKind, Side};
|
|
use notescraft_ui_egui::canvas_view::{CanvasEnv, CanvasView, show_canvas};
|
|
use notescraft_ui_egui::theme::Tokens;
|
|
|
|
struct Snapshot {
|
|
tokens: Tokens,
|
|
state: CanvasView,
|
|
canvas: Canvas,
|
|
fonts_ready: bool,
|
|
}
|
|
|
|
fn main() {
|
|
if let Err(error) = run() {
|
|
eprintln!("canvas snapshot failed: {error}");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
fn run() -> Result<(), Box<dyn Error>> {
|
|
let theme = notescraft_themes::builtin("catppuccin-mocha").ok_or("missing Catppuccin theme")?;
|
|
let mut canvas = Canvas::default();
|
|
canvas.add_node(Node::new("text", NodeKind::Text { text: String::from("# Canvas\nMarkdown preview") }, 40.0, 50.0, 260.0, 180.0))?;
|
|
canvas.add_node(Node::new("file", NodeKind::File { file: String::from("Notes/Ideas.md"), subpath: None }, 380.0, 50.0, 260.0, 180.0))?;
|
|
canvas.add_node(Node::new("link", NodeKind::Link { url: String::from("https://obsidian.md") }, 40.0, 300.0, 260.0, 100.0))?;
|
|
canvas.add_node(Node::new(
|
|
"group",
|
|
NodeKind::Group { label: Some(String::from("Research")), background: None, background_style: None },
|
|
380.0,
|
|
300.0,
|
|
260.0,
|
|
150.0,
|
|
))?;
|
|
let mut edge = Edge::new("edge", "text", "file");
|
|
edge.from_side = Some(Side::Right);
|
|
edge.to_side = Some(Side::Left);
|
|
edge.label = Some(String::from("related"));
|
|
canvas.add_edge(edge)?;
|
|
let snapshot = Snapshot { tokens: Tokens::new(theme), state: CanvasView::default(), canvas, fonts_ready: false };
|
|
let env = CanvasEnv { resolve_file: &|_| None, open_note: &|_| {} };
|
|
let rendered = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
let mut harness =
|
|
egui_kittest::Harness::builder().with_size(egui::vec2(900.0, 620.0)).with_pixels_per_point(1.0).with_max_steps(32).wgpu().build_ui_state(
|
|
|ui, state: &mut Snapshot| {
|
|
if !state.fonts_ready {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
state.fonts_ready = true;
|
|
return;
|
|
}
|
|
let _ = show_canvas(ui, &state.tokens, &mut state.state, &mut state.canvas, &env);
|
|
},
|
|
snapshot,
|
|
);
|
|
harness.run();
|
|
harness.render()
|
|
}))
|
|
.map_err(|_| std::io::Error::other("egui_kittest could not initialize an offscreen GPU adapter"))?
|
|
.map_err(std::io::Error::other)?;
|
|
let image = rendered;
|
|
image.save("target/agent-canvas-view/canvas-view.png")?;
|
|
Ok(())
|
|
}
|