gui

package module
v0.0.0-...-9de25cc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2021 License: MIT Imports: 3 Imported by: 0

README

faiface/gui GoDoc Discord

Super minimal, rock-solid foundation for concurrent GUI in Go.

Installation

go get -u github.com/faiface/gui

Currently uses GLFW under the hood, so have these dependencies.

Why concurrent GUI?

GUI is concurrent by nature. Elements like buttons, text fields, or canvases are conceptually independent. Conventional GUI frameworks solve this by implementing huge architectures: the event loop, call-backs, tickers, you name it.

In a concurrent GUI, the story is different. Each element is actually handled by its own goroutine, or event multiple ones. Elements communicate with each other via channels.

This has several advantages:

  • Make a new element at any time just by spawning a goroutine.
  • Implement animations using simple for-loops.
  • An intenstive computation in one element won't block the whole app.
  • Enables decentralized design - since elements communicate via channels, multiple communications may be going on at once, without any central entity.

Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeEventsChan

func MakeEventsChan() (<-chan Event, chan<- Event)

MakeEventsChan implements a channel of events with an unlimited capacity. It does so by creating a goroutine that queues incoming events. Sending to this channel never blocks and no events get lost.

The unlimited capacity channel is very suitable for delivering events because the consumer may be unavailable for some time (doing a heavy computation), but will get to the events later.

An unlimited capacity channel has its dangers in general, but is completely fine for the purpose of delivering events. This is because the production of events is fairly infrequent and should never out-run their consumption in the long term.

func NewMux

func NewMux(env Env) (mux *Mux, master Env)

NewMux creates a new Mux that multiplexes the given Env. It returns the Mux along with a master Env. The master Env is just like any other Env created by the Mux, except that closing the Draw() channel on the master Env closes the whole Mux and all other Envs created by the Mux.

func StartQueue

func StartQueue(in <-chan Event, out chan<- Event)

Types

type Draw

type Draw interface {
	Draw(dst draw.Image) image.Rectangle
}

type DrawFunc

type DrawFunc func(dst draw.Image) image.Rectangle

func (DrawFunc) Draw

func (d DrawFunc) Draw(dst draw.Image) image.Rectangle

type Env

type Env interface {
	Events() <-chan Event
	Draw() chan<- Draw
}

Env is the most important thing in this package. It is an interactive graphical environment, such as a window.

It has two channels: Events() and Draw().

The Events() channel produces events, like mouse and keyboard presses, while the Draw() channel receives drawing functions. A drawing function draws onto the supplied draw.Image, which is the drawing area of the Env and returns a rectangle covering the whole part of the image that got changed.

An Env guarantees to produce a "resize/<x0>/<y0>/<x1>/<y1>" event as its first event.

The Events() channel must be unlimited in capacity. Use MakeEventsChan() to create a channel of events with an unlimited capacity.

The Draw() channel may be synchronous.

Drawing functions sent to the Draw() channel are not guaranteed to be executed.

Closing the Draw() channel results in closing the Env. The Env will subsequently close the Events() channel. On the other hand, when the Events() channel gets closed the user of the Env should subsequently close the Draw() channel.

type Event

type Event interface{}

Event is something that can happen in an environment.

This package defines only one kind of event: Resize. Other packages implementing environments may implement more kinds of events. For example, the win package implements all kinds of events for mouse and keyboard.

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux can be used to multiplex an Env, let's call it a root Env. Mux implements a way to create multiple virtual Envs that all interact with the root Env. They receive the same events and their draw functions get redirected to the root Env.

func (*Mux) MakeEnv

func (mux *Mux) MakeEnv() Env

MakeEnv creates a new virtual Env that interacts with the root Env of the Mux. Closing the Draw() channel of the Env will not close the Mux, or any other Env created by the Mux but will delete the Env from the Mux.

type Resize

type Resize struct {
	image.Rectangle
}

Resize is an event that happens when the environment changes the size of its drawing area.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL