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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ only its damage. It is a `Repainter` like the native backends: `Repaint`, from
any goroutine, draws the next animation frame, so a result that arrives with
nobody touching the page still reaches it.

The tab is readable by a screen reader. The tree's accessibility nodes (role,
name, value, focus, disabled, read-only, multiline, range, a stable key) become
webcanvas's accessibility mirror, a hidden DOM subtree beside the canvas, and
every `TextView` or `CodeEditor` gets webcanvas's text layer, so a text field is
one `textbox` that the browser's find, selection and assistive technology see.
Names come from widgets and reach the DOM only as text. A field with no name is
still published, as on the native bridges. The conversion is one exported
package, `github.com/go-widgets/window/webmirror`, which any scene drawn on a
webcanvas can reuse (`webmirror.Host` implements `webcanvas.A11yAware` and
`webcanvas.TextInputHost` over one or several widget trees).

```html
<canvas id="screen" style="width:100vw;height:100vh"></canvas>
<script src="wasm_exec.js"></script>
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ require (
github.com/go-widgets/android v0.16.0
github.com/go-widgets/gpupainter v0.4.0
github.com/go-widgets/painter v0.17.0
github.com/go-widgets/toolkit v0.356.0
github.com/go-widgets/webcanvas v0.7.0
github.com/go-widgets/textinput v0.1.1
github.com/go-widgets/toolkit v0.370.0
github.com/go-widgets/webcanvas v0.14.1
github.com/godbus/dbus/v5 v5.2.4
golang.org/x/sys v0.49.0
)
Expand All @@ -36,5 +37,5 @@ require (
github.com/go-richdoc/richdoc v0.7.0 // indirect
github.com/go-typeset/bidi v0.3.1 // indirect
github.com/go-widgets/mvvm v0.15.0 // indirect
github.com/go-widgets/textinput v0.1.0 // indirect
github.com/go-widgets/vcs v0.1.0 // indirect
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ github.com/go-widgets/mvvm v0.15.0 h1:VvZiLB7gm+5CIEeQH3GNI53ytUndzOznT/fJvaG8hO
github.com/go-widgets/mvvm v0.15.0/go.mod h1:ZJj5M8zDg/gdzcvdWnJgZj/fcV0qe84fALq+EQVzUOM=
github.com/go-widgets/painter v0.17.0 h1:PB/Xr0flFR9qL2/RPhbJOeyU6PPzcGyf3Qx4RRngowg=
github.com/go-widgets/painter v0.17.0/go.mod h1:8kERcZUoWNyLIas1NsMWiYynvvyx+hcB2cNIh0Bx0yc=
github.com/go-widgets/textinput v0.1.0 h1:ifx1RjbPIWtlokoBLV1ZwlH+0CeixoMbswlqhNvYNmw=
github.com/go-widgets/textinput v0.1.0/go.mod h1:Y/k3PDtUEMQ/4psCVKdmUStNWa/EsVAMcCKG2lEtQ5c=
github.com/go-widgets/toolkit v0.356.0 h1:perOh1Tcdw6VkxyaVmXvPHjt6lhsFchgIQjYgAXoGYc=
github.com/go-widgets/toolkit v0.356.0/go.mod h1:dAs4uFSnRAV0aJ1IZkxQld5PP5ZdZDOS9bf9DF4jqQ8=
github.com/go-widgets/webcanvas v0.7.0 h1:6+A1Hw9BBhBUmXEqceElUrc7M5HClVBpWBPEXBpkT2o=
github.com/go-widgets/webcanvas v0.7.0/go.mod h1:GF9MyBmSlH/aNk4RjNwnC0LfAT3fng33EOSW7+r4LHs=
github.com/go-widgets/textinput v0.1.1 h1:3NZD5CJm5XYSWA3YzQ64tXC86gnEQ0UlNPL5qJeIUSs=
github.com/go-widgets/textinput v0.1.1/go.mod h1:Y/k3PDtUEMQ/4psCVKdmUStNWa/EsVAMcCKG2lEtQ5c=
github.com/go-widgets/toolkit v0.370.0 h1:XjeCt7m+g3cj273qYJu4CAZdTdXWnbUseoURZStBUWk=
github.com/go-widgets/toolkit v0.370.0/go.mod h1:zVKiLPMvy2igZZtU7IQ1rqoH31+kb1z2C6PptbCwaHI=
github.com/go-widgets/vcs v0.1.0 h1:1pZQygSsKBWZepAgsTdqh1bwHBjPwaLM4p7bAAONGnk=
github.com/go-widgets/vcs v0.1.0/go.mod h1:c2SbtsVVLJUJAf/vOeMKkFWZRFyMAeDrY8rfyUyIevs=
github.com/go-widgets/webcanvas v0.14.1 h1:FKCY96ERrfLGX2D6QZmUGZ32qbwlAPFOMidkuY3E3UM=
github.com/go-widgets/webcanvas v0.14.1/go.mod h1:H0lf7ZOm5QLap3MyizCjb2ArVszjZd8lDLk1SRRCfZk=
github.com/godbus/dbus/v5 v5.2.4 h1:F2gUCy++NMIxGmo9J+z5le/+ZC8W1OgeR51P4i9ZB/U=
github.com/godbus/dbus/v5 v5.2.4/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
18 changes: 18 additions & 0 deletions internal/tab/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"sync/atomic"

"github.com/go-widgets/painter"
"github.com/go-widgets/textinput"
"github.com/go-widgets/toolkit"
"github.com/go-widgets/webcanvas"
"github.com/go-widgets/window/internal/dnd"
"github.com/go-widgets/window/webmirror"
)

// damageRenderer is the opt-in incremental repaint, as the other back-ends
Expand Down Expand Up @@ -47,8 +49,24 @@ var (
_ webcanvas.Scroller = (*Tree)(nil)
_ webcanvas.ModifierAware = (*Tree)(nil)
_ webcanvas.RepaintAware = (*Tree)(nil)
_ webcanvas.A11yAware = (*Tree)(nil)
_ webcanvas.TextInputHost = (*Tree)(nil)
)

// host is the tree as webmirror sees it: the one conversion from the toolkit's
// accessibility tree to webcanvas's mirror and text layer, shared with the
// gallery.
func (t *Tree) host() webmirror.Host {
return webmirror.Host{Sources: func() []webmirror.Source { return webmirror.Single(t.root) }}
}

// A11yNodes is the tree as the accessibility mirror shows it, read after each
// frame is drawn.
func (t *Tree) A11yNodes() []webcanvas.A11yNode { return t.host().A11yNodes() }

// TextInputFor gives a TextView (and a CodeEditor) its text layer.
func (t *Tree) TextInputFor(key string) textinput.Client { return t.host().TextInputFor(key) }

// NewTree adapts root, laid out on a w×h surface painted with theme.
func NewTree(root toolkit.Widget, w, h int, theme *toolkit.Theme) *Tree {
if theme == nil {
Expand Down
46 changes: 46 additions & 0 deletions internal/tab/tree_a11y_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) the go-widgets/window authors. All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

package tab

import (
"testing"

"github.com/go-widgets/toolkit"
)

// A Tree is what the browser tab hands webcanvas, so what it publishes is what
// a screen reader gets: the buttons by name and a text view as one textbox with
// its text layer.
func TestTreePublishesItsWidgets(t *testing.T) {
btn := toolkit.NewButton("Run", func() {})
tv := toolkit.NewTextView("print(1)")
tv.AccessibleName = "Cell 1"
root := toolkit.NewVBox()
root.AddFixed(btn, 24)
root.AddFixed(tv, 90)
tr := NewTree(root, 320, 200, nil)
root.SetBounds(toolkit.Rect{W: 320, H: 200})

var button, text int
var tvKey string
for _, n := range tr.A11yNodes() {
switch {
case n.Role == "button" && n.Name == "Run":
button++
case n.Role == "textbox" && n.Name == "Cell 1":
text++
tvKey = n.Key
}
}
if button != 1 || text != 1 {
t.Fatalf("buttons %d, textboxes %d; want 1 and 1 (%+v)", button, text, tr.A11yNodes())
}
if c := tr.TextInputFor(tvKey); c == nil {
t.Error("the text view has no text layer")
}
if c := tr.TextInputFor("nope"); c != nil {
t.Error("an unknown key got a text layer")
}
}
175 changes: 175 additions & 0 deletions webmirror/webmirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright (c) the go-widgets/window authors. All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

// Package webmirror feeds go-widgets/webcanvas's accessibility mirror and text
// layer from a toolkit widget tree. It is the one place a toolkit.A11yNode
// becomes a webcanvas.A11yNode, shared by the browser-tab backend of this
// module and by any other scene that draws on a webcanvas (the gallery).
//
// It is plain Go with no build tag: the conversion is tested natively, and only
// webcanvas itself touches the DOM.
//
// What it publishes follows internal/a11yrole, the rule the native bridges
// share: a node with no area is dropped, a static node with no name is dropped,
// and a nameless interactive node (a text field) is kept so a screen reader
// still announces the control. Names and values are passed through verbatim;
// webcanvas writes them with textContent and setAttribute only, so nothing here
// builds markup.
package webmirror

import (
"github.com/go-widgets/textinput"
"github.com/go-widgets/toolkit"
"github.com/go-widgets/webcanvas"
"github.com/go-widgets/window/internal/a11yrole"
)

// Source is one widget tree a scene publishes. A scene made of several
// independent roots (the gallery draws a menu bar, three columns and a status
// bar, each its own tree) gives each a distinct Prefix, because every tree's
// own keys start "<Type>#0" and two trees of the same type would collide.
type Source struct {
// Prefix is put before the tree's keys ("<Prefix>|<key>"). Empty for a
// scene with a single tree: the toolkit's keys are then published as they
// are.
Prefix string
// Root is the tree.
Root toolkit.Widget
}

// Single is the sources of a scene with one tree.
func Single(root toolkit.Widget) []Source { return []Source{{Root: root}} }

// Host implements [webcanvas.A11yAware] and [webcanvas.TextInputHost] over the
// trees Sources returns. Embed it in a scene, or call it from the scene's two
// methods. Sources is asked on every call, so a scene whose roots change (a
// dialog opened, a tab switched) needs no extra wiring.
type Host struct {
Sources func() []Source
}

var (
_ webcanvas.A11yAware = Host{}
_ webcanvas.TextInputHost = Host{}
)

// A11yNodes returns the accessible nodes of every source, in reading order.
func (h Host) A11yNodes() []webcanvas.A11yNode {
if h.Sources == nil {
return nil
}
return Nodes(h.Sources()...)
}

// TextInputFor returns the text-input client of the widget published under
// key, or nil when that widget is not a text-input client (an Entry is not one
// yet) or the key is unknown.
func (h Host) TextInputFor(key string) textinput.Client {
if h.Sources == nil {
return nil
}
return TextInput(key, h.Sources()...)
}

// Nodes converts the accessibility trees of srcs into webcanvas nodes.
func Nodes(srcs ...Source) []webcanvas.A11yNode {
var out []webcanvas.A11yNode
for _, s := range srcs {
if s.Root == nil {
continue
}
for _, n := range a11yrole.Nodes(s.Root) {
out = append(out, Convert(n, s.Prefix))
}
}
return out
}

// Convert is the one conversion from a toolkit node to a webcanvas node. The
// role is the WAI-ARIA name the toolkit role already is; the value keeps the
// toolkit's words ("checked", "pressed", "selected"), which webcanvas maps to
// aria-checked and aria-pressed.
func Convert(n toolkit.A11yNode, prefix string) webcanvas.A11yNode {
return webcanvas.A11yNode{
Key: Key(prefix, n.Key),
Role: string(n.Role),
Name: n.Name,
Value: n.Value,
X: n.Rect.X,
Y: n.Rect.Y,
W: n.Rect.W,
H: n.Rect.H,
Focused: n.Focused,
Disabled: n.Disabled,
ReadOnly: n.ReadOnly,
Multiline: n.Multiline,
HasRange: n.HasRange,
Min: n.Min,
Max: n.Max,
Now: n.Now,
}
}

// Key is the published key of a toolkit key under prefix.
func Key(prefix, key string) string {
if prefix == "" {
return key
}
return prefix + "|" + key
}

// TextInput returns the textinput.Client of the widget published under key in
// srcs, or nil. The toolkit walk reports nodes, not widgets, so the widgets are
// collected by the same traversal (same order, same filter) and paired with
// the nodes by position; if the two ever disagree the answer is nil and the
// node stays a plain textbox, never the wrong widget.
func TextInput(key string, srcs ...Source) textinput.Client {
for _, s := range srcs {
if s.Root == nil {
continue
}
nodes := toolkit.WalkA11y(s.Root)
ws := accessibleWidgets(s.Root)
if len(ws) != len(nodes) {
continue
}
for i, n := range nodes {
if Key(s.Prefix, n.Key) != key {
continue
}
if info := ws[i].(toolkit.Accessible).A11y(); info.Role != n.Role {
return nil
}
c, _ := ws[i].(textinput.Client)
return c
}
}
return nil
}

// childContainer is the interface toolkit.WalkA11y descends through.
type childContainer interface{ Children() []toolkit.Widget }

// accessibleWidgets is the widgets toolkit.WalkA11y turns into nodes, in the
// same order: accessible, and not RolePresentation, descending through every
// child container.
func accessibleWidgets(root toolkit.Widget) []toolkit.Widget {
var out []toolkit.Widget
var walk func(w toolkit.Widget)
walk = func(w toolkit.Widget) {
if w == nil {
return
}
if a, ok := w.(toolkit.Accessible); ok && a.A11y().Role != toolkit.RolePresentation {
out = append(out, w)
}
if c, ok := w.(childContainer); ok {
for _, k := range c.Children() {
walk(k)
}
}
}
walk(root)
return out
}
Loading
Loading