Skip to content

Add window dimension limits #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (a *Application) Run() error {
case WindowModeBorderlessFullscreen:
monitor = glfw.GetPrimaryMonitor()
mode := monitor.GetVideoMode()
a.config.windowInitialDimensions.x = mode.Width
a.config.windowInitialDimensions.y = mode.Height
a.config.windowInitialDimensions.width = mode.Width
a.config.windowInitialDimensions.height = mode.Height
glfw.WindowHint(glfw.RedBits, mode.RedBits)
glfw.WindowHint(glfw.GreenBits, mode.GreenBits)
glfw.WindowHint(glfw.BlueBits, mode.BlueBits)
Expand All @@ -83,7 +83,7 @@ func (a *Application) Run() error {
return errors.Errorf("invalid window mode %T", a.config.windowMode)
}

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

if a.config.windowDimensionLimits.minWidth != 0 {
a.window.SetSizeLimits(
a.config.windowDimensionLimits.minWidth,
a.config.windowDimensionLimits.minHeight,
a.config.windowDimensionLimits.maxWidth,
a.config.windowDimensionLimits.maxHeight,
)
}

a.engine = embedder.NewFlutterEngine()

messenger := newMessenger(a.engine)
Expand Down
54 changes: 45 additions & 9 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type config struct {
windowInitializerDeprecated func(*glfw.Window) error
windowIconProvider func() ([]image.Image, error)
windowInitialDimensions windowDimensions
windowDimensionLimits windowDimensionLimits
windowMode windowMode

forcePixelRatio float64
Expand All @@ -25,16 +26,23 @@ type config struct {
}

type windowDimensions struct {
x int
y int
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{
x: 800,
y: 600,
width: 800,
height: 600,
},
keyboardLayout: KeyboardQwertyLayout,
windowMode: WindowModeDefault,
Expand Down Expand Up @@ -94,19 +102,47 @@ func ApplicationWindowDimension(x, y int) Option {
}

// WindowInitialDimensions specify the startup's dimension of the window.
func WindowInitialDimensions(x, y int) Option {
if x < 1 {
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 y < 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.x = x
c.windowInitialDimensions.y = y
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
}
}

Expand Down