Skip to content

Commit efa81a1

Browse files
authored
Merge pull request #2503 from switchifyapp/feature/pc-keyboard-navigation-component-2502
Add reusable PC keyboard navigation cluster
2 parents 6b2f633 + b26da78 commit efa81a1

7 files changed

Lines changed: 99 additions & 12 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.enaboapps.switchify.screens.pc
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.ui.Modifier
5+
import com.enaboapps.switchify.pc.PcKeyboardKey
6+
7+
@Composable
8+
fun PcKeyboardNavigationCluster(
9+
enabled: Boolean,
10+
onKeySelected: (PcKeyboardKey) -> Unit,
11+
modifier: Modifier = Modifier
12+
) {
13+
PcCompactCommandGrid(
14+
columns = 3,
15+
minTileHeightDp = 52,
16+
cells = pcKeyboardNavigationKeys().map { key ->
17+
PcCompactCommandCell(
18+
labelResId = key.labelResId,
19+
enabled = enabled,
20+
onClick = { onKeySelected(key) }
21+
)
22+
},
23+
modifier = modifier
24+
)
25+
}
26+
27+
fun pcKeyboardNavigationKeys(): List<PcKeyboardKey> {
28+
return listOf(
29+
PcKeyboardKey.Escape,
30+
PcKeyboardKey.ArrowUp,
31+
PcKeyboardKey.Enter,
32+
PcKeyboardKey.ArrowLeft,
33+
PcKeyboardKey.ArrowDown,
34+
PcKeyboardKey.ArrowRight
35+
)
36+
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ fun PcTypingControlScreen(
5858
onClear = onClear,
5959
onKeySelected = onKeySelected
6060
)
61+
PcKeyboardNavigationCluster(
62+
enabled = enabled,
63+
onKeySelected = onKeySelected
64+
)
6165
}
6266
}
6367

@@ -157,14 +161,8 @@ fun pcTypingCompactCommandSpecs(): List<PcTypingCompactCommandSpec> {
157161
PcTypingCompactCommandSpec.Clear,
158162
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_backspace, PcKeyboardKey.Backspace)),
159163
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_delete, PcKeyboardKey.Delete)),
160-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_enter, PcKeyboardKey.Enter)),
161164
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_space, PcKeyboardKey.Space)),
162165
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_tab, PcKeyboardKey.Tab)),
163-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_escape, PcKeyboardKey.Escape)),
164-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_arrow_left, PcKeyboardKey.ArrowLeft)),
165-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_arrow_up, PcKeyboardKey.ArrowUp)),
166-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_arrow_down, PcKeyboardKey.ArrowDown)),
167-
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_arrow_right, PcKeyboardKey.ArrowRight)),
168166
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_home, PcKeyboardKey.Home)),
169167
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_end, PcKeyboardKey.End)),
170168
PcTypingCompactCommandSpec.Key(PcTypingKeySpec(R.string.pc_key_page_up, PcKeyboardKey.PageUp)),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ fun PcWindowControlScreen(
4747
}
4848
}
4949
)
50+
PcKeyboardNavigationCluster(
51+
enabled = enabled,
52+
onKeySelected = { key ->
53+
onCommandSelected(PcControlCommand.PressKey(key))
54+
}
55+
)
5056
}
5157
}
5258

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.enaboapps.switchify.screens.pc
2+
3+
import com.enaboapps.switchify.pc.PcKeyboardKey
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Test
6+
7+
class PcKeyboardNavigationClusterTest {
8+
@Test
9+
fun navigationKeysUseStableClusterOrder() {
10+
assertEquals(
11+
listOf(
12+
PcKeyboardKey.Escape,
13+
PcKeyboardKey.ArrowUp,
14+
PcKeyboardKey.Enter,
15+
PcKeyboardKey.ArrowLeft,
16+
PcKeyboardKey.ArrowDown,
17+
PcKeyboardKey.ArrowRight
18+
),
19+
pcKeyboardNavigationKeys()
20+
)
21+
}
22+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,21 @@ class PcMouseControlViewModelTest {
242242
assertEquals(PcControlSurface.Window, viewModel.uiState.value.activeSurface)
243243
}
244244

245+
@Test
246+
fun windowSurfaceCanSendNavigationKeyCommand() = runTest(dispatcher) {
247+
val connector = FakeConnector()
248+
val controller = connectedController(connector = connector)
249+
val viewModel = viewModel(controller)
250+
251+
viewModel.selectControlSurface(PcControlSurface.Window)
252+
viewModel.send(PcControlCommand.PressKey(PcKeyboardKey.Escape))
253+
advanceUntilIdle()
254+
255+
assertEquals(listOf(PcControlCommand.PressKey(PcKeyboardKey.Escape)), connector.realtimeCommands)
256+
assertTrue(connector.commands.isEmpty())
257+
assertEquals(PcControlSurface.Window, viewModel.uiState.value.activeSurface)
258+
}
259+
245260
@Test
246261
fun controlCommandDoesNotSetBusy() = runTest(dispatcher) {
247262
val connector = FakeConnector()

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,8 @@ class PcTypingControlScreenTest {
8585
listOf(
8686
PcKeyboardKey.Backspace,
8787
PcKeyboardKey.Delete,
88-
PcKeyboardKey.Enter,
8988
PcKeyboardKey.Space,
9089
PcKeyboardKey.Tab,
91-
PcKeyboardKey.Escape,
92-
PcKeyboardKey.ArrowLeft,
93-
PcKeyboardKey.ArrowUp,
94-
PcKeyboardKey.ArrowDown,
95-
PcKeyboardKey.ArrowRight,
9690
PcKeyboardKey.Home,
9791
PcKeyboardKey.End,
9892
PcKeyboardKey.PageUp,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.enaboapps.switchify.screens.pc
22

33
import com.enaboapps.switchify.R
44
import com.enaboapps.switchify.pc.PcControlCommand
5+
import com.enaboapps.switchify.pc.PcKeyboardKey
56
import com.enaboapps.switchify.pc.PcWindowControlAction
67
import org.junit.Assert.assertEquals
78
import org.junit.Test
@@ -56,4 +57,19 @@ class PcWindowControlScreenTest {
5657
assertEquals(null, specs[7])
5758
assertEquals(null, specs[8])
5859
}
60+
61+
@Test
62+
fun windowKeyboardNavigationUsesSharedOrder() {
63+
assertEquals(
64+
listOf(
65+
PcKeyboardKey.Escape,
66+
PcKeyboardKey.ArrowUp,
67+
PcKeyboardKey.Enter,
68+
PcKeyboardKey.ArrowLeft,
69+
PcKeyboardKey.ArrowDown,
70+
PcKeyboardKey.ArrowRight
71+
),
72+
pcKeyboardNavigationKeys()
73+
)
74+
}
5975
}

0 commit comments

Comments
 (0)