From 93eb1295065c794e7de00a37be43cbc8abde3477 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 21:25:39 +0700 Subject: [PATCH] =?UTF-8?q?Composite=20Fonts'=20Save=E2=80=A6=20saves=20th?= =?UTF-8?q?rough=20the=20file=20service=20(Files::save),=20which=20writes?= =?UTF-8?q?=20the=20file=20on=20the=20desktop=20and=20opens=20the=20browse?= =?UTF-8?q?r's=20save=20picker=20or=20downloads=20it=20on=20the=20web?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/dialogs/composite_fonts.rs | 28 +++++++++---------- .../src/dialogs/composite_fonts/tests.rs | 5 +++- crates/ui-egui/src/file_asks.rs | 27 ++++++++++++++++-- crates/ui-egui/src/tests/picks.rs | 4 +++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/crates/ui-egui/src/dialogs/composite_fonts.rs b/crates/ui-egui/src/dialogs/composite_fonts.rs index aefe389997..a8f096adb5 100644 --- a/crates/ui-egui/src/dialogs/composite_fonts.rs +++ b/crates/ui-egui/src/dialogs/composite_fonts.rs @@ -60,7 +60,7 @@ use vectorcraft_engine::cmd::compositefonts::{EXTS, file_bytes}; use vectorcraft_text::FontDb; use crate::VectorcraftApp; -use crate::file_asks::{Owner, ask, open_params}; +use crate::file_asks::{Owner, ask, open_params, save}; use crate::font_menu::{PickerHold, family_picker, family_sets}; mod sample; @@ -871,7 +871,7 @@ fn ask_file(app: &mut VectorcraftApp, request: PresetFileRequest) { if let Some(ext) = exts.first() { pick = pick.suggested_extension(*ext); } - ask(app, pick, Owner::Free, move |app, picked| save_file(app, &request, &bytes, picked)); + save(app, pick, bytes, Owner::Free, move |app, picked| saved(app, &request, picked)); } _ => {} } @@ -893,20 +893,18 @@ fn load_file(app: &mut VectorcraftApp, request: &PresetFileRequest, replace: boo Ok(Value::Null) } -/// Writes `bytes` to the path picked (Save…); the file service has already asked about replacing a file. -fn save_file(app: &mut VectorcraftApp, request: &PresetFileRequest, bytes: &[u8], picked: Picked) -> Result { - let Picked::Saved { path, .. } = picked else { return Ok(Value::Null) }; - let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); - let event = match vectorcraft_format::write_atomic(&path, bytes) { - Ok(()) => request.saved(name), - Err(e) => { - let failure = match e.kind() { - std::io::ErrorKind::NotFound => PresetFileFailure::NotFound, - std::io::ErrorKind::PermissionDenied => PresetFileFailure::Denied, - _ => PresetFileFailure::Other, - }; - Some(request.failed(name, failure)) +/// Reports the save Save… asked for: the file service wrote it where the user picked, asking first about +/// replacing a file, or the browser took it (its save picker or a download). +fn saved(app: &mut VectorcraftApp, request: &PresetFileRequest, picked: Picked) -> Result { + let event = match picked { + Picked::Saved { path, .. } => request.saved(path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()), + Picked::Downloaded(name) => request.saved(name), + // The service's message is the system's; the words give the reason. + Picked::Failed(_) => { + let PresetFileRequest::Save { file_name, .. } = request else { return Ok(Value::Null) }; + Some(request.failed(file_name.clone(), PresetFileFailure::Other)) } + _ => return Ok(Value::Null), }; if let Some(event) = event { report(app, event); diff --git a/crates/ui-egui/src/dialogs/composite_fonts/tests.rs b/crates/ui-egui/src/dialogs/composite_fonts/tests.rs index 8d562baca6..431a1508cb 100644 --- a/crates/ui-egui/src/dialogs/composite_fonts/tests.rs +++ b/crates/ui-egui/src/dialogs/composite_fonts/tests.rs @@ -179,7 +179,10 @@ fn composite_fonts_go_through_files() { request(&mut app, PresetCommands::Save, Some("UI Composite IO")); picker.answer_saved(&file, None); poll(&mut app, &ctx); - assert!(std::fs::read_to_string(&file).unwrap().contains("UI Composite IO")); + let saved = picker.file(&file).unwrap(); + assert!(String::from_utf8_lossy(&saved).contains("UI Composite IO")); + // The file service holds what it saved; Load… reads the file from disk. + std::fs::write(&file, &saved).unwrap(); // Load: the same name is replaced, not added. dialog(&mut app, |d| d.fonts[0].parts.roman.baseline = 0.0); request(&mut app, PresetCommands::Load, None); diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index a9f3bfa555..2b249d1c27 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use craft::dialogs::ui::UnavailableWords; -use craft::host::{FileFilter, Host, PickId, PickKind, PickRequest, Picked}; +use craft::host::{FileFilter, Files, Host, PickId, PickKind, PickRequest, Picked}; use craft::ui::dialog::{DialogId, DialogResult, PromptChoice, StandardPrompt}; // PLAN: assumed re-export paths for `HostedParamDialog`, `PathRequest` and `Env`. use craft::ui::patterns::form_dialogs::HostedParamDialog; @@ -103,9 +103,32 @@ pub(crate) fn ask( request: PickRequest, owner: Owner, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, +) -> Value { + start(app, owner, |files| files.pick(request), then) +} + +/// Save `bytes` as the file service saves them (`Files::save`: the desktop asks where with `request` and +/// writes the file, the browser opens its save picker or downloads them) and run `then` with the answer. +/// Returns as [`ask`] does. +pub(crate) fn save( + app: &mut VectorcraftApp, + request: PickRequest, + bytes: Vec, + owner: Owner, + then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, +) -> Value { + start(app, owner, |files| files.save(request, bytes), then) +} + +/// Asks the file service with `ask` and files `then` for its answer. +fn start( + app: &mut VectorcraftApp, + owner: Owner, + ask: impl FnOnce(&dyn Files) -> PickId, + then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { let Some(host) = app.ui_ctx.as_ref().and_then(Host::try_of_ctx) else { return Value::Null }; - let ticket = host.files().pick(request); + let ticket = ask(host.files()); let doc = match owner { Owner::Document => app.session.active().map(|d| d.uid), Owner::Free => None, diff --git a/crates/ui-egui/src/tests/picks.rs b/crates/ui-egui/src/tests/picks.rs index bbdcae2e0a..dc47460f3b 100644 --- a/crates/ui-egui/src/tests/picks.rs +++ b/crates/ui-egui/src/tests/picks.rs @@ -44,6 +44,10 @@ impl Picker { pub(crate) fn cancel(&self) { self.files.answer(Picked::Cancelled); } + /// The bytes a save through the service (`Files::save`) wrote to `path`. + pub(crate) fn file(&self, path: &str) -> Option> { + self.files.file(std::path::Path::new(path)) + } /// The paths files were written to. pub(crate) fn written(&self) -> Vec { self.written.borrow().clone()