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

286 lines
12 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strconv"
"strings"
)
const (
2021-11-04 21:13:34 +03:00
// ClickEvent is the constant for "click-event" property tag.
2021-09-07 17:36:50 +03:00
// The "click-event" event occurs when the user clicks on the View.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
ClickEvent = "click-event"
2021-11-04 21:13:34 +03:00
// DoubleClickEvent is the constant for "double-click-event" property tag.
2021-09-07 17:36:50 +03:00
// The "double-click-event" event occurs when the user double clicks on the View.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
DoubleClickEvent = "double-click-event"
// MouseDown is the constant for "mouse-down" property tag.
// The "mouse-down" event is fired at a View when a pointing device button is pressed
// while the pointer is inside the view.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
MouseDown = "mouse-down"
// MouseUp is the constant for "mouse-up" property tag.
// The "mouse-up" event is fired at a View when a button on a pointing device (such as a mouse
// or trackpad) is released while the pointer is located inside it.
// "mouse-up" events are the counterpoint to "mouse-down" events.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
MouseUp = "mouse-up"
// MouseMove is the constant for "mouse-move" property tag.
// The "mouse-move" event is fired at a view when a pointing device (usually a mouse) is moved
// while the cursor's hotspot is inside it.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
MouseMove = "mouse-move"
// MouseOut is the constant for "mouse-out" property tag.
// The "mouse-out" event is fired at a View when a pointing device (usually a mouse) is used to move
// the cursor so that it is no longer contained within the view or one of its children.
// "mouse-out" is also delivered to a view if the cursor enters a child view,
// because the child view obscures the visible area of the view.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
MouseOut = "mouse-out"
// MouseOver is the constant for "mouse-over" property tag.
// The "mouse-over" event is fired at a View when a pointing device (such as a mouse or trackpad)
// is used to move the cursor onto the view or one of its child views.
2021-11-04 21:13:34 +03:00
// The main listener formats:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
MouseOver = "mouse-over"
2021-11-04 21:13:34 +03:00
// ContextMenuEvent is the constant for "context-menu-event" property tag.
2021-09-07 17:36:50 +03:00
// The "context-menu-event" event occurs when the user calls the context menu by the right mouse clicking.
2021-11-04 21:13:34 +03:00
// The main listener format:
// func(View, MouseEvent).
// The additional listener formats:
// func(MouseEvent), func(View), and func().
2021-09-07 17:36:50 +03:00
ContextMenuEvent = "context-menu-event"
// PrimaryMouseButton is a number of the main pressed button, usually the left button or the un-initialized state
PrimaryMouseButton = 0
// AuxiliaryMouseButton is a number of the auxiliary pressed button, usually the wheel button
// or the middle button (if present)
AuxiliaryMouseButton = 1
// SecondaryMouseButton is a number of the secondary pressed button, usually the right button
SecondaryMouseButton = 2
// MouseButton4 is a number of the fourth button, typically the Browser Back button
MouseButton4 = 3
// MouseButton5 is a number of the fifth button, typically the Browser Forward button
MouseButton5 = 4
// PrimaryMouseMask is the mask of the primary button (usually the left button)
PrimaryMouseMask = 1
// SecondaryMouseMask is the mask of the secondary button (usually the right button)
SecondaryMouseMask = 2
// AuxiliaryMouseMask is the mask of the auxiliary button (usually the mouse wheel button or middle button)
AuxiliaryMouseMask = 4
// MouseMask4 is the mask of the 4th button (typically the "Browser Back" button)
MouseMask4 = 8
//MouseMask5 is the mask of the 5th button (typically the "Browser Forward" button)
MouseMask5 = 16
)
type MouseEvent 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
// Button indicates which button was pressed on the mouse to trigger the event:
// PrimaryMouseButton (0), AuxiliaryMouseButton (1), SecondaryMouseButton (2),
// MouseButton4 (3), and MouseButton5 (4)
Button int
// Buttons indicates which buttons are pressed on the mouse (or other input device)
// when a mouse event is triggered. Each button that can be pressed is represented by a given mask:
// PrimaryMouseMask (1), SecondaryMouseMask (2), AuxiliaryMouseMask (4), MouseMask4 (8), and MouseMask5 (16)
Buttons int
// X provides the horizontal coordinate within the view's viewport.
X float64
// Y provides the vertical coordinate within the view's viewport.
Y float64
// ClientX provides the horizontal coordinate within the application's viewport at which the event occurred.
ClientX float64
// ClientY provides the vertical coordinate within the application's viewport at which the event occurred.
ClientY float64
// ScreenX provides the horizontal coordinate (offset) of the mouse pointer in global (screen) coordinates.
ScreenX float64
// ScreenY provides the vertical coordinate (offset) of the mouse pointer in global (screen) coordinates.
ScreenY float64
// 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
}
var mouseEvents = map[string]struct{ jsEvent, jsFunc string }{
ClickEvent: {jsEvent: "onclick", jsFunc: "clickEvent"},
DoubleClickEvent: {jsEvent: "ondblclick", jsFunc: "doubleClickEvent"},
MouseDown: {jsEvent: "onmousedown", jsFunc: "mouseDownEvent"},
MouseUp: {jsEvent: "onmouseup", jsFunc: "mouseUpEvent"},
MouseMove: {jsEvent: "onmousemove", jsFunc: "mouseMoveEvent"},
MouseOut: {jsEvent: "onmouseout", jsFunc: "mouseOutEvent"},
MouseOver: {jsEvent: "onmouseover", jsFunc: "mouseOverEvent"},
ContextMenuEvent: {jsEvent: "oncontextmenu", jsFunc: "contextMenuEvent"},
}
2022-07-26 18:36:00 +03:00
func (view *viewData) setMouseListener(tag string, value any) bool {
2022-07-27 20:31:57 +03:00
listeners, ok := valueToEventListeners[View, MouseEvent](value)
2021-09-07 17:36:50 +03:00
if !ok {
notCompatibleType(tag, value)
return false
}
if listeners == nil {
view.removeMouseListener(tag)
} else if js, ok := mouseEvents[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) removeMouseListener(tag string) {
delete(view.properties, tag)
if view.created {
if js, ok := mouseEvents[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 mouseEventsHtml(view View, buffer *strings.Builder) {
for tag, js := range mouseEvents {
if value := view.getRaw(tag); value != nil {
if listeners, ok := value.([]func(View, MouseEvent)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent + `="` + js.jsFunc + `(this, event)" `)
}
}
}
}
func getTimeStamp(data DataObject) uint64 {
if value, ok := data.PropertyValue("timeStamp"); ok {
if index := strings.Index(value, "."); index > 0 {
value = value[:index]
}
if n, err := strconv.ParseUint(value, 10, 64); err == nil {
return n
}
}
return 0
}
func (event *MouseEvent) init(data DataObject) {
event.TimeStamp = getTimeStamp(data)
2021-11-04 14:59:25 +03:00
event.Button, _ = dataIntProperty(data, "button")
event.Buttons, _ = dataIntProperty(data, "buttons")
2021-09-07 17:36:50 +03:00
event.X = dataFloatProperty(data, "x")
event.Y = dataFloatProperty(data, "y")
event.ClientX = dataFloatProperty(data, "clientX")
event.ClientY = dataFloatProperty(data, "clientY")
event.ScreenX = dataFloatProperty(data, "screenX")
event.ScreenY = dataFloatProperty(data, "screenY")
event.CtrlKey = dataBoolProperty(data, "ctrlKey")
event.ShiftKey = dataBoolProperty(data, "shiftKey")
event.AltKey = dataBoolProperty(data, "altKey")
event.MetaKey = dataBoolProperty(data, "metaKey")
}
func handleMouseEvents(view View, tag string, data DataObject) {
listeners := getEventListeners[View, MouseEvent](view, nil, tag)
2022-07-27 20:31:57 +03:00
if len(listeners) > 0 {
var event MouseEvent
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
}
}
// GetClickListeners returns the "click-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 GetClickListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, ClickEvent)
2021-09-07 17:36:50 +03:00
}
// GetDoubleClickListeners returns the "double-click-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 GetDoubleClickListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, DoubleClickEvent)
2021-09-07 17:36:50 +03:00
}
// GetContextMenuListeners returns the "context-menu" 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 GetContextMenuListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, ContextMenuEvent)
2021-09-07 17:36:50 +03:00
}
// GetMouseDownListeners returns the "mouse-down" 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 GetMouseDownListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, MouseDown)
2021-09-07 17:36:50 +03:00
}
// GetMouseUpListeners returns the "mouse-up" 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 GetMouseUpListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, MouseUp)
2021-09-07 17:36:50 +03:00
}
// GetMouseMoveListeners returns the "mouse-move" 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 GetMouseMoveListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, MouseMove)
2021-09-07 17:36:50 +03:00
}
// GetMouseOverListeners returns the "mouse-over" 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 GetMouseOverListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, MouseOver)
2021-09-07 17:36:50 +03:00
}
// GetMouseOutListeners returns the "mouse-out" 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 GetMouseOutListeners(view View, subviewID ...string) []func(View, MouseEvent) {
2022-07-27 20:31:57 +03:00
return getEventListeners[View, MouseEvent](view, subviewID, MouseOut)
2021-09-07 17:36:50 +03:00
}