Skip to content

Commit 4c1046e

Browse files
GeertJohanpchampio
authored andcommitted
Add window dimension limits (#145)
* Add window dimensions limits
1 parent d54add2 commit 4c1046e

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

application.go

Lines changed: 12 additions & 3 deletions
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

Lines changed: 45 additions & 9 deletions
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
windowMode windowMode
2021

2122
forcePixelRatio float64
@@ -25,16 +26,23 @@ type config struct {
2526
}
2627

2728
type windowDimensions struct {
28-
x int
29-
y int
29+
width int
30+
height int
31+
}
32+
33+
type windowDimensionLimits struct {
34+
minWidth int
35+
minHeight int
36+
maxWidth int
37+
maxHeight int
3038
}
3139

3240
// defaultApplicationConfig define the default configuration values for a new
3341
// Application. These values may be changed at any time.
3442
var defaultApplicationConfig = config{
3543
windowInitialDimensions: windowDimensions{
36-
x: 800,
37-
y: 600,
44+
width: 800,
45+
height: 600,
3846
},
3947
keyboardLayout: KeyboardQwertyLayout,
4048
windowMode: WindowModeDefault,
@@ -94,19 +102,47 @@ func ApplicationWindowDimension(x, y int) Option {
94102
}
95103

96104
// WindowInitialDimensions specify the startup's dimension of the window.
97-
func WindowInitialDimensions(x, y int) Option {
98-
if x < 1 {
105+
func WindowInitialDimensions(width, height int) Option {
106+
if width < 1 {
99107
fmt.Println("go-flutter: invalid initial value for width, must be 1 or greater.")
100108
os.Exit(1)
101109
}
102-
if y < 1 {
110+
if height < 1 {
103111
fmt.Println("go-flutter: invalid initial value for height, must be 1 or greater.")
104112
os.Exit(1)
105113
}
106114

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

0 commit comments

Comments
 (0)