word selection

This commit is contained in:
2025-11-22 15:33:28 -05:00
parent fc89826794
commit c24c517c60
3 changed files with 54 additions and 20 deletions

View File

@@ -163,10 +163,7 @@ impl<'a> TextEditCtx<'a> {
pub fn motion(&mut self, motion: Motion) {
if let TextSelection::Pos(cursor) = self.text.selection
&& let Some((cursor, _)) =
self.text
.buf
.cursor_motion(self.font_system, cursor, None, motion)
&& let Some(cursor) = self.buf_motion(cursor, motion)
{
self.text.selection = TextSelection::Pos(cursor);
} else if let TextSelection::Span { start, end } = self.text.selection {
@@ -275,11 +272,7 @@ impl<'a> TextEditCtx<'a> {
{
if word {
let start = *cursor;
if let Some((end, _)) =
self.text
.buf
.cursor_motion(self.font_system, start, None, Motion::RightWord)
{
if let Some(end) = self.buf_motion(start, Motion::RightWord) {
self.delete_between(start, end);
}
} else {
@@ -303,7 +296,23 @@ impl<'a> TextEditCtx<'a> {
}
}
pub fn select(&mut self, pos: Vec2, size: Vec2, drag: bool) {
fn buf_motion(&mut self, cursor: Cursor, motion: Motion) -> Option<Cursor> {
self.text
.buf
.cursor_motion(self.font_system, cursor, None, motion)
.map(|r| r.0)
}
pub fn select_word_at(&mut self, cursor: Cursor) {
if let (Some(start), Some(end)) = (
self.buf_motion(cursor, Motion::LeftWord),
self.buf_motion(cursor, Motion::RightWord),
) {
self.text.selection = TextSelection::Span { start, end };
}
}
pub fn select(&mut self, pos: Vec2, size: Vec2, drag: bool, recent: bool) {
let pos = pos - self.text.region().top_left().to_abs(size);
let hit = self.text.buf.hit(pos.x, pos.y);
let sel = &mut self.text.selection;
@@ -316,7 +325,13 @@ impl<'a> TextEditCtx<'a> {
TextSelection::Pos(pos) => match (hit, drag) {
(None, false) => *sel = TextSelection::None,
(None, true) => (),
(Some(hit), false) => *pos = hit,
(Some(hit), false) => {
if recent && hit == *pos {
return self.select_word_at(hit);
} else {
*pos = hit
}
}
(Some(end), true) => *sel = TextSelection::Span { start: *pos, end },
},
TextSelection::Span { start, end } => match (hit, drag) {