Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/kas-core/src/core/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub enum Role<'a> {
/// Whether the text input supports multi-line text
multi_line: bool,
/// The cursor index and selection range
cursor: CursorRange,
cursor: CursorRange<usize>,
},
/// A gripable handle
///
Expand Down
8 changes: 4 additions & 4 deletions crates/kas-core/src/event/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ impl TextInput {
/// otherwise it is expanded in word mode.
pub fn expand_range(
text: &str,
mut range: CursorRange,
mut range: CursorRange<usize>,
line_range: Option<&dyn Fn(usize) -> Option<std::ops::Range<usize>>>,
) -> CursorRange {
) -> CursorRange<usize> {
let index = range.cursor;
if range.cursor < range.anchor {
range.reverse();
Expand Down Expand Up @@ -652,11 +652,11 @@ impl TextInput {
/// Utility function to adjust an already-expanded range in word or line mode
pub fn adjust_range(
text: &str,
mut range: CursorRange,
mut range: CursorRange<usize>,
index: usize,
repeats: u32,
line_range: Option<&dyn Fn(usize) -> Option<std::ops::Range<usize>>>,
) -> CursorRange {
) -> CursorRange<usize> {
if range.anchor < range.cursor && index <= range.anchor
|| range.anchor > range.cursor && index >= range.anchor
{
Expand Down
38 changes: 11 additions & 27 deletions crates/kas-core/src/text/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ use std::ops::Range;

/// Cursor index and selection range
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CursorRange {
pub struct CursorRange<Index> {
/// The start or end of the selection.
pub anchor: usize,
pub anchor: Index,
/// The cursor (edit) index.
pub cursor: usize,
pub cursor: Index,
}

impl From<usize> for CursorRange {
impl<Index: Copy> From<Index> for CursorRange<Index> {
#[inline]
fn from(index: usize) -> Self {
fn from(index: Index) -> Self {
CursorRange {
anchor: index,
cursor: index,
}
}
}

impl From<Range<usize>> for CursorRange {
impl<Index> From<Range<Index>> for CursorRange<Index> {
#[inline]
fn from(range: Range<usize>) -> Self {
fn from(range: Range<Index>) -> Self {
CursorRange {
anchor: range.start,
cursor: range.end,
}
}
}

impl CursorRange {
impl<Index: Copy + Eq + Ord> CursorRange<Index> {
/// True if the selection index equals the cursor index
#[inline]
pub fn is_empty(&self) -> bool {
Expand All @@ -46,7 +46,7 @@ impl CursorRange {
/// Convert to a [`Range`], increasing
///
/// The return value has `range.start <= range.end`.
pub fn to_range(&self) -> Range<usize> {
pub fn to_range(&self) -> Range<Index> {
let mut range = *self;
if range.anchor > range.cursor {
range.reverse();
Expand Down Expand Up @@ -75,7 +75,7 @@ impl CursorRange {
///
/// Both indices are set to `index`.
#[inline]
pub fn set_position(&mut self, index: usize) {
pub fn set_position(&mut self, index: Index) {
self.anchor = index;
self.cursor = index;
}
Expand All @@ -85,24 +85,8 @@ impl CursorRange {
/// Call this method if the string changes under the selection to ensure
/// that the selection does not exceed the length of the new string.
#[inline]
pub fn set_max_len(&mut self, len: usize) {
pub fn set_max_len(&mut self, len: Index) {
self.cursor = self.cursor.min(len);
self.anchor = self.anchor.min(len);
}

/// Adjust all indices for a deletion from the source text
pub fn delete_range(&mut self, range: Range<usize>) {
let len = range.len();
let adjust = |index: usize| -> usize {
if index >= range.end {
index - len
} else if index > range.start {
range.start
} else {
index
}
};
self.cursor = adjust(self.cursor);
self.anchor = adjust(self.anchor);
}
}
2 changes: 0 additions & 2 deletions crates/kas-widgets/src/edit/edit_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,10 @@ mod EditBoxCore {

if let Some(SetValueText(string)) = cx.try_pop() {
self.edit(cx, data, |edit, cx| {
edit.pre_commit();
edit.set_string(cx, string);
});
} else if let Some(ReplaceSelectedText(text)) = cx.try_pop() {
self.edit(cx, data, |edit, cx| {
edit.pre_commit();
edit.replace_selected_text(cx, &text);
});
}
Expand Down
Loading
Loading