aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xexamples/layout/layoutbin5570192 -> 0 bytes
-rw-r--r--examples/layout/main.go27
-rw-r--r--layout/box.go7
-rw-r--r--layout/grid.go8
-rw-r--r--layout/mux.go40
5 files changed, 44 insertions, 38 deletions
diff --git a/examples/layout/layout b/examples/layout/layout
deleted file mode 100755
index 050fbdf..0000000
--- a/examples/layout/layout
+++ /dev/null
Binary files differ
diff --git a/examples/layout/main.go b/examples/layout/main.go
index 20d82c2..f7023a2 100644
--- a/examples/layout/main.go
+++ b/examples/layout/main.go
@@ -14,7 +14,7 @@ import (
"golang.org/x/image/font/gofont/goregular"
)
-func Blinker(env gui.Env, closed bool) {
+func Blinker(env gui.Env) {
defer func() {
if recover() != nil {
log.Print("recovered blinker")
@@ -23,7 +23,7 @@ func Blinker(env gui.Env, closed bool) {
var r image.Rectangle
var visible bool = true
- // redraw takes a bool and produces a draw command
+
redraw := func() func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
if visible {
@@ -37,7 +37,7 @@ func Blinker(env gui.Env, closed bool) {
// first we draw a white rectangle
env.Draw() <- redraw()
- go func() {
+ func() {
for event := range env.Events() {
switch event := event.(type) {
case win.MoDown:
@@ -60,11 +60,6 @@ func Blinker(env gui.Env, closed bool) {
}
}
}()
-
- if closed {
- time.Sleep(time.Second * 1)
- close(env.Draw())
- }
}
func run() {
@@ -116,9 +111,9 @@ func run() {
return ret
}),
)
- go Blinker(right, false)
- go Blinker(left, false)
- go Blinker(bottomRight, false)
+ go Blinker(right)
+ go Blinker(left)
+ go Blinker(bottomRight)
var (
b1, b2, b3, b4, b5, b6 gui.Env
@@ -131,8 +126,8 @@ func run() {
layout.BoxGap(10),
layout.BoxBackground(colornames.Lightblue),
)
- go Blinker(b1, false)
- go Blinker(b2, false)
+ go Blinker(b1)
+ go Blinker(b2)
layout.NewBox(
b3,
[]*gui.Env{
@@ -156,9 +151,9 @@ func run() {
return ret
}),
)
- go Blinker(b4, false)
- go Blinker(b5, false)
- go Blinker(b6, false)
+ go Blinker(b4)
+ go Blinker(b5)
+ go Blinker(b6)
var (
btn1, btn2, btn3 gui.Env
diff --git a/layout/box.go b/layout/box.go
index 568ac58..4e16a76 100644
--- a/layout/box.go
+++ b/layout/box.go
@@ -33,7 +33,7 @@ type Box struct {
vertical bool
}
-func NewBox(env gui.Env, contents []*gui.Env, options ...func(*Box)) {
+func NewBox(env gui.Env, contents []*gui.Env, options ...func(*Box)) gui.Env {
ret := &Box{
Background: image.Black,
Contents: contents,
@@ -43,10 +43,11 @@ func NewBox(env gui.Env, contents []*gui.Env, options ...func(*Box)) {
f(ret)
}
- mux := NewMux(env, ret)
+ mux, env := NewMux(env, ret)
for _, item := range contents {
- *item, _ = mux.makeEnv(false)
+ *item = mux.MakeEnv()
}
+ return env
}
func BoxVertical(b *Box) {
diff --git a/layout/grid.go b/layout/grid.go
index 55111cb..1831794 100644
--- a/layout/grid.go
+++ b/layout/grid.go
@@ -18,7 +18,7 @@ type Grid struct {
SplitY func(int, int) []int
}
-func NewGrid(env gui.Env, contents [][]*gui.Env, options ...func(*Grid)) {
+func NewGrid(env gui.Env, contents [][]*gui.Env, options ...func(*Grid)) gui.Env {
ret := &Grid{
Background: image.Black,
Gap: 0,
@@ -30,12 +30,14 @@ func NewGrid(env gui.Env, contents [][]*gui.Env, options ...func(*Grid)) {
f(ret)
}
- mux := NewMux(env, ret)
+ mux, env := NewMux(env, ret)
for _, row := range contents {
for _, item := range row {
- *item, _ = mux.makeEnv(false)
+ *item = mux.MakeEnv()
}
}
+
+ return env
}
func GridBackground(c color.Color) func(*Grid) {
diff --git a/layout/mux.go b/layout/mux.go
index abae087..df499df 100644
--- a/layout/mux.go
+++ b/layout/mux.go
@@ -9,8 +9,7 @@ import (
)
type Mux struct {
- masterEnv *muxEnv
- inEvent chan<- gui.Event
+ inEvent chan<- gui.Event
mu sync.Mutex
lastResize gui.Event
@@ -20,13 +19,15 @@ type Mux struct {
Layout
}
-func NewMux(env gui.Env, l Layout) (mux *Mux) {
+func (m *Mux) InEvent() chan<- gui.Event { return m.inEvent }
+
+func NewMux(env gui.Env, l Layout) (mux *Mux, master gui.Env) {
drawChan := make(chan func(draw.Image) image.Rectangle)
mux = &Mux{
Layout: l,
draw: drawChan,
}
- mux.masterEnv, mux.inEvent = mux.makeEnv(true)
+ master, mux.inEvent = mux.makeEnv(true)
mux.eventsIns = make([]chan<- gui.Event, 0)
go func() {
@@ -68,8 +69,7 @@ func NewMux(env gui.Env, l Layout) (mux *Mux) {
}
mux.mu.Unlock()
}()
-
- return mux
+ return
}
type muxEnv struct {
@@ -80,20 +80,27 @@ type muxEnv struct {
func (m *muxEnv) Events() <-chan gui.Event { return m.events }
func (m *muxEnv) Draw() chan<- func(draw.Image) image.Rectangle { return m.draw }
+func (mux *Mux) MakeEnv() gui.Env {
+ env, _ := mux.makeEnv(false)
+ return env
+}
+
// We do not store master env
func (mux *Mux) makeEnv(master bool) (*muxEnv, chan<- gui.Event) {
eventsOut, eventsIn := gui.MakeEventsChan()
drawChan := make(chan func(draw.Image) image.Rectangle)
env := &muxEnv{eventsOut, drawChan}
- mux.mu.Lock()
- mux.eventsIns = append(mux.eventsIns, eventsIn)
- // make sure to always send a resize event to a new Env if we got the size already
- // that means it missed the resize event by the root Env
- if mux.lastResize != nil {
- eventsIn <- mux.lastResize
+ if !master {
+ mux.mu.Lock()
+ mux.eventsIns = append(mux.eventsIns, eventsIn)
+ // make sure to always send a resize event to a new Env if we got the size already
+ // that means it missed the resize event by the root Env
+ if mux.lastResize != nil {
+ eventsIn <- mux.lastResize
+ }
+ mux.mu.Unlock()
}
- mux.mu.Unlock()
go func() {
func() {
@@ -126,6 +133,7 @@ func (mux *Mux) makeEnv(master bool) (*muxEnv, chan<- gui.Event) {
close(eventsIn)
}
mux.eventsIns = nil
+ close(mux.inEvent)
close(mux.draw)
mux.mu.Unlock()
} else {
@@ -139,11 +147,11 @@ func (mux *Mux) makeEnv(master bool) (*muxEnv, chan<- gui.Event) {
if i != -1 {
mux.eventsIns = append(mux.eventsIns[:i], mux.eventsIns[i+1:]...)
}
+ if mux.lastResize != nil {
+ mux.InEvent() <- mux.lastResize
+ }
mux.mu.Unlock()
}
- if mux.lastResize != nil {
- mux.inEvent <- mux.lastResize
- }
}()
return env, eventsIn