Skip to content

GLFW Plugin example

Pierre Champion edited this page May 17, 2019 · 25 revisions

The dart code cannot control the glfw.Window size, position, ...
To gain control over the glfw.Window we need to build your own plugin in order to access the underlying API, please read the plugin wiki in order to learn more about the plugin architecture.

Examples

Case study #1

In this example, we answer the issue #126, How to make the window as not resizable.

To make the window not resizable for the entire lifetime of the application, there is no need to use the dart and Golang platform-channels (one time call, no communication needed).

Using hover the file to edit is desktop/cmd/options.go. By creating a custom GLFW Plugin, we gain access to the glfw.Window interface. Having access to the glfw.Window interface allows users to customize the window to our needs.

Source code:

package main

import (
	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
	"github.com/go-gl/glfw/v3.2/glfw"
)

var options = []flutter.Option{
	flutter.WindowInitialDimensions(800, 600),
	flutter.AddPlugin(&WindowNotResizable{}),
	// ...
}

type WindowNotResizable struct{}

var _ flutter.PluginGLFW = &WindowNotResizable{} // compile-time type check
// WindowNotResizable struct must implement InitPlugin and InitPluginGLFW

func (p *WindowNotResizable) InitPlugin(messenger plugin.BinaryMessenger) error {
	// nothing to do
	return nil
}

func (p *WindowNotResizable) InitPluginGLFW(window *glfw.Window) error {
	window.SetSizeLimits(800, 600, 800, 600)
	return nil
}

Case study #2

In this example, we answer the issue #137, How to support borderless windows.

The go-flutter package takes care of instantiating the GLFW window context and flutter engine. To make the windows borderless we need to set one special flag before the GLFW window context is created go-gl/glfw docs. Therefore the use of a PluginGLFW won't allow us to control whether the window has borderless or not, it's already too late. When using regular Plugin, the InitPlugin function is called before creating the GLFW window. (In opposition of PluginGLFW which is call after the windows is created) We can use this trick to set custom flags (called Hint) before the the GLFW window context is created.

Using hover the file to edit is desktop/cmd/options.go. And use the following custom plugin:

package main

import (
	"github.com/go-flutter-desktop/go-flutter"
	"github.com/go-flutter-desktop/go-flutter/plugin"
	"github.com/go-gl/glfw/v3.2/glfw"
)

var options = []flutter.Option{
	flutter.WindowInitialDimensions(800, 600),
	flutter.AddPlugin(&BorderlessWindow{}),
	// ...
}

type BorderlessWindow struct{}

var _ flutter.Plugin = &BorderlessWindow{} // compile-time type check
// BorderlessWindow struct must implement InitPlugin.

func (p *BorderlessWindow) InitPlugin(messenger plugin.BinaryMessenger) error {
	glfw.WindowHint(glfw.Decorated, glfw.False)
	return nil
}
Clone this wiki locally