-
Notifications
You must be signed in to change notification settings - Fork 6
/
utf8.go
120 lines (112 loc) · 2.72 KB
/
utf8.go
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
// +build ignore
package main
import (
"image"
"image/draw"
"sync"
"github.com/as/font"
"github.com/as/frame"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
var wg sync.WaitGroup
var winSize = image.Pt(1024, 768)
func pt(e mouse.Event) image.Point {
return image.Pt(int(e.X), int(e.Y))
}
func main() {
driver.Main(func(src screen.Screen) {
var dirty = true
wind, _ := src.NewWindow(&screen.NewWindowOptions{winSize.X, winSize.Y, "basic"})
b, _ := src.NewBuffer(winSize)
draw.Draw(b.RGBA(), b.Bounds(), frame.A.Back, image.ZP, draw.Src)
fr := frame.New(b.RGBA(), image.Rectangle{image.ZP, winSize}, &frame.Config{Face: font.NewGoMono(14), Color: frame.A, Flag: frame.FrUTF8})
fr.Refresh()
wind.Send(paint.Event{})
ck := func() {
if dirty || fr.Dirty() {
wind.Send(paint.Event{})
}
dirty = false
}
utf := `Π Ρ Σ Τ Υ Φ Χ Ψ Ω Ϊ Ϋ ά έ ή ί ΰ α β γ δ ε ζ η θ ι κ λ μ ν ξ οπ ρ ς σ τ υ φ χ ψ ω ϊ ϋ ό ύ ώ Ϗ ϐ ϑ ϒ ϓ ϔ ϕ ϖ ϗ Ϙ ϙ Ϛ ϛ Ϝ ϝ Ϟ ϟϠ ϡ Ϣ ϣ Ϥ ϥ Ϧ ϧ Ϩ ϩ Ϫ ϫ Ϭ ϭ Ϯ ϯ ϰ ϱ ϲ ϳ ϴ ϵ ϶ Ϸ ϸ Ϲ Ϻ ϻ ϼ Ͻ Ͼ Ͽ`
fr.Insert([]byte("utf8 test"), fr.Len())
fr.Insert([]byte(utf), fr.Len())
fr.Insert([]byte("end"), fr.Len())
flush := func() {
wind.Upload(fr.Bounds().Min, b, fr.Bounds())
wind.Publish()
}
for {
switch e := wind.NextEvent().(type) {
case mouse.Event:
if e.Button == 1 && e.Direction == 1 {
p0 := fr.IndexOf(pt(e))
fr.Select(p0, p0)
flush()
frameSweep(fr, wind, flush)
wind.Send(paint.Event{})
}
case key.Event:
if e.Direction == 2 {
continue
}
if e.Rune == '\r' {
e.Rune = '\n'
}
if e.Rune > 0x79 || e.Rune < 0 {
continue
}
p0, p1 := fr.Dot()
if e.Rune == '\x08' {
if p0 == p1 && p0 > 0 {
p0--
}
fr.Delete(p0, p1)
} else {
fr.Insert([]byte{byte(e.Rune)}, p0)
p0++
}
fr.Select(p0, p0)
dirty = true
ck()
case size.Event:
wind.Upload(image.ZP, b, b.Bounds())
fr.Refresh()
ck()
case paint.Event:
flush()
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return
}
}
}
})
}
func frameSweep(f *frame.Frame, ep screen.Window, flush func()) {
p0, p1 := f.Dot()
for {
switch e := ep.NextEvent().(type) {
case mouse.Event:
if e.Direction != 0 {
ep.SendFirst(e)
return
}
if p1 = f.IndexOf(pt(e)); p0 > p1 {
f.Select(p1, p0)
} else {
f.Select(p0, p1)
}
flush()
case interface{}:
ep.SendFirst(e)
return
}
}
}