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 fae58f693d..7020161e7f 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 b83fc8d530..94889a7ac9 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,14 @@ 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); let mut settings_part = |ui: &mut Ui| { crate::panels::section(tl!("Export Settings")).show(ui, |ui, _| { let (change, files_each) = settings(ui, st); (seen.change, seen.files_each) = (change, files_each); seen.format_3d = format_3d(ui, st, &sel_or_all(&shown_sel, &all)); - 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() { @@ -926,6 +932,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 2feb352aef..8b139b928d 100644 --- a/crates/ui-egui/src/panels/tests_asset_export.rs +++ b/crates/ui-egui/src/panels/tests_asset_export.rs @@ -987,6 +987,29 @@ 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);