This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
indicator.go
353 lines (304 loc) · 8.35 KB
/
indicator.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
package appindicator
// #cgo pkg-config: appindicator3-0.1
// #include <stdlib.h>
// #include <libappindicator/app-indicator.h>
import "C"
import (
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"unsafe"
)
// Indicator represents a single indicator object.
//
// It holds only a pointer to C counterpart.
type Indicator struct {
indicator *C.AppIndicator
}
// Object returns a pointer to glib.Object of indicator.
//
// Useful for connecting signals to indicator for example.
func (indicator *Indicator) Object() *glib.Object {
return glib.Take(
unsafe.Pointer(
indicator.indicator,
),
)
}
/* Create stuff */
// New creates a fresh indicator.
func New(id, iconName string, category Category) *Indicator {
idC := C.CString(id)
defer C.free(unsafe.Pointer(idC))
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
categoryC := (C.AppIndicatorCategory)(category)
return &Indicator{
indicator: C.app_indicator_new(
idC,
iconNameC,
categoryC,
),
}
}
// NewWithPath creates a fresh indicator with custom icon theme path.
func NewWithPath(id, iconName string, category Category, iconThemePath string) *Indicator {
idC := C.CString(id)
defer C.free(unsafe.Pointer(idC))
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
categoryC := (C.AppIndicatorCategory)(category)
iconThemePathC := C.CString(iconThemePath)
defer C.free(unsafe.Pointer(iconThemePathC))
return &Indicator{
indicator: C.app_indicator_new_with_path(
idC,
iconNameC,
categoryC,
iconThemePathC,
),
}
}
/* Set properties */
// SetStatus sets status of indicator.
func (indicator *Indicator) SetStatus(status Status) {
statusC := (C.AppIndicatorStatus)(status)
C.app_indicator_set_status(
indicator.indicator,
statusC,
)
}
// SetAttentionIcon sets attention icon of indicator.
func (indicator *Indicator) SetAttentionIcon(iconName string) {
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
C.app_indicator_set_attention_icon(
indicator.indicator,
iconNameC,
)
}
// SetAttentionIconFull sets attention icon of indicator with description.
func (indicator *Indicator) SetAttentionIconFull(iconName, iconDesc string) {
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
iconDescC := C.CString(iconDesc)
defer C.free(unsafe.Pointer(iconDescC))
C.app_indicator_set_attention_icon_full(
indicator.indicator,
iconNameC,
iconDescC,
)
}
// SetMenu sets gtk.Menu for indicator.
//
// Note that this is mandatory for indicator to show and work properly.
func (indicator *Indicator) SetMenu(menu *gtk.Menu) {
C.app_indicator_set_menu(
indicator.indicator,
(*C.GtkMenu)(unsafe.Pointer(menu.Native())),
)
}
// SetIcon sets icon of indicator.
func (indicator *Indicator) SetIcon(iconName string) {
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
C.app_indicator_set_icon(
indicator.indicator,
iconNameC,
)
}
// SetIconFull sets icon of indicator with description.
func (indicator *Indicator) SetIconFull(iconName, iconDesc string) {
iconNameC := C.CString(iconName)
defer C.free(unsafe.Pointer(iconNameC))
iconDescC := C.CString(iconDesc)
defer C.free(unsafe.Pointer(iconDescC))
C.app_indicator_set_icon_full(
indicator.indicator,
iconNameC,
iconDescC,
)
}
// SetLabel sets label of indicator.
//
// Second parameter "guide" is used to set maximum width for label.
// Don't know if it works. Feel free to pass empty string.
func (indicator *Indicator) SetLabel(label, guide string) {
labelC := C.CString(label)
defer C.free(unsafe.Pointer(labelC))
guideC := C.CString(guide)
defer C.free(unsafe.Pointer(guideC))
C.app_indicator_set_label(
indicator.indicator,
labelC,
guideC,
)
}
// SetIconThemePath sets icon theme path of indicator.
func (indicator *Indicator) SetIconThemePath(iconThemePath string) {
iconThemePathC := C.CString(iconThemePath)
defer C.free(unsafe.Pointer(iconThemePathC))
C.app_indicator_set_icon_theme_path(
indicator.indicator,
iconThemePathC,
)
}
// SetOrderingIndex sets ordering index of indicator.
//
// It may or may not work.
func (indicator *Indicator) SetOrderingIndex(orderingIndex uint) {
orderingIndexC := C.guint(orderingIndex)
C.app_indicator_set_ordering_index(
indicator.indicator,
orderingIndexC,
)
}
// SetSecondaryActivateTarget sets which menu item's callback
// will be activated when middle clicked indicator.
func (indicator *Indicator) SetSecondaryActivateTarget(menuItem *gtk.MenuItem) {
C.app_indicator_set_secondary_activate_target(
indicator.indicator,
(*C.GtkWidget)(unsafe.Pointer(menuItem.Native())),
)
}
// SetTitle sets title of indicator.
func (indicator *Indicator) SetTitle(title string) {
titleC := C.CString(title)
defer C.free(unsafe.Pointer(titleC))
C.app_indicator_set_title(
indicator.indicator,
titleC,
)
}
/* Get properties */
// GetId returns the ID of indicator specified in New() or NewWithPath().
func (indicator *Indicator) GetId() string {
return C.GoString(
C.app_indicator_get_id(
indicator.indicator,
),
)
}
// GetCategory returns the category of indicator specified in New() or NewWithPath().
func (indicator *Indicator) GetCategory() Category {
return Category(
C.app_indicator_get_category(
indicator.indicator,
),
)
}
// GetStatus returns current status of indicator.
func (indicator *Indicator) GetStatus() Status {
return Status(
C.app_indicator_get_status(
indicator.indicator,
),
)
}
// GetIcon returns current icon of indicator.
func (indicator *Indicator) GetIcon() string {
return C.GoString(
C.app_indicator_get_icon(
indicator.indicator,
),
)
}
// GetIconDesc returns current icon description of indicator.
func (indicator *Indicator) GetIconDesc() string {
return C.GoString(
C.app_indicator_get_icon_desc(
indicator.indicator,
),
)
}
// GetIconThemePath returns current icon theme path of indicator.
func (indicator *Indicator) GetIconThemePath() string {
return C.GoString(
C.app_indicator_get_icon_theme_path(
indicator.indicator,
),
)
}
// GetAttentionIcon returns current attention icon of indicator.
func (indicator *Indicator) GetAttentionIcon() string {
return C.GoString(
C.app_indicator_get_attention_icon(
indicator.indicator,
),
)
}
// GetAttentionIconDesc returns current attention icon description of indicator.
func (indicator *Indicator) GetAttentionIconDesc() string {
return C.GoString(
C.app_indicator_get_attention_icon_desc(
indicator.indicator,
),
)
}
// GetTitle returns current title of indicator.
func (indicator *Indicator) GetTitle() string {
return C.GoString(
C.app_indicator_get_title(
indicator.indicator,
),
)
}
// GetMenu returns pointer to gtk.Menu.
func (indicator *Indicator) GetMenu() *gtk.Menu {
object := glib.Take(unsafe.Pointer(
C.app_indicator_get_menu(
indicator.indicator,
),
))
fn := gtk.WrapMap["GtkMenu"].(func(*glib.Object) *gtk.Menu)
return fn(object)
}
// GetLabel returns current label of indicator.
func (indicator *Indicator) GetLabel() string {
return C.GoString(
C.app_indicator_get_label(
indicator.indicator,
),
)
}
// GetLabelGuide returns current label guide of indicator.
func (indicator *Indicator) GetLabelGuide() string {
return C.GoString(
C.app_indicator_get_label_guide(
indicator.indicator,
),
)
}
// GetOrderingIndex returns current ordering index of indicator.
func (indicator *Indicator) GetOrderingIndex() uint {
return uint(
C.app_indicator_get_ordering_index(
indicator.indicator,
),
)
}
// GetSecondaryActivateTarget returns pointer to gtk.MenuItem which
// connected callback is activated when middle clicked indicator.
func (indicator *Indicator) GetSecondaryActivateTarget() *gtk.MenuItem {
object := glib.Take(unsafe.Pointer(
C.app_indicator_get_secondary_activate_target(
indicator.indicator,
),
))
fn := gtk.WrapMap["GtkMenuItem"].(func(*glib.Object) *gtk.MenuItem)
return fn(object)
}
/* Helpers */
// BuildMenuFromDesktop builds whole menu with items from desktop file.
//
// To be honest I don't know how it works.
func (indicator *Indicator) BuildMenuFromDesktop(desktopFile, desktopProfile string) {
desktopFileC := C.CString(desktopFile)
defer C.free(unsafe.Pointer(desktopFileC))
desktopProfileC := C.CString(desktopProfile)
defer C.free(unsafe.Pointer(desktopProfileC))
C.app_indicator_build_menu_from_desktop(
indicator.indicator,
desktopFileC,
desktopProfileC,
)
}