diff --git a/events.go b/events.go new file mode 100644 index 0000000..e264187 --- /dev/null +++ b/events.go @@ -0,0 +1,46 @@ +package zntg + +type EventFn func() + +type EventStateFn func(interface{}) + +func NewEvents() *Events { + return &Events{events: map[uint]EventStateFn{}} +} + +type Events struct { + nextID uint + events map[uint]EventStateFn +} + +type EventHandler interface { + AddHandler(EventStateFn) uint + AddHandlerEmpty(EventFn) uint + RemoveHandler(uint) +} + +func (e *Events) Notify(state interface{}) bool { + if e.events == nil { + return false + } + for _, handler := range e.events { + handler(state) + } + return len(e.events) > 0 +} + +func (e *Events) AddHandler(handler EventStateFn) uint { + if e.events == nil { + e.events = map[uint]EventStateFn{} + } + id := e.nextID + e.nextID++ + e.events[id] = handler + return id +} + +func (e *Events) AddHandlerEmpty(handler EventFn) uint { + return e.AddHandler(func(interface{}) { handler() }) +} + +func (e *Events) RemoveHandler(id uint) { delete(e.events, id) } diff --git a/ui/controlbase.go b/ui/controlbase.go index df54d3b..65c12e7 100644 --- a/ui/controlbase.go +++ b/ui/controlbase.go @@ -20,7 +20,7 @@ type ControlClickedEvents struct { } func (e *ControlClickedEvents) AddHandler(handler func(Context, ControlClickedArgs)) uint { - return e.Events.AddHandlerState(func(ctx Context, state interface{}) { + return e.Events.AddHandler(func(ctx Context, state interface{}) { args := state.(ControlClickedArgs) handler(ctx, args) }) @@ -40,7 +40,7 @@ type DragEndedEvents struct { } func (e *DragEndedEvents) AddHandler(handler func(Context, DragEndedArgs)) uint { - return e.Events.AddHandlerState(func(ctx Context, state interface{}) { + return e.Events.AddHandler(func(ctx Context, state interface{}) { args := state.(DragEndedArgs) handler(ctx, args) }) @@ -60,7 +60,7 @@ type DragMovedEvents struct { } func (e *DragMovedEvents) AddHandler(handler func(Context, DragMovedArgs)) uint { - return e.Events.AddHandlerState(func(ctx Context, state interface{}) { + return e.Events.AddHandler(func(ctx Context, state interface{}) { args := state.(DragMovedArgs) handler(ctx, args) }) @@ -79,7 +79,7 @@ type DragStartedEvents struct { } func (e *DragStartedEvents) AddHandler(handler func(Context, DragStartedArgs)) uint { - return e.Events.AddHandlerState(func(ctx Context, state interface{}) { + return e.Events.AddHandler(func(ctx Context, state interface{}) { args := state.(DragStartedArgs) handler(ctx, args) }) diff --git a/ui/events.go b/ui/events.go index 066627b..404f36c 100644 --- a/ui/events.go +++ b/ui/events.go @@ -1,46 +1,37 @@ package ui -type EventFn func(Context) bool +import "opslag.de/schobers/zntg" + +type EventFn func(Context) type EventStateFn func(Context, interface{}) -func NewEvents() *Events { - return &Events{events: map[uint]EventStateFn{}} +type EventArgs struct { + Context Context + State interface{} } type Events struct { - nextID uint - events map[uint]EventStateFn + zntg.Events } type EventHandler interface { - AddHandler(EventFn) uint - AddHandlerState(EventStateFn) uint + AddHandler(EventStateFn) uint + AddHandlerEmpty(EventFn) uint RemoveHandler(uint) } func (e *Events) Notify(ctx Context, state interface{}) bool { - if e.events == nil { - return false - } - for _, handler := range e.events { - handler(ctx, state) - } - return len(e.events) > 0 + return e.Events.Notify(EventArgs{Context: ctx, State: state}) } -func (e *Events) AddHandler(handler EventFn) uint { - return e.AddHandlerState(func(ctx Context, _ interface{}) { handler(ctx) }) +func (e *Events) AddHandler(handler EventStateFn) uint { + return e.Events.AddHandler(func(state interface{}) { + args := state.(EventArgs) + handler(args.Context, args.State) + }) } -func (e *Events) AddHandlerState(handler EventStateFn) uint { - if e.events == nil { - e.events = map[uint]EventStateFn{} - } - id := e.nextID - e.nextID++ - e.events[id] = handler - return id +func (e *Events) AddHandlerEmpty(handler EventFn) uint { + return e.AddHandler(func(ctx Context, _ interface{}) { handler(ctx) }) } - -func (e *Events) RemoveHandler(id uint) { delete(e.events, id) }