Use a RangeInclusive for text selection.

This commit is contained in:
Nolan Darilek 2021-05-19 10:47:18 -05:00
parent 875d3a650d
commit 68499cebf6
2 changed files with 14 additions and 26 deletions

View file

@ -249,10 +249,8 @@ pub struct WidgetInfo {
pub selected: Option<bool>, pub selected: Option<bool>,
/// The current value of sliders etc. /// The current value of sliders etc.
pub value: Option<f64>, pub value: Option<f64>,
// Location of primary cursor. // Selected range of characters in [`Self::current_text_value`].
pub primary_cursor: Option<usize>, pub text_selection: Option<std::ops::RangeInclusive<usize>>,
// Location of secondary cursor.
pub secondary_cursor: Option<usize>,
} }
impl std::fmt::Debug for WidgetInfo { impl std::fmt::Debug for WidgetInfo {
@ -265,8 +263,7 @@ impl std::fmt::Debug for WidgetInfo {
prev_text_value, prev_text_value,
selected, selected,
value, value,
primary_cursor, text_selection,
secondary_cursor,
} = self; } = self;
let mut s = f.debug_struct("WidgetInfo"); let mut s = f.debug_struct("WidgetInfo");
@ -289,11 +286,8 @@ impl std::fmt::Debug for WidgetInfo {
if let Some(value) = value { if let Some(value) = value {
s.field("value", value); s.field("value", value);
} }
if let Some(primary_cursor) = primary_cursor { if let Some(text_selection) = text_selection {
s.field("primary_cursor", primary_cursor); s.field("text_selection", text_selection);
}
if let Some(secondary_cursor) = secondary_cursor {
s.field("secondary_cursor", secondary_cursor);
} }
s.finish() s.finish()
@ -310,8 +304,7 @@ impl WidgetInfo {
prev_text_value: None, prev_text_value: None,
selected: None, selected: None,
value: None, value: None,
primary_cursor: None, text_selection: None,
secondary_cursor: None,
} }
} }
@ -361,14 +354,12 @@ impl WidgetInfo {
#[allow(clippy::needless_pass_by_value)] #[allow(clippy::needless_pass_by_value)]
pub fn text_selection_changed( pub fn text_selection_changed(
primary_cursor: usize, text_selection: std::ops::RangeInclusive<usize>,
secondary_cursor: usize, current_text_value: impl ToString,
text_value: impl ToString,
) -> Self { ) -> Self {
Self { Self {
primary_cursor: Some(primary_cursor), text_selection: Some(text_selection),
secondary_cursor: Some(secondary_cursor), current_text_value: Some(current_text_value.to_string()),
current_text_value: Some(text_value.to_string()),
..Self::new(WidgetType::TextEdit) ..Self::new(WidgetType::TextEdit)
} }
} }
@ -383,8 +374,7 @@ impl WidgetInfo {
prev_text_value: _, prev_text_value: _,
selected, selected,
value, value,
primary_cursor: _, text_selection: _,
secondary_cursor: _,
} = self; } = self;
// TODO: localization // TODO: localization

View file

@ -663,11 +663,9 @@ impl<'t> TextEdit<'t> {
if response.changed { if response.changed {
response.widget_info(|| WidgetInfo::text_edit(&*text, &*prev_text)); response.widget_info(|| WidgetInfo::text_edit(&*text, &*prev_text));
} else if let Some(text_cursor) = text_cursor { } else if let Some(text_cursor) = text_cursor {
let info = WidgetInfo::text_selection_changed( let char_range =
text_cursor.primary.ccursor.index, text_cursor.primary.ccursor.index..=text_cursor.secondary.ccursor.index;
text_cursor.secondary.ccursor.index, let info = WidgetInfo::text_selection_changed(char_range, &*text);
&*text,
);
response response
.ctx .ctx
.output() .output()