forked from fyne-io/fyne
/
window.go
855 lines (745 loc) · 18.8 KB
/
window.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
package gl
import (
"bytes"
"image"
_ "image/png" // for the icon
"os"
"runtime"
"strconv"
"time"
"fyne.io/fyne"
"fyne.io/fyne/driver/desktop"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
"github.com/go-gl/gl/v3.2-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
const (
scrollSpeed = 10
)
var (
defaultCursor, entryCursor, hyperlinkCursor *glfw.Cursor
)
func initCursors() {
defaultCursor = glfw.CreateStandardCursor(glfw.ArrowCursor)
entryCursor = glfw.CreateStandardCursor(glfw.IBeamCursor)
hyperlinkCursor = glfw.CreateStandardCursor(glfw.HandCursor)
}
type window struct {
viewport *glfw.Window
painted int // part of the macOS GL fix, updated GLFW should fix this
canvas *glCanvas
title string
icon fyne.Resource
clipboard fyne.Clipboard
master bool
fullScreen bool
fixedSize bool
padded bool
visible bool
mousePos fyne.Position
onClosed func()
xpos, ypos int
ignoreResize bool
}
func (w *window) Title() string {
return w.title
}
func (w *window) SetTitle(title string) {
w.title = title
runOnMainAsync(func() {
w.viewport.SetTitle(title)
})
}
func (w *window) FullScreen() bool {
return w.fullScreen
}
func (w *window) SetFullScreen(full bool) {
w.fullScreen = full
if !w.visible {
return
}
runOnMainAsync(func() {
monitor := w.getMonitorForWindow()
mode := monitor.GetVideoMode()
if full {
w.viewport.SetMonitor(monitor, 0, 0, mode.Width, mode.Height, mode.RefreshRate)
} else {
min := w.canvas.content.MinSize()
winWidth, winHeight := scaleInt(w.canvas, min.Width), scaleInt(w.canvas, min.Height)
w.viewport.SetMonitor(nil, 0, 0, winWidth, winHeight, 0) // TODO remember position?
}
})
}
func (w *window) CenterOnScreen() {
viewWidth, viewHeight := w.minSizeOnScreen()
runOnMainAsync(func() {
// get window dimensions in pixels
monitor := w.getMonitorForWindow()
monMode := monitor.GetVideoMode()
// these come into play when dealing with multiple monitors
monX, monY := monitor.GetPos()
// math them to the middle
newX := (monMode.Width / 2) - (viewWidth / 2) + monX
newY := (monMode.Height / 2) - (viewHeight / 2) + monY
// set new window coordinates
w.viewport.SetPos(newX, newY)
})
}
// minSizeOnScreen gets the size of a window content in screen pixels
func (w *window) minSizeOnScreen() (int, int) {
// get current size of content inside the window
winContentSize := w.canvas.content.MinSize()
// add padding, if required
if w.Padded() {
pad := theme.Padding() * 2
winContentSize = fyne.NewSize(winContentSize.Width+pad, winContentSize.Height+pad)
}
// calculate how many pixels will be used at this scale
viewWidth := scaleInt(w.canvas, winContentSize.Width)
viewHeight := scaleInt(w.canvas, winContentSize.Height)
return viewWidth, viewHeight
}
func (w *window) RequestFocus() {
runOnMainAsync(func() {
err := w.viewport.Focus()
if err != nil {
fyne.LogError("Error requesting focus", err)
}
})
}
func (w *window) Resize(size fyne.Size) {
runOnMainAsync(func() {
scale := w.canvas.Scale()
w.viewport.SetSize(int(float32(size.Width)*scale), int(float32(size.Height)*scale))
})
}
func (w *window) FixedSize() bool {
return w.fixedSize
}
func (w *window) SetFixedSize(fixed bool) {
w.fixedSize = fixed
runOnMainAsync(w.fitContent)
}
func (w *window) Padded() bool {
return w.padded
}
func (w *window) SetPadded(padded bool) {
w.padded = padded
if w.canvas.content == nil {
return
}
if padded {
w.canvas.content.Move(fyne.NewPos(theme.Padding(), theme.Padding()))
} else {
w.canvas.content.Move(fyne.NewPos(0, 0))
}
runOnMainAsync(w.fitContent)
}
func (w *window) Icon() fyne.Resource {
if w.icon == nil {
return fyne.CurrentApp().Icon()
}
return w.icon
}
func (w *window) SetIcon(icon fyne.Resource) {
w.icon = icon
}
func (w *window) fitContent() {
if w.canvas.content == nil {
return
}
winWidth, winHeight := w.minSizeOnScreen()
if w.fixedSize {
w.viewport.SetSizeLimits(winWidth, winHeight, winWidth, winHeight)
} else {
w.viewport.SetSizeLimits(winWidth, winHeight, glfw.DontCare, glfw.DontCare)
}
width, height := w.viewport.GetSize()
if width < winWidth || height < winHeight {
w.viewport.SetSize(fyne.Max(width, winWidth), fyne.Max(height, winHeight))
}
}
func (w *window) SetOnClosed(closed func()) {
w.onClosed = closed
}
func scaleForDpi(xdpi int) float32 {
if xdpi > 1000 { // assume that this is a mistake and bail
return float32(1.0)
}
if xdpi > 192 {
return float32(1.5)
} else if xdpi > 144 {
return float32(1.35)
} else if xdpi > 120 {
return float32(1.2)
}
return float32(1.0)
}
func (w *window) getMonitorForWindow() *glfw.Monitor {
for _, monitor := range glfw.GetMonitors() {
x, y := monitor.GetPos()
if x > w.xpos || y > w.ypos {
continue
}
if x+monitor.GetVideoMode().Width <= w.xpos || y+monitor.GetVideoMode().Height <= w.ypos {
continue
}
return monitor
}
// try built-in function to detect monitor if above logic didn't succeed
// if it doesn't work then return primary monitor as default
monitor := w.viewport.GetMonitor()
if monitor == nil {
monitor = glfw.GetPrimaryMonitor()
}
return monitor
}
func (w *window) detectScale() float32 {
env := os.Getenv("FYNE_SCALE")
if env != "" {
scale, err := strconv.ParseFloat(env, 32)
if err != nil {
fyne.LogError("Error reading scale", err)
} else if scale != 0 {
return float32(scale)
}
}
monitor := w.getMonitorForWindow()
widthMm, _ := monitor.GetPhysicalSize()
widthPx := monitor.GetVideoMode().Width
dpi := float32(widthPx) / (float32(widthMm) / 25.4)
return scaleForDpi(int(dpi))
}
func (w *window) Show() {
runOnMainAsync(func() {
w.visible = true
w.viewport.Show()
if w.fullScreen {
w.SetFullScreen(true)
}
})
}
func (w *window) Hide() {
runOnMainAsync(func() {
w.viewport.Hide()
w.visible = false
})
}
func (w *window) Close() {
w.closed(w.viewport)
}
func (w *window) ShowAndRun() {
w.Show()
fyne.CurrentApp().Driver().Run()
}
//Clipboard returns the system clipboard
func (w *window) Clipboard() fyne.Clipboard {
if w.clipboard == nil {
w.clipboard = &clipboard{window: w.viewport}
}
return w.clipboard
}
func (w *window) Content() fyne.CanvasObject {
return w.canvas.content
}
func (w *window) resize(size fyne.Size) {
if w.Padded() {
pad := theme.Padding() * 2
size = fyne.NewSize(size.Width-pad, size.Height-pad)
}
w.canvas.content.Resize(size)
w.canvas.setDirty(true)
}
func (w *window) SetContent(content fyne.CanvasObject) {
w.canvas.SetContent(content)
min := content.MinSize()
if w.Padded() {
pad := theme.Padding() * 2
min = fyne.NewSize(min.Width+pad, min.Height+pad)
}
runOnMain(func() {
w.fitContent()
w.resize(min)
})
}
func (w *window) Canvas() fyne.Canvas {
return w.canvas
}
func (w *window) closed(viewport *glfw.Window) {
viewport.SetShouldClose(true)
w.canvas.walkObjects(w.canvas.content, fyne.NewPos(0, 0), func(obj fyne.CanvasObject, _ fyne.Position) {
switch co := obj.(type) {
case fyne.Widget:
widget.DestroyRenderer(co)
}
})
// trigger callbacks
if w.onClosed != nil {
w.onClosed()
}
}
func (w *window) moved(viewport *glfw.Window, x, y int) {
// save coordinates
w.xpos, w.ypos = x, y
scale := w.canvas.scale
newScale := w.detectScale()
if scale == newScale {
return
}
contentSize := w.canvas.content.Size()
w.canvas.SetScale(newScale)
// this can trigger resize events that we need to ignore
w.ignoreResize = true
w.fitContent()
w.canvas.content.Resize(contentSize)
w.canvas.setDirty(true)
if w.Padded() {
pad := theme.Padding() * 2
contentSize = fyne.NewSize(contentSize.Width+pad, contentSize.Height+pad)
}
newWidth, newHeight := scaleInt(w.canvas, contentSize.Width),
scaleInt(w.canvas, contentSize.Height)
w.viewport.SetSize(newWidth, newHeight)
go func() {
// attempt the resizing until it works... (Gnome and others don't support resize whist moving)
for ww, wh := viewport.GetSize(); ww != newWidth || wh != newHeight; {
time.Sleep(time.Second / 10)
runOnMain(func() {
w.viewport.SetSize(newWidth, newHeight)
ww, wh = viewport.GetSize()
})
}
w.ignoreResize = false
}()
}
func (w *window) resized(viewport *glfw.Window, width, height int) {
if w.ignoreResize {
return
}
w.resize(fyne.NewSize(unscaleInt(w.canvas, width), unscaleInt(w.canvas, height)))
}
func (w *window) frameSized(viewport *glfw.Window, width, height int) {
winWidth, _ := w.viewport.GetSize()
w.canvas.texScale = float32(width) / float32(winWidth) // This will be > 1.0 on a HiDPI screen
gl.Viewport(0, 0, int32(width), int32(height))
}
func (w *window) refresh(viewport *glfw.Window) {
updateWinSize(w)
w.canvas.setDirty(true)
}
func findMouseObj(canvas *glCanvas, mouse fyne.Position, scroll bool) (fyne.CanvasObject, int, int) {
found := canvas.content
foundX, foundY := 0, 0
canvas.walkObjects(canvas.content, fyne.NewPos(0, 0), func(walked fyne.CanvasObject, pos fyne.Position) {
if mouse.X < pos.X || mouse.Y < pos.Y {
return
}
x2 := pos.X + walked.Size().Width
y2 := pos.Y + walked.Size().Height
if mouse.X >= x2 || mouse.Y >= y2 {
return
}
if !walked.Visible() {
return
}
if scroll {
if _, ok := walked.(fyne.Scrollable); ok {
found = walked
foundX, foundY = pos.X, pos.Y
}
return
}
switch walked.(type) {
case fyne.Tappable, desktop.Mouseable:
found = walked
foundX, foundY = pos.X, pos.Y
case fyne.Focusable:
found = walked
foundX, foundY = pos.X, pos.Y
}
})
return found, foundX, foundY
}
func (w *window) mouseMoved(viewport *glfw.Window, xpos float64, ypos float64) {
w.mousePos = fyne.NewPos(unscaleInt(w.canvas, int(xpos)), unscaleInt(w.canvas, int(ypos)))
co, _, _ := findMouseObj(w.canvas, w.mousePos, false)
cursor := defaultCursor
switch wid := co.(type) {
case *widget.Entry:
if !wid.ReadOnly {
cursor = entryCursor
}
case *widget.Hyperlink:
cursor = hyperlinkCursor
}
runOnMainAsync(func() {
viewport.SetCursor(cursor)
})
}
func (w *window) mouseClicked(viewport *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
co, x, y := findMouseObj(w.canvas, w.mousePos, false)
ev := new(fyne.PointEvent)
pad := 0
if w.padded {
pad = theme.Padding()
}
ev.Position = fyne.NewPos(w.mousePos.X-pad-x, w.mousePos.Y-pad-y)
if wid, ok := co.(desktop.Mouseable); ok {
mev := new(desktop.MouseEvent)
mev.Position = ev.Position
mev.Button = convertMouseButton(button)
if action == glfw.Press {
go wid.MouseDown(mev)
} else if action == glfw.Release {
go wid.MouseUp(mev)
}
}
switch wid := co.(type) {
case fyne.Tappable:
if action == glfw.Press {
switch button {
case glfw.MouseButtonRight:
go wid.TappedSecondary(ev)
default:
go wid.Tapped(ev)
}
}
case fyne.Focusable:
w.canvas.Focus(wid)
}
}
func (w *window) mouseScrolled(viewport *glfw.Window, xoff float64, yoff float64) {
co, _, _ := findMouseObj(w.canvas, w.mousePos, true)
switch wid := co.(type) {
case fyne.Scrollable:
ev := &fyne.ScrollEvent{}
ev.DeltaX = int(xoff * scrollSpeed)
ev.DeltaY = int(yoff * scrollSpeed)
wid.Scrolled(ev)
}
}
func convertMouseButton(button glfw.MouseButton) desktop.MouseButton {
switch button {
case glfw.MouseButton1:
return desktop.LeftMouseButton
case glfw.MouseButton2:
return desktop.RightMouseButton
default:
return 0
}
}
func keyToName(key glfw.Key) fyne.KeyName {
switch key {
// non-printable
case glfw.KeyEscape:
return fyne.KeyEscape
case glfw.KeyEnter:
return fyne.KeyReturn
case glfw.KeyTab:
return fyne.KeyTab
case glfw.KeyBackspace:
return fyne.KeyBackspace
case glfw.KeyInsert:
return fyne.KeyInsert
case glfw.KeyDelete:
return fyne.KeyDelete
case glfw.KeyRight:
return fyne.KeyRight
case glfw.KeyLeft:
return fyne.KeyLeft
case glfw.KeyDown:
return fyne.KeyDown
case glfw.KeyUp:
return fyne.KeyUp
case glfw.KeyPageUp:
return fyne.KeyPageUp
case glfw.KeyPageDown:
return fyne.KeyPageDown
case glfw.KeyHome:
return fyne.KeyHome
case glfw.KeyEnd:
return fyne.KeyEnd
case glfw.KeyF1:
return fyne.KeyF1
case glfw.KeyF2:
return fyne.KeyF2
case glfw.KeyF3:
return fyne.KeyF3
case glfw.KeyF4:
return fyne.KeyF4
case glfw.KeyF5:
return fyne.KeyF5
case glfw.KeyF6:
return fyne.KeyF6
case glfw.KeyF7:
return fyne.KeyF7
case glfw.KeyF8:
return fyne.KeyF8
case glfw.KeyF9:
return fyne.KeyF9
case glfw.KeyF10:
return fyne.KeyF10
case glfw.KeyF11:
return fyne.KeyF11
case glfw.KeyF12:
return fyne.KeyF12
case glfw.KeyKPEnter:
return fyne.KeyEnter
// printable
case glfw.KeyA:
return fyne.KeyA
case glfw.KeyB:
return fyne.KeyB
case glfw.KeyC:
return fyne.KeyC
case glfw.KeyD:
return fyne.KeyD
case glfw.KeyE:
return fyne.KeyE
case glfw.KeyF:
return fyne.KeyF
case glfw.KeyG:
return fyne.KeyG
case glfw.KeyH:
return fyne.KeyH
case glfw.KeyI:
return fyne.KeyI
case glfw.KeyJ:
return fyne.KeyJ
case glfw.KeyK:
return fyne.KeyK
case glfw.KeyL:
return fyne.KeyL
case glfw.KeyM:
return fyne.KeyM
case glfw.KeyN:
return fyne.KeyN
case glfw.KeyO:
return fyne.KeyO
case glfw.KeyP:
return fyne.KeyP
case glfw.KeyQ:
return fyne.KeyQ
case glfw.KeyR:
return fyne.KeyR
case glfw.KeyS:
return fyne.KeyS
case glfw.KeyT:
return fyne.KeyT
case glfw.KeyU:
return fyne.KeyU
case glfw.KeyV:
return fyne.KeyV
case glfw.KeyW:
return fyne.KeyW
case glfw.KeyX:
return fyne.KeyX
case glfw.KeyY:
return fyne.KeyY
case glfw.KeyZ:
return fyne.KeyZ
case glfw.Key0:
return fyne.Key0
case glfw.Key1:
return fyne.Key1
case glfw.Key2:
return fyne.Key2
case glfw.Key3:
return fyne.Key3
case glfw.Key4:
return fyne.Key4
case glfw.Key5:
return fyne.Key5
case glfw.Key6:
return fyne.Key6
case glfw.Key7:
return fyne.Key7
case glfw.Key8:
return fyne.Key8
case glfw.Key9:
return fyne.Key9
// desktop
case glfw.KeyLeftShift:
return desktop.KeyShiftLeft
case glfw.KeyRightShift:
return desktop.KeyShiftRight
case glfw.KeyLeftControl:
return desktop.KeyControlLeft
case glfw.KeyRightControl:
return desktop.KeyControlRight
case glfw.KeyLeftAlt:
return desktop.KeyAltLeft
case glfw.KeyRightAlt:
return desktop.KeyAltRight
case glfw.KeyLeftSuper:
return desktop.KeySuperLeft
case glfw.KeyRightSuper:
return desktop.KeySuperRight
case glfw.KeyMenu:
return desktop.KeyMenu
}
return ""
}
func (w *window) keyPressed(viewport *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
keyName := keyToName(key)
keyEvent := &fyne.KeyEvent{Name: keyName}
if action == glfw.Press {
if w.canvas.Focused() != nil {
if focused, ok := w.canvas.Focused().(desktop.Keyable); ok {
go focused.KeyDown(keyEvent)
}
} else if w.canvas.onKeyDown != nil {
go w.canvas.onKeyDown(keyEvent)
}
} else { // ignore key up / repeat in core events
if action == glfw.Release {
if w.canvas.Focused() != nil {
if focused, ok := w.canvas.Focused().(desktop.Keyable); ok {
go focused.KeyUp(keyEvent)
}
} else if w.canvas.onKeyDown != nil {
go w.canvas.onKeyUp(keyEvent)
}
}
return
}
keyDesktopModifier := desktopModifier(mods)
var shortcut fyne.Shortcut
ctrlMod := desktop.ControlModifier
if runtime.GOOS == "darwin" {
ctrlMod = desktop.SuperModifier
}
if keyDesktopModifier == ctrlMod {
switch keyName {
case fyne.KeyV:
// detect paste shortcut
shortcut = &fyne.ShortcutPaste{
Clipboard: w.Clipboard(),
}
case fyne.KeyC:
// detect copy shortcut
shortcut = &fyne.ShortcutCopy{
Clipboard: w.Clipboard(),
}
case fyne.KeyX:
// detect cut shortcut
shortcut = &fyne.ShortcutCut{
Clipboard: w.Clipboard(),
}
}
}
if keyDesktopModifier != 0 {
shortcut = &desktop.CustomShortcut{
KeyName: keyName,
Modifier: keyDesktopModifier,
}
}
if shortcutable, ok := w.canvas.Focused().(fyne.Shortcutable); ok {
if shortcutable.TypedShortcut(shortcut) {
return
}
} else if w.canvas.shortcut.TypedShortcut(shortcut) {
return
}
// No shortcut detected, pass down to TypedKey
if w.canvas.Focused() != nil {
go w.canvas.Focused().TypedKey(keyEvent)
} else if w.canvas.onTypedKey != nil {
go w.canvas.onTypedKey(keyEvent)
}
}
func desktopModifier(mods glfw.ModifierKey) desktop.Modifier {
var m desktop.Modifier
if (mods & glfw.ModShift) != 0 {
m |= desktop.ShiftModifier
}
if (mods & glfw.ModControl) != 0 {
m |= desktop.ControlModifier
}
if (mods & glfw.ModAlt) != 0 {
m |= desktop.AltModifier
}
if (mods & glfw.ModSuper) != 0 {
m |= desktop.SuperModifier
}
return m
}
// charModInput defines the character with modifiers callback which is called when a
// Unicode character is input regardless of what modifier keys are used.
//
// The character with modifiers callback is intended for implementing custom
// Unicode character input. Characters do not map 1:1 to physical keys,
// as a key may produce zero, one or more characters.
func (w *window) charModInput(viewport *glfw.Window, char rune, mods glfw.ModifierKey) {
if mods != 0 && mods != glfw.ModShift { // don't progress if it's part of a combination
return
}
if w.canvas.Focused() == nil && w.canvas.onTypedRune == nil {
return
}
if w.canvas.Focused() != nil {
w.canvas.Focused().TypedRune(char)
} else if w.canvas.onTypedRune != nil {
w.canvas.onTypedRune(char)
}
}
func (d *gLDriver) CreateWindow(title string) fyne.Window {
var ret *window
runOnMain(func() {
master := len(d.windows) == 0
if master {
err := glfw.Init()
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}
initCursors()
}
// make the window hidden, we will set it up and then show it later
glfw.WindowHint(glfw.Visible, 0)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 0)
win, err := glfw.CreateWindow(10, 10, title, nil, nil)
if err != nil {
fyne.LogError("window creation error", err)
return
}
win.MakeContextCurrent()
iconRes := fyne.CurrentApp().Icon()
if iconRes != nil {
icon, _, _ := image.Decode(bytes.NewReader(iconRes.Content()))
win.SetIcon([]image.Image{icon})
}
if master {
err := gl.Init()
if err != nil {
fyne.LogError("failed to initialise OpenGL", err)
return
}
gl.Disable(gl.DEPTH_TEST)
}
ret = &window{viewport: win, title: title}
ret.canvas = newCanvas(ret)
ret.master = master
ret.padded = true
ret.canvas.SetScale(ret.detectScale())
ret.canvas.texScale = 1.0
d.windows = append(d.windows, ret)
win.SetCloseCallback(ret.closed)
win.SetPosCallback(ret.moved)
win.SetSizeCallback(ret.resized)
win.SetFramebufferSizeCallback(ret.frameSized)
win.SetRefreshCallback(ret.refresh)
win.SetCursorPosCallback(ret.mouseMoved)
win.SetMouseButtonCallback(ret.mouseClicked)
win.SetScrollCallback(ret.mouseScrolled)
win.SetKeyCallback(ret.keyPressed)
win.SetCharModsCallback(ret.charModInput)
glfw.DetachCurrentContext()
})
return ret
}
func (d *gLDriver) AllWindows() []fyne.Window {
return d.windows
}