Use shared internal UI toolkit v0.2.0
This commit is contained in:
1 parent
c1ca5746d0
commit
3d515b8810
11 files changed
+131
-831
No files matched your search
@@ -18,7 +18,10 @@ Cthulhu is a Go Linux screen reader. `cmd/` contains executables; `internal/` ow
|
||||
|
||||
Reader-independent Go code shared with Fenrir lives in
|
||||
`git.stormux.org/storm/screenreader-common`; check it before duplicating pure
|
||||
data or behavior. The emoji catalog is shared there. Keep Cthulhu's picker form,
|
||||
data or behavior. The emoji catalog and internal interaction toolkit are shared there.
|
||||
`internal/interaction` is a compatibility adapter to the common `internalui`
|
||||
toolkit; core rich forms use `internalUI.OpenUI`. Change control behavior and
|
||||
its model tests in the common module. Keep Cthulhu's picker form,
|
||||
AT-SPI insertion, clipboard choice, speech, and Braille integration here. For
|
||||
shared changes, test the common package, tag a version, and update the pinned
|
||||
module in `go.mod`; keep local `go.work` or `replace` paths out of commits.
|
||||
|
||||
@@ -8,4 +8,4 @@ require github.com/godbus/dbus/v5 v5.1.0
|
||||
|
||||
require github.com/gorilla/websocket v1.5.3
|
||||
|
||||
require git.stormux.org/storm/screenreader-common v0.1.1
|
||||
require git.stormux.org/storm/screenreader-common v0.2.0
|
||||
@@ -1,5 +1,7 @@
|
||||
git.stormux.org/storm/screenreader-common v0.1.1 h1:r/qcWUv4ODZ+XzXw1J+qe4hVnyr7MqH0W7L5tp6XOcE=
|
||||
git.stormux.org/storm/screenreader-common v0.1.1/go.mod h1:iLmD0KXRvR4ar3U80JMwbaNUOyR82/gY7xHUY+zeIoY=
|
||||
git.stormux.org/storm/screenreader-common v0.2.0 h1:PbGwyIi/nT0qwvscje4pvIIsAxDLBYwLV1eTXT36hxk=
|
||||
git.stormux.org/storm/screenreader-common v0.2.0/go.mod h1:iLmD0KXRvR4ar3U80JMwbaNUOyR82/gY7xHUY+zeIoY=
|
||||
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
|
||||
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
|
||||
@@ -7,6 +7,7 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
commonui "git.stormux.org/storm/screenreader-common/internalui"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
@@ -32,6 +33,7 @@ type accessibleActionSource interface {
|
||||
PerformAccessibleAction(context.Context, atspi.ActionTarget, string) error
|
||||
}
|
||||
type internalUIResult struct {
|
||||
Controls map[string]commonui.Control // Submission only; may contain secrets.
|
||||
Activated string
|
||||
Values map[string]interaction.Value
|
||||
Cancelled bool
|
||||
@@ -85,6 +87,19 @@ func (ui *internalUI) open_form(ctx context.Context, form interaction.Form, loca
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ui.open_session(ctx, session, localOnly)
|
||||
}
|
||||
|
||||
// OpenUI is the core-only rich form API. localOnly preserves the same input
|
||||
// authorization boundary as OpenLocal; rich controls do not extend the wire API.
|
||||
func (ui *internalUI) OpenUI(ctx context.Context, form commonui.Form, localOnly bool) (<-chan internalUIResult, error) {
|
||||
session, err := interaction.NewUnifiedSession(form, commonui.Messages{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ui.open_session(ctx, session, localOnly)
|
||||
}
|
||||
func (ui *internalUI) open_session(ctx context.Context, session *interaction.Session, localOnly bool) (<-chan internalUIResult, error) {
|
||||
request := internalUIRequest{ctx: ctx, session: session, result: make(chan internalUIResult, 1), ready: make(chan error, 1), localOnly: localOnly}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -157,7 +172,9 @@ func (ui *internalUI) run(ctx context.Context) {
|
||||
var requestDone <-chan struct{}
|
||||
finish := func(reply internalUIResult) error {
|
||||
if emojiActive && session != nil {
|
||||
copyEmoji = session.Values()["copy"].Checked
|
||||
if values := session.Values(); values != nil {
|
||||
copyEmoji = values["copy"].Checked
|
||||
}
|
||||
}
|
||||
emojiActive = false
|
||||
if session != nil {
|
||||
@@ -177,7 +194,7 @@ func (ui *internalUI) run(ctx context.Context) {
|
||||
reply.Err = closeErr
|
||||
}
|
||||
if reply.Cancelled || reply.Err != nil {
|
||||
reply.Activated, reply.Values = "", nil
|
||||
reply.Activated, reply.Values, reply.Controls = "", nil, nil
|
||||
}
|
||||
result <- reply
|
||||
close(result)
|
||||
@@ -339,6 +356,9 @@ func (ui *internalUI) run(ctx context.Context) {
|
||||
continue
|
||||
}
|
||||
key := internal_ui_event(event)
|
||||
if emojiActive {
|
||||
copyEmoji = session.Values()["copy"].Checked
|
||||
}
|
||||
effect := session.Handle(key)
|
||||
if effect.Cancelled {
|
||||
_ = finish(internalUIResult{Cancelled: true})
|
||||
@@ -394,7 +414,8 @@ func (ui *internalUI) run(ctx context.Context) {
|
||||
}
|
||||
actionTarget := target
|
||||
selectedList := documentList
|
||||
closeErr := finish(internalUIResult{Activated: effect.Activated, Values: values})
|
||||
controls := session.ControlValues()
|
||||
closeErr := finish(internalUIResult{Activated: effect.Activated, Values: values, Controls: controls})
|
||||
if len(selectedList.Entries) > 0 && closeErr == nil && !ui.gate.Locked() && !ui.gate.Suspended() {
|
||||
index, err := strconv.Atoi(effect.Activated)
|
||||
if err == nil {
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestInternalUISecretOutputAndLifetime(t *testing.T) {
|
||||
if got.Err != nil || got.Cancelled || got.Activated != "key" || got.Values["key"].Text != "fixture猫" {
|
||||
t.Fatal("secret not delivered on successful submission")
|
||||
}
|
||||
} else if got.Values != nil || got.Activated != "" || !got.Cancelled && got.Err == nil {
|
||||
} else if got.Values != nil || got.Controls != nil || got.Activated != "" || !got.Cancelled && got.Err == nil {
|
||||
t.Fatal("failed or cancelled form delivered secret values")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.stormux.org/storm/cthulhu/internal/input"
|
||||
common "git.stormux.org/storm/screenreader-common/internalui"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInternalUIRichFormUsesExistingCaptureAndTypedValues(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
keys := &uiTestKeys{}
|
||||
output := &ocrTestOutput{speech: make(chan speechRequest, 16)}
|
||||
ui := &internalUI{keys: keys, speech: output, gate: &focusGate{}, events: make(chan input.CommandEvent, 16), requests: make(chan internalUIRequest, 1), done: make(chan struct{})}
|
||||
go ui.run(ctx)
|
||||
defer func() { cancel(); <-ui.done }()
|
||||
form := common.Form{Type: common.TabForm, Title: "Settings", Tabs: []common.Tab{{ID: "page", Label: "Page", Controls: []common.Control{
|
||||
{ID: "amount", Label: "Amount", Kind: common.Number, Number: 2, Min: 0, Max: 10, Step: 0.5},
|
||||
{ID: "ok", Label: "OK", Kind: common.Button, Action: "save"},
|
||||
}}}}
|
||||
// An unsupported local-only backend must still refuse authorization.
|
||||
if _, err := ui.OpenUI(ctx, form, true); err == nil {
|
||||
t.Fatal("local-only capture silently downgraded")
|
||||
}
|
||||
result, err := ui.OpenUI(ctx, form, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, symbol := range []uint32{0xff09, 0xff52, 0xff09, 0xff0d} {
|
||||
ui.Handle(input.CommandEvent{Command: input.InternalUIKey, UISession: 1, Chord: input.CommandChord{Symbol: symbol}})
|
||||
}
|
||||
select {
|
||||
case got := <-result:
|
||||
if got.Err != nil || got.Cancelled || got.Activated != "save" || got.Controls["page.amount"].Number != 3 || keys.InternalUISession() != 0 {
|
||||
t.Fatal("rich form submission or release failed")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("rich form timed out")
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UnmarshalJSON keeps a malformed secret flag from silently becoming an
|
||||
// ordinary entry (encoding/json otherwise accepts null for a boolean). The
|
||||
// wire decoder separately rejects duplicate keys and bounds nesting/framing.
|
||||
func (element *Element) UnmarshalJSON(data []byte) error {
|
||||
type plain Element
|
||||
var value plain
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return err
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
for name, raw := range fields {
|
||||
if strings.EqualFold(name, "secret") {
|
||||
flag := string(bytes.TrimSpace(raw))
|
||||
if name != "secret" || flag != "true" && flag != "false" {
|
||||
return errors.New("secret must be a boolean")
|
||||
}
|
||||
}
|
||||
}
|
||||
*element = Element(value)
|
||||
return nil
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// GridTab holds one category. Filtering and selection belong to each session.
|
||||
type GridTab struct {
|
||||
ID, Label string
|
||||
Items []Item
|
||||
}
|
||||
type gridPage struct {
|
||||
GridTab
|
||||
query string
|
||||
selected int
|
||||
}
|
||||
type gridSession struct {
|
||||
pages []gridPage
|
||||
tab, focus, columns int // focus: 0 tab strip, 1 grid, 2 checkbox
|
||||
checkbox Element
|
||||
closed bool
|
||||
}
|
||||
|
||||
// NewGridSession is a core tabbed grid with a shared checkbox. External forms
|
||||
// keep their existing protocol and limits. Navigation follows Fenrir's picker.
|
||||
func NewGridSession(title string, tabs []GridTab, columns int, checkbox Element) (*Session, error) {
|
||||
if title == "" || len(tabs) == 0 || columns < 1 || columns > 64 || checkbox.Kind != CheckBox || checkbox.ID == "" || checkbox.Label == "" {
|
||||
return nil, errors.New("invalid tabbed grid")
|
||||
}
|
||||
grid := &gridSession{columns: columns, checkbox: checkbox}
|
||||
ids := map[string]bool{}
|
||||
for _, tab := range tabs {
|
||||
if tab.ID == "" || tab.Label == "" || ids[tab.ID] {
|
||||
return nil, errors.New("invalid grid tab")
|
||||
}
|
||||
ids[tab.ID] = true
|
||||
items := map[string]bool{}
|
||||
for _, item := range tab.Items {
|
||||
if item.ID == "" || item.Label == "" || items[item.ID] {
|
||||
return nil, errors.New("invalid grid item")
|
||||
}
|
||||
items[item.ID] = true
|
||||
}
|
||||
tab.Items = append([]Item(nil), tab.Items...)
|
||||
page := gridPage{GridTab: tab}
|
||||
page.filter()
|
||||
grid.pages = append(grid.pages, page)
|
||||
}
|
||||
return &Session{title: title, grid: grid}, nil
|
||||
}
|
||||
func (page *gridPage) matches() []int {
|
||||
var result []int
|
||||
for index, item := range page.Items {
|
||||
if !item.Disabled && strings.HasPrefix(strings.ToLower(item.Label), strings.ToLower(page.query)) {
|
||||
result = append(result, index)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func (page *gridPage) filter() {
|
||||
page.selected = -1
|
||||
if matches := page.matches(); len(matches) > 0 {
|
||||
page.selected = matches[0]
|
||||
}
|
||||
}
|
||||
func (grid *gridSession) values() map[string]Value {
|
||||
page := &grid.pages[grid.tab]
|
||||
values := map[string]Value{grid.checkbox.ID: {Checked: grid.checkbox.Checked}}
|
||||
if grid.focus == 1 && page.selected >= 0 {
|
||||
values["grid"] = Value{Selected: page.Items[page.selected].ID, Text: page.Items[page.selected].Label}
|
||||
}
|
||||
return values
|
||||
}
|
||||
func (grid *gridSession) presentation() Presentation {
|
||||
if grid.closed {
|
||||
return Presentation{}
|
||||
}
|
||||
page := &grid.pages[grid.tab]
|
||||
text := page.Label + " tab"
|
||||
switch grid.focus {
|
||||
case 1:
|
||||
text = "No matches"
|
||||
for position, index := range page.matches() {
|
||||
if index == page.selected {
|
||||
text = fmt.Sprintf("%s. row %d, column %d. %d of %d", page.Items[index].Label, position/grid.columns+1, position%grid.columns+1, position+1, len(page.matches()))
|
||||
break
|
||||
}
|
||||
}
|
||||
case 2:
|
||||
text = grid.checkbox.Label + ", unchecked"
|
||||
if grid.checkbox.Checked {
|
||||
text = grid.checkbox.Label + ", checked"
|
||||
}
|
||||
}
|
||||
return Presentation{Speech: text, Braille: text, Cursor: -1}
|
||||
}
|
||||
func (grid *gridSession) handle(event Event) Effect {
|
||||
if grid.closed {
|
||||
return Effect{}
|
||||
}
|
||||
if event.Key == Escape {
|
||||
grid.closed = true
|
||||
return Effect{Cancelled: true}
|
||||
}
|
||||
if event.Alt {
|
||||
return Effect{}
|
||||
}
|
||||
if event.Key == Tab && !event.Control {
|
||||
delta := 1
|
||||
if event.Shift {
|
||||
delta = -1
|
||||
}
|
||||
grid.focus = (grid.focus + delta + 3) % 3
|
||||
return Effect{Presentation: grid.presentation()}
|
||||
}
|
||||
page := &grid.pages[grid.tab]
|
||||
if grid.focus == 0 {
|
||||
if event.Control {
|
||||
return Effect{}
|
||||
}
|
||||
switch event.Key {
|
||||
case Up, Left:
|
||||
grid.tab = (grid.tab + len(grid.pages) - 1) % len(grid.pages)
|
||||
case Down, Right:
|
||||
grid.tab = (grid.tab + 1) % len(grid.pages)
|
||||
case Enter:
|
||||
grid.focus = 1
|
||||
case Text:
|
||||
if event.Text != " " {
|
||||
return Effect{}
|
||||
}
|
||||
grid.focus = 1
|
||||
default:
|
||||
return Effect{}
|
||||
}
|
||||
return Effect{Presentation: grid.presentation()}
|
||||
}
|
||||
if grid.focus == 2 {
|
||||
if !event.Control && (event.Key == Enter || event.Key == Text && event.Text == " ") {
|
||||
grid.checkbox.Checked = !grid.checkbox.Checked
|
||||
return Effect{Presentation: grid.presentation()}
|
||||
}
|
||||
return Effect{}
|
||||
}
|
||||
if event.Control && event.Key != Backspace {
|
||||
return Effect{}
|
||||
}
|
||||
if event.Key == Backspace || event.Key == Text && event.Text != "" {
|
||||
query := []rune(page.query)
|
||||
if event.Key == Backspace {
|
||||
if event.Control {
|
||||
query = nil
|
||||
} else if len(query) > 0 {
|
||||
query = query[:len(query)-1]
|
||||
}
|
||||
} else {
|
||||
if !utf8.ValidString(event.Text) || len(query)+len([]rune(event.Text)) > 4096 {
|
||||
return Effect{}
|
||||
}
|
||||
for _, character := range event.Text {
|
||||
if !unicode.IsPrint(character) {
|
||||
return Effect{}
|
||||
}
|
||||
}
|
||||
query = append(query, []rune(event.Text)...)
|
||||
}
|
||||
page.query = string(query)
|
||||
page.filter()
|
||||
output := grid.presentation()
|
||||
prefix := "Search cleared"
|
||||
if page.query != "" {
|
||||
prefix = "Search " + page.query
|
||||
}
|
||||
output.Speech = prefix + ". " + output.Speech
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
matches := page.matches()
|
||||
if len(matches) == 0 {
|
||||
return Effect{Presentation: grid.presentation()}
|
||||
}
|
||||
position := 0
|
||||
for current, index := range matches {
|
||||
if index == page.selected {
|
||||
position = current
|
||||
break
|
||||
}
|
||||
}
|
||||
switch event.Key {
|
||||
case Left:
|
||||
position = max(0, position-1)
|
||||
case Right:
|
||||
position = min(len(matches)-1, position+1)
|
||||
case Up:
|
||||
if position >= grid.columns {
|
||||
position -= grid.columns
|
||||
}
|
||||
case Down:
|
||||
if position/grid.columns < (len(matches)-1)/grid.columns {
|
||||
position = min(len(matches)-1, position+grid.columns)
|
||||
}
|
||||
case Home:
|
||||
position = 0
|
||||
case End:
|
||||
position = len(matches) - 1
|
||||
case PageUp:
|
||||
position = max(0, position-grid.columns*5)
|
||||
case PageDown:
|
||||
position = min(len(matches)-1, position+grid.columns*5)
|
||||
case Enter:
|
||||
return Effect{Activated: "select-grid-item"}
|
||||
default:
|
||||
return Effect{}
|
||||
}
|
||||
page.selected = matches[position]
|
||||
return Effect{Presentation: grid.presentation()}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func TestSecretEntryCloseAndValidation(t *testing.T) {
|
||||
session.Close()
|
||||
}
|
||||
session.Close()
|
||||
if session.Values() != nil || session.elements[0].element.Text != "" || session.Presentation() != (Presentation{}) || session.Insert("more") != (Effect{}) {
|
||||
if session.Values() != nil || session.Presentation() != (Presentation{}) || session.Insert("more") != (Effect{}) {
|
||||
t.Fatal("closed editor retained or accepted a secret")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,561 +0,0 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// Authors: Storm Dragon, Angus Kola
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Package interaction implements focus-free controls. It owns no keyboard,
|
||||
// accessibility objects, output services or callbacks; the reader owns those.
|
||||
package interaction
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Kind uint8
|
||||
|
||||
const (
|
||||
List Kind = iota + 1
|
||||
Entry
|
||||
CheckBox
|
||||
Button
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
}
|
||||
type Element struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Kind Kind `json:"kind"`
|
||||
Items []Item `json:"items,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Checked bool `json:"checked,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Required bool `json:"required,omitempty"`
|
||||
MaxLength int `json:"max_length,omitempty"`
|
||||
// Secret is supported only on transient entries, never preference pages.
|
||||
Secret bool `json:"secret,omitempty"`
|
||||
}
|
||||
type Form struct {
|
||||
Title string `json:"title"`
|
||||
Elements []Element `json:"elements"`
|
||||
}
|
||||
type Key uint8
|
||||
|
||||
const (
|
||||
Text Key = iota
|
||||
Up
|
||||
Down
|
||||
Left
|
||||
Right
|
||||
Home
|
||||
End
|
||||
PageUp
|
||||
PageDown
|
||||
Tab
|
||||
Enter
|
||||
Escape
|
||||
Backspace
|
||||
Delete
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Key Key
|
||||
Text string
|
||||
Shift, Control, Alt bool
|
||||
}
|
||||
type Presentation struct {
|
||||
Speech, Braille string
|
||||
Cursor int
|
||||
}
|
||||
|
||||
// Effect describes a host operation. Activation does not close the form: the
|
||||
// caller can validate, replace it with another form, or finish the interaction.
|
||||
type Effect struct {
|
||||
Presentation
|
||||
Activated string
|
||||
Cancelled, Paste bool
|
||||
Copy *string
|
||||
}
|
||||
type Value struct {
|
||||
Text string `json:"text,omitempty"`
|
||||
Selected string `json:"selected,omitempty"`
|
||||
Checked bool `json:"checked,omitempty"`
|
||||
}
|
||||
type elementState struct {
|
||||
element Element
|
||||
selected, caret, anchor int
|
||||
prefix string
|
||||
}
|
||||
type Session struct {
|
||||
grid *gridSession
|
||||
title string
|
||||
elements []elementState
|
||||
focus int
|
||||
closed bool
|
||||
}
|
||||
|
||||
const maxTextLength = 8192
|
||||
|
||||
func NewSession(form Form) (*Session, error) {
|
||||
return new_session(form, 256, 64)
|
||||
}
|
||||
|
||||
// ValidatePreferences shares control validation with internal forms, while
|
||||
// allowing a native, scrolling page to contain several full voice editors.
|
||||
func ValidatePreferences(form Form) error {
|
||||
for _, element := range form.Elements {
|
||||
if element.Secret {
|
||||
return errors.New("secret entries are not supported in preferences")
|
||||
}
|
||||
}
|
||||
_, err := new_session(form, 256, 256)
|
||||
return err
|
||||
}
|
||||
|
||||
// NewNavigationSession is for core document lists. External forms retain
|
||||
// their smaller protocol limit. The selected row follows the reading caret.
|
||||
func NewNavigationSession(title string, items []Item, selected int) (*Session, error) {
|
||||
session, err := new_session(Form{Title: title, Elements: []Element{{ID: "items", Label: title, Kind: List, Items: items}}}, 65536, 64)
|
||||
if err == nil && selected >= 0 && selected < len(items) && !items[selected].Disabled {
|
||||
session.elements[0].selected = selected
|
||||
}
|
||||
return session, err
|
||||
}
|
||||
|
||||
func new_session(form Form, maxItems, maxElements int) (*Session, error) {
|
||||
if form.Title == "" || len(form.Title) > 1024 || len(form.Elements) == 0 || len(form.Elements) > maxElements {
|
||||
return nil, fmt.Errorf("a titled form with 1 to %d controls is required", maxElements)
|
||||
}
|
||||
session := &Session{title: form.Title, focus: -1}
|
||||
ids := map[string]bool{}
|
||||
for _, element := range form.Elements {
|
||||
if element.ID == "" || len(element.ID) > 1024 || element.Label == "" || len(element.Label) > 1024 || ids[element.ID] || element.Kind < List || element.Kind > Button {
|
||||
return nil, errors.New("controls need unique IDs, labels and supported kinds")
|
||||
}
|
||||
ids[element.ID] = true
|
||||
if element.Secret && element.Kind != Entry {
|
||||
return nil, errors.New("only entries may be secret")
|
||||
}
|
||||
if element.MaxLength == 0 {
|
||||
element.MaxLength = maxTextLength
|
||||
}
|
||||
if element.MaxLength < 1 || element.MaxLength > maxTextLength || len([]rune(element.Text)) > element.MaxLength {
|
||||
return nil, errors.New("invalid entry length")
|
||||
}
|
||||
if !utf8.ValidString(element.Text) || strings.ContainsAny(element.Text, "\r\n\x00") {
|
||||
return nil, errors.New("entries must contain single-line text")
|
||||
}
|
||||
for _, character := range element.Text {
|
||||
if unicode.IsControl(character) {
|
||||
return nil, errors.New("entries must contain single-line text")
|
||||
}
|
||||
}
|
||||
element.Items = append([]Item(nil), element.Items...)
|
||||
state := elementState{element: element, selected: -1, caret: len([]rune(element.Text)), anchor: len([]rune(element.Text))}
|
||||
itemIDs := map[string]bool{}
|
||||
if len(element.Items) > maxItems {
|
||||
return nil, errors.New("too many list items")
|
||||
}
|
||||
for index, item := range element.Items {
|
||||
if item.ID == "" || len(item.ID) > 1024 || item.Label == "" || len(item.Label) > 1024 || itemIDs[item.ID] {
|
||||
return nil, errors.New("list items need unique IDs and labels")
|
||||
}
|
||||
itemIDs[item.ID] = true
|
||||
if state.selected < 0 && !item.Disabled {
|
||||
state.selected = index
|
||||
}
|
||||
}
|
||||
if element.Kind == List && state.selected < 0 {
|
||||
state.element.Disabled = true
|
||||
}
|
||||
session.elements = append(session.elements, state)
|
||||
if session.focus < 0 && !state.element.Disabled {
|
||||
session.focus = len(session.elements) - 1
|
||||
}
|
||||
}
|
||||
if session.focus < 0 {
|
||||
return nil, errors.New("form has no enabled controls")
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (session *Session) Presentation() Presentation {
|
||||
if session.grid != nil {
|
||||
return session.grid.presentation()
|
||||
}
|
||||
if session.closed {
|
||||
return Presentation{}
|
||||
}
|
||||
state := &session.elements[session.focus]
|
||||
element := state.element
|
||||
text := element.Label
|
||||
cursor := -1
|
||||
switch element.Kind {
|
||||
case List:
|
||||
text += fmt.Sprintf(": %s, %d of %d", element.Items[state.selected].Label, state.selected+1, len(element.Items))
|
||||
case Entry:
|
||||
prefix, value := ", edit: ", element.Text
|
||||
if element.Secret {
|
||||
prefix, value = ", password edit: ", strings.Repeat("*", utf8.RuneCountInString(element.Text))
|
||||
}
|
||||
text += prefix + value
|
||||
cursor = len([]rune(element.Label+prefix)) + state.caret
|
||||
if state.caret != state.anchor {
|
||||
text += fmt.Sprintf(" (selected %d characters)", abs(state.caret-state.anchor))
|
||||
}
|
||||
case CheckBox:
|
||||
if element.Checked {
|
||||
text += ", checked"
|
||||
} else {
|
||||
text += ", unchecked"
|
||||
}
|
||||
case Button:
|
||||
text += ", button"
|
||||
}
|
||||
speech := text
|
||||
if element.Secret {
|
||||
speech = element.Label + ", password edit, " + characterCount(utf8.RuneCountInString(element.Text))
|
||||
}
|
||||
if element.Kind == List {
|
||||
speech = element.Items[state.selected].Label
|
||||
}
|
||||
return Presentation{Speech: speech, Braille: text, Cursor: cursor}
|
||||
}
|
||||
|
||||
func (session *Session) Opening() Presentation {
|
||||
if session.grid != nil {
|
||||
output := session.grid.presentation()
|
||||
output.Speech = session.title + ". " + output.Speech
|
||||
return output
|
||||
}
|
||||
output := session.Presentation()
|
||||
if !session.closed && len(session.elements) > 1 && session.elements[session.focus].element.Kind == List {
|
||||
output.Speech = session.elements[session.focus].element.Label + ". " + output.Speech
|
||||
}
|
||||
output.Speech = session.title + ". " + output.Speech
|
||||
return output
|
||||
}
|
||||
|
||||
// Values includes plaintext secrets for the submitting caller. It must never
|
||||
// be used for presentation, logging or generic settings persistence.
|
||||
func (session *Session) Values() map[string]Value {
|
||||
if session.closed {
|
||||
return nil
|
||||
}
|
||||
if session.grid != nil {
|
||||
return session.grid.values()
|
||||
}
|
||||
values := make(map[string]Value, len(session.elements))
|
||||
for _, state := range session.elements {
|
||||
value := Value{Text: state.element.Text, Checked: state.element.Checked}
|
||||
if state.element.Kind == List && state.selected >= 0 {
|
||||
value.Selected = state.element.Items[state.selected].ID
|
||||
}
|
||||
values[state.element.ID] = value
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// Close drops the editor's references to secrets on submission, cancellation
|
||||
// or capture revocation. Go strings do not provide guaranteed memory erasure.
|
||||
func (session *Session) Close() {
|
||||
session.closed = true
|
||||
for index := range session.elements {
|
||||
if session.elements[index].element.Secret {
|
||||
session.elements[index].element.Text = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (session *Session) Handle(event Event) Effect {
|
||||
if session.grid != nil {
|
||||
return session.grid.handle(event)
|
||||
}
|
||||
if session.closed {
|
||||
return Effect{}
|
||||
}
|
||||
if event.Key == Escape {
|
||||
session.Close()
|
||||
return Effect{Cancelled: true}
|
||||
}
|
||||
if event.Key == Tab {
|
||||
step := 1
|
||||
if event.Shift {
|
||||
step = -1
|
||||
}
|
||||
for range len(session.elements) {
|
||||
session.focus = (session.focus + step + len(session.elements)) % len(session.elements)
|
||||
if !session.elements[session.focus].element.Disabled {
|
||||
break
|
||||
}
|
||||
}
|
||||
output := session.Presentation()
|
||||
// A list in a multi-control form needs its field label when Tab enters
|
||||
// it. A menu's only control uses its opening title instead.
|
||||
state := &session.elements[session.focus]
|
||||
if len(session.elements) > 1 && state.element.Kind == List {
|
||||
output.Speech = state.element.Label + ". " + output.Speech
|
||||
}
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
state := &session.elements[session.focus]
|
||||
if event.Alt {
|
||||
return Effect{}
|
||||
}
|
||||
if event.Key == Enter || state.element.Kind == Button && event.Key == Text && event.Text == " " && !event.Control {
|
||||
for _, candidate := range session.elements {
|
||||
if candidate.element.Kind == Entry && candidate.element.Required && !candidate.element.Disabled && strings.TrimSpace(candidate.element.Text) == "" {
|
||||
return Effect{Presentation: Presentation{Speech: candidate.element.Label + " is required", Braille: candidate.element.Label + " is required", Cursor: -1}}
|
||||
}
|
||||
}
|
||||
id := state.element.ID
|
||||
if state.element.Kind == List {
|
||||
id = state.element.Items[state.selected].ID
|
||||
}
|
||||
return Effect{Activated: id}
|
||||
}
|
||||
switch state.element.Kind {
|
||||
case List:
|
||||
step := 0
|
||||
switch event.Key {
|
||||
case Up:
|
||||
step = -1
|
||||
case Down:
|
||||
step = 1
|
||||
case PageUp:
|
||||
step = -10
|
||||
case PageDown:
|
||||
step = 10
|
||||
case Home:
|
||||
state.selected = 0
|
||||
step = 1
|
||||
case End:
|
||||
state.selected = len(state.element.Items) - 1
|
||||
step = -1
|
||||
case Text:
|
||||
if event.Control || event.Text == "" {
|
||||
return Effect{}
|
||||
}
|
||||
prefix := state.prefix + strings.ToLower(event.Text)
|
||||
if !state.find_prefix(prefix) {
|
||||
prefix = strings.ToLower(event.Text)
|
||||
state.find_prefix(prefix)
|
||||
}
|
||||
state.prefix = prefix
|
||||
return Effect{Presentation: session.Presentation()}
|
||||
default:
|
||||
return Effect{}
|
||||
}
|
||||
state.prefix = ""
|
||||
if event.Key == Home || event.Key == End {
|
||||
for state.element.Items[state.selected].Disabled {
|
||||
state.selected += step
|
||||
}
|
||||
} else {
|
||||
for count := 0; count < abs(step); count++ {
|
||||
previous := state.selected
|
||||
for next := previous + sign(step); next >= 0 && next < len(state.element.Items); next += sign(step) {
|
||||
if !state.element.Items[next].Disabled {
|
||||
state.selected = next
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case CheckBox:
|
||||
if event.Key != Text || event.Text != " " || event.Control {
|
||||
return Effect{}
|
||||
}
|
||||
state.element.Checked = !state.element.Checked
|
||||
case Button:
|
||||
return Effect{}
|
||||
case Entry:
|
||||
return session.edit(event)
|
||||
}
|
||||
return Effect{Presentation: session.Presentation()}
|
||||
}
|
||||
|
||||
func (state *elementState) find_prefix(prefix string) bool {
|
||||
for offset := 1; offset <= len(state.element.Items); offset++ {
|
||||
index := (state.selected + offset) % len(state.element.Items)
|
||||
item := state.element.Items[index]
|
||||
if !item.Disabled && strings.HasPrefix(strings.ToLower(item.Label), prefix) {
|
||||
state.selected = index
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func sign(value int) int {
|
||||
if value < 0 {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
func abs(value int) int {
|
||||
if value < 0 {
|
||||
return -value
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func characterCount(count int) string {
|
||||
if count == 1 {
|
||||
return "1 character"
|
||||
}
|
||||
return fmt.Sprintf("%d characters", count)
|
||||
}
|
||||
|
||||
func (session *Session) edit(event Event) Effect {
|
||||
state := &session.elements[session.focus]
|
||||
characters := []rune(state.element.Text)
|
||||
start, end := min(state.caret, state.anchor), max(state.caret, state.anchor)
|
||||
if event.Control && event.Key == Text {
|
||||
switch strings.ToLower(event.Text) {
|
||||
case "a":
|
||||
state.anchor, state.caret = 0, len(characters)
|
||||
output := session.Presentation()
|
||||
output.Speech = "Selected all"
|
||||
return Effect{Presentation: output}
|
||||
case "v":
|
||||
return Effect{Paste: true}
|
||||
case "c", "x":
|
||||
if state.element.Secret {
|
||||
output := session.Presentation()
|
||||
output.Speech = "Copy and cut are unavailable for password fields"
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
if start == end {
|
||||
return Effect{}
|
||||
}
|
||||
text := string(characters[start:end])
|
||||
// Cutting is completed by the host only after clipboard write succeeds.
|
||||
return Effect{Copy: &text}
|
||||
default:
|
||||
return Effect{}
|
||||
}
|
||||
}
|
||||
if event.Key == Left || event.Key == Right || event.Key == Home || event.Key == End {
|
||||
next := state.caret
|
||||
switch event.Key {
|
||||
case Left:
|
||||
if !event.Shift && start != end {
|
||||
next = start
|
||||
} else {
|
||||
next = max(0, next-1)
|
||||
if event.Control {
|
||||
for next > 0 && unicode.IsSpace(characters[next]) {
|
||||
next--
|
||||
}
|
||||
for next > 0 && !unicode.IsSpace(characters[next-1]) {
|
||||
next--
|
||||
}
|
||||
}
|
||||
}
|
||||
case Right:
|
||||
if !event.Shift && start != end {
|
||||
next = end
|
||||
} else {
|
||||
next = min(len(characters), next+1)
|
||||
if event.Control {
|
||||
for next < len(characters) && !unicode.IsSpace(characters[next]) {
|
||||
next++
|
||||
}
|
||||
for next < len(characters) && unicode.IsSpace(characters[next]) {
|
||||
next++
|
||||
}
|
||||
}
|
||||
}
|
||||
case Home:
|
||||
next = 0
|
||||
case End:
|
||||
next = len(characters)
|
||||
}
|
||||
state.caret = next
|
||||
if !event.Shift {
|
||||
state.anchor = next
|
||||
}
|
||||
output := session.Presentation()
|
||||
if next == len(characters) {
|
||||
output.Speech = "End of text"
|
||||
} else if state.element.Secret {
|
||||
output.Speech = "Hidden character"
|
||||
} else {
|
||||
output.Speech = string(characters[next])
|
||||
}
|
||||
if event.Shift {
|
||||
output.Speech += ", selected"
|
||||
}
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
if event.Key == Backspace || event.Key == Delete {
|
||||
if start == end {
|
||||
if event.Key == Backspace {
|
||||
start = max(0, start-1)
|
||||
} else {
|
||||
end = min(len(characters), end+1)
|
||||
}
|
||||
}
|
||||
if start == end {
|
||||
return Effect{}
|
||||
}
|
||||
removed := string(characters[start:end])
|
||||
state.element.Text = string(append(characters[:start], characters[end:]...))
|
||||
state.caret, state.anchor = start, start
|
||||
output := session.Presentation()
|
||||
if state.element.Secret {
|
||||
output.Speech = "Deleted " + characterCount(end-start)
|
||||
} else {
|
||||
output.Speech = "Deleted " + removed
|
||||
}
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
if event.Key == Text && event.Text != "" && !event.Control {
|
||||
return session.Insert(event.Text)
|
||||
}
|
||||
return Effect{}
|
||||
}
|
||||
|
||||
// Insert handles typing and paste atomically; invalid/oversized input leaves
|
||||
// the field and selection untouched.
|
||||
func (session *Session) Insert(text string) Effect {
|
||||
if session.grid != nil {
|
||||
return Effect{}
|
||||
}
|
||||
if session.closed {
|
||||
return Effect{}
|
||||
}
|
||||
state := &session.elements[session.focus]
|
||||
if state.element.Kind != Entry {
|
||||
return Effect{}
|
||||
}
|
||||
if !utf8.ValidString(text) {
|
||||
return Effect{Presentation: Presentation{Speech: "Invalid text", Braille: "Invalid text", Cursor: -1}}
|
||||
}
|
||||
characters := []rune(state.element.Text)
|
||||
start, end := min(state.caret, state.anchor), max(state.caret, state.anchor)
|
||||
inserted := []rune(text)
|
||||
for _, character := range inserted {
|
||||
if unicode.IsControl(character) {
|
||||
return Effect{Presentation: Presentation{Speech: "Only single-line text is accepted", Braille: "Only single-line text is accepted", Cursor: -1}}
|
||||
}
|
||||
}
|
||||
if len(characters)-(end-start)+len(inserted) > state.element.MaxLength {
|
||||
return Effect{Presentation: Presentation{Speech: "Text limit reached", Braille: "Text limit reached", Cursor: -1}}
|
||||
}
|
||||
state.element.Text = string(characters[:start]) + text + string(characters[end:])
|
||||
state.caret, state.anchor = start+len(inserted), start+len(inserted)
|
||||
output := session.Presentation()
|
||||
if state.element.Secret {
|
||||
output.Speech = "Inserted " + characterCount(len(inserted))
|
||||
} else {
|
||||
output.Speech = text
|
||||
}
|
||||
return Effect{Presentation: output}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2026 Storm Dragon
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Package interaction preserves the reader API for the shared UI toolkit.
|
||||
package interaction
|
||||
|
||||
import shared "git.stormux.org/storm/screenreader-common/interaction"
|
||||
import ui "git.stormux.org/storm/screenreader-common/internalui"
|
||||
|
||||
type Kind = shared.Kind
|
||||
type Item = shared.Item
|
||||
type Element = shared.Element
|
||||
type Form = shared.Form
|
||||
type Key = shared.Key
|
||||
type Event = shared.Event
|
||||
type Presentation = shared.Presentation
|
||||
type Effect = shared.Effect
|
||||
type Value = shared.Value
|
||||
type Session = shared.Session
|
||||
type GridTab = shared.GridTab
|
||||
|
||||
const (
|
||||
List = shared.List
|
||||
Entry = shared.Entry
|
||||
CheckBox = shared.CheckBox
|
||||
Button = shared.Button
|
||||
Text = shared.Text
|
||||
Up = shared.Up
|
||||
Down = shared.Down
|
||||
Left = shared.Left
|
||||
Right = shared.Right
|
||||
Home = shared.Home
|
||||
End = shared.End
|
||||
PageUp = shared.PageUp
|
||||
PageDown = shared.PageDown
|
||||
Tab = shared.Tab
|
||||
Enter = shared.Enter
|
||||
Escape = shared.Escape
|
||||
Backspace = shared.Backspace
|
||||
Delete = shared.Delete
|
||||
)
|
||||
|
||||
func NewSession(form Form) (*Session, error) { return shared.NewSession(form) }
|
||||
func ValidatePreferences(form Form) error { return shared.ValidatePreferences(form) }
|
||||
func NewNavigationSession(title string, items []Item, selected int) (*Session, error) {
|
||||
return shared.NewNavigationSession(title, items, selected)
|
||||
}
|
||||
func NewGridSession(title string, tabs []GridTab, columns int, checkbox Element) (*Session, error) {
|
||||
return shared.NewGridSession(title, tabs, columns, checkbox)
|
||||
}
|
||||
|
||||
func NewUnifiedSession(form ui.Form, messages ui.Messages) (*Session, error) {
|
||||
return shared.NewUnifiedSession(form, messages)
|
||||
}
|
||||
Reference in new issue
Block a user