Composite Fonts' Save… saves through the file service (Files::save), which writes the file on the desktop and opens the browser's save picker or downloads it on the web

This commit is contained in:
bflatastic
2026-11-23 21:25:39 +07:00
parent bc0cd31082
commit 93eb129506
4 changed files with 46 additions and 18 deletions
+13 -15
View File
@@ -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<Value, String> {
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<Value, String> {
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);
@@ -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);
+25 -2
View File
@@ -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<Value, String> + '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<u8>,
owner: Owner,
then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result<Value, String> + '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<Value, String> + '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,
+4
View File
@@ -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<Vec<u8>> {
self.files.file(std::path::Path::new(path))
}
/// The paths files were written to.
pub(crate) fn written(&self) -> Vec<String> {
self.written.borrow().clone()