Skip to content

Commit ee4667e

Browse files
authored
Merge pull request #2608 from switchifyapp/fix/pc-drag-clears-after-click-2607
Clear PC drag toggle after other mouse clicks
2 parents bc708e3 + e0797a7 commit ee4667e

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

app/src/main/java/com/enaboapps/switchify/screens/pc/PcMouseControlViewModel.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class PcMouseControlViewModel(
142142
isDragging = when (commandToSend) {
143143
is PcControlCommand.DragStart -> true
144144
is PcControlCommand.DragEnd -> false
145-
else -> it.isDragging
145+
else -> if (it.isDragging && commandToSend.endsActiveDragWithClick()) false else it.isDragging
146146
},
147147
isBusy = false,
148148
busyCommand = null,
@@ -1134,6 +1134,12 @@ class PcMouseControlViewModel(
11341134
}
11351135
}
11361136

1137+
private fun PcControlCommand.endsActiveDragWithClick(): Boolean {
1138+
return this is PcControlCommand.LeftClick ||
1139+
this is PcControlCommand.DoubleClick ||
1140+
this is PcControlCommand.RightClick
1141+
}
1142+
11371143
private class InMemoryControlSurfaceStore : PcControlSurfaceStore {
11381144
private var surface = PcControlSurface.Mouse
11391145

app/src/test/java/com/enaboapps/switchify/screens/pc/PcMouseControlViewModelTest.kt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,107 @@ class PcMouseControlViewModelTest {
14691469
assertEquals(PcMouseControlViewModel.COMMAND_FAILED_MESSAGE, viewModel.uiState.value.message)
14701470
}
14711471

1472+
@Test
1473+
fun leftClickClearsDraggingAfterSuccessfulSend() = runTest(dispatcher) {
1474+
val connector = FakeConnector()
1475+
val controller = connectedController(connector = connector)
1476+
val viewModel = viewModel(controller)
1477+
1478+
viewModel.send(PcControlCommand.DragStart())
1479+
advanceUntilIdle()
1480+
viewModel.send(PcControlCommand.LeftClick)
1481+
advanceUntilIdle()
1482+
1483+
assertEquals(
1484+
listOf(PcControlCommand.DragStart(), PcControlCommand.LeftClick),
1485+
connector.realtimeCommands
1486+
)
1487+
assertFalse(viewModel.uiState.value.isDragging)
1488+
}
1489+
1490+
@Test
1491+
fun rightClickClearsDraggingAfterSuccessfulSend() = runTest(dispatcher) {
1492+
val controller = connectedController()
1493+
val viewModel = viewModel(controller)
1494+
1495+
viewModel.send(PcControlCommand.DragStart())
1496+
advanceUntilIdle()
1497+
viewModel.send(PcControlCommand.RightClick)
1498+
advanceUntilIdle()
1499+
1500+
assertFalse(viewModel.uiState.value.isDragging)
1501+
}
1502+
1503+
@Test
1504+
fun doubleClickClearsDraggingAfterSuccessfulSend() = runTest(dispatcher) {
1505+
val controller = connectedController()
1506+
val viewModel = viewModel(controller)
1507+
1508+
viewModel.send(PcControlCommand.DragStart())
1509+
advanceUntilIdle()
1510+
viewModel.send(PcControlCommand.DoubleClick)
1511+
advanceUntilIdle()
1512+
1513+
assertFalse(viewModel.uiState.value.isDragging)
1514+
}
1515+
1516+
@Test
1517+
fun failedClickDoesNotClearDraggingMirror() = runTest(dispatcher) {
1518+
val connector = FakeConnector(
1519+
realtimeResults = mutableListOf(PcCommandResult.Ack, PcCommandResult.Failed())
1520+
)
1521+
val controller = connectedController(connector = connector)
1522+
val viewModel = viewModel(controller)
1523+
1524+
viewModel.send(PcControlCommand.DragStart())
1525+
advanceUntilIdle()
1526+
viewModel.send(PcControlCommand.LeftClick)
1527+
advanceUntilIdle()
1528+
1529+
assertTrue(viewModel.uiState.value.isDragging)
1530+
assertEquals(PcMouseControlViewModel.COMMAND_FAILED_MESSAGE, viewModel.uiState.value.message)
1531+
}
1532+
1533+
@Test
1534+
fun moveDoesNotClearDragging() = runTest(dispatcher) {
1535+
val controller = connectedController()
1536+
val viewModel = viewModel(controller)
1537+
1538+
viewModel.send(PcControlCommand.DragStart())
1539+
advanceUntilIdle()
1540+
viewModel.send(PcControlCommand.Move(dx = 10, dy = 0))
1541+
advanceUntilIdle()
1542+
1543+
assertTrue(viewModel.uiState.value.isDragging)
1544+
}
1545+
1546+
@Test
1547+
fun scrollDoesNotClearDragging() = runTest(dispatcher) {
1548+
val controller = connectedController()
1549+
val viewModel = viewModel(controller)
1550+
1551+
viewModel.send(PcControlCommand.DragStart())
1552+
advanceUntilIdle()
1553+
viewModel.send(PcControlCommand.Scroll(dx = 0, dy = -1))
1554+
advanceUntilIdle()
1555+
1556+
assertTrue(viewModel.uiState.value.isDragging)
1557+
}
1558+
1559+
@Test
1560+
fun modifierToggleDoesNotClearDragging() = runTest(dispatcher) {
1561+
val controller = connectedController(pointerProfile = modifierPointerProfile())
1562+
val viewModel = viewModel(controller)
1563+
advanceUntilIdle()
1564+
1565+
viewModel.send(PcControlCommand.DragStart())
1566+
advanceUntilIdle()
1567+
viewModel.toggleModifier(PcKeyboardModifierKey.Ctrl)
1568+
advanceUntilIdle()
1569+
1570+
assertTrue(viewModel.uiState.value.isDragging)
1571+
}
1572+
14721573
@Test
14731574
fun supportedProfileEnablesModifierToggles() = runTest(dispatcher) {
14741575
val controller = connectedController(pointerProfile = modifierPointerProfile())

0 commit comments

Comments
 (0)