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
30 changes: 28 additions & 2 deletions focus_ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDisabledInputsAreNotFocusable(t *testing.T) {
Input("off", WithValue("keep"), func(e *TextInputEl) { e.Disabled = true }),
TextArea("area-off", AreaDisabled()),
Input("on"),
), 80)
), 80, nil)
if len(fs) != 1 || fs[0].ID != "on" {
t.Fatalf("focus ring = %+v, want only the enabled input", fs)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestIDlessControlsNeverLookFocused(t *testing.T) {
if st, ok := r.inputs[""]; ok && st.model.Focused() {
t.Error("an input without an ID is focused while nothing has focus")
}
if fs := collectFocusables(Checkbox("", "box", false, nil), 80); len(fs) != 0 {
if fs := collectFocusables(Checkbox("", "box", false, nil), 80, nil); len(fs) != 0 {
t.Errorf("checkbox without an ID joined the ring: %+v", fs)
}
}
Expand Down Expand Up @@ -211,3 +211,29 @@ func TestInputStateIsDroppedWhenTheControlLeaves(t *testing.T) {
t.Errorf("a returning control brought back stale text:\n%s", out)
}
}

func TestTabRingFollowsTheVariantThePaneDraws(t *testing.T) {
m := newRingModel(func(ringModel) Element {
return Sidebar(
Responsive(
At(0, Button("Small", func() Msg { return testMsg("small") })),
At(60, Button("Big", func() Msg { return testMsg("big") })),
),
Text("main"),
)
})
a := newTrapAdapter(m)
a.renderer.Width = 100
out := a.View()
if !strings.Contains(out, "Small") {
t.Fatalf("the 24-cell pane should draw the Small variant:\n%s", out)
}
key(a, "tab")
if got := a.focus.FocusedID(); got != "btn:Small" {
t.Fatalf("Tab focused %q, want the drawn btn:Small", got)
}
key(a, "enter")
if got := lastMsg(m); got != testMsg("small") {
t.Fatalf("Enter dispatched %v", got)
}
}
7 changes: 4 additions & 3 deletions program_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (a *teaAdapter) handleKey(m tea.KeyMsg) (tea.Model, tea.Cmd) {
return a, tea.Quit
}

focusables := collectFocusables(a.model.View(), a.renderer.Width)
focusables := collectFocusables(a.model.View(), a.renderer.Width, a.renderer.picks)

if key == "tab" {
a.focus.SetIDs(focusableIDs(focusables))
Expand Down Expand Up @@ -203,7 +203,7 @@ func (a *teaAdapter) handleMouse(m tea.MouseMsg) (tea.Model, tea.Cmd) {
return a, nil
}

focusables := collectFocusables(a.model.View(), a.renderer.Width)
focusables := collectFocusables(a.model.View(), a.renderer.Width, a.renderer.picks)
for _, f := range focusables {
z := a.zones.Get(f.ID)
if z == nil || z.IsZero() || !z.InBounds(m) {
Expand Down Expand Up @@ -379,7 +379,6 @@ func (a *teaAdapter) submitForm(form *FormEl) Msg {

func (a *teaAdapter) View() string {
el := a.model.View()
a.focus.SetIDs(focusableIDs(collectFocusables(el, a.renderer.Width)))
a.renderer.FocusedID = a.focus.FocusedID()
// The bar is built before the body so Fill-height layouts know how
// many rows remain for them.
Expand All @@ -389,6 +388,8 @@ func (a *teaAdapter) View() string {
a.renderer.Reserved = 1
}
view := a.renderer.Render(el)
a.focus.SetIDs(focusableIDs(collectFocusables(el, a.renderer.Width, a.renderer.picks)))
a.renderer.FocusedID = a.focus.FocusedID()
if bar != "" {
view = a.withStatusBar(view, bar)
}
Expand Down
37 changes: 27 additions & 10 deletions render_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type tuiRenderer struct {
// see focusKeys.
buttonKeys focusKeys

// picks records, in tree order, which variant each Responsive
// element drew in the last frame. A variant is chosen from the width
// of its pane, which only rendering knows, so the focus ring follows
// these picks rather than re-deciding from the terminal width.
picks []int

// 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.
Expand Down Expand Up @@ -109,6 +115,7 @@ func newTUIRenderer(width, height int, zones *zone.Manager) *tuiRenderer {
// generated per-row IDs does not accumulate.
func (r *tuiRenderer) Render(el Element) string {
r.buttonKeys = focusKeys{}
r.picks = r.picks[:0]
r.rendered = map[string]bool{}
out := r.renderEl(el)
for id := range r.inputs {
Expand Down Expand Up @@ -182,11 +189,13 @@ type focusable struct {
}

// collectFocusables walks the tree and returns focus-ring entries in
// tree order: text inputs, buttons, and links. width is the terminal
// width, used to resolve which Responsive variant is on screen — only
// visible controls join the ring.
func collectFocusables(el Element, width int) []focusable {
// tree order: text inputs, buttons, and links. Only visible controls
// join the ring: picks are the Responsive variants the last render drew,
// in tree order (tuiRenderer.picks), and width, the terminal width,
// decides any Responsive the picks do not cover.
func collectFocusables(el Element, width int, picks []int) []focusable {
var out []focusable
seen := 0
keys := focusKeys{}
var walk func(Element, *FormEl)
walk = func(e Element, form *FormEl) {
Expand Down Expand Up @@ -236,9 +245,15 @@ func collectFocusables(el Element, width int) []focusable {
walk(c, &f)
}
case ResponsiveEl:
// Only the variant that actually draws is focusable, so
// Tab never lands on a control hidden at this width.
walk(v.pick(width), form)
i := -2
if seen < len(picks) {
i = picks[seen]
}
seen++
if i == -2 {
i = v.pickIndex(width)
}
walk(v.variant(i), form)
default:
for _, c := range Children(e) {
walk(c, form)
Expand Down Expand Up @@ -389,7 +404,9 @@ func (r *tuiRenderer) renderEl(el Element) string {
case TextAreaEl:
return r.renderTextArea(e)
case ResponsiveEl:
return r.renderEl(e.pick(r.availWidth()))
i := e.pickIndex(r.availWidth())
r.picks = append(r.picks, i)
return r.renderEl(e.variant(i))
case KeyedEl:
return r.renderEl(e.Child)
default:
Expand Down Expand Up @@ -646,7 +663,7 @@ func (r *tuiRenderer) renderBox(e BoxEl) string {
if shareAvail > 0 && e.Direction == Row && len(e.Children) > 1 {
shareAvail = (shareAvail - e.Gap*(len(e.Children)-1)) / len(e.Children)
}
keysBefore := r.buttonKeys.clone()
keysBefore, picksBefore := r.buttonKeys.clone(), len(r.picks)
parts := make([]string, 0, len(e.Children))
for _, c := range e.Children {
w := shareAvail
Expand All @@ -666,7 +683,7 @@ func (r *tuiRenderer) renderBox(e BoxEl) string {
// vertical stack, the TUI's flex-wrap. Growing children take
// the full width so the collapsed panes stay aligned.
dir = Column
r.buttonKeys = keysBefore
r.buttonKeys, r.picks = keysBefore, r.picks[:picksBefore]
for i, c := range e.Children {
parts[i] = r.withAvail(childAvail, func() string { return r.renderEl(c) })
if s, ok := styleOf(c); ok && s.Grow > 0 {
Expand Down
2 changes: 1 addition & 1 deletion render_tui_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestCollectFocusablesIncludesSelectAndArea(t *testing.T) {
Select("disabled-one", Options("a"), "a", nil, SelectDisabled()),
)

got := collectFocusables(tree, 80)
got := collectFocusables(tree, 80, nil)
if len(got) != 2 {
t.Fatalf("focusables = %d, want 2: %+v", len(got), got)
}
Expand Down
4 changes: 2 additions & 2 deletions render_tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestCheckboxFocusableAndToggleMsg(t *testing.T) {
Checkbox("cb1", "milk", false, nil),
Button("Save", func() Msg { return nil }),
)
fs := collectFocusables(tree, 80)
fs := collectFocusables(tree, 80, nil)
if len(fs) != 2 {
t.Fatalf("want 2 focusables, got %d", len(fs))
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestCardChildrenReachableByWalkers(t *testing.T) {
Input("name"),
Button("Go", func() Msg { return nil }),
))
fs := collectFocusables(tree, 80)
fs := collectFocusables(tree, 80, nil)
if len(fs) != 2 {
t.Fatalf("focusables inside Card not collected: got %d, want 2", len(fs))
}
Expand Down
24 changes: 18 additions & 6 deletions responsive.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,31 @@ func TextAt(minWidth int, content string) TextVariant {
// them. With avail below every breakpoint the narrowest variant wins,
// so there is always something to draw.
func (e ResponsiveEl) pick(avail int) Element {
return e.variant(e.pickIndex(avail))
}

// pickIndex returns the index of the variant pick would choose, or -1
// when there are no variants.
func (e ResponsiveEl) pickIndex(avail int) int {
if len(e.Variants) == 0 {
return Empty()
return -1
}
chosen := e.Variants[0].Content
for _, v := range e.Variants {
chosen := 0
for i, v := range e.Variants {
if avail >= v.MinWidth {
chosen = v.Content
chosen = i
}
}
if chosen == nil {
return chosen
}

// variant returns the content of variant i, or Empty when i is out of
// range or the content is nil.
func (e ResponsiveEl) variant(i int) Element {
if i < 0 || i >= len(e.Variants) || e.Variants[i].Content == nil {
return Empty()
}
return chosen
return e.Variants[i].Content
}

// widest returns the variant with the largest MinWidth — what an
Expand Down
6 changes: 3 additions & 3 deletions responsive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ func TestResponsiveFocusOnlyVisibleVariant(t *testing.T) {
return out
}

got := ids(collectFocusables(tree, 100))
got := ids(collectFocusables(tree, 100, nil))
if len(got) != 1 || got[0] != "btn:wide" {
t.Errorf("at 100 cols focusables = %v, want just the wide button", got)
}
got = ids(collectFocusables(tree, 20))
got = ids(collectFocusables(tree, 20, nil))
if len(got) != 1 || got[0] != "btn:narrow" {
t.Errorf("at 20 cols focusables = %v, want just the narrow button", got)
}
Expand Down Expand Up @@ -374,7 +374,7 @@ func TestKeyedRendersChildUnchangedInTUI(t *testing.T) {
t.Errorf("Keyed changed TUI output:\n%q\nwant\n%q", got, want)
}
nop := func() Msg { return nil }
fs := collectFocusables(Box(Column, Keyed("x", Button("Go", nop))), 80)
fs := collectFocusables(Box(Column, Keyed("x", Button("Go", nop))), 80, nil)
if len(fs) != 1 || fs[0].ID != "btn:Go" {
t.Errorf("button inside Keyed not focusable: %+v", fs)
}
Expand Down
Loading