-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
120 lines (106 loc) · 4.26 KB
/
Copy pathexample_test.go
File metadata and controls
120 lines (106 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved.
// Use of this source code is governed by a BSD-3-Clause license that can be
// found in the LICENSE file at the root of this repository.
package toolkit_test
import (
"fmt"
"github.com/go-widgets/painter"
"github.com/go-widgets/toolkit"
)
// newSurface returns a PixelPainter over a fresh w×h RGBA buffer — the render
// target the examples draw into. A CellPainter would render the same widgets to
// a terminal grid instead.
func newSurface(w, h int) *painter.PixelPainter {
return painter.NewPixelPainter(make([]byte, 4*w*h), w, h)
}
// ExampleLineChart plots a series as a polyline over an axis frame.
func ExampleLineChart() {
chart := toolkit.NewLineChart([]float64{3, 7, 2, 8, 5, 9, 4})
chart.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 220, H: 80})
chart.Draw(newSurface(220, 80), toolkit.DefaultLight())
}
// ExampleBarChart plots non-negative values as vertical bars.
func ExampleBarChart() {
chart := toolkit.NewBarChart([]float64{4, 7, 2, 8, 5})
chart.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 200, H: 80})
chart.Draw(newSurface(200, 80), toolkit.DefaultLight())
}
// ExamplePieChart fills a disc with one proportional wedge per value.
func ExamplePieChart() {
chart := toolkit.NewPieChart([]float64{3, 5, 2, 4})
chart.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 120, H: 120})
chart.Draw(newSurface(120, 120), toolkit.DefaultLight())
}
// ExampleMarkdownView renders a Markdown subset (headings, lists, code,
// paragraphs) laid out for the bitmap font.
func ExampleMarkdownView() {
md := toolkit.NewMarkdownView("# Title\n\nA paragraph.\n\n- one\n- two")
md.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 260, H: 120})
md.Draw(newSurface(260, 120), toolkit.DefaultLight())
}
// ExampleDatePicker shows a date field with a drop-down calendar.
func ExampleDatePicker() {
dp := toolkit.NewDatePicker(2026, 7, 10)
// A host observes the picked date through the embedded Calendar's accessors.
dp.Cal.Day().Subscribe(func(d int) {
fmt.Printf("picked %04d-%02d-%02d\n", dp.Cal.Year().Get(), dp.Cal.Month().Get(), d)
})
dp.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 170, H: toolkit.DatePickerFieldH()})
fmt.Println(dp.Text())
// Output: 2026-07-10
}
// ExampleRangeSlider selects a sub-interval; SetRange orders + clamps its ends.
func ExampleRangeSlider() {
rs := toolkit.NewRangeSlider(0, 100, 20, 80)
rs.SetRange(90, 10) // passed out of order → normalised to Low <= High
fmt.Printf("%.0f-%.0f\n", rs.Low().Get(), rs.High().Get())
// Output: 10-90
}
// ExampleSetFont swaps the active font; every widget re-lays-out against the
// new metrics. NewBitmapFont(2) doubles the built-in bitmap ("retina" text).
func ExampleSetFont() {
fmt.Println(toolkit.GlyphHeight()) // default 5×7 bitmap
toolkit.SetFont(toolkit.NewBitmapFont(2))
fmt.Println(toolkit.GlyphHeight()) // doubled
toolkit.SetFont(nil) // restore the default
fmt.Println(toolkit.GlyphHeight())
// Output:
// 7
// 14
// 7
}
// ExampleContextMenu shows a right-click popup that auto-sizes, clamps inside
// the surface, and dismisses on an outside click.
func ExampleContextMenu() {
menu := toolkit.NewMenu([]toolkit.MenuItem{
{Label: "Cut", Action: func() {}},
{Label: "Copy", Action: func() {}, Shortcut: "Ctrl+C"},
})
cm := toolkit.NewContextMenu(menu)
cm.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 200, H: 160})
cm.Popup(8, 8) // open at the cursor
cm.Draw(newSurface(200, 160), toolkit.DefaultLight())
}
// ExampleOverlay stacks z-ordered layers above a primary child. Events route
// top-down; a Modal overlay swallows clicks that miss every layer.
func ExampleOverlay() {
base := toolkit.NewLabel("main content")
ov := toolkit.NewOverlay(base)
ov.Push(toolkit.NewTooltip("floating layer"))
ov.SetBounds(toolkit.Rect{X: 0, Y: 0, W: 200, H: 120})
ov.Draw(newSurface(200, 120), toolkit.DefaultLight())
}
// ExampleCollectA11y walks a host-composed widget list and returns each
// Accessible widget's role, name, and value for a screen-reader bridge.
func ExampleCollectA11y() {
widgets := []toolkit.Widget{
toolkit.NewButton("Save", nil),
toolkit.NewCheckButton("Wrap", true),
}
for _, info := range toolkit.CollectA11y(widgets) {
fmt.Printf("%s %q %q\n", info.Role, info.Name, info.Value)
}
// Output:
// button "Save" ""
// checkbox "Wrap" "checked"
}