Skip to content

Commit

Permalink
Various fixes.
Browse files Browse the repository at this point in the history
* Only dispatch `SelectionChanged` if the selection actually changes.
* Fix missing focus events.
  • Loading branch information
ndarilek committed May 20, 2021
1 parent 1fef49b commit 6bf558d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion egui/src/widgets/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ impl<'t> TextEdit<'t> {
}

let mut text_cursor = None;
let prev_text_cursor = state.cursorp;
if ui.memory().has_focus(id) && enabled {
ui.memory().lock_focus(id, lock_focus);

Expand Down Expand Up @@ -660,9 +661,19 @@ impl<'t> TextEdit<'t> {

ui.memory().id_data.insert(id, state);

let selection_changed = if let (Some(text_cursor), Some(prev_text_cursor)) =
(text_cursor, prev_text_cursor)
{
text_cursor.primary.ccursor.index != prev_text_cursor.primary.ccursor.index
|| text_cursor.secondary.ccursor.index != prev_text_cursor.secondary.ccursor.index
} else {
false
};

if response.changed {
response.widget_info(|| WidgetInfo::text_edit(&*prev_text, &*text));
} else if let Some(text_cursor) = text_cursor {
} else if selection_changed {
let text_cursor = text_cursor.unwrap();
let char_range =
text_cursor.primary.ccursor.index..=text_cursor.secondary.ccursor.index;
let info = WidgetInfo::text_selection_changed(char_range, &*text);
Expand All @@ -671,6 +682,8 @@ impl<'t> TextEdit<'t> {
.output()
.events
.push(OutputEvent::TextSelectionChanged(info));
} else {
response.widget_info(|| WidgetInfo::text_edit(&*prev_text, &*text));
}
response
}
Expand Down

0 comments on commit 6bf558d

Please sign in to comment.