mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Load browser-picked composite fonts into the draft
This commit is contained in:
1 parent
4d3db83100
commit
f60fa22607
2 files changed
+39
-3
No files matched your search
@@ -26,6 +26,7 @@
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::ops::RangeInclusive;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft::behavior::{ColumnKey, ColumnWidth, Selection, SelectionChange, keyed};
|
||||
@@ -892,14 +893,19 @@ fn ask_file(app: &mut VectorcraftApp, request: PresetFileRequest, owner: Arc<()>
|
||||
|
||||
/// Reads the fonts of the file picked and gives them to the dialog (Load… or Replace…).
|
||||
fn load_file(app: &mut VectorcraftApp, request: &PresetFileRequest, replace: bool, picked: &Picked) -> Result<Value, String> {
|
||||
// PLAN: assumed fl.A23 `file_asks::open_params(&Picked) -> Option<Map<String, Value>>`; `None` is a cancelled pick.
|
||||
let Some(mut params) = open_params(picked) else { return Ok(Value::Null) };
|
||||
let browser_name = params.remove("name");
|
||||
let name = picked
|
||||
.first_path()
|
||||
.as_deref()
|
||||
.or_else(|| browser_name.as_ref().and_then(Value::as_str).map(Path::new))
|
||||
.and_then(Path::file_name)
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
params.insert("apply".into(), json!(false));
|
||||
let read = app.run(IMPORT, Value::Object(params))?;
|
||||
let fonts: Vec<CompositeFont> = serde_json::from_value(read["fonts"].clone()).map_err(|e| e.to_string())?;
|
||||
with_dialog(app, |d| d.load(fonts, replace));
|
||||
let name = picked.first_path().and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())).unwrap_or_default();
|
||||
// Only the file's name goes into the status line.
|
||||
if let Some(event) = request.loaded(name, Vec::new()) {
|
||||
report(app, event);
|
||||
}
|
||||
|
||||
@@ -97,6 +97,36 @@ fn delayed_load_and_replace_leave_the_reopened_composite_fonts_draft_alone() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_and_replace_accept_a_browser_picked_file() {
|
||||
let choices: [(PresetCommands, &[&str]); 2] = [(PresetCommands::Load, &["Draft", "From Web"]), (PresetCommands::Replace, &["From Web"])];
|
||||
for (command, expected) in choices {
|
||||
let mut app = app();
|
||||
let picker = scripted(&mut app);
|
||||
let ctx = ctx(&app);
|
||||
open(&mut app).unwrap();
|
||||
let bytes = dialog(&mut app, |d| {
|
||||
d.new_font("From Web".into());
|
||||
file_bytes(&d.fonts).unwrap()
|
||||
});
|
||||
cancel(&mut app);
|
||||
open(&mut app).unwrap();
|
||||
dialog(&mut app, |d| d.new_font("Draft".into()));
|
||||
let dialogs = app.dialogs().unwrap();
|
||||
ctx.run_ui(egui::RawInput::default(), |ui| dialogs.with(|host| host.run(ui))).textures_delta.clear();
|
||||
let step = steps(&app);
|
||||
|
||||
request(&mut app, command, None);
|
||||
picker.answer_contents(&[("picked.vccomposite", &bytes)]);
|
||||
poll(&mut app, &ctx);
|
||||
let draft_names = dialog(&mut app, |d| d.fonts.iter().map(|font| font.name.clone()).collect::<Vec<_>>());
|
||||
assert_eq!(draft_names, expected, "{}", app.ui.status);
|
||||
assert!(app.ui.status.contains("picked.vccomposite"), "{}", app.ui.status);
|
||||
assert!(names(&app).is_empty(), "picking a file edits only the draft");
|
||||
assert_eq!(steps(&app), step);
|
||||
}
|
||||
}
|
||||
|
||||
fn steps(app: &VectorcraftApp) -> usize {
|
||||
app.session.active().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user