/
delegate.go
53 lines (47 loc) · 952 Bytes
/
delegate.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
package main
import (
"fmt"
"gopkg.in/qml.v1"
"image/color"
"math/rand"
"os"
"time"
)
func main() {
if err := qml.Run(run); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
engine := qml.NewEngine()
colors := &Colors{}
engine.Context().SetVar("colors", colors)
component, err := engine.LoadFile("delegate.qml")
if err != nil {
return err
}
window := component.CreateWindow(nil)
window.Show()
go func() {
n := func() uint8 { return uint8(rand.Intn(256)) }
for i := 0; i < 100; i++ {
colors.Add(color.RGBA{n(), n(), n(), 0xff})
time.Sleep(1 * time.Second)
}
}()
window.Wait()
return nil
}
type Colors struct {
list []color.RGBA
Len int
}
func (colors *Colors) Add(c color.RGBA) {
colors.list = append(colors.list, c)
colors.Len = len(colors.list)
qml.Changed(colors, &colors.Len)
}
func (colors *Colors) Color(index int) color.RGBA {
return colors.list[index]
}