-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainWindow.go
92 lines (78 loc) · 2.1 KB
/
mainWindow.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
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"image"
"image/png"
"os"
)
func makeClockToggleButton(application fyne.App) *widget.Button {
var clockWindow fyne.Window
clockWindowHidden := true
button := widget.NewButton("Clockit", func() {
// And for giggles, something that updates itself
if clockWindow == nil {
clockWindow = createClockWindow(application)
clockWindowHidden = false
// we have a new window, let's intercept the close action so that we can handle that
clockWindow.SetOnClosed(func() {
// if we close this, reset the state correctly
clockWindow = nil
})
} else {
if clockWindowHidden {
clockWindow.Show()
} else {
clockWindow.Hide()
}
// toggle the clock window state
clockWindowHidden = !clockWindowHidden
}
})
return button
}
func getGoLogoImage() image.Image {
imgFile, err := os.Open("./resources/go_logo_png.png")
defer func(imgFile *os.File) {
err := imgFile.Close()
if err != nil {
fmt.Println("Closing the file went quite meh!", err)
}
}(imgFile)
if err != nil {
fmt.Println("Oh snap! Where's the image dude?", err)
}
imgData, err := png.Decode(imgFile)
if err != nil {
fmt.Println("Ah no. The PNG is weird!", err)
}
return imgData
}
func createGoLogoCanvasObject() fyne.CanvasObject {
logoImage := canvas.NewImageFromImage(getGoLogoImage())
logoImage.FillMode = canvas.ImageFillOriginal
return logoImage
}
func createMainWindow(windowTitle string, application fyne.App) {
clockButton := makeClockToggleButton(application)
mainWindow := application.NewWindow(windowTitle)
mainWindow.SetMaster() // if we close this chap, it's over folks!
mainWindow.SetContent(container.NewVBox(
widget.NewLabel("This is our super boring label. Yeah, it sucks."),
clockButton,
createGoLogoCanvasObject(),
widget.NewButton("What?", func() {
createAnotherWindow(application)
}),
))
// Let's set the size and position of the window
mainWindow.Resize(fyne.Size{
Width: 800,
Height: 600,
})
mainWindow.CenterOnScreen()
mainWindow.Show()
}