-
Notifications
You must be signed in to change notification settings - Fork 80
/
ui_reader.go
231 lines (188 loc) · 5.34 KB
/
ui_reader.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//go:generate ../.build-tools/esc -o definitions.go -modtime 1489449600 -pkg gui -ignore "Makefile" definitions/
package gui
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"sync"
"unsafe"
log "github.com/sirupsen/logrus"
"reflect"
"github.com/coyim/gotk3adapter/gdki"
"github.com/coyim/gotk3adapter/glibi"
"github.com/coyim/gotk3adapter/gtki"
)
const (
xmlExtension = ".xml"
imagesFolder = "/definitions/images/"
)
func getActualDefsFolder() string {
wd, _ := os.Getwd()
if strings.HasSuffix(wd, "/gui") {
return "definitions"
}
return "gui/definitions"
}
func getDefinitionWithFileFallback(uiName string) string {
fname := path.Join("/definitions", uiName+xmlExtension)
embeddedFile, err := FSString(false, fname)
if err != nil {
//Enforce the file is embedded, but dont use it.
panic(fmt.Sprintf("No definition found for %s", uiName))
}
fileName := filepath.Join(getActualDefsFolder(), uiName+xmlExtension)
if fileNotFound(fileName) {
return embeddedFile
}
log.WithField("file", fileName).Debug("Loading definition from local file")
return readFile(fileName)
}
func readFile(fileName string) string {
data, _ := ioutil.ReadFile(filepath.Clean(fileName))
return string(data)
}
// This must be called from the UI thread - otherwise bad things will happen sooner or later
func builderForDefinition(uiName string) gtki.Builder {
template := getDefinitionWithFileFallback(uiName)
return builderForString(template)
}
func fileNotFound(fileName string) bool {
_, fnf := os.Stat(fileName)
return os.IsNotExist(fnf)
}
type builder struct {
gtki.Builder
}
func newBuilder(filename string) *builder {
return &builder{builderForDefinition(filename)}
}
func newBuilderFromString(template string) *builder {
return &builder{builderForString(template)}
}
func builderForString(template string) gtki.Builder {
builder, err := g.gtk.BuilderNew()
if err != nil {
//We cant recover from this
panic(err)
}
//We dont use NewFromString because it doesnt give us an error message
err = builder.AddFromString(template)
if err != nil {
//This is a programming error
panic(fmt.Sprintf("gui: wrong template format: %s\n", err.Error()))
}
return builder
}
// optionallySetWidgetNameFromID will set the name from the ID, making it possible to use the ID to refer to the object from
// CSS using the name as ID.
func optionallySetWidgetNameFromID(o glibi.Object, ident string) {
w, ok := o.(gtki.Widget)
if ok {
w.SetName(ident)
}
}
func (b *builder) getObj(name string) glibi.Object {
obj, _ := b.GetObject(name)
optionallySetWidgetNameFromID(obj, name)
return obj
}
func (b *builder) bindObjects(view interface{}) error {
if reflect.TypeOf(view).Kind() != reflect.Ptr {
return errors.New("view must be a pointer")
}
elem := reflect.ValueOf(view).Elem()
dstType := elem.Type()
if dstType.Kind() != reflect.Struct {
return errors.New("view must be a pointer to a struct value")
}
for i := 0; i < dstType.NumField(); i++ {
objectID := dstType.Field(i).Tag.Get("gtk-widget")
if objectID == "" {
continue
}
dstValue := elem.Field(i)
if !dstValue.CanSet() {
//Unexported field. This is fine by unsafe pkg documentation
/* #nosec G103 */
dstValue = reflect.NewAt(dstValue.Type(), unsafe.Pointer(dstValue.UnsafeAddr())).Elem()
}
if !dstValue.CanSet() {
return errors.New("cant set value")
}
object := b.get(objectID)
v := reflect.ValueOf(object)
//if dstValue.Type() != v.Type() {
// return errors.New("types do not match")
//}
dstValue.Set(v)
}
return nil
}
func (b *builder) getItem(name string, target interface{}) {
v := reflect.ValueOf(target)
if v.Kind() != reflect.Ptr {
panic("builder.getItem() target argument must be a pointer")
}
elem := v.Elem()
elem.Set(reflect.ValueOf(b.get(name)))
}
//TODO: Why not a map[string]interface{}?
func (b *builder) getItems(args ...interface{}) {
for len(args) >= 2 {
name, ok := args[0].(string)
if !ok {
panic("string argument expected in builder.getItems()")
}
b.getItem(name, args[1])
args = args[2:]
}
}
func (b *builder) get(name string) glibi.Object {
obj, err := b.GetObject(name)
if err != nil {
panic("builder.GetObject() failed: " + err.Error())
}
optionallySetWidgetNameFromID(obj, name)
return obj
}
func mustGetImageBytes(filename string) []byte {
bs, err := FSByte(false, imagesFolder+filename)
if err != nil {
panic("Developer error: getting the image " + filename + " but it does not exist")
}
return bs
}
// getPixbufFromBytes returns the pixbuff from a byte array.
func getPixbufFromBytes(bstream []byte) gdki.Pixbuf {
pl, err := g.gdk.PixbufLoaderNew()
if err != nil {
panic("Developer error: setting the image from >>>>>>>>")
}
var w sync.WaitGroup
w.Add(1)
_, _ = pl.Connect("area-prepared", w.Done)
if _, err := pl.Write(bstream); err != nil {
log.WithError(err).Warn("cannot write to PixbufLoader")
return nil
}
if err := pl.Close(); err != nil {
log.WithError(err).Warn("cannot close PixbufLoader")
return nil
}
w.Wait() //Waiting for Pixbuf to load before using it
pb, err := pl.GetPixbuf()
if err != nil {
log.WithError(err).Warn("cannot write to PixbufLoader")
return nil
}
return pb
}
// setImageFromFile sets an image from a file name.
func setImageFromFile(i gtki.Image, filename string) {
pb := getPixbufFromBytes(mustGetImageBytes(filename))
i.SetFromPixbuf(pb)
}