forked from fyne-io/fyne
/
app.go
123 lines (101 loc) · 2.69 KB
/
app.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
// Package app provides app implementations for working with Fyne graphical interfaces.
// The fastest way to get started is to call app.New() which will normally load a new desktop application.
// If the "ci" tag is passed to go (go run -tags ci myapp.go) it will run an in-memory application.
package app // import "fyne.io/fyne/app"
import (
"os"
"sync"
"fyne.io/fyne"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
type fyneApp struct {
driver fyne.Driver
icon fyne.Resource
settings fyne.Settings
running bool
runMutex sync.Mutex
}
func (app *fyneApp) Icon() fyne.Resource {
return app.icon
}
func (app *fyneApp) SetIcon(icon fyne.Resource) {
app.icon = icon
}
func (app *fyneApp) NewWindow(title string) fyne.Window {
return app.driver.CreateWindow(title)
}
func (app *fyneApp) Run() {
app.runMutex.Lock()
if app.running {
app.runMutex.Unlock()
return
}
app.running = true
app.runMutex.Unlock()
app.driver.Run()
}
func (app *fyneApp) Quit() {
for _, window := range app.driver.AllWindows() {
window.Close()
}
app.driver.Quit()
app.running = false
}
func (app *fyneApp) Driver() fyne.Driver {
return app.driver
}
// Settings returns the application settings currently configured.
func (app *fyneApp) Settings() fyne.Settings {
return app.settings
}
func (app *fyneApp) applyThemeTo(content fyne.CanvasObject, canvas fyne.Canvas) {
if themed, ok := content.(fyne.Themeable); ok {
themed.ApplyTheme()
canvas.Refresh(content)
}
if wid, ok := content.(fyne.Widget); ok {
widget.Renderer(wid).ApplyTheme()
canvas.Refresh(content)
for _, o := range widget.Renderer(wid).Objects() {
app.applyThemeTo(o, canvas)
}
}
if c, ok := content.(*fyne.Container); ok {
for _, o := range c.Objects {
app.applyThemeTo(o, canvas)
}
canvas.Refresh(c)
}
}
func (app *fyneApp) applyTheme() {
for _, window := range app.driver.AllWindows() {
window.Content().Move(fyne.NewPos(theme.Padding(), theme.Padding()))
app.applyThemeTo(window.Content(), window.Canvas())
}
}
func (app *fyneApp) setupTheme() {
env := os.Getenv("FYNE_THEME")
if env == "light" {
app.Settings().SetTheme(theme.LightTheme())
} else {
app.Settings().SetTheme(theme.DarkTheme())
}
}
// NewAppWithDriver initialises a new Fyne application using the specified
// driver and returns a handle to that App.
// Built in drivers are provided in the "driver" package.
func NewAppWithDriver(d fyne.Driver) fyne.App {
newApp := &fyneApp{driver: d, icon: theme.FyneLogo(), settings: loadSettings()}
fyne.SetCurrentApp(newApp)
newApp.setupTheme()
listener := make(chan fyne.Settings)
newApp.Settings().AddChangeListener(listener)
go func() {
for {
<-listener
newApp.applyTheme()
}
}()
return newApp
}