Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Add target window ID to mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
SerVB committed Jul 23, 2020
1 parent bdb1c0d commit b2933e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ class InputController(private val openingTimeStamp: Int,
private fun handleMouseMoveEvent(event: Event) {
require(event is MouseEvent)

val topWindow = windowManager.getTopWindow(event.clientX, event.clientY) ?: return
if (mouseButtonsDown.isEmpty()) {
fireMouseEvent(ClientMouseEvent.MouseEventType.MOVE, event)
fireMouseEvent(ClientMouseEvent.MouseEventType.MOVE, topWindow.id, event)
}
else {
if (eventsInterceptor != null) {
eventsInterceptor!!.onMouseMove(event.clientX, event.clientY)
}
else {
fireMouseEvent(ClientMouseEvent.MouseEventType.DRAG, event)
fireMouseEvent(ClientMouseEvent.MouseEventType.DRAG, topWindow.id, event)
}
}
}
Expand All @@ -72,30 +73,31 @@ class InputController(private val openingTimeStamp: Int,
event.preventDefault()

val touch = event.changedTouches[0] ?: return
val topWindow = windowManager.getTopWindow(touch.clientX, touch.clientY) ?: return

if (mouseButtonsDown.isEmpty()) {
fireMouseEvent(ClientMouseEvent.MouseEventType.MOVE, event, touch)
fireMouseEvent(ClientMouseEvent.MouseEventType.MOVE, topWindow.id, event, touch)
}
else {
if (eventsInterceptor != null) {
eventsInterceptor!!.onMouseMove(touch.clientX, touch.clientY)
}
else {
fireMouseEvent(ClientMouseEvent.MouseEventType.TOUCH_DRAG, event, touch)
fireMouseEvent(ClientMouseEvent.MouseEventType.TOUCH_DRAG, topWindow.id, event, touch)
}
}
}

private fun handleMouseDownEvent(event: Event) {
require(event is MouseEvent)

val topWindow = windowManager.getTopWindow(event.clientX, event.clientY)
eventsInterceptor = topWindow?.onMouseDown(event.clientX, event.clientY)
val topWindow = windowManager.getTopWindow(event.clientX, event.clientY) ?: return
eventsInterceptor = topWindow.onMouseDown(event.clientX, event.clientY)
if (eventsInterceptor == null) {
fireMouseEvent(ClientMouseEvent.MouseEventType.DOWN, event)
fireMouseEvent(ClientMouseEvent.MouseEventType.DOWN, topWindow.id, event)
}
else {
windowManager.bringToFront(topWindow!!)
windowManager.bringToFront(topWindow)
}
mouseButtonsDown.add(event.button)
}
Expand All @@ -106,13 +108,13 @@ class InputController(private val openingTimeStamp: Int,

val touch = event.changedTouches[0] ?: return

val topWindow = windowManager.getTopWindow(touch.clientX, touch.clientY)
eventsInterceptor = topWindow?.onMouseDown(touch.clientX, touch.clientY)
val topWindow = windowManager.getTopWindow(touch.clientX, touch.clientY) ?: return
eventsInterceptor = topWindow.onMouseDown(touch.clientX, touch.clientY)
if (eventsInterceptor == null) {
fireMouseEvent(ClientMouseEvent.MouseEventType.DOWN, event, touch)
fireMouseEvent(ClientMouseEvent.MouseEventType.DOWN, topWindow.id, event, touch)
}
else {
windowManager.bringToFront(topWindow!!)
windowManager.bringToFront(topWindow)
}
mouseButtonsDown.add(LEFT_MOUSE_BUTTON_ID)
}
Expand All @@ -124,7 +126,9 @@ class InputController(private val openingTimeStamp: Int,
eventsInterceptor = null
}
else {
fireMouseEvent(ClientMouseEvent.MouseEventType.UP, event)
windowManager.getTopWindow(event.clientX, event.clientY)?.id?.let {
fireMouseEvent(ClientMouseEvent.MouseEventType.UP, it, event)
}
}
mouseButtonsDown.remove(event.button)
}
Expand All @@ -134,13 +138,14 @@ class InputController(private val openingTimeStamp: Int,
event.preventDefault()

val touch = event.changedTouches[0] ?: return
val topWindow = windowManager.getTopWindow(touch.clientX, touch.clientY) ?: return

if (eventsInterceptor != null) {
eventsInterceptor!!.onMouseUp(touch.clientX, touch.clientY)
eventsInterceptor = null
}
else {
fireMouseEvent(ClientMouseEvent.MouseEventType.UP, event, touch)
fireMouseEvent(ClientMouseEvent.MouseEventType.UP, topWindow.id, event, touch)
}
mouseButtonsDown.remove(LEFT_MOUSE_BUTTON_ID)

Expand All @@ -163,15 +168,17 @@ class InputController(private val openingTimeStamp: Int,

private fun handleClickEvent(event: Event) {
require(event is MouseEvent)
if (windowManager.getTopWindow(event.clientX, event.clientY)?.onMouseClick(event.clientX, event.clientY) == null) {
fireMouseEvent(ClientMouseEvent.MouseEventType.CLICK, event)
val topWindow = windowManager.getTopWindow(event.clientX, event.clientY) ?: return
if (topWindow.onMouseClick(event.clientX, event.clientY) == null) {
fireMouseEvent(ClientMouseEvent.MouseEventType.CLICK, topWindow.id, event)
}
}

// This is extremely dangerous method, because it is called when mouse leave ANY canvas inside document!
private fun handleMouseOutEvent(event: Event) {
require(event is MouseEvent)
fireMouseEvent(ClientMouseEvent.MouseEventType.OUT, event)
val topWindow = windowManager.getTopWindow(event.clientX, event.clientY) ?: return
fireMouseEvent(ClientMouseEvent.MouseEventType.OUT, topWindow.id, event)
}

private fun handleKeyDownEvent(event: Event) {
Expand Down Expand Up @@ -259,10 +266,12 @@ class InputController(private val openingTimeStamp: Int,
private fun fireWheelEvent(event: Event) {
require(event is WheelEvent)

val topWindow = windowManager.getTopWindow(event.clientX, event.clientY) ?: return
val userScalingRatio = ParamsProvider.USER_SCALING_RATIO

val message = ClientWheelEvent(
timeStamp = event.timeStamp.toInt() - openingTimeStamp,
windowId = topWindow.id,
modifiers = event.modifiers,
mode = event.deltaMode.toScrollingMode(),
x = (event.clientX / userScalingRatio).roundToInt(),
Expand All @@ -274,8 +283,9 @@ class InputController(private val openingTimeStamp: Int,
stateMachine.fire(ClientAction.AddEvent(message))
}

private fun fireMouseEvent(type: ClientMouseEvent.MouseEventType, event: MouseEvent) = fireMouseEvent(
private fun fireMouseEvent(type: ClientMouseEvent.MouseEventType, windowId: Int, event: MouseEvent) = fireMouseEvent(
type = type,
windowId = windowId,
eventTimeStamp = event.timeStamp,
x = event.clientX,
y = event.clientY,
Expand All @@ -284,8 +294,9 @@ class InputController(private val openingTimeStamp: Int,
modifiers = event.modifiers
)

private fun fireMouseEvent(type: ClientMouseEvent.MouseEventType, event: TouchEvent, touch: Touch) = fireMouseEvent(
private fun fireMouseEvent(type: ClientMouseEvent.MouseEventType, windowId: Int, event: TouchEvent, touch: Touch) = fireMouseEvent(
type = type,
windowId = windowId,
eventTimeStamp = event.timeStamp,
x = touch.clientX,
y = touch.clientY,
Expand All @@ -296,6 +307,7 @@ class InputController(private val openingTimeStamp: Int,

private fun fireMouseEvent(
type: ClientMouseEvent.MouseEventType,
windowId: Int,
eventTimeStamp: Number,
x: Int,
y: Int,
Expand All @@ -307,6 +319,7 @@ class InputController(private val openingTimeStamp: Int,

val message = ClientMouseEvent(
timeStamp = eventTimeStamp.toInt() - openingTimeStamp,
windowId = windowId,
x = (x / userScalingRatio).roundToInt(),
y = (y / userScalingRatio).roundToInt(),
button = button,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WindowManager(private val stateMachine: ClientStateMachine) : Iterable<Win
fun getWindowZIndex(windowId: Int): Int? = windows[windowId]?.zIndex

/** Returns topmost visible window, containing point. Contain check includes window header and borders. */
fun getTopWindow(x: Int, y: Int): Window? = windows.filter { it.value.contains(x, y) }.maxBy { it.value.zIndex }?.value
fun getTopWindow(x: Int, y: Int): Window? = windows.values.filter { it.isShowing && it.contains(x, y) }.maxBy { it.zIndex }

fun getOrCreate(windowData: WindowData): Window {
val window = windows[windowData.id] ?: Window(windowData, stateMachine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ val COMMON_VERSION = listOf(ServerEvent.serializer(), ClientEvent.serializer())
val commonVersionList = listOf(
-1663032476,
615706807,
891030124
891030124,
-1205505588
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sealed class ClientEvent
data class ClientMouseEvent(
/** From connection opening. */
val timeStamp: Int,
val windowId: Int,
val x: Int,
val y: Int,
val button: Short,
Expand All @@ -84,6 +85,7 @@ data class ClientMouseEvent(
data class ClientWheelEvent(
/** From connection opening. */
val timeStamp: Int,
val windowId: Int,
val modifiers: Set<MouseModifier>,
val mode: ScrollingMode,
val x: Int,
Expand Down

0 comments on commit b2933e1

Please sign in to comment.