From 68499cebf65f7f4ae7573d509f8d4134b07da250 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 19 May 2021 10:47:18 -0500 Subject: [PATCH] Use a `RangeInclusive` for text selection. --- egui/src/data/output.rs | 32 +++++++++++--------------------- egui/src/widgets/text_edit.rs | 8 +++----- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/egui/src/data/output.rs b/egui/src/data/output.rs index 7bbf5a76..47b5e372 100644 --- a/egui/src/data/output.rs +++ b/egui/src/data/output.rs @@ -249,10 +249,8 @@ pub struct WidgetInfo { pub selected: Option, /// The current value of sliders etc. pub value: Option, - // Location of primary cursor. - pub primary_cursor: Option, - // Location of secondary cursor. - pub secondary_cursor: Option, + // Selected range of characters in [`Self::current_text_value`]. + pub text_selection: Option>, } impl std::fmt::Debug for WidgetInfo { @@ -265,8 +263,7 @@ impl std::fmt::Debug for WidgetInfo { prev_text_value, selected, value, - primary_cursor, - secondary_cursor, + text_selection, } = self; let mut s = f.debug_struct("WidgetInfo"); @@ -289,11 +286,8 @@ impl std::fmt::Debug for WidgetInfo { if let Some(value) = value { s.field("value", value); } - if let Some(primary_cursor) = primary_cursor { - s.field("primary_cursor", primary_cursor); - } - if let Some(secondary_cursor) = secondary_cursor { - s.field("secondary_cursor", secondary_cursor); + if let Some(text_selection) = text_selection { + s.field("text_selection", text_selection); } s.finish() @@ -310,8 +304,7 @@ impl WidgetInfo { prev_text_value: None, selected: None, value: None, - primary_cursor: None, - secondary_cursor: None, + text_selection: None, } } @@ -361,14 +354,12 @@ impl WidgetInfo { #[allow(clippy::needless_pass_by_value)] pub fn text_selection_changed( - primary_cursor: usize, - secondary_cursor: usize, - text_value: impl ToString, + text_selection: std::ops::RangeInclusive, + current_text_value: impl ToString, ) -> Self { Self { - primary_cursor: Some(primary_cursor), - secondary_cursor: Some(secondary_cursor), - current_text_value: Some(text_value.to_string()), + text_selection: Some(text_selection), + current_text_value: Some(current_text_value.to_string()), ..Self::new(WidgetType::TextEdit) } } @@ -383,8 +374,7 @@ impl WidgetInfo { prev_text_value: _, selected, value, - primary_cursor: _, - secondary_cursor: _, + text_selection: _, } = self; // TODO: localization diff --git a/egui/src/widgets/text_edit.rs b/egui/src/widgets/text_edit.rs index d1824f27..15299f8b 100644 --- a/egui/src/widgets/text_edit.rs +++ b/egui/src/widgets/text_edit.rs @@ -663,11 +663,9 @@ impl<'t> TextEdit<'t> { if response.changed { response.widget_info(|| WidgetInfo::text_edit(&*text, &*prev_text)); } else if let Some(text_cursor) = text_cursor { - let info = WidgetInfo::text_selection_changed( - text_cursor.primary.ccursor.index, - text_cursor.secondary.ccursor.index, - &*text, - ); + let char_range = + text_cursor.primary.ccursor.index..=text_cursor.secondary.ccursor.index; + let info = WidgetInfo::text_selection_changed(char_range, &*text); response .ctx .output()