Skip to content

Drivers

Andy Williams edited this page Jan 12, 2020 · 3 revisions

A driver in Fyne is responsible for providing all of the operating system specific implementation of the API to create a complete application. It may also rely on a painter to perform the drawing operations (see later in this document).

Driver

There are currently 3 drivers available glfw, gomobile and test.

GLFW

The main Fyne driver uses the GLFW library to open windows and handle user input. The Go bindings from go-gl project's glfw project support Windows, Linux, macOS and BSD and so power all standard Fyne desktop applications.

You can see this in use in the example applications - invoking the app.New() is setting up the GLFW driver by default. From this point onwards any call like app.CreateWindow() will call into the GL painter code for drawing the application interface.

import "fyne.io/fyne/app" 

Gomobile

The Gomobile driver embeds the gomobile project to build applications for iOS and Android platforms. To package an application for mobile the easiest way is to use the fyne command which will build the binary and then package the application for deployment to a device.

fyne package -os ios -appID com.company.myapp .

The gomobile driver is loaded automatically for ios or android builds but it includes a useful "simulated build" mode. To load a fyne app in mobile mode without compiling for the mobile device you can use the mobile build tag. Note that this is not a full device simulator so should not be used in place of real testing, but it is great for quickly loading your app interface on a mobile form factor.

go run -tags mobile main.go

Test

The test driver is really helpful for unit testing the Fyne code and also application logic. This driver never renders anything to screen but executes all other code as expected. This driver can be used without invoking an application constructor, if you include the following import in your _test.go file you can simulate an application in memory.

import _ "fyne.io/fyne/test"

New drivers

Over time it may become useful to add a new driver or migrate portions of an existing one to new technologies. For example a new driver may enable Fyne to run in a web browser or an update to the existing glfw driver may add support for new platforms or better performance.

A new driver would need to implement all of the fyne.Driver interface which is mainly responsible for managing native windows within an application. Every window that a driver creates will need to contain a canvas, and this is the link to the core responsibility of a driver - rendering.

A driver must be capable of rendering all the types of canvas object defined in the canvas package (currently line, rectangle, text and image (both bitmap and SVG)). Widgets and GUIs are drawn using these primitive objects and so the driver does not need to have any widget rendering programmed in. It will need to traverse the object tree from each Window instance and so must understand how fyne.Container and fyne.Widget wrap child objects. Layouts are not part of the driver, all the positioning is handled in Go code and pushed into the object state ready to be rendered.

A driver must also handle user input, typically mouse, keyboard and touch screen. It should create the appropriate fyne.KeyEvent and fyne.MouseEvent objects and send them to the objects that are set up to handle the events.

Painter

There are 3 different painters in Fyne that have different benefits gl, gles and software.

GL

The main Fyne driver uses OpenGL to render a user interface. The Go bindings from go-gl project connect to the highly efficient system renderer on desktop applications and so power all desktop Fyne applications. The OpenGL driver uses the version 2.0 specification from 2008 so most devices should support it, though there are some exceptions.

Embedded devices such as Raspberry Pi don't provide a full OpenGL implementation and so there is a variant on this painter which uses OpenGL ES (see next).

GLES

For lower power devices the GLES specification is used in Fyne. This is automatically selected for Raspberry Pi, iOS and Android builds. Applications using GLES will look exactly the same as those using the full GL painter but may not support some advanced functionality in the future. For systems that support both GL and GLES then a full GL implementation will be used by default, you can change this using the gles build tag.

go run -tags gles main.go

Software

There is a convenience painter named software that renders a user interface into memory using basic drawing procedures. The result of this render can be found using Canvas.Capture() which returns a standard image.Image. This is not an efficient painter but can be very helpful in testing or other scenarios.

You can specifically request a software painter without loading a different type of application or driver. Just use the following code to avoid loading the full user interface.

import "fyne.io/fyne/tools/playground"

func main() {
  ...

  canvas := playground.NewSoftwareCanvas()
  // add items to the canvas as normal

  img := canvas.Capture()
  // use the image as you wish
}

New Painters

To work on adding a new renderer it would need to cover the canvas requirements in the section above - draw the primitive types in the correct place and handle user input. To include this in the existing driver would require build tags or OS specific files - and may require additional work in the driver as well.