-
Notifications
You must be signed in to change notification settings - Fork 162
/
button.go
149 lines (129 loc) · 3.1 KB
/
button.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package view
import (
"image"
"image/color"
"runtime"
"strings"
"gomatcha.io/matcha/comm"
"gomatcha.io/matcha/internal"
"gomatcha.io/matcha/layout"
"gomatcha.io/matcha/paint"
"gomatcha.io/matcha/pointer"
pb "gomatcha.io/matcha/proto"
pbview "gomatcha.io/matcha/proto/view"
"gomatcha.io/matcha/text"
)
// Button implements a native button view.
type Button struct {
Embed
String string
Color color.Color
OnPress func()
Enabled bool
PaintStyle *paint.Style
}
// NewButton returns a new view.
func NewButton() *Button {
return &Button{
Enabled: true,
}
}
// Build implements view.View.
func (v *Button) Build(ctx Context) Model {
painter := paint.Painter(nil)
if v.PaintStyle != nil {
painter = v.PaintStyle
}
return Model{
Painter: painter,
Layouter: &buttonLayouter{str: v.String},
NativeViewName: "gomatcha.io/matcha/view/button",
NativeViewState: internal.MarshalProtobuf(&pbview.Button{
Str: v.String,
Enabled: v.Enabled,
Color: pb.ColorEncode(v.Color),
}),
NativeFuncs: map[string]interface{}{
"OnPress": func() {
if v.OnPress != nil {
v.OnPress()
}
},
},
}
}
type buttonLayouter struct {
str string
}
func (l *buttonLayouter) Layout(ctx layout.Context) (layout.Guide, []layout.Guide) {
if runtime.GOOS == "android" {
style := &text.Style{}
style.SetFont(text.DefaultFont(14))
st := text.NewStyledText(strings.ToUpper(l.str), style)
size := st.Size(layout.Pt(0, 0), ctx.MaxSize(), 1)
const padding = 16.0
g := layout.Guide{Frame: layout.Rt(0, 0, size.X+padding*2+16, 48)}
return g, nil
} else if runtime.GOOS == "darwin" {
style := &text.Style{}
style.SetFont(text.DefaultFont(20))
st := text.NewStyledText(l.str, style)
size := st.Size(layout.Pt(0, 0), ctx.MaxSize(), 1)
const padding = 10.0
g := layout.Guide{Frame: layout.Rt(0, 0, size.X+padding*2, size.Y+padding*2)}
return g, nil
}
return layout.Guide{}, nil
}
func (l *buttonLayouter) Notify(f func()) comm.Id {
return 0 // no-op
}
func (l *buttonLayouter) Unnotify(id comm.Id) {
// no-op
}
type ImageButton struct {
Embed
Image image.Image
OnPress func()
PaintStyle *paint.Style
}
func NewImageButton() *ImageButton {
return &ImageButton{}
}
func (v *ImageButton) Build(ctx Context) Model {
iv := NewImageView()
iv.ResizeMode = ImageResizeModeCenter
iv.Image = v.Image
painter := paint.Painter(nil)
if v.PaintStyle != nil {
painter = v.PaintStyle
}
t := &pointer.ButtonGesture{
OnEvent: func(e *pointer.ButtonEvent) {
if e.Kind == pointer.EventKindRecognized && v.OnPress != nil {
v.OnPress()
}
},
}
return Model{
Children: []View{iv},
Layouter: &imageButtonLayouter{},
Painter: painter,
Options: []Option{
pointer.GestureList{t},
},
}
}
type imageButtonLayouter struct {
str string
}
func (l *imageButtonLayouter) Layout(ctx layout.Context) (layout.Guide, []layout.Guide) {
g := ctx.LayoutChild(0, ctx.MinSize(), ctx.MaxSize())
return g, []layout.Guide{g}
}
func (l *imageButtonLayouter) Notify(f func()) comm.Id {
return 0 // no-op
}
func (l *imageButtonLayouter) Unnotify(id comm.Id) {
// no-op
}