#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::let_underscore_untyped)]
mod document_widget;
mod engine;
mod frame_history;
mod painters;
mod render_data;
pub mod viewer;
#[cfg(target_arch = "wasm32")]
pub mod web_handle;
use eframe::CreationContext;
use std::sync::Arc;
pub use viewer::Viewer;
pub use crate::document_widget::DocumentWidget;
pub mod exports {
pub use ::eframe;
pub use ::egui;
pub use ::wgpu;
}
struct ShowViewerApp {
document: Arc<vsvg::Document>,
tolerance: f64,
}
impl ViewerApp for ShowViewerApp {
fn setup(
&mut self,
_cc: &CreationContext,
document_widget: &mut DocumentWidget,
) -> anyhow::Result<()> {
document_widget.set_tolerance(self.tolerance);
document_widget.set_document(self.document.clone());
Ok(())
}
}
const DEFAULT_RENDERER_TOLERANCE: f64 = 0.01;
#[cfg(not(target_arch = "wasm32"))]
pub fn show(document: Arc<vsvg::Document>) -> anyhow::Result<()> {
show_tolerance(document, DEFAULT_RENDERER_TOLERANCE)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn show_tolerance(document: Arc<vsvg::Document>, tolerance: f64) -> anyhow::Result<()> {
let native_options = eframe::NativeOptions::default();
eframe::run_native(
"vsvg-viewer",
native_options,
Box::new(move |cc| {
let style = egui::Style {
visuals: egui::Visuals::light(),
..egui::Style::default()
};
cc.egui_ctx.set_style(style);
Box::new(
Viewer::new(
cc,
Box::new(ShowViewerApp {
document: document.clone(),
tolerance,
}),
)
.expect("viewer requires wgpu backend"),
)
}),
)?;
Ok(())
}
pub trait ViewerApp {
fn setup(
&mut self,
_cc: &eframe::CreationContext,
_document_widget: &mut DocumentWidget,
) -> anyhow::Result<()> {
Ok(())
}
fn handle_input(&mut self, _ctx: &egui::Context, _document_widget: &mut DocumentWidget) {}
fn show_panels(
&mut self,
_ctx: &egui::Context,
_document_widget: &mut DocumentWidget,
) -> anyhow::Result<()> {
Ok(())
}
fn show_central_panel(
&mut self,
_ui: &mut egui::Ui,
_document_widget: &mut DocumentWidget,
) -> anyhow::Result<()> {
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
fn native_options(&self) -> eframe::NativeOptions {
eframe::NativeOptions::default()
}
fn title(&self) -> String {
"vsvg ViewerApp".to_owned()
}
fn load(&mut self, _storage: &dyn eframe::Storage) {}
fn save(&self, _storage: &mut dyn eframe::Storage) {}
fn on_exit(&mut self) {}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn show_with_viewer_app(viewer_app: impl ViewerApp + 'static) -> anyhow::Result<()> {
vsvg::trace_function!();
let viewer_app = Box::new(viewer_app);
let native_options = viewer_app.native_options();
eframe::run_native(
viewer_app.title().as_str(),
native_options,
Box::new(move |cc| {
let style = egui::Style {
visuals: egui::Visuals::light(),
..egui::Style::default()
};
cc.egui_ctx.set_style(style);
Box::new(Viewer::new(cc, viewer_app).expect("viewer requires wgpu backend"))
}),
)?;
Ok(())
}