From b06a9ca3841e85fe1dd0c14f1f92410c3b0ffaba Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 20 Apr 2022 09:02:25 -0400 Subject: [PATCH] seperate move_cursor_to_end --- src/function.rs | 24 +++--------------------- src/widgets.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/function.rs b/src/function.rs index 739886e..2f50444 100644 --- a/src/function.rs +++ b/src/function.rs @@ -8,16 +8,11 @@ use crate::widgets::{AutoComplete, Movement}; use eframe::{egui, emath, epaint}; use egui::{ plot::{BarChart, PlotUi, Value}, - text::CCursor, - text_edit::CursorRange, widgets::plot::Bar, - Button, Checkbox, Context, Key, Modifiers, TextEdit, + Button, Checkbox, Context, Key, Modifiers, }; use emath::{pos2, vec2}; -use epaint::{ - text::cursor::{Cursor, PCursor, RCursor}, - Color32, -}; +use epaint::Color32; use std::fmt::{self, Debug}; use std::ops::BitXorAssign; @@ -196,20 +191,7 @@ impl FunctionEntry { // Push cursor to end if needed if movement == Movement::Complete { - let mut state = TextEdit::load_state(ui.ctx(), te_id).unwrap(); - state.set_cursor_range(Some(CursorRange::one(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); + crate::widgets::move_cursor_to_end(ui.ctx(), te_id); } } diff --git a/src/widgets.rs b/src/widgets.rs index afe0f9c..4a0859c 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,5 +1,9 @@ use crate::suggestions::{generate_hint, Hint}; +use eframe::{egui, epaint}; +use egui::{text::CCursor, text_edit::CursorRange, TextEdit}; +use epaint::text::cursor::{Cursor, PCursor, RCursor}; + #[derive(PartialEq, Debug)] pub enum Movement { Complete, @@ -78,6 +82,24 @@ impl<'a> AutoComplete<'a> { } } +/// Moves cursor of TextEdit `te_id` to the end +pub fn move_cursor_to_end(ctx: &egui::Context, te_id: egui::Id) { + let mut state = TextEdit::load_state(ctx, te_id).unwrap(); + state.set_cursor_range(Some(CursorRange::one(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(ctx, te_id, state); +} + #[cfg(test)] mod autocomplete_tests { use super::*;