2020-05-15 17:00:43 +00:00
|
|
|
package ui
|
|
|
|
|
2020-05-16 07:36:04 +00:00
|
|
|
import "opslag.de/schobers/zntg"
|
|
|
|
|
2020-05-16 10:07:13 +00:00
|
|
|
type EventEmptyFn func(Context)
|
2020-05-15 17:00:43 +00:00
|
|
|
|
2020-05-16 10:07:13 +00:00
|
|
|
type EventFn func(Context, interface{})
|
2020-05-15 17:00:43 +00:00
|
|
|
|
2020-05-16 07:36:04 +00:00
|
|
|
type EventArgs struct {
|
|
|
|
Context Context
|
|
|
|
State interface{}
|
2020-05-15 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Events struct {
|
2020-05-16 07:36:04 +00:00
|
|
|
zntg.Events
|
2020-05-15 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventHandler interface {
|
2020-05-16 10:07:13 +00:00
|
|
|
AddHandler(EventFn) uint
|
|
|
|
AddHandlerEmpty(EventEmptyFn) uint
|
2020-05-15 17:00:43 +00:00
|
|
|
RemoveHandler(uint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) Notify(ctx Context, state interface{}) bool {
|
2020-05-16 07:36:04 +00:00
|
|
|
return e.Events.Notify(EventArgs{Context: ctx, State: state})
|
2020-05-15 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 10:07:13 +00:00
|
|
|
func (e *Events) AddHandler(handler EventFn) uint {
|
2020-05-16 07:36:04 +00:00
|
|
|
return e.Events.AddHandler(func(state interface{}) {
|
|
|
|
args := state.(EventArgs)
|
|
|
|
handler(args.Context, args.State)
|
|
|
|
})
|
2020-05-15 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 10:07:13 +00:00
|
|
|
func (e *Events) AddHandlerEmpty(handler EventEmptyFn) uint {
|
2020-05-16 07:36:04 +00:00
|
|
|
return e.AddHandler(func(ctx Context, _ interface{}) { handler(ctx) })
|
2020-05-15 17:00:43 +00:00
|
|
|
}
|