forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysdata_unix.go
369 lines (339 loc) · 8.3 KB
/
sysdata_unix.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// +build !windows,!darwin,!plan9
// 16 february 2014
package ui
import (
"time"
)
// #include "gtk_unix.h"
import "C"
type sysData struct {
cSysData
widget *C.GtkWidget
container *C.GtkWidget // for moving
pulse chan bool // for sysData.progressPulse()
clickCounter clickCounter // for Areas
// we probably don't need to save these, but we'll do so for sysData.preferredSize() just in case
areawidth int
areaheight int
}
type classData struct {
make func() *C.GtkWidget
makeAlt func() *C.GtkWidget
setText func(widget *C.GtkWidget, text string)
text func(widget *C.GtkWidget) string
append func(widget *C.GtkWidget, text string)
insert func(widget *C.GtkWidget, index int, text string)
selected func(widget *C.GtkWidget) int
selMulti func(widget *C.GtkWidget) []int
smtexts func(widget *C.GtkWidget) []string
delete func(widget *C.GtkWidget, index int)
len func(widget *C.GtkWidget) int
// ...
signals callbackMap
child func(widget *C.GtkWidget) *C.GtkWidget
childsigs callbackMap
}
var classTypes = [nctypes]*classData{
c_window: &classData{
make: gtk_window_new,
setText: gtk_window_set_title,
text: gtk_window_get_title,
signals: callbackMap{
"delete-event": window_delete_event_callback,
"configure-event": window_configure_event_callback,
},
},
c_button: &classData{
make: gtk_button_new,
setText: gtk_button_set_label,
text: gtk_button_get_label,
signals: callbackMap{
"clicked": button_clicked_callback,
},
},
c_checkbox: &classData{
make: gtk_check_button_new,
setText: gtk_button_set_label,
text: gtk_button_get_label,
},
c_combobox: &classData{
make: gtk_combo_box_text_new,
makeAlt: gtk_combo_box_text_new_with_entry,
text: gtk_combo_box_text_get_active_text,
append: gtk_combo_box_text_append_text,
insert: gtk_combo_box_text_insert_text,
selected: gtk_combo_box_get_active,
delete: gtk_combo_box_text_remove,
len: gtkComboBoxLen,
},
c_lineedit: &classData{
make: gtk_entry_new,
makeAlt: gtkPasswordEntryNew,
setText: gtk_entry_set_text,
text: gtk_entry_get_text,
},
c_label: &classData{
make: gtk_label_new,
setText: gtk_label_set_text,
text: gtk_label_get_text,
},
c_listbox: &classData{
make: gListboxNewSingle,
makeAlt: gListboxNewMulti,
text: gListboxText,
append: gListboxAppend,
insert: gListboxInsert,
selMulti: gListboxSelectedMulti,
smtexts: gListboxSelMultiTexts,
delete: gListboxDelete,
len: gListboxLen,
},
c_progressbar: &classData{
make: gtk_progress_bar_new,
},
c_area: &classData{
make: gtkAreaNew,
child: gtkAreaGetControl,
childsigs: callbackMap{
"draw": area_draw_callback,
"button-press-event": area_button_press_event_callback,
"button-release-event": area_button_release_event_callback,
"motion-notify-event": area_motion_notify_event_callback,
"enter-notify-event": area_enterleave_notify_event_callback,
"leave-notify-event": area_enterleave_notify_event_callback,
"key-press-event": area_key_press_event_callback,
"key-release-event": area_key_release_event_callback,
},
},
}
func (s *sysData) make(window *sysData) error {
ct := classTypes[s.ctype]
ret := make(chan *C.GtkWidget)
defer close(ret)
uitask <- func() {
if s.alternate {
ret <- ct.makeAlt()
return
}
ret <- ct.make()
}
s.widget = <-ret
if window == nil {
uitask <- func() {
fixed := gtkNewWindowLayout()
gtk_container_add(s.widget, fixed)
for signame, sigfunc := range ct.signals {
g_signal_connect(s.widget, signame, sigfunc, s)
}
ret <- fixed
}
s.container = <-ret
} else {
s.container = window.container
uitask <- func() {
gtkAddWidgetToLayout(s.container, s.widget)
for signame, sigfunc := range ct.signals {
g_signal_connect(s.widget, signame, sigfunc, s)
}
if ct.child != nil {
child := ct.child(s.widget)
for signame, sigfunc := range ct.childsigs {
g_signal_connect(child, signame, sigfunc, s)
}
}
ret <- nil
}
<-ret
}
return nil
}
// used for Windows; nothing special needed elsewhere
func (s *sysData) firstShow() error {
s.show()
return nil
}
func (s *sysData) show() {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
gtk_widget_show(s.widget)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) hide() {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
gtk_widget_hide(s.widget)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) setText(text string) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
classTypes[s.ctype].setText(s.widget, text)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) setRect(x int, y int, width int, height int, winheight int) error {
gtkMoveWidgetInLayout(s.container, s.widget, x, y)
gtk_widget_set_size_request(s.widget, width, height)
return nil
}
func (s *sysData) isChecked() bool {
ret := make(chan bool)
defer close(ret)
uitask <- func() {
ret <- gtk_toggle_button_get_active(s.widget)
}
return <-ret
}
func (s *sysData) text() string {
ret := make(chan string)
defer close(ret)
uitask <- func() {
ret <- classTypes[s.ctype].text(s.widget)
}
return <-ret
}
func (s *sysData) append(what string) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
classTypes[s.ctype].append(s.widget, what)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) insertBefore(what string, before int) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
classTypes[s.ctype].insert(s.widget, before, what)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) selectedIndex() int {
ret := make(chan int)
defer close(ret)
uitask <- func() {
ret <- classTypes[s.ctype].selected(s.widget)
}
return <-ret
}
func (s *sysData) selectedIndices() []int {
ret := make(chan []int)
defer close(ret)
uitask <- func() {
ret <- classTypes[s.ctype].selMulti(s.widget)
}
return <-ret
}
func (s *sysData) selectedTexts() []string {
ret := make(chan []string)
defer close(ret)
uitask <- func() {
ret <- classTypes[s.ctype].smtexts(s.widget)
}
return <-ret
}
func (s *sysData) setWindowSize(width int, height int) error {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
// does not take window geometry into account (and cannot, since the window manager won't give that info away)
// thanks to TingPing in irc.gimp.net/#gtk+
gtk_window_resize(s.widget, width, height)
ret <- struct{}{}
}
<-ret
return nil
}
func (s *sysData) delete(index int) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
classTypes[s.ctype].delete(s.widget, index)
ret <- struct{}{}
}
<-ret
}
// With GTK+, we must manually pulse the indeterminate progressbar ourselves. This goroutine does that.
func (s *sysData) progressPulse() {
pulse := func() {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
gtk_progress_bar_pulse(s.widget)
ret <- struct{}{}
}
<-ret
}
var ticker *time.Ticker
var tickchan <-chan time.Time
// the pulse rate used by Zenity (https://git.gnome.org/browse/zenity/tree/src/progress.c#n69 for blob cbffe08e8337ba1375a0ac7210eff5a2e4313bb8)
const pulseRate = 100 * time.Millisecond
for {
select {
case start := <-s.pulse:
if start {
ticker = time.NewTicker(pulseRate)
tickchan = ticker.C
pulse() // start the pulse animation now, not 100ms later
} else {
if ticker != nil {
ticker.Stop()
}
ticker = nil
tickchan = nil
s.pulse <- true // notify sysData.setProgress()
}
case <-tickchan:
pulse()
}
}
}
func (s *sysData) setProgress(percent int) {
if s.pulse == nil {
s.pulse = make(chan bool)
go s.progressPulse()
}
if percent == -1 {
s.pulse <- true
return
}
s.pulse <- false
<-s.pulse // wait for sysData.progressPulse() to register that
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
gtk_progress_bar_set_fraction(s.widget, percent)
ret <- struct{}{}
}
<-ret
}
func (s *sysData) len() int {
ret := make(chan int)
defer close(ret)
uitask <- func() {
ret <- classTypes[s.ctype].len(s.widget)
}
return <-ret
}
func (s *sysData) setAreaSize(width int, height int) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
c := gtkAreaGetControl(s.widget)
gtk_widget_set_size_request(c, width, height)
s.areawidth = width // for sysData.preferredSize()
s.areaheight = height
ret <- struct{}{}
}
<-ret
}