forked from mbk-lab/rui_orig
2
0
Fork 0

Bug fixing & optimisation

This commit is contained in:
anoshenko 2023-05-02 17:20:01 +03:00
parent 43cb889ab1
commit 43a8b9fe58
7 changed files with 90 additions and 26 deletions

View File

@ -91,7 +91,10 @@ func transitionEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range transitionEvents { for tag, js := range transitionEvents {
if value := view.getRaw(tag); value != nil { if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View, string)); ok && len(listeners) > 0 { if listeners, ok := value.([]func(View, string)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }
@ -157,7 +160,10 @@ func animationEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range animationEvents { for tag, js := range animationEvents {
if value := view.getRaw(tag); value != nil { if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View)); ok && len(listeners) > 0 { if listeners, ok := value.([]func(View)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }

View File

@ -143,7 +143,10 @@ func getFocusListeners(view View, subviewID []string, tag string) []func(View) {
func focusEventsHtml(view View, buffer *strings.Builder) { func focusEventsHtml(view View, buffer *strings.Builder) {
if view.Focusable() { if view.Focusable() {
for _, js := range focusEvents { for _, js := range focusEvents {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }

View File

@ -372,11 +372,12 @@ func valueToEventWithOldListeners[V View, E any](value any) ([]func(V, E, E), bo
return nil, false return nil, false
} }
/*
var keyEvents = map[string]struct{ jsEvent, jsFunc string }{ var keyEvents = map[string]struct{ jsEvent, jsFunc string }{
KeyDownEvent: {jsEvent: "onkeydown", jsFunc: "keyDownEvent"}, KeyDownEvent: {jsEvent: "onkeydown", jsFunc: "keyDownEvent"},
KeyUpEvent: {jsEvent: "onkeyup", jsFunc: "keyUpEvent"}, KeyUpEvent: {jsEvent: "onkeyup", jsFunc: "keyUpEvent"},
} }
*/
func (view *viewData) setKeyListener(tag string, value any) bool { func (view *viewData) setKeyListener(tag string, value any) bool {
listeners, ok := valueToEventListeners[View, KeyEvent](value) listeners, ok := valueToEventListeners[View, KeyEvent](value)
if !ok { if !ok {
@ -386,22 +387,37 @@ func (view *viewData) setKeyListener(tag string, value any) bool {
if listeners == nil { if listeners == nil {
view.removeKeyListener(tag) view.removeKeyListener(tag)
} else if js, ok := keyEvents[tag]; ok {
view.properties[tag] = listeners
if view.created {
view.session.updateProperty(view.htmlID(), js.jsEvent, js.jsFunc+"(this, event)")
}
} else { } else {
switch tag {
case KeyDownEvent:
if view.created {
view.session.updateProperty(view.htmlID(), "onkeydown", "keyDownEvent(this, event)")
}
case KeyUpEvent:
if view.created {
view.session.updateProperty(view.htmlID(), "onkeyup", "keyUpEvent(this, event)")
}
default:
return false return false
} }
}
return true return true
} }
func (view *viewData) removeKeyListener(tag string) { func (view *viewData) removeKeyListener(tag string) {
delete(view.properties, tag) delete(view.properties, tag)
if view.created { if view.created {
if js, ok := keyEvents[tag]; ok { switch tag {
view.session.removeProperty(view.htmlID(), js.jsEvent) case KeyDownEvent:
if !view.Focusable() {
view.session.removeProperty(view.htmlID(), "onkeydown")
}
case KeyUpEvent:
view.session.removeProperty(view.htmlID(), "onkeyup")
} }
} }
} }
@ -434,22 +450,52 @@ func getEventListeners[V View, E any](view View, subviewID []string, tag string)
} }
func keyEventsHtml(view View, buffer *strings.Builder) { func keyEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range keyEvents { if len(getEventListeners[View, KeyEvent](view, nil, KeyDownEvent)) > 0 {
if listeners := getEventListeners[View, KeyEvent](view, nil, tag); len(listeners) > 0 { buffer.WriteString(`onkeydown="keyDownEvent(this, event)" `)
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) } else if view.Focusable() {
if len(getEventListeners[View, MouseEvent](view, nil, ClickEvent)) > 0 {
buffer.WriteString(`onkeydown="keyDownEvent(this, event)" `)
} }
} }
if listeners := getEventListeners[View, KeyEvent](view, nil, KeyUpEvent); len(listeners) > 0 {
buffer.WriteString(`onkeyup="keyUpEvent(this, event)" `)
}
} }
func handleKeyEvents(view View, tag string, data DataObject) { func handleKeyEvents(view View, tag string, data DataObject) {
listeners := getEventListeners[View, KeyEvent](view, nil, tag)
if len(listeners) > 0 {
var event KeyEvent var event KeyEvent
event.init(data) event.init(data)
listeners := getEventListeners[View, KeyEvent](view, nil, tag)
if len(listeners) > 0 {
for _, listener := range listeners { for _, listener := range listeners {
listener(view, event) listener(view, event)
} }
return
}
if tag == KeyDownEvent && view.Focusable() && (event.Key == " " || event.Key == "Enter") && !IsDisabled(view) {
if listeners := getEventListeners[View, MouseEvent](view, nil, ClickEvent); len(listeners) > 0 {
clickEvent := MouseEvent{
TimeStamp: event.TimeStamp,
Button: PrimaryMouseButton,
Buttons: PrimaryMouseMask,
CtrlKey: event.CtrlKey,
AltKey: event.AltKey,
ShiftKey: event.ShiftKey,
MetaKey: event.MetaKey,
ClientX: view.Frame().Width / 2,
ClientY: view.Frame().Height / 2,
X: view.Frame().Width / 2,
Y: view.Frame().Height / 2,
ScreenX: view.Frame().Left + view.Frame().Width/2,
ScreenY: view.Frame().Top + view.Frame().Height/2,
}
for _, listener := range listeners {
listener(view, clickEvent)
}
}
} }
} }

View File

@ -188,7 +188,10 @@ func mouseEventsHtml(view View, buffer *strings.Builder, hasTooltip bool) {
for tag, js := range mouseEvents { for tag, js := range mouseEvents {
if value := view.getRaw(tag); value != nil { if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View, MouseEvent)); ok && len(listeners) > 0 { if listeners, ok := value.([]func(View, MouseEvent)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }

View File

@ -129,7 +129,10 @@ func pointerEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range pointerEvents { for tag, js := range pointerEvents {
if value := view.getRaw(tag); value != nil { if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View, PointerEvent)); ok && len(listeners) > 0 { if listeners, ok := value.([]func(View, PointerEvent)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }

View File

@ -528,7 +528,7 @@ func (theme *theme) cssText(session Session) string {
styleList := func(styles map[string]ViewStyle) []string { styleList := func(styles map[string]ViewStyle) []string {
ruiStyles := []string{} ruiStyles := []string{}
customStyles := []string{} customStyles := []string{}
for tag, _ := range styles { for tag := range styles {
if strings.HasPrefix(tag, "rui") { if strings.HasPrefix(tag, "rui") {
ruiStyles = append(ruiStyles, tag) ruiStyles = append(ruiStyles, tag)
} else { } else {

View File

@ -130,7 +130,10 @@ func touchEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range touchEvents { for tag, js := range touchEvents {
if value := view.getRaw(tag); value != nil { if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View, TouchEvent)); ok && len(listeners) > 0 { if listeners, ok := value.([]func(View, TouchEvent)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `) buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
} }
} }
} }