forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
174 lines (144 loc) · 4.19 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
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
// 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/v2/app"
import (
"os"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/app"
intRepo "fyne.io/fyne/v2/internal/repository"
"fyne.io/fyne/v2/storage/repository"
)
// Declare conformity with App interface
var _ fyne.App = (*fyneApp)(nil)
type fyneApp struct {
driver fyne.Driver
icon fyne.Resource
uniqueID string
cloud fyne.CloudProvider
lifecycle app.Lifecycle
settings *settings
storage fyne.Storage
prefs fyne.Preferences
}
func (a *fyneApp) CloudProvider() fyne.CloudProvider {
return a.cloud
}
func (a *fyneApp) Icon() fyne.Resource {
if a.icon != nil {
return a.icon
}
if a.Metadata().Icon == nil || len(a.Metadata().Icon.Content()) == 0 {
return nil
}
return a.Metadata().Icon
}
func (a *fyneApp) SetIcon(icon fyne.Resource) {
a.icon = icon
}
func (a *fyneApp) UniqueID() string {
if a.uniqueID != "" {
return a.uniqueID
}
if a.Metadata().ID != "" {
return a.Metadata().ID
}
fyne.LogError("Preferences API requires a unique ID, use app.NewWithID() or the FyneApp.toml ID field", nil)
a.uniqueID = "missing-id-" + strconv.FormatInt(time.Now().Unix(), 10) // This is a fake unique - it just has to not be reused...
return a.uniqueID
}
func (a *fyneApp) NewWindow(title string) fyne.Window {
return a.driver.CreateWindow(title)
}
func (a *fyneApp) Run() {
go a.lifecycle.RunEventQueue()
a.driver.Run()
}
func (a *fyneApp) Quit() {
for _, window := range a.driver.AllWindows() {
window.Close()
}
a.driver.Quit()
a.settings.stopWatching()
}
func (a *fyneApp) Driver() fyne.Driver {
return a.driver
}
// Settings returns the application settings currently configured.
func (a *fyneApp) Settings() fyne.Settings {
return a.settings
}
func (a *fyneApp) Storage() fyne.Storage {
return a.storage
}
func (a *fyneApp) Preferences() fyne.Preferences {
if a.UniqueID() == "" {
fyne.LogError("Preferences API requires a unique ID, use app.NewWithID() or the FyneApp.toml ID field", nil)
}
return a.prefs
}
func (a *fyneApp) Lifecycle() fyne.Lifecycle {
return &a.lifecycle
}
func (a *fyneApp) newDefaultPreferences() *preferences {
p := newPreferences(a)
if a.uniqueID != "" {
p.load()
}
return p
}
// New returns a new application instance with the default driver and no unique ID (unless specified in FyneApp.toml)
func New() fyne.App {
if meta.ID == "" {
checkLocalMetadata() // if no ID passed, check if it was in toml
if meta.ID == "" {
internal.LogHint("Applications should be created with a unique ID using app.NewWithID()")
}
}
return NewWithID(meta.ID)
}
func makeStoreDocs(id string, s *store) *internal.Docs {
if id == "" {
return &internal.Docs{} // an empty impl to avoid crashes
}
if root := s.a.storageRoot(); root != "" {
err := os.MkdirAll(root, 0755) // make the space before anyone can use it
if err != nil {
fyne.LogError("Failed to create app storage space", err)
}
root, _ := s.docRootURI()
return &internal.Docs{RootDocURI: root}
} else {
return &internal.Docs{} // an empty impl to avoid crashes
}
}
func newAppWithDriver(d fyne.Driver, id string) fyne.App {
newApp := &fyneApp{uniqueID: id, driver: d}
fyne.SetCurrentApp(newApp)
newApp.prefs = newApp.newDefaultPreferences()
newApp.lifecycle.InitEventQueue()
newApp.lifecycle.SetOnStoppedHookExecuted(func() {
if prefs, ok := newApp.prefs.(*preferences); ok {
prefs.forceImmediateSave()
}
})
newApp.settings = loadSettings()
store := &store{a: newApp}
store.Docs = makeStoreDocs(id, store)
newApp.storage = store
if !d.Device().IsMobile() {
newApp.settings.watchSettings()
}
httpHandler := intRepo.NewHTTPRepository()
repository.Register("http", httpHandler)
repository.Register("https", httpHandler)
return newApp
}
// marker interface to pass system tray to supporting drivers
type systrayDriver interface {
SetSystemTrayMenu(*fyne.Menu)
SetSystemTrayIcon(resource fyne.Resource)
}