forked from mbk-lab/rui_orig
2
0
Fork 0
rui/keyEvents.go

275 lines
6.8 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import "strings"
const (
// KeyDown is the constant for "key-down-event" property tag.
// The "key-down-event" event is fired when a key is pressed.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, KeyEvent).
// The additional listener formats:
// func(KeyEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
KeyDownEvent = "key-down-event"
2021-11-04 21:13:34 +03:00
// KeyPp is the constant for "key-up-event" property tag.
2021-09-07 17:36:50 +03:00
// The "key-up-event" event is fired when a key is released.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, KeyEvent).
// The additional listener formats:
// func(KeyEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
KeyUpEvent = "key-up-event"
)
type KeyEvent struct {
// TimeStamp is the time at which the event was created (in milliseconds).
// This value is time since epoch—but in reality, browsers' definitions vary.
TimeStamp uint64
// Key is the key value of the key represented by the event. If the value has a printed representation,
// this attribute's value is the same as the char property. Otherwise, it's one of the key value strings
// specified in Key values. If the key can't be identified, its value is the string "Unidentified".
Key string
// Code holds a string that identifies the physical key being pressed. The value is not affected
// by the current keyboard layout or modifier state, so a particular key will always return the same value.
Code string
// Repeat == true if a key has been depressed long enough to trigger key repetition, otherwise false.
Repeat bool
// CtrlKey == true if the control key was down when the event was fired. false otherwise.
CtrlKey bool
// ShiftKey == true if the shift key was down when the event was fired. false otherwise.
ShiftKey bool
// AltKey == true if the alt key was down when the event was fired. false otherwise.
AltKey bool
// MetaKey == true if the meta key was down when the event was fired. false otherwise.
MetaKey bool
}
2022-07-27 20:31:57 +03:00
func (event *KeyEvent) init(data DataObject) {
getBool := func(tag string) bool {
if value, ok := data.PropertyValue(tag); ok && value == "1" {
return true
}
return false
}
event.Key, _ = data.PropertyValue("key")
event.Code, _ = data.PropertyValue("code")
event.TimeStamp = getTimeStamp(data)
event.Repeat = getBool("repeat")
event.CtrlKey = getBool("ctrlKey")
event.ShiftKey = getBool("shiftKey")
event.AltKey = getBool("altKey")
event.MetaKey = getBool("metaKey")
}
func valueToEventListeners[V View, E any](value any) ([]func(V, E), bool) {
2021-09-07 17:36:50 +03:00
if value == nil {
return nil, true
}
switch value := value.(type) {
2022-07-27 20:31:57 +03:00
case func(V, E):
return []func(V, E){value}, true
2021-09-07 17:36:50 +03:00
2022-07-27 20:31:57 +03:00
case func(E):
fn := func(_ V, event E) {
2021-09-07 17:36:50 +03:00
value(event)
}
2022-07-27 20:31:57 +03:00
return []func(V, E){fn}, true
2021-09-07 17:36:50 +03:00
2022-07-27 20:31:57 +03:00
case func(V):
fn := func(view V, _ E) {
2021-09-07 17:36:50 +03:00
value(view)
}
2022-07-27 20:31:57 +03:00
return []func(V, E){fn}, true
2021-09-07 17:36:50 +03:00
case func():
2022-07-27 20:31:57 +03:00
fn := func(V, E) {
2021-09-07 17:36:50 +03:00
value()
}
2022-07-27 20:31:57 +03:00
return []func(V, E){fn}, true
2021-09-07 17:36:50 +03:00
2022-07-27 20:31:57 +03:00
case []func(V, E):
2021-09-07 17:36:50 +03:00
if len(value) == 0 {
return nil, true
}
for _, fn := range value {
if fn == nil {
return nil, false
}
}
return value, true
2022-07-27 20:31:57 +03:00
case []func(E):
2021-09-07 17:36:50 +03:00
count := len(value)
if count == 0 {
return nil, true
}
2022-07-27 20:31:57 +03:00
listeners := make([]func(V, E), count)
2021-09-07 17:36:50 +03:00
for i, v := range value {
if v == nil {
return nil, false
}
2022-07-27 20:31:57 +03:00
listeners[i] = func(_ V, event E) {
2021-09-07 17:36:50 +03:00
v(event)
}
}
return listeners, true
2022-07-27 20:31:57 +03:00
case []func(V):
2021-09-07 17:36:50 +03:00
count := len(value)
if count == 0 {
return nil, true
}
2022-07-27 20:31:57 +03:00
listeners := make([]func(V, E), count)
2021-09-07 17:36:50 +03:00
for i, v := range value {
if v == nil {
return nil, false
}
2022-07-27 20:31:57 +03:00
listeners[i] = func(view V, _ E) {
2021-09-07 17:36:50 +03:00
v(view)
}
}
return listeners, true
case []func():
count := len(value)
if count == 0 {
return nil, true
}
2022-07-27 20:31:57 +03:00
listeners := make([]func(V, E), count)
2021-09-07 17:36:50 +03:00
for i, v := range value {
if v == nil {
return nil, false
}
2022-07-27 20:31:57 +03:00
listeners[i] = func(V, E) {
2021-09-07 17:36:50 +03:00
v()
}
}
return listeners, true
2022-07-26 18:36:00 +03:00
case []any:
2021-09-07 17:36:50 +03:00
count := len(value)
if count == 0 {
return nil, true
}
2022-07-27 20:31:57 +03:00
listeners := make([]func(V, E), count)
2021-09-07 17:36:50 +03:00
for i, v := range value {
if v == nil {
return nil, false
}
switch v := v.(type) {
2022-07-27 20:31:57 +03:00
case func(V, E):
2021-09-07 17:36:50 +03:00
listeners[i] = v
2022-07-27 20:31:57 +03:00
case func(E):
listeners[i] = func(_ V, event E) {
2021-09-07 17:36:50 +03:00
v(event)
}
2022-07-27 20:31:57 +03:00
case func(V):
listeners[i] = func(view V, _ E) {
2021-09-07 17:36:50 +03:00
v(view)
}
case func():
2022-07-27 20:31:57 +03:00
listeners[i] = func(V, E) {
2021-09-07 17:36:50 +03:00
v()
}
default:
return nil, false
}
}
return listeners, true
}
return nil, false
}
var keyEvents = map[string]struct{ jsEvent, jsFunc string }{
KeyDownEvent: {jsEvent: "onkeydown", jsFunc: "keyDownEvent"},
KeyUpEvent: {jsEvent: "onkeyup", jsFunc: "keyUpEvent"},
}
2022-07-26 18:36:00 +03:00
func (view *viewData) setKeyListener(tag string, value any) bool {
2022-07-27 20:31:57 +03:00
listeners, ok := valueToEventListeners[View, KeyEvent](value)
2021-09-07 17:36:50 +03:00
if !ok {
notCompatibleType(tag, value)
return false
}
if listeners == nil {
view.removeKeyListener(tag)
} else if js, ok := keyEvents[tag]; ok {
view.properties[tag] = listeners
if view.created {
2022-10-30 17:22:33 +03:00
view.session.updateProperty(view.htmlID(), js.jsEvent, js.jsFunc+"(this, event)")
2021-09-07 17:36:50 +03:00
}
} else {
return false
}
return true
}
func (view *viewData) removeKeyListener(tag string) {
delete(view.properties, tag)
if view.created {
if js, ok := keyEvents[tag]; ok {
2022-10-30 17:22:33 +03:00
view.session.removeProperty(view.htmlID(), js.jsEvent)
2021-09-07 17:36:50 +03:00
}
}
}
func getEventListeners[V View, E any](view View, subviewID []string, tag string) []func(V, E) {
if len(subviewID) > 0 && subviewID[0] != "" {
view = ViewByID(view, subviewID[0])
2021-09-07 17:36:50 +03:00
}
if view != nil {
if value := view.Get(tag); value != nil {
2022-07-27 20:31:57 +03:00
if result, ok := value.([]func(V, E)); ok {
2021-09-07 17:36:50 +03:00
return result
}
}
}
2022-07-27 20:31:57 +03:00
return []func(V, E){}
2021-09-07 17:36:50 +03:00
}
func keyEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range keyEvents {
if listeners := getEventListeners[View, KeyEvent](view, nil, tag); len(listeners) > 0 {
2021-09-07 17:36:50 +03:00
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `)
}
}
}
func handleKeyEvents(view View, tag string, data DataObject) {
listeners := getEventListeners[View, KeyEvent](view, nil, tag)
2022-07-27 20:31:57 +03:00
if len(listeners) > 0 {
var event KeyEvent
event.init(data)
2021-09-07 17:36:50 +03:00
2022-07-27 20:31:57 +03:00
for _, listener := range listeners {
listener(view, event)
2021-09-07 17:36:50 +03:00
}
}
}
// GetKeyDownListeners returns the "key-down-event" listener list. If there are no listeners then the empty list is returned.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetKeyDownListeners(view View, subviewID ...string) []func(View, KeyEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, KeyEvent](view, subviewID, KeyDownEvent)
2021-09-07 17:36:50 +03:00
}
// GetKeyUpListeners returns the "key-up-event" listener list. If there are no listeners then the empty list is returned.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetKeyUpListeners(view View, subviewID ...string) []func(View, KeyEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, KeyEvent](view, subviewID, KeyUpEvent)
2021-09-07 17:36:50 +03:00
}