Categorygithub.com/peterhellberg/gui
repositorypackage
0.0.0-20200301230217-9df2358f61ea
Repository: https://github.com/peterhellberg/gui.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# 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()
}