Improved modifying selection with cursor movement

This commit is contained in:
Luke I. Wilson
2021-03-16 17:30:38 -05:00
parent 7971ef16ab
commit d5ec12c1c8

View File

@@ -471,11 +471,60 @@ func (t *TextEdit) HandleEvent(event tcell.Event) bool {
switch ev.Key() { switch ev.Key() {
// Cursor movement // Cursor movement
case tcell.KeyUp: case tcell.KeyUp:
if ev.Modifiers() == tcell.ModShift {
if !t.selectMode {
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
t.selectMode = true
}
prevCurX, prevCurY := t.curx, t.cury
t.CursorUp() t.CursorUp()
//
if prevCurY <= t.selection.StartLine && prevCurX <= t.selection.StartCol {
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
} else {
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
}
} else {
t.selectMode = false
t.CursorUp()
}
case tcell.KeyDown: case tcell.KeyDown:
if ev.Modifiers() == tcell.ModShift {
if !t.selectMode {
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
t.selectMode = true
}
prevCurX, prevCurY := t.curx, t.cury
t.CursorDown() t.CursorDown()
if prevCurY >= t.selection.EndLine && prevCurX >= t.selection.EndCol {
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
} else {
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
}
} else {
t.selectMode = false
t.CursorDown()
}
case tcell.KeyLeft: case tcell.KeyLeft:
if ev.Modifiers() == tcell.ModShift {
if !t.selectMode {
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
t.selectMode = true
}
prevCurX, prevCurY := t.curx, t.cury
t.CursorLeft() t.CursorLeft()
if prevCurY == t.selection.StartLine && prevCurX == t.selection.StartCol { // We are moving the start...
t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
} else {
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
}
} else {
t.selectMode = false
t.CursorLeft()
}
case tcell.KeyRight: case tcell.KeyRight:
if ev.Modifiers() == tcell.ModShift { if ev.Modifiers() == tcell.ModShift {
if !t.selectMode { // If we are not already selecting... if !t.selectMode { // If we are not already selecting...
@@ -484,12 +533,15 @@ func (t *TextEdit) HandleEvent(event tcell.Event) bool {
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
t.selectMode = true t.selectMode = true
} }
prevCurX, prevCurY := t.curx, t.cury
t.CursorRight() // Advance the cursor t.CursorRight() // Advance the cursor
if prevCurY == t.selection.EndLine && prevCurX == t.selection.EndCol {
t.selection.EndLine, t.selection.EndCol = t.cury, t.curx t.selection.EndLine, t.selection.EndCol = t.cury, t.curx
} else { } else {
if t.selectMode { t.selection.StartLine, t.selection.StartCol = t.cury, t.curx
t.selectMode = false
} }
} else {
t.selectMode = false
t.CursorRight() t.CursorRight()
} }
case tcell.KeyHome: case tcell.KeyHome: