gui

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

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

Go to latest
Published: Mar 1, 2020 License: MIT Imports: 10 Imported by: 0

README

peterhellberg/gui

Build Status Go Report Card GoDoc

Minimal GUI in Go, it was initially based on https://github.com/faiface/gui but has since diverged from the original project.

Dependencies

This package has a few third party dependencies:

Examples

Minimal

gui-minimal

package main

import (
	"image"
	"image/draw"

	"github.com/peterhellberg/gui"
)

func main() {
	gui.Run(func() {
		win, err := gui.Open(gui.Title("gui-minimal"))
		if err != nil {
			panic(err)
		}

		for event := range win.Events() {
			switch event.(type) {
			case gui.EventClose:
				win.Close()
			case gui.EventResize:
				win.Draw(func(dst draw.Image) image.Rectangle {
					return dst.Bounds()
				})
			}
		}
	})
}
XOR

gui-xor

package main

import (
	"image"
	"image/color"
	"image/draw"

	"github.com/peterhellberg/gui"
)

func main() {
	gui.Run(loop)
}

func loop() {
	win, err := gui.Open(
		gui.Title("gui-xor"),
		gui.Size(512, 512),
		gui.Decorated(true),
		gui.Resizable(true),
	)
	if err != nil {
		panic(err)
	}

	for event := range win.Events() {
		switch event := event.(type) {
		case gui.EventClose:
			win.Close()
		case gui.EventKeyboardDown:
			if event.Key == "escape" {
				win.Close()
			}
		case gui.EventKeyboardChar:
			if event.Char == 'q' {
				win.Close()
			}
		case gui.EventResize:
			win.Draw(update)
		}

		gui.Log("Event: %+v", event)
	}
}

func update(dst draw.Image) image.Rectangle {
	bounds := dst.Bounds()

	for x := 0; x < bounds.Max.X; x++ {
		for y := 0; y < bounds.Max.Y; y++ {
			c := uint8(x ^ y)

			dst.Set(x, y, color.NRGBA{c, c % 192, c, 255})
		}
	}

	return bounds
}

Blinker

gui-blinker

package main

import (
	"image"
	"image/draw"
	"time"

	"github.com/peterhellberg/gui"
)

func main() {
	gui.Run(loop)
}

func loop() {
	win, err := gui.Open(
		gui.Title("gui-blinker"),
		gui.Size(800, 600),
	)
	if err != nil {
		panic(err)
	}

	mux, env := gui.NewMux(win)

	// we create four blinkers, each with its own Env from the mux
	go blinker(mux.Env(), image.Rect(100, 100, 350, 250))
	go blinker(mux.Env(), image.Rect(450, 100, 700, 250))
	go blinker(mux.Env(), image.Rect(100, 350, 350, 500))
	go blinker(mux.Env(), image.Rect(450, 350, 700, 500))

	// we use the master env now, win is used by the mux
	for event := range env.Events() {
		switch event := event.(type) {
		case gui.EventClose:
			win.Close()
		case gui.EventKeyboardDown:
			if event.Key == "escape" {
				win.Close()
			}
		case gui.EventKeyboardChar:
			if event.Char == 'q' {
				win.Close()
			}
		}
	}
}

func blinker(env gui.Env, r image.Rectangle) {
	// redraw takes a bool and produces a draw command
	redraw := func(visible bool) func(draw.Image) image.Rectangle {
		return func(dst draw.Image) image.Rectangle {
			if visible {
				draw.Draw(dst, r, image.White, image.ZP, draw.Src)
			} else {
				draw.Draw(dst, r, image.Black, image.ZP, draw.Src)
			}

			return r
		}
	}

	// first we draw a white rectangle
	env.Draw(redraw(true))

	for event := range env.Events() {
		switch event := event.(type) {
		case gui.EventMouseLeftDown:
			if event.In(r) {
				// user clicked on the rectangle we blink 3 times
				for i := 0; i < 3; i++ {
					env.Draw(redraw(false))
					time.Sleep(time.Second / 3)

					env.Draw(redraw(true))
					time.Sleep(time.Second / 3)
				}
			}
		}
	}

	env.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Log

func Log(format string, a ...interface{})

Log to standard output.

func NewMux

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

NewMux creates a new Mux that multiplexes the given Env.

func Run

func Run(fn func())

Run calls mainthread.Run

Types

type Env

type Env interface {
	Events() <-chan Event
	Draw(func(draw.Image) image.Rectangle)
	Close()
}

Env is an interactive graphical environment, such as a window.

type Event

type Event interface {
	Name() string
	Data() interface{}
}

Event includes its name and data.

type EventClose

type EventClose struct{}

EventClose event

func (EventClose) Data

func (c EventClose) Data() interface{}

Data for event

func (EventClose) Name

func (c EventClose) Name() string

Name of event

type EventKeyboardChar

type EventKeyboardChar struct {
	Char rune
}

EventKeyboardChar event

func (EventKeyboardChar) Data

func (kc EventKeyboardChar) Data() interface{}

Data for event

func (EventKeyboardChar) Name

func (kc EventKeyboardChar) Name() string

Name of event

type EventKeyboardDown

type EventKeyboardDown struct {
	Key string
}

EventKeyboardDown event

func (EventKeyboardDown) Data

func (kd EventKeyboardDown) Data() interface{}

Data for event

func (EventKeyboardDown) Name

func (kd EventKeyboardDown) Name() string

Name of event

type EventKeyboardRepeat

type EventKeyboardRepeat struct {
	Key string
}

EventKeyboardRepeat event

func (EventKeyboardRepeat) Data

func (kr EventKeyboardRepeat) Data() interface{}

Data for event

func (EventKeyboardRepeat) Name

func (kr EventKeyboardRepeat) Name() string

Name of event

type EventKeyboardUp

type EventKeyboardUp struct {
	Key string
}

EventKeyboardUp event

func (EventKeyboardUp) Data

func (ku EventKeyboardUp) Data() interface{}

Data for event

func (EventKeyboardUp) Name

func (ku EventKeyboardUp) Name() string

Name of event

type EventMouseLeftDown

type EventMouseLeftDown struct {
	image.Point
}

EventMouseLeftDown event

func (EventMouseLeftDown) Data

func (mld EventMouseLeftDown) Data() interface{}

Data for event

func (EventMouseLeftDown) Name

func (mld EventMouseLeftDown) Name() string

Name of event

type EventMouseLeftUp

type EventMouseLeftUp struct {
	image.Point
}

EventMouseLeftUp event

func (EventMouseLeftUp) Data

func (mlu EventMouseLeftUp) Data() interface{}

Data for event

func (EventMouseLeftUp) Name

func (mlu EventMouseLeftUp) Name() string

Name of event

type EventMouseMiddleDown

type EventMouseMiddleDown struct {
	image.Point
}

EventMouseMiddleDown event

func (EventMouseMiddleDown) Data

func (mmd EventMouseMiddleDown) Data() interface{}

Data for event

func (EventMouseMiddleDown) Name

func (mmd EventMouseMiddleDown) Name() string

Name of event

type EventMouseMiddleUp

type EventMouseMiddleUp struct {
	image.Point
}

EventMouseMiddleUp event

func (EventMouseMiddleUp) Data

func (mmu EventMouseMiddleUp) Data() interface{}

Data for event

func (EventMouseMiddleUp) Name

func (mmu EventMouseMiddleUp) Name() string

Name of event

type EventMouseMove

type EventMouseMove struct {
	image.Point
}

EventMouseMove event

func (EventMouseMove) Data

func (mm EventMouseMove) Data() interface{}

Data for event

func (EventMouseMove) Name

func (mm EventMouseMove) Name() string

Name of event

type EventMouseRightDown

type EventMouseRightDown struct {
	image.Point
}

EventMouseRightDown event

func (EventMouseRightDown) Data

func (mrd EventMouseRightDown) Data() interface{}

Data for event

func (EventMouseRightDown) Name

func (mrd EventMouseRightDown) Name() string

Name of event

type EventMouseRightUp

type EventMouseRightUp struct {
	image.Point
}

EventMouseRightUp event

func (EventMouseRightUp) Data

func (mru EventMouseRightUp) Data() interface{}

Data for event

func (EventMouseRightUp) Name

func (mru EventMouseRightUp) Name() string

Name of event

type EventMouseScroll

type EventMouseScroll struct {
	image.Point
}

EventMouseScroll event

func (EventMouseScroll) Data

func (ms EventMouseScroll) Data() interface{}

Data for event

func (EventMouseScroll) Name

func (ms EventMouseScroll) Name() string

Name of event

type EventResize

type EventResize struct {
	image.Rectangle
}

EventResize event

func (EventResize) Data

func (r EventResize) Data() interface{}

Data for event

func (EventResize) Name

func (r EventResize) Name() string

Name of event

type EventUpdate

type EventUpdate struct {
	time.Time
}

EventUpdate event

func (EventUpdate) Data

func (t EventUpdate) Data() interface{}

Data for event

func (EventUpdate) Name

func (t EventUpdate) Name() string

Name of event

type Mux

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

Mux can be used to multiplex an Env.

func (*Mux) Env

func (mux *Mux) Env() Env

Env creates a new virtual Env that interacts with the root Env of the Mux.

type Option

type Option func(*options)

Option is a functional option to the window constructor New.

func Decorated

func Decorated(decorated bool) Option

Decorated options controls if the window should have any chrome.

func Resizable

func Resizable(resizable bool) Option

Resizable option makes the window resizable by the user.

func Size

func Size(width, height int) Option

Size option sets the width and height of the window.

func Title

func Title(title string) Option

Title option sets the title (caption) of the window.

type Window

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

Window is an Env that handles an actual graphical window.

func Open

func Open(opts ...Option) (*Window, error)

Open a new window with all the supplied options.

The default title is empty and the default size is 640x480.

func (*Window) Close

func (w *Window) Close()

Close closes the draw channel

func (*Window) Draw

func (w *Window) Draw(fn func(draw.Image) image.Rectangle)

Draw to the window using the provided function.

func (*Window) Events

func (w *Window) Events() <-chan Event

Events returns the events channel of the window.

func (*Window) Send

func (w *Window) Send(e Event)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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