From dc577c03c90d9e6e2675ceae2d9857ad60c56eaa Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 30 Mar 2022 15:18:30 -0400 Subject: [PATCH] move cursor to end when completion accepted --- src/egui_app.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/egui_app.rs b/src/egui_app.rs index 800902f..f8cf263 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -4,10 +4,12 @@ use crate::misc::{dyn_mut_iter, option_vec_printer, JsonFileOutput, SerdeValueHe use crate::parsing::{process_func_str, test_func}; use crate::suggestions::generate_hint; use eframe::{egui, epi}; +use egui::epaint::text::cursor::{PCursor, RCursor}; use egui::plot::Plot; use egui::{ - Button, CentralPanel, Color32, ComboBox, Context, FontData, FontDefinitions, FontFamily, Key, - RichText, SidePanel, Slider, TextEdit, TopBottomPanel, Vec2, Visuals, Widget, Window, + text::CCursor, text_edit::CursorRange, Button, CentralPanel, Color32, ComboBox, Context, + FontData, FontDefinitions, FontFamily, Key, RichText, SidePanel, Slider, TextEdit, + TopBottomPanel, Vec2, Visuals, Widget, Window, }; use epi::Frame; use instant::Duration; @@ -503,9 +505,12 @@ impl MathApp { // Contains the function string in a text box that the user can edit let hint = generate_hint(&self.func_strs[i]).unwrap_or_default(); + let te_id = ui.make_persistent_id("text_edit_ac".to_string()); + let func_edit_focus = TextEdit::singleline(&mut self.func_strs[i]) .hint_text(&hint) .hint_forward(true) + .id(te_id) .ui(ui) .has_focus(); @@ -515,6 +520,22 @@ impl MathApp { self.text_boxes_focused = true; if ui.input().key_down(Key::ArrowRight) { self.func_strs[i] += &hint; + let mut state = TextEdit::load_state(ui.ctx(), te_id).unwrap(); + state.set_cursor_range(Some(CursorRange::one( + egui::epaint::text::cursor::Cursor { + ccursor: CCursor { + index: 0, + prefer_next_row: false, + }, + rcursor: RCursor { row: 0, column: 0 }, + pcursor: PCursor { + paragraph: 0, + offset: 10000, + prefer_next_row: false, + }, + }, + ))); + TextEdit::store_state(ui.ctx(), te_id, state); } } });