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 {
|
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)
|
args := state.(ControlClickedArgs)
|
||||||
handler(ctx, args)
|
handler(ctx, args)
|
||||||
})
|
})
|
||||||
@ -40,7 +40,7 @@ type DragEndedEvents struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *DragEndedEvents) AddHandler(handler func(Context, DragEndedArgs)) uint {
|
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)
|
args := state.(DragEndedArgs)
|
||||||
handler(ctx, args)
|
handler(ctx, args)
|
||||||
})
|
})
|
||||||
@ -60,7 +60,7 @@ type DragMovedEvents struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *DragMovedEvents) AddHandler(handler func(Context, DragMovedArgs)) uint {
|
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)
|
args := state.(DragMovedArgs)
|
||||||
handler(ctx, args)
|
handler(ctx, args)
|
||||||
})
|
})
|
||||||
@ -79,7 +79,7 @@ type DragStartedEvents struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *DragStartedEvents) AddHandler(handler func(Context, DragStartedArgs)) uint {
|
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)
|
args := state.(DragStartedArgs)
|
||||||
handler(ctx, args)
|
handler(ctx, args)
|
||||||
})
|
})
|
||||||
|
43
ui/events.go
43
ui/events.go
@ -1,46 +1,37 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
type EventFn func(Context) bool
|
import "opslag.de/schobers/zntg"
|
||||||
|
|
||||||
|
type EventFn func(Context)
|
||||||
|
|
||||||
type EventStateFn func(Context, interface{})
|
type EventStateFn func(Context, interface{})
|
||||||
|
|
||||||
func NewEvents() *Events {
|
type EventArgs struct {
|
||||||
return &Events{events: map[uint]EventStateFn{}}
|
Context Context
|
||||||
|
State interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Events struct {
|
type Events struct {
|
||||||
nextID uint
|
zntg.Events
|
||||||
events map[uint]EventStateFn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventHandler interface {
|
type EventHandler interface {
|
||||||
AddHandler(EventFn) uint
|
AddHandler(EventStateFn) uint
|
||||||
AddHandlerState(EventStateFn) uint
|
AddHandlerEmpty(EventFn) uint
|
||||||
RemoveHandler(uint)
|
RemoveHandler(uint)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Events) Notify(ctx Context, state interface{}) bool {
|
func (e *Events) Notify(ctx Context, state interface{}) bool {
|
||||||
if e.events == nil {
|
return e.Events.Notify(EventArgs{Context: ctx, State: state})
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, handler := range e.events {
|
|
||||||
handler(ctx, state)
|
|
||||||
}
|
|
||||||
return len(e.events) > 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Events) AddHandler(handler EventFn) uint {
|
func (e *Events) AddHandler(handler EventStateFn) uint {
|
||||||
return e.AddHandlerState(func(ctx Context, _ interface{}) { handler(ctx) })
|
return e.Events.AddHandler(func(state interface{}) {
|
||||||
|
args := state.(EventArgs)
|
||||||
|
handler(args.Context, args.State)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Events) AddHandlerState(handler EventStateFn) uint {
|
func (e *Events) AddHandlerEmpty(handler EventFn) uint {
|
||||||
if e.events == nil {
|
return e.AddHandler(func(ctx Context, _ interface{}) { handler(ctx) })
|
||||||
e.events = map[uint]EventStateFn{}
|
|
||||||
}
|
|
||||||
id := e.nextID
|
|
||||||
e.nextID++
|
|
||||||
e.events[id] = handler
|
|
||||||
return id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Events) RemoveHandler(id uint) { delete(e.events, id) }
|
|
||||||
|
Loading…
Reference in New Issue
Block a user