diff --git a/src/app.rs b/src/app.rs index 8b32c474..39dc8e01 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3550,19 +3550,8 @@ impl App { ui.set_min_width(360.0); let r = ui.add(egui::TextEdit::singleline(&mut p.text).desired_width(f32::INFINITY)).named(title); if p.focus { - r.request_focus(); - - if matches!(p.cmd, "layer.rename" | "scene.rename") - && let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), r.id) - { - let start = egui::text::CCursor::new(0); - let end = egui::text::CCursor::new(p.text.chars().count()); - - state.cursor.set_char_range(Some(egui::text::CCursorRange::two(start, end))); - - state.store(ui.ctx(), r.id); - } - + // What the prompt offers (a name to change) is selected: typing replaces it. + crate::theme::focus_selected(ui, &r, &p.text); p.focus = false; } if r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) { diff --git a/src/convert_symbol.rs b/src/convert_symbol.rs index 41455569..6f92096f 100644 --- a/src/convert_symbol.rs +++ b/src/convert_symbol.rs @@ -7,7 +7,6 @@ use crate::app::App; use crate::theme::{FORM_LABEL_DOCK, Type, form_row, text}; use anicraft_engine::doc::{Document, FolderId, LibraryEntry, SymbolKind}; use egui::Key; -use egui::text::{CCursor, CCursorRange}; use serde_json::json; /// The dialog while it is open. @@ -75,13 +74,7 @@ pub fn window(app: &mut App, ctx: &egui::Context) { form_row(ui, FORM_LABEL_DOCK, tr!("Name"), |ui| { let r = ui.add(egui::TextEdit::singleline(&mut d.name).desired_width(f32::INFINITY)).named(tr!("Symbol name")); if d.focus { - r.request_focus(); - if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), r.id) { - let start = CCursor::new(0); - let end = CCursor::new(d.name.chars().count()); - state.cursor.set_char_range(Some(CCursorRange::two(start, end))); - state.store(ui.ctx(), r.id); - } + crate::theme::focus_selected(ui, &r, &d.name); d.focus = false; } if r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) { diff --git a/src/layer_props.rs b/src/layer_props.rs index 2b3e2502..69be725b 100644 --- a/src/layer_props.rs +++ b/src/layer_props.rs @@ -103,17 +103,7 @@ pub fn window(app: &mut App, ctx: &egui::Context) { form_row(ui, FORM_LABEL_DIALOG, tr!("Name"), |ui| { let r = ui.add(egui::TextEdit::singleline(&mut d.name).desired_width(f32::INFINITY)).named(tr!("Layer name")); if d.focus { - r.request_focus(); - - if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), r.id) { - let start = egui::text::CCursor::new(0); - let end = egui::text::CCursor::new(d.name.chars().count()); - - state.cursor.set_char_range(Some(egui::text::CCursorRange::two(start, end))); - - state.store(ui.ctx(), r.id); - } - + crate::theme::focus_selected(ui, &r, &d.name); d.focus = false; } if r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) { @@ -285,7 +275,7 @@ mod tests { assert!(app.layer_props.is_some()); } - #[test] + #[test] fn opening_layer_properties_selects_entire_name() { let ctx = egui::Context::default(); @@ -330,4 +320,4 @@ mod tests { assert_eq!(a.min(b), egui::text::CharIndex(0)); assert_eq!(a.max(b), egui::text::CharIndex(original.chars().count())); } -} \ No newline at end of file +} diff --git a/src/theme.rs b/src/theme.rs index 24737eb9..c7bef118 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -842,6 +842,18 @@ pub fn heat(w: f32) -> Color32 { Color32::from_rgb(mix(a.r(), b.r()), mix(a.g(), b.g()), mix(a.b(), b.b())) } +/// Gives a text field the keyboard with all of `text` selected, so typing replaces what a +/// dialog offers (a name to change, a default name) and the arrow keys still edit it +/// (#196, #322). +pub fn focus_selected(ui: &egui::Ui, field: &egui::Response, text: &str) { + field.request_focus(); + if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), field.id) { + let all = egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(text.chars().count())); + state.cursor.set_char_range(Some(all)); + state.store(ui.ctx(), field.id); + } +} + /// Construct an RGB colour derived from artwork rather than a UI theme token. /// UI control colours must still come from T. pub(crate) fn artwork_rgb(r: u8, g: u8, b: u8) -> Color32 {