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
5 changes: 3 additions & 2 deletions pkg/menukeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions pkg/menukeys_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"strings"
"testing"

cv "codeberg.org/tslocum/cview"
Expand Down Expand Up @@ -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() })
}
59 changes: 28 additions & 31 deletions pkg/ui_construct_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -200,7 +220,6 @@ func initConstruct() *cv.Grid {

fourthIntervalOption = func() {
list.Clear()
grid.Clear()

tv.SetText("Select time interval.")
form := cv.NewForm()
Expand Down Expand Up @@ -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]
}
Expand All @@ -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() {
Expand Down Expand Up @@ -482,42 +490,34 @@ 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{})

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]
Expand All @@ -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() {
Expand Down
Loading