rui_orig/mouseEvents.go

364 lines
13 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strconv"
"strings"
)
// Constants related to [View] mouse events properties
2021-09-07 17:36:50 +03:00
const (
2021-11-04 21:13:34 +03:00
// ClickEvent is the constant for "click-event" property tag.
//
// Used by `View`.
// Occur when the user clicks on the view.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
ClickEvent PropertyName = "click-event"
2021-09-07 17:36:50 +03:00
2021-11-04 21:13:34 +03:00
// DoubleClickEvent is the constant for "double-click-event" property tag.
//
// Used by `View`.
// Occur when the user double clicks on the view.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
DoubleClickEvent PropertyName = "double-click-event"
2021-09-07 17:36:50 +03:00
// MouseDown is the constant for "mouse-down" property tag.
//
// Used by `View`.
// Is fired at a View when a pointing device button is pressed while the pointer is inside the view.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
MouseDown PropertyName = "mouse-down"
2021-09-07 17:36:50 +03:00
// MouseUp is the constant for "mouse-up" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// 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.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
MouseUp PropertyName = "mouse-up"
2021-09-07 17:36:50 +03:00
// MouseMove is the constant for "mouse-move" property tag.
//
// Used by `View`.
// Is fired at a view when a pointing device(usually a mouse) is moved while the cursor's hotspot is inside it.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
MouseMove PropertyName = "mouse-move"
2021-09-07 17:36:50 +03:00
// MouseOut is the constant for "mouse-out" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// 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.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
MouseOut PropertyName = "mouse-out"
2021-09-07 17:36:50 +03:00
// MouseOver is the constant for "mouse-over" property tag.
//
// Used by `View`.
2024-11-13 12:56:39 +03:00
// 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.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
MouseOver PropertyName = "mouse-over"
2021-09-07 17:36:50 +03:00
2021-11-04 21:13:34 +03:00
// ContextMenuEvent is the constant for "context-menu-event" property tag.
//
// Used by `View`.
// Occur when the user calls the context menu by the right mouse clicking.
//
// General listener format:
// `func(view rui.View, event rui.MouseEvent)`.
//
// where:
// view - Interface of a view which generated this event,
// event - Mouse event.
//
// Allowed listener formats:
// `func(view rui.View)`,
// `func(event rui.MouseEvent)`,
// `func()`.
2024-11-13 12:56:39 +03:00
ContextMenuEvent PropertyName = "context-menu-event"
2021-09-07 17:36:50 +03:00
// PrimaryMouseButton is a number of the main pressed button, usually the left button or the un-initialized state
PrimaryMouseButton = 0
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// AuxiliaryMouseButton is a number of the auxiliary pressed button, usually the wheel button
// or the middle button (if present)
AuxiliaryMouseButton = 1
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// SecondaryMouseButton is a number of the secondary pressed button, usually the right button
SecondaryMouseButton = 2
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// MouseButton4 is a number of the fourth button, typically the Browser Back button
MouseButton4 = 3
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// 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
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// SecondaryMouseMask is the mask of the secondary button (usually the right button)
SecondaryMouseMask = 2
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// AuxiliaryMouseMask is the mask of the auxiliary button (usually the mouse wheel button or middle button)
AuxiliaryMouseMask = 4
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// MouseMask4 is the mask of the 4th button (typically the "Browser Back" button)
MouseMask4 = 8
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
//MouseMask5 is the mask of the 5th button (typically the "Browser Forward" button)
MouseMask5 = 16
)
// MouseEvent represent a mouse event
2021-09-07 17:36:50 +03:00
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
}
2024-11-13 12:56:39 +03:00
/*
func setMouseListener(properties Properties, tag PropertyName, value any) bool {
if listeners, ok := valueToOneArgEventListeners[View, MouseEvent](value); ok {
2024-11-13 12:56:39 +03:00
if len(listeners) == 0 {
properties.setRaw(tag, nil)
} else {
properties.setRaw(tag, listeners)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
return true
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
notCompatibleType(tag, value)
return false
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func (view *viewData) removeMouseListener(tag PropertyName) {
2021-09-07 17:36:50 +03:00
delete(view.properties, tag)
if view.created {
2024-11-13 12:56:39 +03:00
if js, ok := eventJsFunc[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
}
}
}
2023-04-25 17:20:47 +03:00
func mouseEventsHtml(view View, buffer *strings.Builder, hasTooltip bool) {
2024-11-13 12:56:39 +03:00
for _, tag := range []PropertyName{ClickEvent, DoubleClickEvent, MouseDown, MouseUp, MouseMove, MouseOut, MouseOver, ContextMenuEvent} {
2021-09-07 17:36:50 +03:00
if value := view.getRaw(tag); value != nil {
2024-11-13 12:56:39 +03:00
if js, ok := eventJsFunc[tag]; ok {
if listeners, ok := value.([]func(View, MouseEvent)); ok && len(listeners) > 0 {
buffer.WriteString(js.jsEvent)
buffer.WriteString(`="`)
buffer.WriteString(js.jsFunc)
buffer.WriteString(`(this, event)" `)
}
2021-09-07 17:36:50 +03:00
}
}
}
2023-04-25 17:20:47 +03:00
if hasTooltip {
buffer.WriteString(`onmouseenter="mouseEnterEvent(this, event)" `)
buffer.WriteString(`onmouseleave="mouseLeaveEvent(this, event)" `)
}
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
*/
2021-09-07 17:36:50 +03:00
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")
}
2024-11-13 12:56:39 +03:00
func handleMouseEvents(view View, tag PropertyName, data DataObject) {
listeners := getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[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) {
return getOneArgEventListeners[View, MouseEvent](view, subviewID, MouseOut)
2021-09-07 17:36:50 +03:00
}