Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 562a7ae

Browse files
committed
Improve editor event handling
The editor now always handles the externally set input handlers first. This was changed due to incorrect values being returned from the input handler, which in turn caused the upcoming draw to be skipped. While this didn't cause any critical problems, it made the UI feel laggy at times. Technically certain editor shortcuts could now be overwritten from outside, but if the user choses to do so, we shall not care.
1 parent 88629aa commit 562a7ae

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

ui/editor.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,14 @@ func NewEditor() *Editor {
407407
editor.buffer.Cursor.SetSelectionEnd(editor.buffer.End())
408408

409409
editor.internalTextView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
410-
// TODO: This entire chunk could be cleaned up by assigning handlers to each event type,
411-
// e.g. event.trigger()
410+
inputCapture := editor.inputCapture
411+
if inputCapture != nil {
412+
event = inputCapture(event)
413+
if event == nil {
414+
return nil
415+
}
416+
}
417+
412418
if shortcuts.MoveCursorLeft.Equals(event) {
413419
editor.MoveCursorLeft()
414420
} else if shortcuts.ExpandSelectionToLeft.Equals(event) {
@@ -462,12 +468,8 @@ func NewEditor() *Editor {
462468
return nil
463469
} else if shortcuts.InputNewLine.Equals(event) {
464470
editor.InsertCharacter('\n')
465-
} else if shortcuts.SendMessage.Equals(event) && editor.inputCapture != nil {
466-
return editor.inputCapture(event)
467-
} else if (editor.inputCapture == nil || editor.inputCapture(event) != nil) && event.Rune() != 0 {
471+
} else if event.Rune() != 0 {
468472
editor.InsertCharacter(event.Rune())
469-
} else {
470-
return event
471473
}
472474

473475
editor.TriggerHeightRequestIfNecessary()

ui/window.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
631631
return autocompleteValues
632632
})
633633

634-
captureFunc := func(event *tcell.EventKey) *tcell.EventKey {
635-
messageToSend := window.messageInput.GetText()
636-
634+
window.messageInput.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
637635
if event.Modifiers() == tcell.ModCtrl {
638636
if event.Key() == tcell.KeyUp {
639637
window.chatView.internalTextView.ScrollUp()
@@ -685,13 +683,16 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
685683
}
686684

687685
//When you are already typing a message, you probably don't want to risk loosing it.
688-
if event.Key() == tcell.KeyUp && (messageToSend == "" || window.editingMessageID != nil) {
689-
messageToEdit := chooseNextMessageToEdit(len(window.chatView.data)-1, -1, func(i int) int { return i - 1 })
690-
if messageToEdit != nil {
691-
window.startEditingMessage(messageToEdit)
692-
}
686+
if event.Key() == tcell.KeyUp {
687+
messageToSend := window.messageInput.GetText()
688+
if messageToSend == "" || window.editingMessageID != nil {
689+
messageToEdit := chooseNextMessageToEdit(len(window.chatView.data)-1, -1, func(i int) int { return i - 1 })
690+
if messageToEdit != nil {
691+
window.startEditingMessage(messageToEdit)
692+
}
693693

694-
return nil
694+
return nil
695+
}
695696
}
696697

697698
if event.Key() == tcell.KeyDown && window.editingMessageID != nil {
@@ -744,15 +745,15 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
744745
window.insertNewLineAtCursor()
745746
return nil
746747
} else if shortcuts.SendMessage.Equals(event) {
748+
messageToSend := window.messageInput.GetText()
747749
if window.selectedChannel != nil {
748750
window.TrySendMessage(window.selectedChannel, messageToSend)
749751
}
750752
return nil
751753
}
752754

753755
return event
754-
}
755-
window.messageInput.SetInputCapture(captureFunc)
756+
})
756757

757758
//FIXME Buffering might just be retarded, as the event handlers are launched in separate routines either way.
758759
messageInputChan := make(chan *discordgo.Message)
@@ -2420,8 +2421,10 @@ func (window *Window) startEditingMessage(message *discordgo.Message) {
24202421
}
24212422

24222423
func (window *Window) exitMessageEditMode() {
2423-
window.exitMessageEditModeAndKeepText()
2424-
window.messageInput.SetText("")
2424+
if window.editingMessageID != nil {
2425+
window.exitMessageEditModeAndKeepText()
2426+
window.messageInput.SetText("")
2427+
}
24252428
}
24262429

24272430
func (window *Window) exitMessageEditModeAndKeepText() {

0 commit comments

Comments
 (0)