-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindow_linux.go
More file actions
488 lines (430 loc) · 11.8 KB
/
Window_linux.go
File metadata and controls
488 lines (430 loc) · 11.8 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// +build freebsd linux netbsd openbsd solaris
package frame
/*
#ifndef WEBVIEW_GTK
#define WEBVIEW_GTK
#endif
#include "c_linux.h"
typedef struct GSR {
GSourceFunc windowSetModal;
GSourceFunc windowUnsetModal;
GSourceFunc windowSetIcon;
GSourceFunc windowSetOpacity;
GSourceFunc windowSetType;
GSourceFunc windowSetDecorated;
GSourceFunc windowSetDeletable;
GSourceFunc windowKeepAbove;
GSourceFunc windowKeepBelow;
GSourceFunc windowIconify;
GSourceFunc windowStick;
GSourceFunc windowMaximize;
GSourceFunc windowFullscreen;
GSourceFunc windowSetTitle;
GSourceFunc windowSetSize;
GSourceFunc windowMove;
GSourceFunc windowSetBackgroundColor;
GSourceFunc windowSkipTaskbar;
GSourceFunc windowSkipPager;
GSourceFunc windowSetResizeble;
GSourceFunc windowSetCenter;
GSourceFunc windowShow;
GSourceFunc windowHide;
GSourceFunc evalJS;
GSourceFunc loadURI;
GSourceFunc loadHTML;
} GSR;
GSR fn = {
windowSetModal,
windowUnsetModal,
windowSetIcon,
windowSetOpacity,
windowSetType,
windowSetDecorated,
windowSetDeletable,
windowKeepAbove,
windowKeepBelow,
windowIconify,
windowStick,
windowMaximize,
windowFullscreen,
windowSetTitle,
windowSetSize,
windowMove,
windowSetBackgroundColor,
windowSkipTaskbar,
windowSkipPager,
windowSetResizeble,
windowSetCenter,
windowShow,
windowHide,
evalJS,
loadURI,
loadHTML
};
*/
import "C"
type (
// Window struct
Window struct {
id int64
window *C.GtkWidget
box *C.GtkWidget
webview *C.GtkWidget
menubar *C.GtkWidget
MainMenu *Menu
StateEvent func(State)
Invoke func(string)
app *App
state State
maxLimited bool
maxWidth int
maxHeight int
minWidth int
minHeight int
OnHide func()
OnShow func()
OnFocus func()
OnUnfocus func()
OnIconize func()
OnDeiconize func()
OnDemaximize func()
OnMaximize func()
OnFullscreen func()
OnExitFullscreen func()
}
// WindowType struct
WindowType int
// WindowPosition struct
WindowPosition int
// StrutPosition struct
StrutPosition int
)
const (
// Window types
TypeNormal = WindowType(C.GDK_WINDOW_TYPE_HINT_NORMAL) // Normal toplevel window.
TypeDialog = WindowType(C.GDK_WINDOW_TYPE_HINT_DIALOG) // Dialog window.
TypeMenu = WindowType(C.GDK_WINDOW_TYPE_HINT_MENU) // Window used to implement a menu; GTK+ uses this hint only for torn-off menus, see GtkTearoffMenuItem.
TypeToolbar = WindowType(C.GDK_WINDOW_TYPE_HINT_TOOLBAR) // Window used to implement toolbars.
TypeSplashscreen = WindowType(C.GDK_WINDOW_TYPE_HINT_SPLASHSCREEN) // Window used to display a splash screen during application startup.
TypeUtility = WindowType(C.GDK_WINDOW_TYPE_HINT_UTILITY) // Utility windows which are not detached toolbars or dialogs.
TypeDock = WindowType(C.GDK_WINDOW_TYPE_HINT_DOCK) // Used for creating dock or panel windows.
TypeDesktop = WindowType(C.GDK_WINDOW_TYPE_HINT_DESKTOP) // Used for creating the desktop background window.
TypeDropdownMenu = WindowType(C.GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU) // A menu that belongs to a menubar.
TypePopupMenu = WindowType(C.GDK_WINDOW_TYPE_HINT_POPUP_MENU) // A menu that does not belong to a menubar, e.g. a context menu.
TypeTooltip = WindowType(C.GDK_WINDOW_TYPE_HINT_TOOLTIP) // A tooltip.
TypeNotification = WindowType(C.GDK_WINDOW_TYPE_HINT_NOTIFICATION) // A notification - typically a “bubble” that belongs to a status icon.
TypeCombo = WindowType(C.GDK_WINDOW_TYPE_HINT_COMBO) // A popup from a combo box.
TypeDnd = WindowType(C.GDK_WINDOW_TYPE_HINT_DND) // A window that is used to implement a DND cursor.
StrutTop = StrutPosition(C.PANEL_WINDOW_POSITION_TOP)
StrutBottom = StrutPosition(C.PANEL_WINDOW_POSITION_BOTTOM)
StrutLeft = StrutPosition(C.PANEL_WINDOW_POSITION_LEFT)
StrutRight = StrutPosition(C.PANEL_WINDOW_POSITION_RIGHT)
)
func idle(fn C.GSourceFunc, data C.idleData) {
C.gdk_threads_add_idle(fn, C.gpointer(&data))
}
// SetBackgroundColor of the window
func (f *Window) SetBackgroundColor(r, g, b byte, alfa float64) *Window {
idle(C.fn.windowSetBackgroundColor, C.idleData{
window: f.window,
webview: f.webview,
rgba: C.GdkRGBA{
red: C.gdouble(float64(r) / 255),
green: C.gdouble(float64(g) / 255),
blue: C.gdouble(float64(b) / 255),
alpha: C.gdouble(alfa),
},
})
return f
}
// SetMinSize of window
func (f *Window) SetMinSize(width, height int) *Window {
f.minHeight = height
f.minWidth = width
C.setSizes(f.window, C.gint(C.int(f.maxWidth)), C.gint(C.int(f.maxHeight)), C.gint(C.int(f.minWidth)), C.gint(C.int(f.minHeight)))
return f
}
// GetInnerSize returns width and height of window webview content
func (f *Window) GetInnerSize() (width, height int) {
return f.GetSize()
}
// GetPosition returns position of window
func (f *Window) GetPosition() (x, y int) {
var cX, cY C.gint
C.gtk_window_get_position(C.to_GtkWindow(f.window), &cX, &cY)
x, y = int(cX), int(cY)
return
}
// SetMaxSize of window
func (f *Window) SetMaxSize(width, height int) *Window {
f.maxLimited = width > 0 || height > 0
monitorWidth, monitorHeight := f.GetScreenSize()
if height <= 0 {
height = monitorHeight
}
if width <= 0 {
width = monitorWidth
}
f.maxHeight = height
f.maxWidth = width
C.setSizes(f.window, C.gint(C.int(f.maxWidth)), C.gint(C.int(f.maxHeight)), C.gint(C.int(f.minWidth)), C.gint(C.int(f.minHeight)))
return f
}
// SetModal makes current Window attached as modal window to parent
func (f *Window) SetModal(parent *Window) *Window {
idle(C.fn.windowSetModal, C.idleData{
window: f.window,
windowParent: parent.window,
})
return f
}
// UnsetModal unset current Window as modal window from another Frames
func (f *Window) UnsetModal() *Window {
idle(C.fn.windowUnsetModal, C.idleData{
window: f.window,
})
return f
}
// SetIconFromFile for Window
func (f *Window) SetIconFromFile(filename string) *Window {
idle(C.fn.windowSetIcon, C.idleData{
window: f.window,
content: C.gcharptr(C.CString(filename)),
})
return f
}
// SetOpacity of window
func (f *Window) SetOpacity(opacity float64) *Window {
idle(C.fn.windowSetOpacity, C.idleData{
window: f.window,
dbl: C.gdouble(opacity),
})
return f
}
// SetType of window
func (f *Window) SetType(hint WindowType) *Window {
idle(C.fn.windowSetType, C.idleData{
window: f.window,
hint: C.int(int(hint)),
})
return f
}
// SetDecorated of window
func (f *Window) SetDecorated(flag bool) *Window {
idle(C.fn.windowSetDecorated, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// SetDeletable of window
func (f *Window) SetDeletable(flag bool) *Window {
idle(C.fn.windowSetDeletable, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// KeepAbove the window
func (f *Window) KeepAbove(flag bool) *Window {
idle(C.fn.windowKeepAbove, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// KeepBelow of window
func (f *Window) KeepBelow(flag bool) *Window {
idle(C.fn.windowKeepBelow, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// Iconify window
func (f *Window) Iconify(flag bool) *Window {
idle(C.fn.windowIconify, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// Stick window
func (f *Window) Stick(flag bool) *Window {
idle(C.fn.windowStick, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// Maximize window
func (f *Window) Maximize(flag bool) *Window {
idle(C.fn.windowMaximize, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// Fullscreen window
func (f *Window) Fullscreen(flag bool) *Window {
idle(C.fn.windowFullscreen, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// SetTitle of window
func (f *Window) SetTitle(title string) *Window {
idle(C.fn.windowSetTitle, C.idleData{
window: f.window,
content: C.gcharptr(C.CString(title)),
})
return f
}
// SetSize of the window
func (f *Window) SetSize(width, height int) *Window {
idle(C.fn.windowSetSize, C.idleData{
window: f.window,
width: C.int(width),
height: C.int(height),
})
return f
}
// SetInnerSize sets size of webview (without titlebar)
func (f *Window) SetInnerSize(width, height int) *Window {
return f.SetSize(width, height)
}
// Move the window
func (f *Window) Move(x, y int) *Window {
idle(C.fn.windowMove, C.idleData{
window: f.window,
x: C.int(x),
y: C.int(y),
})
return f
}
// SkipTaskbar of the window
func (f *Window) SkipTaskbar(flag bool) *Window {
idle(C.fn.windowSkipTaskbar, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// SkipPager of the window
func (f *Window) SkipPager(flag bool) *Window {
idle(C.fn.windowSkipPager, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// SetResizeble of the window
func (f *Window) SetResizeble(flag bool) *Window {
idle(C.fn.windowSetResizeble, C.idleData{
window: f.window,
flag: gboolean(flag),
})
return f
}
// SetCenter of the window
func (f *Window) SetCenter() *Window {
width, height := f.GetSize()
sWidth, sHeight := f.GetScreenSize()
f.Move(sWidth/2-width/2, sHeight/2-height/2)
return f
}
// Show window
func (f *Window) Show() *Window {
idle(C.fn.windowShow, C.idleData{
window: f.window,
})
return f
}
// Hide window
func (f *Window) Hide() *Window {
idle(C.fn.windowHide, C.idleData{
window: f.window,
})
return f
}
// Eval JS
func (f *Window) Eval(js string) string {
cRet := cRequest(func(id uint64) {
idle(C.fn.evalJS, C.idleData{
webview: f.webview,
content: C.gcharptr(C.CString(js)),
req_id: C.ulonglong(id),
})
})
ret, _ := cRet.(string)
return ret
}
// Load URL to Window webview
func (f *Window) Load(uri string) *Window {
idle(C.fn.loadURI, C.idleData{
webview: f.webview,
uri: C.gcharptr(C.CString(uri)),
})
return f
}
// LoadHTML to Window webview
func (f *Window) LoadHTML(html string, baseURI string) *Window {
idle(C.fn.loadHTML, C.idleData{
webview: f.webview,
content: C.gcharptr(C.CString(html)),
uri: C.gcharptr(C.CString(baseURI)),
})
return f
}
// SetStateEvent set handler function for window state event
func (f *Window) SetStateEvent(fn func(State)) *Window {
f.StateEvent = fn
return f
}
// SetInvoke set handler function for window state event
func (f *Window) SetInvoke(fn func(string)) *Window {
f.Invoke = fn
return f
}
// GetScreenSize returns size of window monitor
func (f *Window) GetScreenSize() (width, height int) {
geometry := C.getMonitorSize(f.window)
width, height = int(geometry.width), int(geometry.height)
return
}
// GetScreenScaleFactor returns scale factor of window monitor
func (f *Window) GetScreenScaleFactor() float64 {
return float64(int(C.getMonitorScaleFactor(f.window)))
}
// GetSize returns width and height of window
func (f *Window) GetSize() (width, height int) {
var cWidth, cHeight C.gint
C.gtk_window_get_size(C.to_GtkWindow(f.window), &cWidth, &cHeight)
width, height = int(cWidth), int(cHeight)
return
}
// Strut reserves wind space on the screen
func (f *Window) Strut(position StrutPosition, size int) *Window {
monitorWidth, monitorHeight := f.GetScreenSize()
scale := f.GetScreenScaleFactor()
var width, height int
switch position {
case StrutBottom, StrutTop:
width, height = monitorWidth, size
case StrutLeft, StrutRight:
width, height = size, monitorHeight
}
f.UnsetModal()
C.windowStrut(C.gtk_widget_get_window(f.window), C.winPosition(position), C.int(width), C.int(height), C.int(monitorWidth), C.int(monitorHeight), C.int(scale))
switch position {
case StrutTop, StrutLeft:
f.Move(0, 0)
case StrutBottom:
f.Move(0, monitorHeight-height)
case StrutRight:
f.Move(monitorWidth-width, 0)
}
f.Stick(true).SetType(TypeDock)
return f
}