forked from gomatcha/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.go
71 lines (63 loc) · 1.38 KB
/
switch.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
package view
import (
"fmt"
"runtime"
"github.com/gogo/protobuf/proto"
"gomatcha.io/matcha/internal"
"gomatcha.io/matcha/layout"
"gomatcha.io/matcha/paint"
protoview "gomatcha.io/matcha/proto/view"
)
type Switch struct {
Embed
Enabled bool
Value bool
OnSubmit func(value bool)
PaintStyle *paint.Style
}
// NewSwitch returns a new view.
func NewSwitch() *Switch {
return &Switch{
Enabled: true,
}
}
// Build implements view.View.
func (v *Switch) Build(ctx Context) Model {
var rect layout.Rect
if runtime.GOOS == "android" {
rect = layout.Rt(0, 0, 61, 40)
} else {
rect = layout.Rt(0, 0, 51, 31)
}
l := &absoluteLayouter{Guide: layout.Guide{Frame: rect}}
painter := paint.Painter(nil)
if v.PaintStyle != nil {
painter = v.PaintStyle
}
return Model{
Painter: painter,
Layouter: l,
NativeViewName: "gomatcha.io/matcha/view/switch",
NativeViewState: internal.MarshalProtobuf(&protoview.SwitchView{
Value: v.Value,
Enabled: v.Enabled,
}),
NativeFuncs: map[string]interface{}{
// "test": func(a, b string) {
// fmt.Println("test", a, b)
// },
"OnChange": func(data []byte) {
event := &protoview.SwitchEvent{}
err := proto.Unmarshal(data, event)
if err != nil {
fmt.Println("error", err)
return
}
v.Value = event.Value
if v.OnSubmit != nil {
v.OnSubmit(v.Value)
}
},
},
}
}