Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions focus_ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package wui

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -175,3 +176,38 @@ func TestClickingSelectInFormCyclesWithoutSubmitting(t *testing.T) {
t.Fatalf("click on a Select dispatched %v, want it to cycle to b", got)
}
}

func TestPasteIntoTextArea(t *testing.T) {
m := newRingModel(func(ringModel) Element {
return TextArea("notes", WithAreaOnChange(func(s string) Msg { return testMsg(s) }))
})
a := newTrapAdapter(m)
a.View()
key(a, "tab")
a.handleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("hello world"), Paste: true})
if got := lastMsg(m); got != testMsg("hello world") {
t.Fatalf("paste produced %v, want the pasted text", got)
}
a.handleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x"), Alt: true})
if got := lastMsg(m); got != testMsg("hello world") {
t.Errorf("alt+x inserted text: %v", got)
}
}

func TestInputStateIsDroppedWhenTheControlLeaves(t *testing.T) {
r := newTUIRenderer(40, 5, zone.New())
with := Box(Column, Input("q"), TextArea("t"))
r.Render(with)
in := r.ensureInput(TextInputEl{ID: "q"})
in.SetValue("typed")
r.setArea("t", "draft")

r.Render(Text("elsewhere"))
if len(r.inputs) != 0 || len(r.areas) != 0 {
t.Fatalf("state kept for controls no longer drawn: %d inputs, %d areas", len(r.inputs), len(r.areas))
}
out := r.Render(with)
if strings.Contains(out, "typed") || strings.Contains(out, "draft") {
t.Errorf("a returning control brought back stale text:\n%s", out)
}
}
5 changes: 2 additions & 3 deletions program_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,10 @@ func (a *teaAdapter) routeKeyToArea(el TextAreaEl, m tea.KeyMsg) (Model, tea.Cmd
// (Esc never reaches here — handleKey blurs on esc first.)
return a.model, nil, true
default:
if r := []rune(m.String()); len(r) == 1 {
next = value + string(r)
} else {
if m.Type != tea.KeyRunes || m.Alt || len(m.Runes) == 0 {
return a.model, nil, true
}
next = value + string(m.Runes)
}

if next == value {
Expand Down
30 changes: 29 additions & 1 deletion render_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type tuiRenderer struct {
// see focusKeys.
buttonKeys focusKeys

// rendered records the input and text-area IDs drawn during the
// current Render, so state for controls that left the view can be
// dropped afterwards. Nil outside Render.
rendered map[string]bool

inputs map[string]*tuiInputState
areas map[string]*tuiAreaState
zones *zone.Manager
Expand Down Expand Up @@ -97,9 +102,26 @@ func newTUIRenderer(width, height int, zones *zone.Manager) *tuiRenderer {
}
}

// Render draws el. Afterwards it drops the editing state of inputs and
// text areas the frame did not draw, so a control that leaves the view
// starts fresh from its spec value when it returns, and state for
// generated per-row IDs does not accumulate.
func (r *tuiRenderer) Render(el Element) string {
r.buttonKeys = focusKeys{}
return r.renderEl(el)
r.rendered = map[string]bool{}
out := r.renderEl(el)
for id := range r.inputs {
if !r.rendered["input:"+id] {
delete(r.inputs, id)
}
}
for id := range r.areas {
if !r.rendered["area:"+id] {
delete(r.areas, id)
}
}
r.rendered = nil
return out
}

// focusKeys makes button focus keys unique within one view. Buttons
Expand Down Expand Up @@ -275,6 +297,9 @@ func focusableIDs(items []focusable) []string {
// value last seen — i.e. when the app changed it programmatically —
// so in-progress typing is never clobbered by re-renders.
func (r *tuiRenderer) ensureInput(spec TextInputEl) *textinput.Model {
if r.rendered != nil {
r.rendered["input:"+spec.ID] = true
}
st, ok := r.inputs[spec.ID]
if !ok {
m := textinput.New()
Expand Down Expand Up @@ -514,6 +539,9 @@ func (r *tuiRenderer) renderTextArea(e TextAreaEl) string {
// spec only when the app changed it programmatically — the same rule
// ensureInput applies to single-line inputs.
func (r *tuiRenderer) ensureArea(spec TextAreaEl) string {
if r.rendered != nil {
r.rendered["area:"+spec.ID] = true
}
st, ok := r.areas[spec.ID]
if !ok {
r.areas[spec.ID] = &tuiAreaState{value: spec.Value, lastSpec: spec.Value}
Expand Down
Loading