Skip to content

How do I detect if a TextEdit's cursor position was changed #4540

Answered by YgorSouza
doonv asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think there is an API that tells you directly if the cursor changed, but you can get the current value and then compare to the previous value that you stored somewhere.

impl eframe::App for MyApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            let id = "prev-cursor".into();
            let previous_range = ui.data(|data| data.get_temp(id));
            let range = egui::TextEdit::singleline(&mut self.name)
                .show(ui)
                .cursor_range;
            ui.strong(format!(
                "Changed: {}",
                previous_range.unwrap_or(range) != range
      …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@doonv
Comment options

Answer selected by doonv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants