diff --git a/crates/ui-egui/src/dialogs/export_for_screens.rs b/crates/ui-egui/src/dialogs/export_for_screens.rs index decb345a71..884e5848bf 100644 --- a/crates/ui-egui/src/dialogs/export_for_screens.rs +++ b/crates/ui-egui/src/dialogs/export_for_screens.rs @@ -697,7 +697,7 @@ impl ExportForScreensDialog { } }; screen_formats::rows(ui, fields, Some(&mut menu)); - let saved = screen_formats::save_preset_row(ui, fields); + let saved = screen_formats::save_preset_row(ui, fields, &mut false); if let Some(pick) = picked.or(saved) { self.pending = Some(pick); } diff --git a/crates/ui-egui/src/dialogs/screen_formats.rs b/crates/ui-egui/src/dialogs/screen_formats.rs index 221807dade..ea745a5e51 100644 --- a/crates/ui-egui/src/dialogs/screen_formats.rs +++ b/crates/ui-egui/src/dialogs/screen_formats.rs @@ -25,6 +25,7 @@ use craft::ui::menu::Menu; use craft::ui::patterns::form_dialogs::ParamRows; use craft::ui::pickers::{ComboBox, Select}; use craft::ui::runtime::env::Label; +use craft::ui::runtime::focus::FocusRequest; use craft::ui::text::TextField; use serde_json::{Map, Value, json}; use vectorcraft_engine::cmd::fileio::{self, ScreenSize}; @@ -152,8 +153,9 @@ pub(crate) fn preset_menu_of(presets: &[Value], ui: &mut Ui, fields: &mut Map) -> Option { +/// is typed and Save is pressed. Cancel drops `__savePreset`. While `focus` holds, the name field asks for the +/// keyboard focus; `focus` clears once the field has it. +pub(crate) fn save_preset_row(ui: &mut Ui, fields: &mut Map, focus: &mut bool) -> Option { if !fields.contains_key("__savePreset") { return None; } @@ -161,7 +163,14 @@ pub(crate) fn save_preset_row(ui: &mut Ui, fields: &mut Map) -> O let (mut pick, mut cancel) = (None, false); Row::new().show(ui, |ui, _| { Text::new(tl!("Name:")).shade(Shade::Muted).show(ui); - TextField::new(tl!("Name:"), Bind::bound(&mut typed)).id_salt("efs-save-preset").show(ui); + let mut field = TextField::new(tl!("Name:"), Bind::bound(&mut typed)).id_salt("efs-save-preset"); + if *focus { + field = field.request_focus(FocusRequest::Take); + } + let shown = field.show(ui); + if craft::ui::runtime::focus::has(ui.ctx(), shown.response.id) { + *focus = false; + } let name = typed.trim(); if Button::new(tl!("Save")).enabled(!name.is_empty()).show(ui).clicked() { pick = Some(PresetPick::Save(name.to_string())); diff --git a/crates/ui-egui/src/panels/asset_export.rs b/crates/ui-egui/src/panels/asset_export.rs index 26828a967d..af614e819f 100644 --- a/crates/ui-egui/src/panels/asset_export.rs +++ b/crates/ui-egui/src/panels/asset_export.rs @@ -34,8 +34,8 @@ use craft::theme::Variant; use craft::theme::variant::Tone; use craft::ui::buttons::{Button, IconButton}; use craft::ui::contract::{ - Bind, CollectionEvent, Entry, Props as _, RowAlign, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing, TooltipSpec, - clicked, request_rename, + Bind, CollectionEvent, Entry, Lazy, Props as _, RowAlign, Scroll, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing, + TooltipSpec, clicked, request_rename, }; use craft::ui::dialog::StackDialog as _; use craft::ui::display::{Divider, Shade, Text}; @@ -81,6 +81,8 @@ pub(crate) struct AssetExportDoc { pub(crate) applied: Option, /// The name being typed to save a preset (none when not saving). pub(crate) saving: Option, + /// Set by Save As Preset: scroll to the name field (once), and focus it (until the field has been shown). + pub(crate) reveal_save: (bool, bool), /// The tile whose context menu the grid last asked for. pub(crate) menu: Option, } @@ -430,11 +432,15 @@ fn settings(ui: &mut Ui, st: &DocState) -> (Option, usize) { } /// The Save As Preset row while a name is being typed (`saving`): the pick it made, if any. The row -/// keeps the typed name in `__savePreset` and removes it when saved or cancelled. -fn save_row(ui: &mut Ui, saving: &mut Option) -> Option { +/// keeps the typed name in `__savePreset` and removes it when saved or cancelled. On the pass after +/// Save As Preset (`reveal`), the column scrolls to the row and its name field takes the focus. +fn save_row(ui: &mut Ui, saving: &mut Option, reveal: &mut (bool, bool)) -> Option { let name = saving.as_ref()?; let mut fields = Map::from_iter([("__savePreset".to_string(), json!(name))]); - let picked = screen_formats::save_preset_row(ui, &mut fields); + // The scroll is asked for once; the focus request lasts until the field has the focus. + let glide = std::mem::take(&mut reveal.0); + let focus = &mut reveal.1; + let picked = Lazy::new("ae-save-preset", 24.0).reveal(glide).show(ui, |ui| screen_formats::save_preset_row(ui, &mut fields, focus)).flatten(); *saving = fields.get("__savePreset").and_then(Value::as_str).map(str::to_string); picked } @@ -521,14 +527,13 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let shown_sel = model.selection.items.clone(); let all: Vec = st.doc.assets.iter().map(|a| a.id).collect(); let has_art = !st.selection.is_empty(); - let (selection, saving) = (&mut model.selection, &mut model.saving); + let (selection, saving, reveal_save) = (&mut model.selection, &mut model.saving, &mut model.reveal_save); let (mut export_clicked, mut footer, mut screens) = (false, None, false); - // The grid takes the room the settings, Export and the footer bar leave. // The settings scroll in at most half the panel's height, so a long list of format rows stays reachable. // The preset name row stays below them, in view when Save As Preset opens it. let settings_room = ui.available_height() / 2.0; let settings_id = ui.scope_id().with(("asset-export-settings", uid)); - let footed = Stack::new().fill().footer(|ui, _| { + let mut settings_part = |ui: &mut Ui| { Stack::new().id(settings_id).label(tl!("Export Settings")).fill_width().scroll(Scroll::vertical()).room_height(settings_room).show( ui, |ui, _| { @@ -539,7 +544,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { }); }, ); - seen.save = save_row(ui, saving); + seen.save = save_row(ui, saving, reveal_save); Row::new().fill_width().align(RowAlign::End).show(ui, |ui, _| { let label = if shown_sel.is_empty() || shown_sel.len() == all.len() { tl!("Export").to_string() @@ -553,6 +558,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { .show(ui); export_clicked = clicked(&button); }); + }; + let mut bar_part = |ui: &mut Ui| { // The first action sits at the end edge, so Remove comes first and Add stands before it. let remove = FooterAction::new(IconSpec::svg(IconId::new("trash-2")), tl!("Remove Selected Assets")).enabled(!shown_sel.is_empty()); let add = FooterAction::new(IconSpec::svg(IconId::new("plus")), tl!("Add Selected Artwork (Alt: as one asset)")).enabled(has_art); @@ -561,27 +568,49 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { }); screens = leading.is_some_and(|r| clicked(&r)); footer = Some(bar); - }); - let zone = footed.show(ui, |ui, _| { - let drop_art = DropAction::::new(move |p| p.artwork_for(uid, reverts).is_some()); - drop_art.sizing(Sizing::Fill).show(ui, |ui, _| { - if st.doc.assets.is_empty() { - Notice::new(Tone::Neutral, tl!("Drag art here, or select it and click +. Hold Alt for one asset of several objects.")) - .title(tl!("No assets")) - .icon(NoticeIcon::Custom(IconSpec::svg(IconId::new("share-2")))) - .layout(NoticeLayout::Centred) - .show(ui); - } else { + }; + let drop_art = move || DropAction::::new(move |p| p.artwork_for(uid, reverts).is_some()); + let dropped = if st.doc.assets.is_empty() { + // The drop area fills the room above the settings and keeps the notice's content height in a scrolling column. + let footed = Stack::new().fill().footer(|ui, _| bar_part(ui)); + let zone = footed.show(ui, |ui, _| { + Stack::new() + .label(tl!("Asset Export")) + .fill() + .scroll(Scroll::vertical()) + .show(ui, |ui, _| { + let zone = drop_art().sizing(Sizing::Fill).show(ui, |ui, _| { + Notice::new(Tone::Neutral, tl!("Drag art here, or select it and click +. Hold Alt for one asset of several objects.")) + .title(tl!("No assets")) + .icon(NoticeIcon::Custom(IconSpec::svg(IconId::new("share-2")))) + .layout(NoticeLayout::Centred) + .show(ui); + }); + settings_part(ui); + zone + }) + .inner + }); + zone.inner.flatten().and_then(|zone| zone.output.dropped) + } else { + // The grid takes the room the settings, Export and the footer bar leave. + let footed = Stack::new().fill().footer(|ui, _| { + settings_part(ui); + bar_part(ui); + }); + let zone = footed.show(ui, |ui, _| { + drop_art().sizing(Sizing::Fill).show(ui, |ui, _| { let items = Assets::of(st); let grid = TileGrid::::new(tl!("Assets")).id(grid_id).select(SelectMode::Multi).selection(Bind::bound(&mut *selection)).rename(true); let out = grid.show_items(ui, &items, &mut |id, _| tile(st, *id)); opener = Some(out.response.response.clone()); events = out.response.events; - } - }) - }); - if let Some(payload) = zone.inner.and_then(|zone| zone.output.dropped) + }) + }); + zone.inner.and_then(|zone| zone.output.dropped) + }; + if let Some(payload) = dropped && let Some(ids) = payload.artwork_for(uid, reverts) { seen.dropped = Some((ids.iter().map(|id| id.0).collect(), alt)); @@ -912,6 +941,7 @@ pub(crate) fn run(app: &mut VectorcraftApp, id: &str, params: &Value) -> Option< "savePresetAs" => { if let Some(d) = doc_panel(app) { d.saving = Some(String::new()); + d.reveal_save = (true, true); } Ok(()) } diff --git a/crates/ui-egui/src/panels/tests_asset_export.rs b/crates/ui-egui/src/panels/tests_asset_export.rs index b71ca788a9..f0539d5df8 100644 --- a/crates/ui-egui/src/panels/tests_asset_export.rs +++ b/crates/ui-egui/src/panels/tests_asset_export.rs @@ -987,6 +987,90 @@ fn tile_menu_rename_opens_the_clicked_assets_inline_field() { assert_eq!(assets(&mut p.app())[0]["name"], "Renamed"); } +#[test] +fn save_as_preset_scrolls_to_its_name_field_in_a_short_panel() { + let (app, _, _) = app(true); + let mut p = Panel::new(app, art_and_assets); + p.room(egui::vec2(244.0, 280.0)); + p.frame(); + p.frame(); + run(&p, "ui.assetExport.savePresetAs", json!({})); + for _ in 0..60 { + p.frame(); + } + let label = p.painted("Name:"); + assert_eq!(label.len(), 1, "the name row is drawn once"); + let clips = p.painted_clips("Name:"); + assert_eq!(clips.len(), 1, "the name label has one paint clip"); + assert!(clips[0].contains_rect(label[0]), "the name row {:?} is scrolled inside the column's clip {:?}", label[0], clips[0]); + p.type_text("Mine"); + p.frame(); + let (typed, reveal) = crate::panels::asset_export::doc_panel(&mut p.app()).map(|d| (d.saving.clone(), d.reveal_save)).unwrap_or_default(); + assert_eq!(typed.as_deref(), Some("Mine"), "the name field has the focus, so typing goes into it"); + assert_eq!(reveal, (false, false), "the scroll and focus requests end once the field has the focus"); +} + +#[test] +fn the_empty_panel_accepts_artwork_below_the_notice() { + let (app, _, [_, b]) = app(true); + let mut p = Panel::new(app, art_and_assets); + p.room(egui::vec2(244.0, 700.0)); + p.frame(); + p.frame(); + let hint = p.painted("Drag art here, or select it and click +. Hold Alt for one asset of several objects."); + assert_eq!(hint.len(), 1); + let settings = p.painted("Export Settings"); + assert_eq!(settings.len(), 1); + let to = egui::pos2(122.0, settings[0].top() - 40.0); + assert!(to.y > hint[0].bottom() + 40.0, "the drop at {to:?} is well below the notice {:?}", hint[0]); + p.drag_to("Dragged artwork", to, egui::Modifiers::NONE); + let list = assets(&mut p.app()); + assert_eq!(list.len(), 1, "dropping below the notice collects artwork"); + assert_eq!(list[0]["nodes"], json!([b])); +} + +/// With no assets, a panel too short for the empty state and the settings under it draws the whole hint, above the +/// settings, and scrolls; at the floating panel's first-open size and in a narrow panel alike. +#[test] +fn the_empty_state_hint_is_whole_in_a_short_panel() { + const HINT: &str = "Drag art here, or select it and click +. Hold Alt for one asset of several objects."; + for room in [egui::vec2(244.0, 280.0), egui::vec2(214.0, 360.0), egui::vec2(244.0, 600.0)] { + let (app, _, _) = app(true); + let mut p = panel(app); + p.room(room); + p.frame(); + p.frame(); + assert!(p.shows_role("Asset Export", craft::ui::contract::Role::GROUP), "the scroll region is named at {room:?}"); + p.scroll_at(egui::pos2(room.x / 2.0, 60.0), 2000.0); + p.frame(); + let hint = p.painted(HINT); + assert_eq!(hint.len(), 1, "the hint is drawn once at {room:?}: {:?}", p.texts()); + let viewport = p.painted_clips(HINT); + assert_eq!(viewport.len(), 1, "the hint has one paint clip at {room:?}"); + assert!(viewport[0].contains_rect(hint[0]), "the whole hint {:?} is inside the column's clip {:?} at {room:?}", hint[0], viewport[0]); + assert_eq!(p.painted("No assets").len(), 1, "the title is drawn at {room:?}"); + let settings = p.painted("Export Settings"); + assert_eq!(settings.len(), 1, "the settings heading is drawn at {room:?}"); + assert!(hint[0].bottom() <= settings[0].top(), "the hint {:?} ends above the settings {:?} at {room:?}", hint[0], settings[0]); + if room.y < 300.0 { + // The column is taller than the panel: the wheel brings Export into view, and the footer bar stays put. + let bar = p.rect("Export for Screens…"); + assert!(p.rect("Export").bottom() > bar.top(), "Export starts under the footer bar at {room:?}"); + let viewport = p.painted_clips("Export"); + assert_eq!(viewport.len(), 1, "the Export label is drawn once at {room:?}"); + assert!(!viewport[0].contains_rect(p.rect("Export")), "the column's viewport cuts Export at {room:?}"); + p.scroll_at(egui::pos2(room.x / 2.0, 60.0), -2000.0); + p.frame(); + let viewport = p.painted_clips("Export"); + assert!(viewport[0].contains_rect(p.rect("Export")), "the wheel scrolls all of Export into the viewport at {room:?}"); + assert!(p.rect("Export").bottom() <= bar.top(), "Export ends above the footer bar at {room:?}"); + assert!(p.painted("Export Settings")[0].top() < settings[0].top(), "the settings scroll up at {room:?}"); + assert!(p.in_view("Export for Screens…"), "the footer bar stays in view at {room:?}"); + assert_eq!(p.rect("Export for Screens…"), bar, "the footer bar does not scroll at {room:?}"); + } + } +} + /// In a panel narrower than the scale row's fields at their full length, the row's Scale, Suffix, Format and Remove /// share the panel's width, and the Save As Preset row under them stays inside it too. #[test] diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index 0b9fa40f9f..ac0ab0ea24 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -432,6 +432,11 @@ impl Panel { self.texts().into_iter().filter(|(t, _)| t == text).map(|(_, r)| r).collect() } + /// The clip rects the latest frame painted `text` under, one for each whole run. + pub(crate) fn painted_clips(&self, text: &str) -> Vec { + self.fixture.texts().iter().filter(|t| t.text == text).map(|t| t.clip).collect() + } + /// Where the control of `role` named `name` is, where nodes of other roles share the name. pub(crate) fn rect_role(&self, name: &str, role: craft::ui::contract::Role) -> Rect { self.look_role(name, role).rect