2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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
|
2024-09-18 13:50:06 +03:00
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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
|
2024-09-18 13:50:06 +03:00
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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
|
2024-09-18 13:50:06 +03:00
|
|
|
// 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.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
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.
|
2022-08-31 22:17:46 +03:00
|
|
|
// 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) {
|
2024-11-18 17:20:25 +03:00
|
|
|
return getOneArgEventListeners[View, MouseEvent](view, subviewID, MouseOut)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|