From 6039f622b608695335065b677d9f8ba5b62ba452 Mon Sep 17 00:00:00 2001 From: Geert-Johan Riemer Date: Sun, 19 May 2019 00:49:43 +0200 Subject: [PATCH 1/2] Add WindowMode Option for bordreless and borderless fullscreen windows. --- application.go | 22 +++++++++++++++++++++- option.go | 2 ++ window.go | 27 +++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 window.go diff --git a/application.go b/application.go index cf55e1d6..7e34cc92 100644 --- a/application.go +++ b/application.go @@ -64,10 +64,30 @@ func (a *Application) Run() error { } defer glfw.Terminate() - a.window, err = glfw.CreateWindow(a.config.windowInitialDimensions.x, a.config.windowInitialDimensions.y, "Loading..", nil, nil) + var monitor *glfw.Monitor + switch a.config.windowMode { + case WindowModeDefault: + // nothing + case WindowModeBorderless: + glfw.WindowHint(glfw.Decorated, glfw.False) + case WindowModeBorderlessFullscreen: + monitor = glfw.GetPrimaryMonitor() + mode := monitor.GetVideoMode() + a.config.windowInitialDimensions.x = mode.Width + a.config.windowInitialDimensions.y = mode.Height + glfw.WindowHint(glfw.RedBits, mode.RedBits) + glfw.WindowHint(glfw.GreenBits, mode.GreenBits) + glfw.WindowHint(glfw.BlueBits, mode.BlueBits) + glfw.WindowHint(glfw.RefreshRate, mode.RefreshRate) + default: + 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) if err != nil { return errors.Wrap(err, "creating glfw window") } + glfw.DefaultWindowHints() defer a.window.Destroy() if a.config.windowIconProvider != nil { diff --git a/option.go b/option.go index 0a92ab6d..4512878e 100644 --- a/option.go +++ b/option.go @@ -16,6 +16,7 @@ type config struct { windowInitializerDeprecated func(*glfw.Window) error windowIconProvider func() ([]image.Image, error) windowInitialDimensions windowDimensions + windowMode windowMode forcePixelRatio float64 keyboardLayout KeyboardShortcuts @@ -36,6 +37,7 @@ var defaultApplicationConfig = config{ y: 600, }, keyboardLayout: KeyboardQwertyLayout, + windowMode: WindowModeDefault, } // Option for Application diff --git a/window.go b/window.go new file mode 100644 index 00000000..75352cf3 --- /dev/null +++ b/window.go @@ -0,0 +1,27 @@ +package flutter + +// windowMode determines the kind of window mode to use for new windows. +type windowMode int + +const ( + // WindowModeDefault is the default window mode. Windows are created with + // borders and close/minimize buttons. + WindowModeDefault windowMode = iota + // WindowModeBorderless removes decorations such as borders and + // close/minimize buttons from the window. + WindowModeBorderless + // WindowModeBorderlessFullscreen starts the application in borderless + // fullscreen mode. Currently, only fullscreen on the primary monitor is + // supported. This option overrides WindowInitialDimensions. Note that on + // some systems a fullscreen window is very hard to close. Make sure your + // Flutter application has a close button and use PopBehaviorIconify to + // minimize or PopBehaviorClose to close the application. + WindowModeBorderlessFullscreen +) + +// WindowMode sets the window mode on the application. +func WindowMode(w windowMode) Option { + return func(c *config) { + c.windowMode = w + } +} From 6efdcfbd02534be3c7c198f632aaef7ce1bb329e Mon Sep 17 00:00:00 2001 From: Geert-Johan Riemer Date: Sun, 19 May 2019 01:54:21 +0200 Subject: [PATCH 2/2] Add window dimensions limits --- application.go | 15 +++++++++++--- option.go | 54 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/application.go b/application.go index 7e34cc92..f4d160fb 100644 --- a/application.go +++ b/application.go @@ -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) @@ -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") } @@ -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) diff --git a/option.go b/option.go index 4512878e..22686528 100644 --- a/option.go +++ b/option.go @@ -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 @@ -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, @@ -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 } }