xevent DRY, expose emitter.

This commit is contained in:
Miroslav Šedivý 2021-02-28 17:22:55 +01:00
parent 09625ecca5
commit 9d60468aea
2 changed files with 22 additions and 42 deletions

View File

@ -5,21 +5,31 @@ import (
) )
func (manager *DesktopManagerCtx) OnCursorChanged(listener func(serial uint64)) { func (manager *DesktopManagerCtx) OnCursorChanged(listener func(serial uint64)) {
xevent.OnCursorChanged(listener) xevent.Emmiter.On("cursor-changed", func(payload ...interface{}) {
listener(payload[0].(uint64))
})
} }
func (manager *DesktopManagerCtx) OnClipboardUpdated(listener func()) { func (manager *DesktopManagerCtx) OnClipboardUpdated(listener func()) {
xevent.OnClipboardUpdated(listener) xevent.Emmiter.On("clipboard-updated", func(payload ...interface{}) {
listener()
})
} }
func (manager *DesktopManagerCtx) OnFileChooserDialogOpened(listener func()) { func (manager *DesktopManagerCtx) OnFileChooserDialogOpened(listener func()) {
xevent.OnFileChooserDialogOpened(listener) xevent.Emmiter.On("file-chooser-dialog-opened", func(payload ...interface{}) {
listener()
})
} }
func (manager *DesktopManagerCtx) OnFileChooserDialogClosed(listener func()) { func (manager *DesktopManagerCtx) OnFileChooserDialogClosed(listener func()) {
xevent.OnFileChooserDialogClosed(listener) xevent.Emmiter.On("file-chooser-dialog-closed", func(payload ...interface{}) {
listener()
})
} }
func (manager *DesktopManagerCtx) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) { func (manager *DesktopManagerCtx) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) {
xevent.OnEventError(listener) xevent.Emmiter.On("event-error", func(payload ...interface{}) {
listener(payload[0].(uint8), payload[1].(string), payload[2].(uint8), payload[3].(uint8))
})
} }

View File

@ -15,11 +15,11 @@ import (
"github.com/kataras/go-events" "github.com/kataras/go-events"
) )
var emmiter events.EventEmmiter var Emmiter events.EventEmmiter
var file_chooser_dialog_window uint32 = 0 var file_chooser_dialog_window uint32 = 0
func init() { func init() {
emmiter = events.New() Emmiter = events.New()
} }
func EventLoop(display string) { func EventLoop(display string) {
@ -29,44 +29,14 @@ func EventLoop(display string) {
C.XEventLoop(displayUnsafe) C.XEventLoop(displayUnsafe)
} }
func OnCursorChanged(listener func(serial uint64)) {
emmiter.On("cursor-changed", func(payload ...interface{}) {
listener(payload[0].(uint64))
})
}
func OnClipboardUpdated(listener func()) {
emmiter.On("clipboard-updated", func(payload ...interface{}) {
listener()
})
}
func OnFileChooserDialogOpened(listener func()) {
emmiter.On("file-chooser-dialog-opened", func(payload ...interface{}) {
listener()
})
}
func OnFileChooserDialogClosed(listener func()) {
emmiter.On("file-chooser-dialog-closed", func(payload ...interface{}) {
listener()
})
}
func OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) {
emmiter.On("event-error", func(payload ...interface{}) {
listener(payload[0].(uint8), payload[1].(string), payload[2].(uint8), payload[3].(uint8))
})
}
//export goXEventCursorChanged //export goXEventCursorChanged
func goXEventCursorChanged(event C.XFixesCursorNotifyEvent) { func goXEventCursorChanged(event C.XFixesCursorNotifyEvent) {
emmiter.Emit("cursor-changed", uint64(event.cursor_serial)) Emmiter.Emit("cursor-changed", uint64(event.cursor_serial))
} }
//export goXEventClipboardUpdated //export goXEventClipboardUpdated
func goXEventClipboardUpdated() { func goXEventClipboardUpdated() {
emmiter.Emit("clipboard-updated") Emmiter.Emit("clipboard-updated")
} }
//export goXEventConfigureNotify //export goXEventConfigureNotify
@ -89,7 +59,7 @@ func goXEventConfigureNotify(display *C.Display, window C.Window, name *C.char,
if file_chooser_dialog_window == 0 { if file_chooser_dialog_window == 0 {
file_chooser_dialog_window = uint32(window) file_chooser_dialog_window = uint32(window)
emmiter.Emit("file-chooser-dialog-opened") Emmiter.Emit("file-chooser-dialog-opened")
} }
} }
@ -100,12 +70,12 @@ func goXEventUnmapNotify(window C.Window) {
} }
file_chooser_dialog_window = 0 file_chooser_dialog_window = 0
emmiter.Emit("file-chooser-dialog-closed") Emmiter.Emit("file-chooser-dialog-closed")
} }
//export goXEventError //export goXEventError
func goXEventError(event *C.XErrorEvent, message *C.char) { func goXEventError(event *C.XErrorEvent, message *C.char) {
emmiter.Emit("event-error", uint8(event.error_code), C.GoString(message), uint8(event.request_code), uint8(event.minor_code)) Emmiter.Emit("event-error", uint8(event.error_code), C.GoString(message), uint8(event.request_code), uint8(event.minor_code))
} }
//export goXEventActive //export goXEventActive