-
Notifications
You must be signed in to change notification settings - Fork 283
/
option.go
269 lines (236 loc) · 7.3 KB
/
option.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
package flutter
import (
"fmt"
"image"
"os"
"path/filepath"
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
)
type config struct {
flutterAssetsPath string
icuDataPath string
elfSnapshotpath string
vmArguments []string
windowIconProvider func() ([]image.Image, error)
windowInitialDimensions windowDimensions
windowInitialLocation windowLocation
windowDimensionLimits windowDimensionLimits
windowMode windowMode
windowAlwaysOnTop bool
windowTransparent bool
backOnEscape bool
forcePixelRatio float64
scrollAmount float64
plugins []Plugin
}
type windowDimensions struct {
width int
height int
}
type windowLocation struct {
xpos int
ypos int
}
type windowDimensionLimits struct {
minWidth int
minHeight int
maxWidth int
maxHeight int
}
// newApplicationConfig define the default configuration values for a new
// Application. These values may be changed at any time.
func newApplicationConfig() config {
execPath, err := execpath.ExecPath()
if err != nil {
fmt.Printf("go-flutter: failed to resolve path for executable: %v", err)
os.Exit(1)
}
return config{
windowInitialDimensions: windowDimensions{
width: 800,
height: 600,
},
windowMode: WindowModeDefault,
windowAlwaysOnTop: false,
windowTransparent: false,
scrollAmount: 100.0,
backOnEscape: true,
// Sane configuration values for the engine.
flutterAssetsPath: filepath.Join(filepath.Dir(execPath), "flutter_assets"),
icuDataPath: filepath.Join(filepath.Dir(execPath), "icudtl.dat"),
// only required for AOT app.
elfSnapshotpath: filepath.Join(filepath.Dir(execPath), "libapp.so"),
}
}
// Option for Application
type Option func(*config)
// ProjectAssetsPath specify the flutter assets directory.
func ProjectAssetsPath(p string) Option {
_, err := os.Stat(p)
if err != nil {
fmt.Printf("go-flutter: failed to stat flutter assets path: %v\n", err)
os.Exit(1)
}
return func(c *config) {
c.flutterAssetsPath = p
}
}
// ApplicationELFSnapshotPath specify the path to the ELF AOT snapshot.
// only required by AOT.
func ApplicationELFSnapshotPath(p string) Option {
_, err := os.Stat(p)
if err != nil {
fmt.Printf("go-flutter: failed to stat ELF snapshot path: %v\n", err)
os.Exit(1)
}
return func(c *config) {
c.elfSnapshotpath = p
}
}
// ApplicationICUDataPath specify the path to the ICUData.
func ApplicationICUDataPath(p string) Option {
_, err := os.Stat(p)
if err != nil {
fmt.Printf("go-flutter: failed to stat icu data path: %v\n", err)
os.Exit(1)
}
return func(c *config) {
c.icuDataPath = p
}
}
// OptionVMArguments specify the arguments to the Dart VM.
func OptionVMArguments(a []string) Option {
return func(c *config) {
// First should be argument is argv[0]
c.vmArguments = append([]string{""}, a...)
}
}
// WindowInitialDimensions specify the startup's dimension of the window.
func WindowInitialDimensions(width, height int) Option {
if width < 1 {
fmt.Println("go-flutter: invalid initial value for width, must be 1 or greater.")
os.Exit(1)
}
if height < 1 {
fmt.Println("go-flutter: invalid initial value for height, must be 1 or greater.")
os.Exit(1)
}
return func(c *config) {
c.windowInitialDimensions.width = width
c.windowInitialDimensions.height = height
}
}
// WindowInitialLocation specify the startup's position of the window.
// Location, in screen coordinates, of the upper-left corner of the client area
// of the window.
func WindowInitialLocation(xpos, ypos int) Option {
if xpos < 1 {
fmt.Println("go-flutter: invalid initial value for xpos location, must be 1 or greater.")
os.Exit(1)
}
if ypos < 1 {
fmt.Println("go-flutter: invalid initial value for ypos location, must be 1 or greater.")
os.Exit(1)
}
return func(c *config) {
c.windowInitialLocation.xpos = xpos
c.windowInitialLocation.ypos = ypos
}
}
// WindowDimensionLimits specify the dimension limits of the window.
// Does not work when the window is fullscreen or not resizable.
func WindowDimensionLimits(minWidth, minHeight, maxWidth, maxHeight int) Option {
if minWidth < 1 {
fmt.Println("go-flutter: invalid initial value for minWidth, must be 1 or greater.")
os.Exit(1)
}
if minHeight < 1 {
fmt.Println("go-flutter: invalid initial value for minHeight, must be 1 or greater.")
os.Exit(1)
}
if maxWidth < minWidth {
fmt.Println("go-flutter: invalid initial value for maxWidth, must be greater or equal to minWidth.")
os.Exit(1)
}
if maxHeight < minHeight {
fmt.Println("go-flutter: invalid initial value for maxHeight, must be greater or equal to minHeight.")
os.Exit(1)
}
return func(c *config) {
c.windowDimensionLimits.minWidth = minWidth
c.windowDimensionLimits.minHeight = minHeight
c.windowDimensionLimits.maxWidth = maxWidth
c.windowDimensionLimits.maxHeight = maxHeight
}
}
// BackOnEscape controls the mapping of the escape key.
//
// If true, pops the current route when escape is pressed.
// If false, escape is delivered to the application.
func BackOnEscape(backOnEscape bool) Option {
return func(c *config) {
c.backOnEscape = backOnEscape
}
}
// WindowIcon sets an icon provider func, which is called during window
// initialization. For tips on the kind of images to provide, see
// https://godoc.org/github.com/go-gl/glfw/v3.3/glfw#Window.SetIcon
func WindowIcon(iconProivder func() ([]image.Image, error)) Option {
return func(c *config) {
c.windowIconProvider = iconProivder
}
}
// ForcePixelRatio forces the the scale factor for the screen. By default,
// go-flutter will calculate the correct pixel ratio for the user, based on
// their monitor DPI. Setting this option is not advised.
func ForcePixelRatio(ratio float64) Option {
return func(c *config) {
c.forcePixelRatio = ratio
}
}
// WindowTransparentBackground sets the init window background to be transparent
func WindowTransparentBackground(enabled bool) Option {
return func(c *config) {
c.windowTransparent = enabled
}
}
// WindowAlwaysOnTop sets the application window to be always on top of other windows
func WindowAlwaysOnTop(enabled bool) Option {
return func(c *config) {
c.windowAlwaysOnTop = enabled
}
}
// AddPlugin adds a plugin to the flutter application.
func AddPlugin(p Plugin) Option {
return func(c *config) {
c.plugins = append(c.plugins, p)
}
}
// VirtualKeyboardShow sets an func called when the flutter framework want to
// show the keyboard.
// This Option is interesting for people wanting to display the on-screen
// keyboard on TextField focus.
// It's up to the flutter developer to implement (or not) this function with
// the OS related call.
func VirtualKeyboardShow(showCallback func()) Option {
return func(c *config) {
// Reference the callback to the platform plugin (singleton) responsible
// for textinput.
defaultTextinputPlugin.virtualKeyboardShow = showCallback
}
}
// VirtualKeyboardHide sets an func called when the flutter framework want to
// hide the keyboard.
func VirtualKeyboardHide(hideCallback func()) Option {
return func(c *config) {
// Reference the callback to the platform plugin (singleton) responsible
// for textinput.
defaultTextinputPlugin.virtualKeyboardHide = hideCallback
}
}
// ScrollAmount sets the number of pixels to scroll with the mouse wheel
func ScrollAmount(amount float64) Option {
return func(c *config) {
c.scrollAmount = amount
}
}