forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
211 lines (185 loc) · 6.09 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
package flutter
import (
"fmt"
"image"
"os"
"github.com/go-gl/glfw/v3.2/glfw"
)
type config struct {
flutterAssetsPath string
icuDataPath string
vmArguments []string
windowInitializerDeprecated func(*glfw.Window) error
windowIconProvider func() ([]image.Image, error)
windowInitialDimensions windowDimensions
windowDimensionLimits windowDimensionLimits
windowMode windowMode
forcePixelRatio float64
keyboardLayout KeyboardShortcuts
plugins []Plugin
}
type windowDimensions struct {
width int
height int
}
type windowDimensionLimits struct {
minWidth int
minHeight int
maxWidth int
maxHeight int
}
// defaultApplicationConfig define the default configuration values for a new
// Application. These values may be changed at any time.
var defaultApplicationConfig = config{
windowInitialDimensions: windowDimensions{
width: 800,
height: 600,
},
keyboardLayout: KeyboardQwertyLayout,
windowMode: WindowModeDefault,
}
// Option for Application
type Option func(*config)
// ProjectAssetPath specify the flutter assets directory.
//
// Deprecated, please use ProjectAssetsPath(path).
func ProjectAssetPath(p string) Option {
// deprecated on 2019-03-05
fmt.Println("go-flutter: ProjectAssetPath is deprecated, use ProjectAssetsPath (with an s).")
return ProjectAssetsPath(p)
}
// 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
}
}
// 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...)
}
}
// ApplicationWindowDimension specify the startup's dimensions of the window.
//
// Deprecated, please use WindowInitialDimensions(x, y).
func ApplicationWindowDimension(x, y int) Option {
// deprecated on 2019-03-10
fmt.Println("go-flutter: ApplicationWindowDimension is deprecated, use WindowInitialDimensions(x, y).")
return WindowInitialDimensions(x, y)
}
// 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
}
}
// 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
}
}
// OptionWindowInitializer allow initializing the window.
//
// Deprecated, please use WindowIcon if you'd like to set the window icon.
func OptionWindowInitializer(ini func(*glfw.Window) error) Option {
// deprecated on 2019-03-05
fmt.Println("go-flutter: OptionWindowInitializer is deprecated. Please read https://is.gd/gflut_window_init_deprecated")
return func(c *config) {
c.windowInitializerDeprecated = ini
}
}
// 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.2/glfw#Window.SetIcon
func WindowIcon(iconProivder func() ([]image.Image, error)) Option {
return func(c *config) {
c.windowIconProvider = iconProivder
}
}
// OptionPixelRatio 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.
//
// Deprecated, please use ForcePixelRatio(ratio).
func OptionPixelRatio(ratio float64) Option {
// deprecated on 2019-03-10
fmt.Println("go-flutter: OptionPixelRatio is deprecated. Please use ForcePixelRatio(ratio)")
return ForcePixelRatio(ratio)
}
// 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
}
}
// AddPlugin adds a plugin to the flutter application.
func AddPlugin(p Plugin) Option {
return func(c *config) {
c.plugins = append(c.plugins, p)
}
}
// OptionKeyboardLayout allow application to support keyboard that have a
// different layout and therefore different keyboard shortcuts.
func OptionKeyboardLayout(keyboardLayout KeyboardShortcuts) Option {
return func(c *config) {
c.keyboardLayout = keyboardLayout
}
}
// KeyboardShortcuts contains the configuration for keyboard shortcut keys. This
// allows an application to support keyboard layout different from US layout.
type KeyboardShortcuts struct {
Cut glfw.Key
Copy glfw.Key
Paste glfw.Key
SelectAll glfw.Key
}