Skip to content

Commit 02bd1ae

Browse files
committed
Add window dimensions limits
1 parent 38f9b44 commit 02bd1ae

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

application.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (a *Application) Run() error {
7373
case WindowModeBorderlessFullscreen:
7474
monitor = glfw.GetPrimaryMonitor()
7575
mode := monitor.GetVideoMode()
76-
a.config.windowInitialDimensions.x = mode.Width
77-
a.config.windowInitialDimensions.y = mode.Height
76+
a.config.windowInitialDimensions.width = mode.Width
77+
a.config.windowInitialDimensions.height = mode.Height
7878
glfw.WindowHint(glfw.RedBits, mode.RedBits)
7979
glfw.WindowHint(glfw.GreenBits, mode.GreenBits)
8080
glfw.WindowHint(glfw.BlueBits, mode.BlueBits)
@@ -83,7 +83,7 @@ func (a *Application) Run() error {
8383
return errors.Errorf("invalid window mode %T", a.config.windowMode)
8484
}
8585

86-
a.window, err = glfw.CreateWindow(a.config.windowInitialDimensions.x, a.config.windowInitialDimensions.y, "Loading..", monitor, nil)
86+
a.window, err = glfw.CreateWindow(a.config.windowInitialDimensions.width, a.config.windowInitialDimensions.height, "Loading..", monitor, nil)
8787
if err != nil {
8888
return errors.Wrap(err, "creating glfw window")
8989
}
@@ -105,6 +105,15 @@ func (a *Application) Run() error {
105105
}
106106
}
107107

108+
if a.config.windowDimensionLimits.minWidth != 0 {
109+
a.window.SetSizeLimits(
110+
a.config.windowDimensionLimits.minWidth,
111+
a.config.windowDimensionLimits.minHeight,
112+
a.config.windowDimensionLimits.maxWidth,
113+
a.config.windowDimensionLimits.maxHeight,
114+
)
115+
}
116+
108117
a.engine = embedder.NewFlutterEngine()
109118

110119
messenger := newMessenger(a.engine)

option.go

+45-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type config struct {
1616
windowInitializerDeprecated func(*glfw.Window) error
1717
windowIconProvider func() ([]image.Image, error)
1818
windowInitialDimensions windowDimensions
19+
windowDimensionLimits windowDimensionLimits
1920

2021
forcePixelRatio float64
2122
keyboardLayout KeyboardShortcuts
@@ -27,16 +28,23 @@ type config struct {
2728
}
2829

2930
type windowDimensions struct {
30-
x int
31-
y int
31+
width int
32+
height int
33+
}
34+
35+
type windowDimensionLimits struct {
36+
minWidth int
37+
minHeight int
38+
maxWidth int
39+
maxHeight int
3240
}
3341

3442
// defaultApplicationConfig define the default configuration values for a new
3543
// Application. These values may be changed at any time.
3644
var defaultApplicationConfig = config{
3745
windowInitialDimensions: windowDimensions{
38-
x: 800,
39-
y: 600,
46+
width: 800,
47+
height: 600,
4048
},
4149
keyboardLayout: KeyboardQwertyLayout,
4250
windowMode: WindowModeDefault,
@@ -96,19 +104,47 @@ func ApplicationWindowDimension(x, y int) Option {
96104
}
97105

98106
// WindowInitialDimensions specify the startup's dimension of the window.
99-
func WindowInitialDimensions(x, y int) Option {
100-
if x < 1 {
107+
func WindowInitialDimensions(width, height int) Option {
108+
if width < 1 {
101109
fmt.Println("go-flutter: invalid initial value for width, must be 1 or greater.")
102110
os.Exit(1)
103111
}
104-
if y < 1 {
112+
if height < 1 {
105113
fmt.Println("go-flutter: invalid initial value for height, must be 1 or greater.")
106114
os.Exit(1)
107115
}
108116

109117
return func(c *config) {
110-
c.windowInitialDimensions.x = x
111-
c.windowInitialDimensions.y = y
118+
c.windowInitialDimensions.width = width
119+
c.windowInitialDimensions.height = height
120+
}
121+
}
122+
123+
// WindowDimensionLimits specify the dimension limits of the window.
124+
// Does not work when the window is fullscreen or not resizable.
125+
func WindowDimensionLimits(minWidth, minHeight, maxWidth, maxHeight int) Option {
126+
if minWidth < 1 {
127+
fmt.Println("go-flutter: invalid initial value for minWidth, must be 1 or greater.")
128+
os.Exit(1)
129+
}
130+
if minHeight < 1 {
131+
fmt.Println("go-flutter: invalid initial value for minHeight, must be 1 or greater.")
132+
os.Exit(1)
133+
}
134+
if maxWidth < minWidth {
135+
fmt.Println("go-flutter: invalid initial value for maxWidth, must be greater or equal to minWidth.")
136+
os.Exit(1)
137+
}
138+
if maxHeight < minHeight {
139+
fmt.Println("go-flutter: invalid initial value for maxHeight, must be greater or equal to minHeight.")
140+
os.Exit(1)
141+
}
142+
143+
return func(c *config) {
144+
c.windowDimensionLimits.minWidth = minWidth
145+
c.windowDimensionLimits.minHeight = minHeight
146+
c.windowDimensionLimits.maxWidth = maxWidth
147+
c.windowDimensionLimits.maxHeight = maxHeight
112148
}
113149
}
114150

0 commit comments

Comments
 (0)