Added a generic Events struct in zntg (without ui.Context).
Refactored ui.Events to re-use zntg.Events.
This commit is contained in:
parent
ff4b04262c
commit
7f3d836254
46
events.go
Normal file
46
events.go
Normal file
@ -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) }
|
@ -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)
|
||||
})
|
||||
|
43
ui/events.go
43
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) }
|
||||
|
Loading…
Reference in New Issue
Block a user