forked from google/gxui
/
attachable.go
63 lines (53 loc) · 1.17 KB
/
attachable.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parts
import (
"github.com/google/gxui"
"github.com/google/gxui/mixins/outer"
)
type AttachableOuter interface {
outer.Relayouter
}
type Attachable struct {
outer AttachableOuter
onAttach gxui.Event
onDetach gxui.Event
attached bool
}
func (a *Attachable) Init(outer AttachableOuter) {
a.outer = outer
}
func (a *Attachable) Attached() bool {
return a.attached
}
func (a *Attachable) Attach() {
if a.attached {
panic("Control already attached")
}
a.attached = true
if a.onAttach != nil {
a.onAttach.Fire()
}
}
func (a *Attachable) Detach() {
if !a.attached {
panic("Control already detached")
}
a.attached = false
if a.onDetach != nil {
a.onDetach.Fire()
}
}
func (a *Attachable) OnAttach(f func()) gxui.EventSubscription {
if a.onAttach == nil {
a.onAttach = gxui.CreateEvent(func() {})
}
return a.onAttach.Listen(f)
}
func (a *Attachable) OnDetach(f func()) gxui.EventSubscription {
if a.onDetach == nil {
a.onDetach = gxui.CreateEvent(func() {})
}
return a.onDetach.Listen(f)
}