hold shift to select text
This commit is contained in:
@@ -202,18 +202,35 @@ impl<'a> TextEditCtx<'a> {
|
|||||||
self.text.selection.clear();
|
self.text.selection.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn motion(&mut self, motion: Motion) {
|
pub fn motion(&mut self, motion: Motion, select: bool) {
|
||||||
if let TextSelection::Pos(cursor) = self.text.selection
|
if let TextSelection::Pos(cursor) = self.text.selection
|
||||||
&& let Some(cursor) = self.buf_motion(cursor, motion)
|
&& let Some(new) = self.buf_motion(cursor, motion)
|
||||||
{
|
{
|
||||||
self.text.selection = TextSelection::Pos(cursor);
|
if select {
|
||||||
|
self.text.selection = TextSelection::Span {
|
||||||
|
start: cursor,
|
||||||
|
end: new,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
self.text.selection = TextSelection::Pos(new);
|
||||||
|
}
|
||||||
} else if let TextSelection::Span { start, end } = self.text.selection {
|
} else if let TextSelection::Span { start, end } = self.text.selection {
|
||||||
let (start, end) = sort_cursors(start, end);
|
if select {
|
||||||
let sel = &mut self.text.selection;
|
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||||
match motion {
|
self.text.selection = TextSelection::Span { start, end: cursor };
|
||||||
Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start),
|
}
|
||||||
Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end),
|
} else {
|
||||||
_ => (),
|
let (start, end) = sort_cursors(start, end);
|
||||||
|
let sel = &mut self.text.selection;
|
||||||
|
match motion {
|
||||||
|
Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start),
|
||||||
|
Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end),
|
||||||
|
_ => {
|
||||||
|
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||||
|
self.text.selection = TextSelection::Pos(cursor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,7 +296,7 @@ impl<'a> TextEditCtx<'a> {
|
|||||||
edit_line(line, line_text);
|
edit_line(line, line_text);
|
||||||
if mov {
|
if mov {
|
||||||
for _ in 0..text.chars().count() {
|
for _ in 0..text.chars().count() {
|
||||||
self.motion(Motion::Right);
|
self.motion(Motion::Right, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,7 +319,7 @@ impl<'a> TextEditCtx<'a> {
|
|||||||
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
||||||
&& (cursor.index != 0 || cursor.line != 0)
|
&& (cursor.index != 0 || cursor.line != 0)
|
||||||
{
|
{
|
||||||
self.motion(if word { Motion::LeftWord } else { Motion::Left });
|
self.motion(if word { Motion::LeftWord } else { Motion::Left }, false);
|
||||||
self.delete(word);
|
self.delete(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -444,20 +461,20 @@ impl<'a> TextEditCtx<'a> {
|
|||||||
}
|
}
|
||||||
NamedKey::ArrowRight => {
|
NamedKey::ArrowRight => {
|
||||||
if modifiers.control {
|
if modifiers.control {
|
||||||
self.motion(Motion::RightWord)
|
self.motion(Motion::RightWord, modifiers.shift)
|
||||||
} else {
|
} else {
|
||||||
self.motion(Motion::Right)
|
self.motion(Motion::Right, modifiers.shift)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NamedKey::ArrowLeft => {
|
NamedKey::ArrowLeft => {
|
||||||
if modifiers.control {
|
if modifiers.control {
|
||||||
self.motion(Motion::LeftWord)
|
self.motion(Motion::LeftWord, modifiers.shift)
|
||||||
} else {
|
} else {
|
||||||
self.motion(Motion::Left)
|
self.motion(Motion::Left, modifiers.shift)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NamedKey::ArrowUp => self.motion(Motion::Up),
|
NamedKey::ArrowUp => self.motion(Motion::Up, modifiers.shift),
|
||||||
NamedKey::ArrowDown => self.motion(Motion::Down),
|
NamedKey::ArrowDown => self.motion(Motion::Down, modifiers.shift),
|
||||||
NamedKey::Escape => {
|
NamedKey::Escape => {
|
||||||
self.deselect();
|
self.deselect();
|
||||||
return TextInputResult::Unfocus;
|
return TextInputResult::Unfocus;
|
||||||
|
|||||||
Reference in New Issue
Block a user