Browse Source

Add example of how to move text cursor in a TextEdit

pull/1392/head
Emil Ernerfeldt 3 years ago
parent
commit
ccbddcfe95
  1. 5
      egui/src/lib.rs
  2. 25
      egui_demo_lib/src/apps/demo/text_edit.rs

5
egui/src/lib.rs

@ -391,9 +391,10 @@ pub use epaint::{
};
pub mod text {
pub use crate::text_edit::CCursorRange;
pub use epaint::text::{
FontData, FontDefinitions, FontFamily, Fonts, Galley, LayoutJob, LayoutSection, TextFormat,
TAB_SIZE,
cursor::CCursor, FontData, FontDefinitions, FontFamily, Fonts, Galley, LayoutJob,
LayoutSection, TextFormat, TAB_SIZE,
};
}

25
egui_demo_lib/src/apps/demo/text_edit.rs

@ -64,6 +64,7 @@ impl super::View for TextEdit {
anything_selected,
egui::Label::new("Press ctrl+T to toggle the case of selected text (cmd+T on Mac)"),
);
if ui
.input_mut()
.consume_key(egui::Modifiers::COMMAND, egui::Key::T)
@ -82,5 +83,29 @@ impl super::View for TextEdit {
text.insert_text(&new_text, selected_chars.start);
}
}
ui.horizontal(|ui| {
ui.label("Move cursor to the:");
if ui.button("start").clicked() {
let text_edit_id = output.response.id;
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
let ccursor = egui::text::CCursor::new(0);
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
state.store(ui.ctx(), text_edit_id);
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the `TextEdit`.
}
}
if ui.button("end").clicked() {
let text_edit_id = output.response.id;
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
let ccursor = egui::text::CCursor::new(text.chars().count());
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
state.store(ui.ctx(), text_edit_id);
ui.ctx().memory().request_focus(text_edit_id); // give focus back to the `TextEdit`.
}
}
});
}
}

Loading…
Cancel
Save