From c2f2dec0d33b1619cb241a0fd4a74c0b2896aa6d Mon Sep 17 00:00:00 2001 From: Harry Witte Date: Tue, 17 Nov 2026 10:05:29 -0500 Subject: [PATCH] fix challenge time and bot-strength sliders The sliders were squeezed into a 2-column strip (the menu column sized itself to the empty menu they replace) and never got the keyboard, so the step looked blank and keys did nothing. They now get the column and the keyboard; Back/Submit/Home put the menu back and return focus to it. The bot-strength slider opens at the level already chosen. --- pkg/menukeys.go | 5 +-- pkg/menukeys_test.go | 42 +++++++++++++++++++++++++ pkg/ui_construct_challenge.go | 59 +++++++++++++++++------------------ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/pkg/menukeys.go b/pkg/menukeys.go index f9fc80c..917ee24 100644 --- a/pkg/menukeys.go +++ b/pkg/menukeys.go @@ -156,12 +156,13 @@ func confirmItem(item *cv.ListItem, text, ask string, f func()) { // fitMenuColumn makes column col of a grid as wide as its menu needs (the // widest entry or description, with the shortcut), so descriptions aren't // cut off; the other columns share the rest. cols are the grid's columns -// otherwise (used when the menu wouldn't leave room for the rest). +// otherwise (used when the menu wouldn't leave room for the rest, or the +// menu is empty because something else has its place for now). func fitMenuColumn(g *cv.Grid, list *cv.List, cols []int, col int) { const minRest = 30 // columns kept for the rest of the screen g.SetDrawFunc(func(_ tc.Screen, x, y, w, h int) (int, int, int, int) { c := append([]int(nil), cols...) - if need := menuWidth(list); need <= w-minRest { + if need := menuWidth(list); list.GetItemCount() > 0 && need <= w-minRest { c[col] = need } g.SetColumns(c...) // the grid's own lock, not the box's held here diff --git a/pkg/menukeys_test.go b/pkg/menukeys_test.go index c561b94..0b210a8 100644 --- a/pkg/menukeys_test.go +++ b/pkg/menukeys_test.go @@ -1,6 +1,7 @@ package pkg import ( + "strings" "testing" cv "codeberg.org/tslocum/cview" @@ -128,3 +129,44 @@ func TestMenuColumnFits(t *testing.T) { } }) } + +// The challenge maker's time step swaps its menu for sliders: they get the +// keyboard and room (not the 2 columns an empty menu needs), and Submit goes +// on to the next step. +func TestChallengeIntervalStep(t *testing.T) { + scr := startTestApp(t, 160, 50) + var list *cv.List + onUI(func() { + gotoChallengeConstruction() + list = menusByScreen["challenge"].lists[0] + Root.App.SetFocus(list) + }) + for _, k := range []string{"a", "a", "a"} { // Random, standard, real time + press(scr, tc.KeyRune, k) + } + var slider *cv.Slider + waitFor(t, "the minutes slider has the keyboard", func() bool { + slider, _ = Root.App.GetFocus().(*cv.Slider) + return slider != nil + }) + onUI(func() { + _, p := Root.nav.GetFrontPanel() + p.SetRect(0, 0, 160, 50) + p.Draw(newMockScreen(t, 160, 50)) + if _, _, w, _ := slider.GetRect(); w < 40 { + t.Errorf("slider %d wide", w) + } + }) + press(scr, tc.KeyRight, "") + waitFor(t, "right moves the slider", func() bool { + return strings.Contains(slider.GetLabel(), "11") + }) + // Tab past the increment slider to Submit + press(scr, tc.KeyTab, "") + press(scr, tc.KeyTab, "") + press(scr, tc.KeyEnter, "") + waitFor(t, "the rated step", func() bool { + return Root.App.GetFocus() == list && list.GetItemCount() > 0 && list.GetItem(0).GetMainText() == "casual" + }) + onUI(func() { gotoWelcome() }) +} diff --git a/pkg/ui_construct_challenge.go b/pkg/ui_construct_challenge.go index 61f038c..ffd0abd 100644 --- a/pkg/ui_construct_challenge.go +++ b/pkg/ui_construct_challenge.go @@ -52,7 +52,27 @@ func initConstruct() *cv.Grid { var Days int = 1 var BotPowerLevel int = 1 + // The time and bot-level steps put a form of sliders where the menu is; + // showList puts the menu back. + showList := func() { + grid.Clear() + grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) + grid.AddItem(list, 1, 1, 1, 1, 0, 0, true) + grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) + grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + Root.App.SetFocus(list) + } + showForm := func(form *cv.Form) { + grid.Clear() + grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) + grid.AddItem(form, 1, 1, 1, 1, 0, 0, true) + grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) + grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + Root.App.SetFocus(form) // the menu it replaces had the keyboard + } + goHome := func() { + showList() path = []string{} firstOption() gotoLichessAfterLogin() @@ -200,7 +220,6 @@ func initConstruct() *cv.Grid { fourthIntervalOption = func() { list.Clear() - grid.Clear() tv.SetText("Select time interval.") form := cv.NewForm() @@ -272,19 +291,11 @@ func initConstruct() *cv.Grid { updateTree(ratedOption) - grid.Clear() - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(list, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showList() fifthRatedOption() }) form.AddButton("Back", func() { - grid.Clear() - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(list, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showList() if len(path) > 0 { path = path[:len(path)-1] } @@ -297,10 +308,7 @@ func initConstruct() *cv.Grid { form.SetTitle("Choose time interval option:") form.SetTitleAlign(cv.AlignCenter) - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(form, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showForm(form) } fifthRatedOption = func() { @@ -482,30 +490,26 @@ func initConstruct() *cv.Grid { BotPowerLevelOption = func() { list.Clear() - grid.Clear() form := cv.NewForm() tv.SetText("Select bot power level.") p := []int{1, 2, 3, 4, 5, 6, 7, 8} slider1 := cv.NewSlider() - slider1.SetLabel("AI Strength: 1") slider1.SetChangedFunc(func(value int) { BotPowerLevel = p[value] slider1.SetLabel(fmt.Sprintf("AI Strength: %3v", p[value])) }) slider1.SetMax(len(p) - 1) slider1.SetIncrement(1) + slider1.SetProgress(indexOf(p, BotPowerLevel)) + slider1.SetLabel(fmt.Sprintf("AI Strength: %3v", BotPowerLevel)) form.AddFormItem(slider1) form.AddButton("Submit", func() { newChallenge.Level = fmt.Sprintf("%v", BotPowerLevel) - grid.Clear() - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(list, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showList() path = append(path, fmt.Sprintf("AI Strength: %v", BotPowerLevel)) updateTree([]string{}) @@ -513,11 +517,7 @@ func initConstruct() *cv.Grid { eightSubmitOption() }) form.AddButton("Back", func() { - grid.Clear() - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(list, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showList() if len(path) > 0 { path = path[:len(path)-1] @@ -531,10 +531,7 @@ func initConstruct() *cv.Grid { form.SetTitle("Choose bot power level:") form.SetTitleAlign(cv.AlignCenter) - grid.AddItem(tv, 0, 0, 1, 2, 0, 0, false) - grid.AddItem(form, 1, 1, 1, 1, 0, 0, true) - grid.AddItem(Ribbon, 2, 0, 1, 2, 0, 0, false) - grid.AddItem(tree, 1, 0, 1, 1, 0, 0, false) + showForm(form) } eightSubmitOption = func() {