Skip to content

GLFW Plugin example

Pierre Champion edited this page Sep 7, 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 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 go/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 their 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.Plugin = &WindowNotResizable{}     // compile-time type check
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 #214, how to drag undecorated window.

go-flutter has multiples option, one of them been flutter.WindowMode(flutter.WindowModeBorderless).
When this option is set, the window decoration (title bar, close/maximize/.. buttons) isn't displayed, meaning the window can't be dragged around, maximize, closed,.. using 'classic' mouse interaction.

Wouldn't it be natural to use the AppBar widget to move the window?

Well, it has been implemented as a example. If you launch draggable_borderless, grab the AppBar and start dragging, the window will follow your cursor!

In order to implement this feature, a GLFW Plugin has been created, the plugin code source is available directly in options.go, making this example a good GLFW Plugin starting point.

Note:

Make sure to read the GLFW documentation to know which functions are thread safe or not.
If you happens to need to call functions on the main thread, use MethodChannel.HandleFuncSync, in your Golang handler (the example draggable_borderless does it).

Clone this wiki locally