diff --git a/focus_ring_test.go b/focus_ring_test.go index 1f388a9..ddce98d 100644 --- a/focus_ring_test.go +++ b/focus_ring_test.go @@ -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) } @@ -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) } } @@ -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) + } +} diff --git a/program_tui.go b/program_tui.go index f568e47..90ebb25 100644 --- a/program_tui.go +++ b/program_tui.go @@ -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)) @@ -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) { @@ -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. @@ -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) } diff --git a/render_tui.go b/render_tui.go index 1c09b39..580c27c 100644 --- a/render_tui.go +++ b/render_tui.go @@ -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. @@ -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 { @@ -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) { @@ -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) @@ -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: @@ -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 @@ -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 { diff --git a/render_tui_new_test.go b/render_tui_new_test.go index 3d89252..72026c1 100644 --- a/render_tui_new_test.go +++ b/render_tui_new_test.go @@ -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) } diff --git a/render_tui_test.go b/render_tui_test.go index 02563f8..8d010ac 100644 --- a/render_tui_test.go +++ b/render_tui_test.go @@ -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)) } @@ -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)) } diff --git a/responsive.go b/responsive.go index dc82de7..1f00093 100644 --- a/responsive.go +++ b/responsive.go @@ -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 diff --git a/responsive_test.go b/responsive_test.go index 1ac3f96..e20d86c 100644 --- a/responsive_test.go +++ b/responsive_test.go @@ -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) } @@ -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) }