Consolidate shared UI, hardware transport, and translation tooling

This commit is contained in:
Storm Dragon committed 2026-11-16 20:07:56 -05:00
1 parent ed6951f4cd
commit 2c612de63b
61 files changed
+2417 -1573

No files matched your search

+3 -3
View File
@@ -29,7 +29,7 @@ func TestGroupedChoicesTraversalIsolationAndSelection(t *testing.T) {
{ID: "sound.enabled", Label: "Sound enabled", Group: "Sound", Kind: CheckBox},
{ID: "ok", Label: "OK", Kind: Button, Action: "save"},
}}}}
session, err := New_session(form)
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -79,7 +79,7 @@ func TestComboChangesSpeakOnlyValueAndFocusRepeatsLabel(t *testing.T) {
{ID: "format", Label: "Format", Kind: ComboBox, Choices: []Choice{{ID: "one", Label: "First"}, {ID: "two", Label: "Second"}, {ID: "three", Label: "Third"}}},
{ID: "ok", Label: "OK", Kind: Button, Action: "save"},
}}}}
session, err := New_session(form)
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -113,7 +113,7 @@ func TestChoiceDefinitionsRejectMissingAndDuplicateSelections(t *testing.T) {
} {
control.ID, control.Label, control.Kind = "driver", "Driver", ComboBox
form := Form{Type: TabForm, Title: "Settings", Tabs: []Tab{{ID: "presentation", Label: "Presentation", Controls: []Control{control}}}}
if _, err := New_session(form); err == nil {
if _, err := NewSession(form, Messages{}); err == nil {
t.Fatal("invalid combo accepted", control)
}
}
+7 -7
View File
@@ -9,10 +9,10 @@ import (
)
func TestEmptyAndRefreshedGridKeepsFocusSearchAndLiteralWords(t *testing.T) {
form := grid_form()
form := gridForm()
form.Tabs[0].Controls[0].Choices = nil
form.Tabs[0].Controls[0].DeleteAction = "remove"
session, err := New_session(form)
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -21,10 +21,10 @@ func TestEmptyAndRefreshedGridKeepsFocusSearchAndLiteralWords(t *testing.T) {
t.Fatal(got)
}
session.Handle(Key{Text: "c"})
session.Set_choices("one", "grid", []Choice{{ID: "cat", Label: "cat", Literal: true}, {ID: "car", Label: "car", Literal: true}})
session.SetChoices("one", "grid", []Choice{{ID: "cat", Label: "cat", Literal: true}, {ID: "car", Label: "car", Literal: true}})
session.Handle(Key{Name: "right"})
session.Set_choices("one", "grid", []Choice{{ID: "car", Label: "car", Literal: true}, {ID: "cat", Label: "cat", Literal: true}})
control, _ := session.Focused_control()
session.SetChoices("one", "grid", []Choice{{ID: "car", Label: "car", Literal: true}, {ID: "cat", Label: "cat", Literal: true}})
control, _ := session.FocusedControl()
if control.Text != "c" || control.Choices[control.Selected].ID != "car" {
t.Fatal(control)
}
@@ -34,13 +34,13 @@ func TestEmptyAndRefreshedGridKeepsFocusSearchAndLiteralWords(t *testing.T) {
if got := session.Handle(Key{Name: "delete"}); got.Action != "remove" {
t.Fatal(got)
}
session.Set_choices("one", "grid", nil)
session.SetChoices("one", "grid", nil)
if !strings.Contains(session.Describe(), "No matches") {
t.Fatal(session.Describe())
}
// Updating a grid must own its choices, independently of worker buffers.
choices := []Choice{{ID: "cat", Label: "cat", Literal: true}}
session.Set_choices("one", "grid", choices)
session.SetChoices("one", "grid", choices)
choices[0].Label = "wrong"
if !strings.Contains(session.Describe(), "cat") {
t.Fatal(session.Describe())
+11 -11
View File
@@ -13,29 +13,29 @@ import (
// Grid selections index Choices, even while the visible results are filtered.
// Choice IDs remain opaque to the model and can carry a host-owned value.
func (s *Session) grid_matches(c Control) []int {
func (s *Session) gridMatches(c Control) []int {
var matches []int
prefix := strings.ToLower(c.Text)
for index, choice := range c.Choices {
if !choice.Disabled && strings.HasPrefix(strings.ToLower(s.choice_label(choice)), prefix) {
if !choice.Disabled && strings.HasPrefix(strings.ToLower(s.choiceLabel(choice)), prefix) {
matches = append(matches, index)
}
}
return matches
}
func (s *Session) filter_grid(c *Control) {
func (s *Session) filterGrid(c *Control) {
c.Selected = -1
if matches := s.grid_matches(*c); len(matches) > 0 {
if matches := s.gridMatches(*c); len(matches) > 0 {
c.Selected = matches[0]
}
}
func (s *Session) describe_grid(c Control) string {
matches := s.grid_matches(c)
func (s *Session) describeGrid(c Control) string {
matches := s.gridMatches(c)
for position, index := range matches {
if index == c.Selected {
label := s.choice_label(c.Choices[index])
label := s.choiceLabel(c.Choices[index])
return gridmodel.Describe(label, position, len(matches), c.Columns, s.announceGridCoordinates, s.announceItemPosition, s.messages)
}
}
@@ -69,14 +69,14 @@ func (s *Session) grid(c *Control, key Key) Result {
runes = append(runes, []rune(key.Text)...)
}
c.Text = string(runes)
s.filter_grid(c)
s.filterGrid(c)
query := s.messages.T("Search cleared")
if c.Text != "" {
query = s.messages.Format("Search {0}", c.Text)
}
return Result{Speech: query + ". " + s.describe_grid(*c)}
return Result{Speech: query + ". " + s.describeGrid(*c)}
}
matches := s.grid_matches(*c)
matches := s.gridMatches(*c)
if len(matches) == 0 {
return Result{Speech: s.messages.T("No matches")}
}
@@ -103,5 +103,5 @@ func (s *Session) grid(c *Control, key Key) Result {
}
c.Selected = matches[position]
return Result{Speech: s.describe_grid(*c)}
return Result{Speech: s.describeGrid(*c)}
}
+13 -13
View File
@@ -9,7 +9,7 @@ import (
"testing"
)
func grid_form() Form {
func gridForm() Form {
return Form{Type: TabForm, Title: "Picker", Tabs: []Tab{{ID: "one", Label: "One", Controls: []Control{{
ID: "grid", Label: "Items", Kind: Grid, Columns: 3, Action: "choose",
Choices: []Choice{{ID: "a", Label: "Apple"}, {ID: "b", Label: "Bat"}, {ID: "c", Label: "Cat"},
@@ -18,7 +18,7 @@ func grid_form() Form {
}
func TestGridNavigationRaggedRowsAndActivation(t *testing.T) {
session, err := New_session(grid_form())
session, err := NewSession(gridForm(), Messages{})
if err != nil {
t.Fatal(err)
}
@@ -28,7 +28,7 @@ func TestGridNavigationRaggedRowsAndActivation(t *testing.T) {
{"up", "d"}, {"left", "c"}, {"home", "a"}, {"left", "a"}, {"end", "é"}, {"right", "é"},
} {
session.Handle(Key{Name: trial.key})
control, _ := session.Focused_control()
control, _ := session.FocusedControl()
if got := control.Choices[control.Selected].ID; got != trial.want {
t.Fatalf("%s: got %s, want %s", trial.key, got, trial.want)
}
@@ -40,8 +40,8 @@ func TestGridNavigationRaggedRowsAndActivation(t *testing.T) {
}
func TestGridPrefixEmptyResultsUnicodeAndIsolation(t *testing.T) {
form := grid_form()
session, err := New_session(form)
form := gridForm()
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -50,7 +50,7 @@ func TestGridPrefixEmptyResultsUnicodeAndIsolation(t *testing.T) {
t.Fatal(got)
}
session.Handle(Key{Name: "right"})
control, _ := session.Focused_control()
control, _ := session.FocusedControl()
if control.Selected != 5 {
t.Fatal("filtered selection lost catalog index", control)
}
@@ -70,7 +70,7 @@ func TestGridPrefixEmptyResultsUnicodeAndIsolation(t *testing.T) {
t.Fatal(got)
}
session.Handle(Key{Name: "backspace"})
control, _ = session.Focused_control()
control, _ = session.FocusedControl()
if control.Text != "" || form.Tabs[0].Controls[0].Selected != 0 || form.Tabs[0].Controls[0].Text != "" {
t.Fatal("Unicode deletion or form isolation failed", control)
}
@@ -80,14 +80,14 @@ func TestGridPrefixEmptyResultsUnicodeAndIsolation(t *testing.T) {
}
func TestGridValidation(t *testing.T) {
form := grid_form()
form := gridForm()
form.Tabs[0].Controls[0].Columns = 0
if _, err := New_session(form); err == nil {
if _, err := NewSession(form, Messages{}); err == nil {
t.Fatal("zero-width grid accepted")
}
form = grid_form()
form = gridForm()
form.Tabs[0].Controls[0].Choices[1].ID = "a"
if _, err := New_session(form); err == nil {
if _, err := NewSession(form, Messages{}); err == nil {
t.Fatal("duplicate values accepted")
}
}
@@ -102,11 +102,11 @@ func TestGridSpeechDetailOptions(t *testing.T) {
{false, true, "Bat. 2 of 7", "Japan. 2 of 2"},
{false, false, "Bat", "Japan"},
} {
session, err := New_session(grid_form())
session, err := NewSession(gridForm(), Messages{})
if err != nil {
t.Fatal(err)
}
session.Configure_grid(trial.coordinates, trial.position)
session.ConfigureGrid(trial.coordinates, trial.position)
session.Handle(Key{Name: "tab"})
if got := session.Handle(Key{Name: "right"}).Speech; got != trial.want {
t.Fatalf("%+v: %q", trial, got)
+7 -7
View File
@@ -38,8 +38,8 @@ func (s *Session) list(control *Control, key Key) Result {
prefix = append(prefix, []rune(key.Text)...)
}
query := strings.ToLower(string(prefix))
for index, item := range s.list_labels(*control) {
if s.list_disabled(*control, index) {
for index, item := range s.listLabels(*control) {
if s.listDisabled(*control, index) {
continue
}
if strings.HasPrefix(strings.ToLower(item), query) {
@@ -50,7 +50,7 @@ func (s *Session) list(control *Control, key Key) Result {
// Reject unmatched input so a typo leaves the last valid search intact.
return Result{Speech: s.messages.T("No matches")}
}
labels := s.list_labels(*control)
labels := s.listLabels(*control)
if key.Name == "enter" {
if control.SelectAction && control.Selected >= 0 && len(control.Choices) > 0 {
return Result{Action: control.Choices[control.Selected].ID}
@@ -83,7 +83,7 @@ func (s *Session) list(control *Control, key Key) Result {
}
for range delta {
for next := control.Selected + step; next >= 0 && next < len(labels); next += step {
if !s.list_disabled(*control, next) {
if !s.listDisabled(*control, next) {
control.Selected = next
break
}
@@ -95,16 +95,16 @@ func (s *Session) list(control *Control, key Key) Result {
}
return Result{Speech: labels[control.Selected]}
}
func (s *Session) list_labels(c Control) []string {
func (s *Session) listLabels(c Control) []string {
if len(c.Choices) == 0 {
return c.Items
}
labels := make([]string, len(c.Choices))
for i, v := range c.Choices {
labels[i] = s.choice_label(v)
labels[i] = s.choiceLabel(v)
}
return labels
}
func (s *Session) list_disabled(c Control, index int) bool {
func (s *Session) listDisabled(c Control, index int) bool {
return len(c.Choices) > 0 && c.Choices[index].Disabled
}
+41
View File
@@ -0,0 +1,41 @@
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
package internalui
import "testing"
func TestDetailedListNamedOnArrival(t *testing.T) {
list := Control{ID: "method", Label: "Method", Kind: List, Choices: []Choice{{ID: "send", Label: "Send"}, {ID: "receive", Label: "Receive"}}}
alone, err := NewSession(Form{Type: FlatForm, Title: "Menu", Detailed: true, Tabs: []Tab{{ID: "form", Label: "Menu", Controls: []Control{list}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
if got := alone.Open(); got != "Menu. Send" {
t.Fatalf("lone list opening = %q", got)
}
if got := alone.Handle(Key{Name: "down"}).Speech; got != "Receive" {
t.Fatalf("row movement = %q", got)
}
if got := alone.Handle(Key{Name: "tab"}).Speech; got != "Receive" {
t.Fatalf("lone list repeated its label on Tab: %q", got)
}
form, err := NewSession(Form{Type: FlatForm, Title: "Example", Detailed: true, Tabs: []Tab{{ID: "form", Label: "Example", Controls: []Control{list, {ID: "code", Label: "Code", Kind: Entry}}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
if got := form.Open(); got != "Example. Method. Send" {
t.Fatalf("form list opening = %q", got)
}
if got := form.Handle(Key{Name: "down"}).Speech; got != "Receive" {
t.Fatalf("row movement repeated the label: %q", got)
}
form.Handle(Key{Name: "tab"})
if got := form.Handle(Key{Name: "tab"}).Speech; got != "Method. Receive" {
t.Fatalf("form list lost its label on Tab: %q", got)
}
plain, _ := NewSession(Form{Type: FlatForm, Title: "Example", Tabs: []Tab{{ID: "form", Label: "Example", Controls: []Control{list, {ID: "code", Label: "Code", Kind: Entry}}}}}, Messages{})
if got := plain.Open(); got != "Example. Method Send" {
t.Fatalf("plain form opening changed: %q", got)
}
}
+2 -2
View File
@@ -7,10 +7,10 @@ package internalui
import "testing"
func TestListPrefixSearch(t *testing.T) {
session, err := New_session(Form{Type: TabForm, Title: "Picker", Tabs: []Tab{{ID: "items", Label: "Items", Controls: []Control{
session, err := NewSession(Form{Type: TabForm, Title: "Picker", Tabs: []Tab{{ID: "items", Label: "Items", Controls: []Control{
{ID: "list", Label: "Item", Kind: List, Items: []string{"Default", "NetworkManager", "Ninjam", "Éclair"}, Action: "choose"},
{ID: "cancel", Label: "Cancel", Kind: Button, Action: "cancel"},
}}}})
}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
+1 -1
View File
@@ -19,7 +19,7 @@ func TestSessionCatalogIsolationAndLiteralGridSearch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
english, err := New_session(form)
english, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
+30 -15
View File
@@ -23,7 +23,22 @@ import (
"unicode/utf8"
)
func (s *Session) Open() string { return s.messages.T(s.form.Title) + ". " + s.Describe() }
func (s *Session) Open() string { return s.messages.T(s.form.Title) + ". " + s.describeArrival() }
// describeArrival names a list when focus lands on it. Detailed forms speak
// only a list's row, which loses its label once the page has other controls.
func (s *Session) describeArrival() string {
speech := s.Describe()
if !s.form.Detailed || s.closed || s.focus < 0 {
return speech
}
tab := s.form.Tabs[s.tab]
control := tab.Controls[s.focus]
if control.Kind != List || control.Selected < 0 || len(tab.Controls) < 2 {
return speech
}
return s.messages.T(control.Label) + ". " + speech
}
func (s *Session) describe() string {
if s.closed {
@@ -50,45 +65,45 @@ func (s *Session) describe() string {
if control.Selected < 0 {
return label
}
return label + " " + s.list_labels(control)[control.Selected]
return label + " " + s.listLabels(control)[control.Selected]
case Grid:
return s.messages.Format("{0} grid. {1}", label, s.describe_grid(control))
return s.messages.Format("{0} grid. {1}", label, s.describeGrid(control))
case ComboBox:
return s.messages.Format("{0} combo box {1}", label, s.describe_choice(control))
return s.messages.Format("{0} combo box {1}", label, s.describeChoice(control))
case Button:
return s.messages.Format("{0} button", label)
}
return label
}
func (s *Session) describe_choice(control Control) string {
func (s *Session) describeChoice(control Control) string {
choice := control.Choices[control.Selected]
text := s.choice_label(choice)
text := s.choiceLabel(choice)
if choice.Unavailable != "" {
text += ". " + s.messages.T("unavailable") + ": " + choice.Unavailable
}
return text
}
func (s *Session) choice_label(choice Choice) string {
func (s *Session) choiceLabel(choice Choice) string {
if choice.Literal {
return choice.Label
}
return s.messages.T(choice.Label)
}
func (s *Session) focused_group() string {
func (s *Session) focusedGroup() string {
if s.focus < 0 {
return ""
}
return s.form.Tabs[s.tab].Controls[s.focus].Group
}
func (s *Session) describe_focus(previousGroup string) string {
if group := s.focused_group(); group != "" && group != previousGroup {
return s.messages.T(group) + ". " + s.Describe()
func (s *Session) describeFocus(previousGroup string) string {
if group := s.focusedGroup(); group != "" && group != previousGroup {
return s.messages.T(group) + ". " + s.describeArrival()
}
return s.Describe()
return s.describeArrival()
}
func (s *Session) Describe() string { return s.Presentation().Speech }
@@ -138,8 +153,8 @@ func (s *Session) Presentation() Result {
switch c.Kind {
case List:
if c.Selected >= 0 {
speech = s.list_labels(c)[c.Selected]
text = s.messages.Format("{0}: {1}, {2} of {3}", label, speech, strconv.Itoa(c.Selected+1), strconv.Itoa(len(s.list_labels(c))))
speech = s.listLabels(c)[c.Selected]
text = s.messages.Format("{0}: {1}, {2} of {3}", label, speech, strconv.Itoa(c.Selected+1), strconv.Itoa(len(s.listLabels(c))))
}
case CheckBox:
state := Mark("unchecked")
@@ -152,7 +167,7 @@ func (s *Session) Presentation() Result {
text = s.messages.Format("{0}, button", label)
speech = text
case Grid:
text = s.describe_grid(c)
text = s.describeGrid(c)
speech = text
}
}
+25 -27
View File
@@ -36,8 +36,6 @@ type Session struct {
closed bool
}
func New_session(form Form) (*Session, error) { return NewSession(form, Messages{}) }
func NewSession(form Form, messages Messages) (*Session, error) {
s := &Session{messages: messages}
if form.Type != TabForm && form.Type != FlatForm {
@@ -122,7 +120,7 @@ func NewSession(form Form, messages Messages) (*Session, error) {
if control.Columns < 1 {
return nil, fmt.Errorf("invalid grid width %q", control.ID)
}
s.filter_grid(control)
s.filterGrid(control)
}
if control.Kind == List && !control.Disabled && len(control.Choices) == 0 && (len(control.Items) == 0 || control.Selected < 0 || control.Selected >= len(control.Items)) {
return nil, fmt.Errorf("invalid list %q", control.ID)
@@ -134,7 +132,7 @@ func NewSession(form Form, messages Messages) (*Session, error) {
if len(form.Tabs) != 1 {
return nil, fmt.Errorf("flat forms require one page")
}
s.move_focus(1)
s.moveFocus(1)
if s.closed || s.focus < 0 {
return nil, fmt.Errorf("form has no enabled controls")
}
@@ -143,8 +141,8 @@ func NewSession(form Form, messages Messages) (*Session, error) {
return s, nil
}
// Configure_grid controls spoken detail without changing grid navigation or filtering.
func (s *Session) Configure_grid(coordinates, position bool) {
// ConfigureGrid controls spoken detail without changing grid navigation or filtering.
func (s *Session) ConfigureGrid(coordinates, position bool) {
s.announceGridCoordinates, s.announceItemPosition = coordinates, position
}
@@ -164,8 +162,8 @@ func (s *Session) Values() map[string]Control {
return values
}
// Focused_control returns an independent value for the host's current action.
func (s *Session) Focused_control() (Control, bool) {
// FocusedControl returns an independent value for the host's current action.
func (s *Session) FocusedControl() (Control, bool) {
if s.closed || s.focus < 0 {
return Control{}, false
}
@@ -175,8 +173,8 @@ func (s *Session) Focused_control() (Control, bool) {
return control, true
}
// Focus_control restores a host's focus after returning from a child dialog.
func (s *Session) Focus_control(tabID, controlID string) bool {
// FocusControl restores a host's focus after returning from a child dialog.
func (s *Session) FocusControl(tabID, controlID string) bool {
if s.closed {
return false
}
@@ -196,9 +194,9 @@ func (s *Session) Focus_control(tabID, controlID string) bool {
return false
}
// Set_choices updates an asynchronous grid without moving keyboard focus or
// SetChoices updates an asynchronous grid without moving keyboard focus or
// discarding its search. Keep the selected word when it still matches.
func (s *Session) Set_choices(tabID, controlID string, choices []Choice) {
func (s *Session) SetChoices(tabID, controlID string, choices []Choice) {
for tabIndex := range s.form.Tabs {
tab := &s.form.Tabs[tabIndex]
if tab.ID != tabID {
@@ -215,8 +213,8 @@ func (s *Session) Set_choices(tabID, controlID string, choices []Choice) {
selected = control.Choices[position].ID
}
control.Choices = append([]Choice(nil), choices...)
s.filter_grid(control)
matches := s.grid_matches(*control)
s.filterGrid(control)
matches := s.gridMatches(*control)
for _, match := range matches {
if control.Choices[match].ID == selected {
control.Selected = match
@@ -231,7 +229,7 @@ func (s *Session) Set_choices(tabID, controlID string, choices []Choice) {
}
}
func (s *Session) move_focus(delta int) {
func (s *Session) moveFocus(delta int) {
if s.focus >= 0 && s.form.Tabs[s.tab].Controls[s.focus].Kind == List {
s.form.Tabs[s.tab].Controls[s.focus].Text = ""
}
@@ -264,9 +262,9 @@ func (s *Session) move_focus(delta int) {
}
}
// Set_hidden changes navigation without rebuilding the form or losing entry
// SetHidden changes navigation without rebuilding the form or losing entry
// edits. If the focused control disappears, move forward to a visible target.
func (s *Session) Set_hidden(tabID, controlID string, hidden bool) {
func (s *Session) SetHidden(tabID, controlID string, hidden bool) {
for tabIndex := range s.form.Tabs {
tab := &s.form.Tabs[tabIndex]
if tab.ID != tabID {
@@ -278,7 +276,7 @@ func (s *Session) Set_hidden(tabID, controlID string, hidden bool) {
}
tab.Controls[index].Hidden = hidden
if hidden && s.tab == tabIndex && s.focus == index {
s.move_focus(1)
s.moveFocus(1)
}
return
}
@@ -294,13 +292,13 @@ func (s *Session) handle(key Key) Result {
return Result{Cancelled: true}
}
if key.Name == "tab" && !key.Ctrl && !key.Alt {
previousGroup := s.focused_group()
previousGroup := s.focusedGroup()
delta := 1
if key.Shift {
delta = -1
}
s.move_focus(delta)
return Result{Speech: s.describe_focus(previousGroup)}
s.moveFocus(delta)
return Result{Speech: s.describeFocus(previousGroup)}
}
if key.Alt {
return Result{}
@@ -323,13 +321,13 @@ func (s *Session) handle(key Key) Result {
case "end":
s.tab = len(s.form.Tabs) - 1
case "enter", "space":
s.move_focus(1)
s.moveFocus(1)
}
if delta != 0 {
s.tab = min(len(s.form.Tabs)-1, max(0, s.tab+delta))
}
if delta != 0 || key.Name == "enter" || key.Name == "space" || key.Name == "home" || key.Name == "end" {
return Result{Speech: s.describe_focus("")}
return Result{Speech: s.describeFocus("")}
}
return Result{}
}
@@ -383,7 +381,7 @@ func (s *Session) handle(key Key) Result {
return Result{}
}
s.list(control, Key{Name: name})
return Result{Speech: s.describe_choice(*control)}
return Result{Speech: s.describeChoice(*control)}
case Button:
if key.Name == "enter" || key.Name == "space" {
return Result{Action: control.Action}
@@ -393,8 +391,8 @@ func (s *Session) handle(key Key) Result {
return Result{Speech: s.Describe()}
}
// Replace_choices updates a dependent combo without changing focus.
func (s *Session) Replace_choices(tabID, controlID string, choices []Choice, selected string) {
// ReplaceChoices updates a dependent combo without changing focus.
func (s *Session) ReplaceChoices(tabID, controlID string, choices []Choice, selected string) {
if len(choices) == 0 {
return
}
@@ -424,7 +422,7 @@ func (s *Session) Replace_choices(tabID, controlID string, choices []Choice, sel
func (s *Session) Handle(key Key) Result {
r := s.handle(key)
if r.Action != "" && r.Action != "cancel" {
c, _ := s.Focused_control()
c, _ := s.FocusedControl()
if !c.SkipValidation {
if problem := s.Validate(); problem != "" {
return Result{Speech: problem, Braille: problem, Cursor: -1}
+8 -8
View File
@@ -24,15 +24,15 @@ import (
func TestInvalidNumberUpDownStepRejected(t *testing.T) {
for _, step := range []float64{-1, math.NaN(), math.Inf(1), math.Inf(-1)} {
form := test_form()
form := testForm()
form.Tabs[0].Controls[1].UpDownStep = step
if _, err := New_session(form); err == nil {
if _, err := NewSession(form, Messages{}); err == nil {
t.Fatalf("invalid Up/Down step %v accepted", step)
}
}
}
func test_form() Form {
func testForm() Form {
return Form{Type: TabForm, Title: "Settings", Tabs: []Tab{
{ID: "speech", Label: "Speech", Controls: []Control{
{ID: "enabled", Label: "Speech enabled", Kind: CheckBox, Checked: true},
@@ -46,8 +46,8 @@ func test_form() Form {
}
func TestTabFormTraversalControlsAndIsolation(t *testing.T) {
form := test_form()
session, err := New_session(form)
form := testForm()
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -134,7 +134,7 @@ func TestTabFormTraversalControlsAndIsolation(t *testing.T) {
func TestListCategoryFormReturnsSelectionWithoutSideEffects(t *testing.T) {
form := Form{Type: TabForm, Title: "Picker", Tabs: []Tab{{ID: "category", Label: "Category", Controls: []Control{{ID: "items", Label: "Items", Kind: List, Items: []string{"First", "Second"}, Action: "choose"}}}}}
session, err := New_session(form)
session, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -154,9 +154,9 @@ func TestListCategoryFormReturnsSelectionWithoutSideEffects(t *testing.T) {
}
func TestUnknownFormTypeRejected(t *testing.T) {
form := test_form()
form := testForm()
form.Type = "unknown"
if _, err := New_session(form); err == nil {
if _, err := NewSession(form, Messages{}); err == nil {
t.Fatal("unknown form type accepted")
}
}
+6 -6
View File
@@ -15,7 +15,7 @@ func TestRichEditorAndValidationInBothLayouts(t *testing.T) {
{ID: "submit", Label: "Connect", Kind: Button, Action: "connect"},
{ID: "cancel", Label: "Cancel", Kind: Button, Action: "cancel"},
}}}}
s, err := New_session(form)
s, err := NewSession(form, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -69,7 +69,7 @@ func TestRichEditorAndValidationInBothLayouts(t *testing.T) {
}
}
func TestPartialSelectionCopyAndWordMovement(t *testing.T) {
s, err := New_session(Form{Type: TabForm, Title: "Edit", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "e", Label: "Entry", Kind: Entry, Text: "one two 猫"}}}}})
s, err := NewSession(Form{Type: TabForm, Title: "Edit", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "e", Label: "Entry", Kind: Entry, Text: "one two 猫"}}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -88,10 +88,10 @@ func TestPartialSelectionCopyAndWordMovement(t *testing.T) {
}
}
func TestNonWrappingListsAndTabs(t *testing.T) {
s, err := New_session(Form{Type: TabForm, Title: "Picker", Tabs: []Tab{
s, err := NewSession(Form{Type: TabForm, Title: "Picker", Tabs: []Tab{
{ID: "a", Label: "First", Controls: []Control{{ID: "list", Label: "List", Kind: List, Items: []string{"One", "Two"}}}},
{ID: "b", Label: "Last"},
}})
}}, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -115,7 +115,7 @@ func TestNonWrappingListsAndTabs(t *testing.T) {
func TestDisabledChoicesAndEmptyEditor(t *testing.T) {
for _, kind := range []Kind{List, ComboBox} {
s, err := New_session(Form{Type: FlatForm, Title: "Choice", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "c", Label: "Choice", Kind: kind, Choices: []Choice{{ID: "a", Label: "Disabled", Disabled: true}, {ID: "b", Label: "Enabled"}, {ID: "c", Label: "Disabled", Disabled: true}}}}}}})
s, err := NewSession(Form{Type: FlatForm, Title: "Choice", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "c", Label: "Choice", Kind: kind, Choices: []Choice{{ID: "a", Label: "Disabled", Disabled: true}, {ID: "b", Label: "Enabled"}, {ID: "c", Label: "Disabled", Disabled: true}}}}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
@@ -126,7 +126,7 @@ func TestDisabledChoicesAndEmptyEditor(t *testing.T) {
}
}
}
s, err := New_session(Form{Type: FlatForm, Title: "Edit", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "e", Label: "Entry", Kind: Entry}}}}})
s, err := NewSession(Form{Type: FlatForm, Title: "Edit", Tabs: []Tab{{ID: "p", Label: "Page", Controls: []Control{{ID: "e", Label: "Entry", Kind: Entry}}}}}, Messages{})
if err != nil {
t.Fatal(err)
}
+8 -8
View File
@@ -6,21 +6,21 @@ package internalui
import "testing"
func TestHiddenControlsRetainDraftAndRemainOutsideBothTabOrders(t *testing.T) {
session, err := New_session(test_form())
session, err := NewSession(testForm(), Messages{})
if err != nil {
t.Fatal(err)
}
session.Set_hidden("speech", "range", true)
session.SetHidden("speech", "range", true)
for _, want := range []string{"enabled", "text", "ok", "cancel", ""} {
session.Handle(Key{Name: "tab"})
got, focused := session.Focused_control()
got, focused := session.FocusedControl()
if focused != (want != "") || got.ID != want {
t.Fatal(got, want)
}
}
for _, want := range []string{"cancel", "ok", "text", "enabled", ""} {
session.Handle(Key{Name: "tab", Shift: true})
got, focused := session.Focused_control()
got, focused := session.FocusedControl()
if focused != (want != "") || got.ID != want {
t.Fatal(got, want)
}
@@ -29,17 +29,17 @@ func TestHiddenControlsRetainDraftAndRemainOutsideBothTabOrders(t *testing.T) {
session.Handle(Key{Name: "tab"})
session.Handle(Key{Ctrl: true, Text: "a"})
session.Handle(Key{Text: "draft"})
session.Set_hidden("speech", "text", true)
if got, _ := session.Focused_control(); got.ID != "ok" {
session.SetHidden("speech", "text", true)
if got, _ := session.FocusedControl(); got.ID != "ok" {
t.Fatal("hidden focus retained", got)
}
session.Set_hidden("speech", "text", false)
session.SetHidden("speech", "text", false)
session.Handle(Key{Name: "tab", Shift: true})
if got := session.Values()["speech.text"]; got.Text != "draft" || got.Hidden {
t.Fatal(got)
}
for _, id := range []string{"enabled", "range", "text", "ok", "cancel"} {
session.Set_hidden("speech", id, true)
session.SetHidden("speech", id, true)
}
if got := session.Handle(Key{Name: "tab"}).Speech; got != "Speech tab" {
t.Fatal("all-hidden tab trapped focus", got)