Added callbacks & events when visibility of an overlay changes.

Added proxy that proxies overlay callbacks as well (on top of control callbacks).
This commit is contained in:
Sander Schobers 2020-05-16 10:12:54 +02:00
parent 7f3d836254
commit 9af85d79a6
3 changed files with 59 additions and 1 deletions

6
ui/overlay.go Normal file
View File

@ -0,0 +1,6 @@
package ui
type Overlay interface {
OverlayShown()
OverlayHidden()
}

19
ui/overlayproxy.go Normal file
View File

@ -0,0 +1,19 @@
package ui
type OverlayProxy struct {
Proxy
}
func (p *OverlayProxy) OverlayShown() {
overlay, ok := p.Content.(Overlay)
if ok {
overlay.OverlayShown()
}
}
func (p *OverlayProxy) OverlayHidden() {
overlay, ok := p.Content.(Overlay)
if ok {
overlay.OverlayHidden()
}
}

View File

@ -1,15 +1,27 @@
package ui package ui
import "opslag.de/schobers/geom" import (
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
)
var _ Control = &Overlays{} var _ Control = &Overlays{}
type OverlayVisibilityChangedArgs struct {
Name string
Visible bool
}
type Overlays struct { type Overlays struct {
Proxy Proxy
overlays map[string]Control overlays map[string]Control
order []string order []string
visible map[string]bool visible map[string]bool
overlayHidden zntg.Events
overlayShown zntg.Events
visibilityChanged zntg.Events
} }
func NewOverlays(over Control) *Overlays { func NewOverlays(over Control) *Overlays {
@ -21,6 +33,27 @@ func NewOverlays(over Control) *Overlays {
} }
} }
func (o *Overlays) setVisibility(name string, visible bool) {
if o.visible[name] == visible {
return
}
o.visible[name] = visible
overlay, ok := o.overlays[name].(Overlay)
if ok {
if visible {
overlay.OverlayShown()
} else {
overlay.OverlayHidden()
}
}
o.visibilityChanged.Notify(OverlayVisibilityChangedArgs{Name: name, Visible: visible})
if visible {
o.overlayShown.Notify(name)
} else {
o.overlayHidden.Notify(name)
}
}
func (o *Overlays) AddOnTop(name string, control Control, visible bool) { func (o *Overlays) AddOnTop(name string, control Control, visible bool) {
o.order = append(o.order, name) o.order = append(o.order, name)
o.overlays[name] = control o.overlays[name] = control