Skip to content

Commit

Permalink
Add EventFunc interface to allow queing of typed events to allow opti…
Browse files Browse the repository at this point in the history
…mizations
  • Loading branch information
sdassow committed Nov 22, 2024
1 parent 6bfd026 commit 29c2216
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 55 deletions.
10 changes: 10 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ type DragEvent struct {
PointEvent
Dragged Delta
}

type EventFunc interface {
Execute()
}

type SimpleEventFunc func()

func (fn SimpleEventFunc) Execute() {
fn()
}
102 changes: 102 additions & 0 deletions internal/async/chan_eventfunc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/async/chan_go1.21.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ func NewUnboundedInterfaceChan() *UnboundedInterfaceChan {
// CanvasObject objects. A channel must be closed via Close method.
type UnboundedCanvasObjectChan = UnboundedChan[fyne.CanvasObject]

type UnboundedEventFuncChan = UnboundedChan[fyne.EventFunc]

// NewUnboundedCanvasObjectChan returns a unbounded channel, of canvas objects, with unlimited capacity.
func NewUnboundedCanvasObjectChan() *UnboundedChan[fyne.CanvasObject] {
return NewUnboundedChan[fyne.CanvasObject]()
}

func NewUnboundedEventFuncChan() *UnboundedChan[fyne.EventFunc] {
return NewUnboundedChan[fyne.EventFunc]()
}

// UnboundedChan is a channel with an unbounded buffer for caching
// Func objects. A channel must be closed via Close method.
type UnboundedChan[T any] struct {
Expand Down
5 changes: 5 additions & 0 deletions internal/async/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func main() {
Name: "Interface",
Imports: "",
},
"chan_eventfunc.go": {
Type: "fyne.EventFunc",
Name: "EventFunc",
Imports: `import "fyne.io/fyne/v2"`,
},
},
}

Expand Down
11 changes: 6 additions & 5 deletions internal/driver/common/window.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package common

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/async"
)

// Window defines common functionality for windows.
type Window struct {
eventQueue *async.UnboundedFuncChan
eventQueue *async.UnboundedEventFuncChan
}

// DestroyEventQueue destroys the event queue.
Expand All @@ -17,20 +18,20 @@ func (w *Window) DestroyEventQueue() {
// InitEventQueue initializes the event queue.
func (w *Window) InitEventQueue() {
// This channel should be closed when the window is closed.
w.eventQueue = async.NewUnboundedFuncChan()
w.eventQueue = async.NewUnboundedEventFuncChan()
}

// QueueEvent uses this method to queue up a callback that handles an event. This ensures
// user interaction events for a given window are processed in order.
func (w *Window) QueueEvent(fn func()) {
func (w *Window) QueueEvent(fn fyne.EventFunc) {
w.eventQueue.In() <- fn
}

// RunEventQueue runs the event queue. This should called inside a go routine.
// This function blocks.
func (w *Window) RunEventQueue() {
for fn := range w.eventQueue.Out() {
fn()
fn.Execute()
}
}

Expand All @@ -39,7 +40,7 @@ func (w *Window) WaitForEvents() {
done := DonePool.Get()
defer DonePool.Put(done)

w.eventQueue.In() <- func() { done <- struct{}{} }
w.eventQueue.In() <- fyne.SimpleEventFunc(func() { done <- struct{}{} })
<-done
}

Expand Down
2 changes: 1 addition & 1 deletion internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (d *gLDriver) Device() fyne.Device {
func (d *gLDriver) Quit() {
if curWindow != nil {
if f := fyne.CurrentApp().Lifecycle().(*intapp.Lifecycle).OnExitedForeground(); f != nil {
curWindow.QueueEvent(f)
curWindow.QueueEvent(fyne.SimpleEventFunc(f))
}
curWindow = nil
if d.trayStop != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/glfw/menu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func registerCallback(w *window, item *fyne.MenuItem, nextItemID int) int {
callbacks = append(callbacks, &menuCallbacks{
action: func() {
if item.Action != nil {
w.QueueEvent(item.Action)
w.QueueEvent(fyne.SimpleEventFunc(item.Action))
}
},
enabled: func() bool {
Expand Down
Loading

0 comments on commit 29c2216

Please sign in to comment.