Drop pasted keys for views without an OnPaste handler

Every view that takes text has an OnPaste handler now, and views that
don't take text already ignored pasted keys. Make this a single rule.
While a paste is in progress, keys go to the focused view's OnPaste
handler or nowhere. This replaces the check in execKeybindings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-11-12 10:22:50 +01:00
co-authored by Claude Opus 5.5
parent 9765b96790
commit c28e3576d1
3 changed files with 18 additions and 11 deletions
+4 -6
View File
@@ -1809,8 +1809,10 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
ev.Key = NewKeyName(KeyEnter)
}
if g.IsPasting && g.currentView != nil && g.currentView.OnPaste != nil {
g.collectPastedKey(g.currentView, ev.Key)
if g.IsPasting {
if g.currentView != nil && g.currentView.OnPaste != nil {
g.collectPastedKey(g.currentView, ev.Key)
}
return nil
}
@@ -2087,10 +2089,6 @@ func (g *Gui) execKeybindings(v *View, ev *GocuiEvent) error {
var globalKb *keybinding
var matchingParentViewKb *keybinding
if g.IsPasting && v != nil && !v.Editable {
return nil
}
// if we're searching, and we've hit n/N/Esc, we ignore the default keybinding
if v != nil && v.IsSearching() {
if lo.SomeBy(g.NextSearchMatchKeys, func(k Key) bool { return ev.Key.Equals(k) }) {
+9 -1
View File
@@ -55,9 +55,16 @@ func TestPasteIntoViewWithoutPasteHandler(t *testing.T) {
_, err := g.SetCurrentView("editor")
assert.NoError(t, err)
enterPressed := false
g.SetKeybinding("editor", NewKeyName(KeyEnter), func(*Gui, *View) error {
enterPressed = true
return nil
})
for _, tev := range []tcell.Event{
tcell.NewEventPaste(true),
tcell.NewEventKey(tcell.KeyRune, "a", tcell.ModNone),
tcell.NewEventKey(tcell.KeyEnter, "", tcell.ModNone),
tcell.NewEventKey(tcell.KeyRune, "b", tcell.ModNone),
tcell.NewEventPaste(false),
} {
@@ -65,5 +72,6 @@ func TestPasteIntoViewWithoutPasteHandler(t *testing.T) {
assert.NoError(t, g.handleEvent(&ev))
}
assert.Equal(t, "ab", v.TextArea.GetContent())
assert.Equal(t, "", v.TextArea.GetContent())
assert.False(t, enterPressed)
}
+5 -4
View File
@@ -163,10 +163,11 @@ type View struct {
// default.
Editor Editor
// OnPaste, if set, receives pasted text in one piece when the paste ends,
// instead of one key press per character. The text consists of the pasted
// printable characters, newlines and tabs; other keys in the paste, such as
// Esc or control characters, are dropped.
// OnPaste, if set, receives pasted text in one piece when the paste ends.
// The text consists of the pasted printable characters, newlines and tabs;
// other keys in the paste, such as Esc or control characters, are dropped.
// Pasting into a view without an OnPaste handler does nothing, so that
// pasted text never runs keybindings.
OnPaste func(text string) error
// Overwrite enables or disables the overwrite mode of the view.