Skip to content

Commit

Permalink
Storage + misc cleanup:
Browse files Browse the repository at this point in the history
- proper handling of window title
- proper handling of storage name
- output default base name based on sketch name
- misc api cleanup
  • Loading branch information
abey79 committed Sep 25, 2023
1 parent 868b61c commit 8a1bca7
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 12 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion vsvg-sketch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ pub fn sketch_derive(input: TokenStream) -> TokenStream {
}

TokenStream::from(quote! {
impl ::vsvg_sketch::SketchApp for #name { }
impl ::vsvg_sketch::SketchApp for #name {
fn name(&self) -> String {
stringify!(#name).to_string()
}
}

impl ::vsvg_sketch::SketchUI for #name {
fn ui(&mut self, ui: &mut egui::Ui) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions vsvg-sketch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ rand_distr.workspace = true
kurbo.workspace = true
anyhow.workspace = true
egui.workspace = true
eframe.workspace = true
web-time.workspace = true
rfd = { version = "0.12.0", default_features = false, features = [
"xdg-portal",
] }
convert_case = "0.6.0"

# for examples
[dev-dependencies]
Expand Down
6 changes: 5 additions & 1 deletion vsvg-sketch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ pub trait SketchUI {
fn ui(&mut self, ui: &mut egui::Ui) -> bool;
}

pub trait SketchApp: App + SketchUI {}
pub trait SketchApp: App + SketchUI {
/// The name of the sketch, used the window title, the default output file name, and persistent
/// settings.
fn name(&self) -> String;
}
16 changes: 14 additions & 2 deletions vsvg-sketch/src/runner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod save_ui;

use crate::Sketch;
use convert_case::Casing;
use rand::SeedableRng;
use vsvg::{PageSize, Unit};

Expand Down Expand Up @@ -63,11 +64,14 @@ pub struct Runner<'a> {
impl Runner<'_> {
/// Create a new [`Runner`] with the provided [`SketchApp`] instance.
pub fn new(app: impl crate::SketchApp + 'static) -> Self {
let mut save_ui = save_ui::SaveUI::default();
save_ui.base_name = app.name().to_case(convert_case::Case::Snake);

Self {
app: Box::new(app),
last_sketch: None,
dirty: true,
save_ui: save_ui::SaveUI::default(),
save_ui,
enable_seed: true,
seed: 0,
enable_time: true,
Expand Down Expand Up @@ -149,7 +153,7 @@ impl Runner<'_> {
impl Runner<'static> {
/// Execute the sketch app.
pub fn run(self) -> anyhow::Result<()> {
vsvg_viewer::show_with_viewer_app(Box::new(self))
vsvg_viewer::show_with_viewer_app(self)
}
}

Expand Down Expand Up @@ -443,4 +447,12 @@ impl vsvg_viewer::ViewerApp for Runner<'_> {

Ok(())
}

fn options(&self, native_option: &mut eframe::NativeOptions) {
native_option.app_id = Some(format!("vsvg.sketch.{}", self.title()));
}

fn title(&self) -> String {
self.app.name()
}
}
2 changes: 1 addition & 1 deletion vsvg-sketch/src/runner/save_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(super) struct SaveUI {
destination_dir: Option<PathBuf>,

/// The output file base name.
base_name: String,
pub(super) base_name: String,

/// The last save result, if any.
///
Expand Down
2 changes: 1 addition & 1 deletion vsvg-viewer/examples/custom_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ impl ViewerApp for SidePanelViewerApp {
}

fn main() -> anyhow::Result<()> {
show_with_viewer_app(Box::new(SidePanelViewerApp::new()))
show_with_viewer_app(SidePanelViewerApp::new())
}
19 changes: 15 additions & 4 deletions vsvg-viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn show(document: &Document) -> anyhow::Result<()> {
let document_data = Arc::new(DocumentData::new(document));

eframe::run_native(
"vsvg",
"vsvg-viewer",
native_options,
Box::new(move |cc| {
let style = egui::Style {
Expand Down Expand Up @@ -65,14 +65,25 @@ pub trait ViewerApp {
) -> anyhow::Result<()> {
Ok(())
}

/// Hook to modify the native options before starting the app.
fn options(&self, _native_option: &mut eframe::NativeOptions) {}

/// Window title
fn title(&self) -> String {
"vsvg ViewerApp".to_owned()
}
}

pub fn show_with_viewer_app(viewer_app: Box<dyn ViewerApp>) -> anyhow::Result<()> {
let native_options = eframe::NativeOptions::default();
pub fn show_with_viewer_app(viewer_app: impl ViewerApp + 'static) -> anyhow::Result<()> {
let viewer_app = Box::new(viewer_app);
let document_data = Arc::new(DocumentData::default());

let mut native_options = eframe::NativeOptions::default();
viewer_app.options(&mut native_options);

eframe::run_native(
"vsvg",
viewer_app.title().as_str(),
native_options,
Box::new(move |cc| {
let style = egui::Style {
Expand Down
6 changes: 4 additions & 2 deletions vsvg-viewer/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::document_widget::DocumentWidget;
use crate::ViewerApp;
use std::sync::Arc;

const VSVG_VIEWER_STORAGE_KEY: &str = "vsvg-viewer-state";

#[derive(serde::Deserialize, serde::Serialize, Default)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
struct ViewerState {
Expand Down Expand Up @@ -40,7 +42,7 @@ impl Viewer {
mut viewer_app: Box<dyn ViewerApp>,
) -> Option<Self> {
let state = if let Some(storage) = cc.storage {
eframe::get_value(storage, "hello").unwrap_or_default()
eframe::get_value(storage, VSVG_VIEWER_STORAGE_KEY).unwrap_or_default()
} else {
ViewerState::default()
};
Expand Down Expand Up @@ -183,6 +185,6 @@ impl eframe::App for Viewer {

/// Called by the framework to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, "hello", &self.state);
eframe::set_value(storage, VSVG_VIEWER_STORAGE_KEY, &self.state);
}
}

0 comments on commit 8a1bca7

Please sign in to comment.