Super minimal golang concurrent gui

概览:

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

摘自https://github.com/faiface/gui这个仓库,gui是一个golang中的gui库。

库官方对并发的解释:

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.

hello world:

今天呢?先入个门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"image"
"image/draw"

"github.com/faiface/gui/win"
"github.com/faiface/mainthread"
)

var (
title string = "hello golang gui"
width int = 1024
height int = 576
)

func run() {
w, err := win.New(win.Title(title), win.Size(width, height), win.Resizable())
if err != nil {
panic(err)
}
w.Draw() <- func(drw draw.Image) image.Rectangle {
rectImage := image.NewRGBA(image.Rect(int(width/2+150), int(height/2+150), int(width/2-150), int(height/2-150)))
draw.Draw(drw, rectImage.Bounds(), image.White, image.Point{}, draw.Src)
return rectImage.Bounds()
}

for event := range w.Events() {
switch event.(type) {
case win.WiClose:
close(w.Draw())
}
}
}

func main() {
mainthread.Run(run)
}

其实就是官方的示例修改而来,如果想自适应,应该需要全部组件以窗口大小为基准而更新窗口。我这个示例还没有做到这些,留到以后再探索吧!