Skip to content

Commit 77b24c7

Browse files
authored
Merge pull request #2600 from switchifyapp/feature/pc-modifier-key-toggle-2599
Add PC modifier key toggles
2 parents 483b9f6 + 5a8e58b commit 77b24c7

14 files changed

Lines changed: 456 additions & 13 deletions

File tree

app/src/main/java/com/enaboapps/switchify/pc/PcConnector.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ sealed class PcControlCommand {
2828
data class TextStreamClose(val streamId: String, val expectedCount: Int) : PcControlCommand()
2929
data class PressKey(val key: PcKeyboardKey) : PcControlCommand()
3030
data class KeyboardShortcut(val keys: List<PcKeyboardShortcutKey>) : PcControlCommand()
31+
data class ModifierDown(val key: PcKeyboardModifierKey) : PcControlCommand()
32+
data class ModifierUp(val key: PcKeyboardModifierKey) : PcControlCommand()
3133
data class WindowControl(val action: PcWindowControlAction) : PcControlCommand()
3234
data object LeftClick : PcControlCommand()
3335
data object DoubleClick : PcControlCommand()

app/src/main/java/com/enaboapps/switchify/pc/PcKeyboardKey.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ enum class PcKeyboardShortcutKey(val protocolValue: String) {
4343
C("C"),
4444
X("X")
4545
}
46+
47+
enum class PcKeyboardModifierKey(
48+
val protocolValue: String,
49+
@param:StringRes val labelResId: Int
50+
) {
51+
Ctrl("Ctrl", R.string.pc_modifier_ctrl),
52+
Alt("Alt", R.string.pc_modifier_alt),
53+
Shift("Shift", R.string.pc_modifier_shift),
54+
Meta("Meta", R.string.pc_modifier_start)
55+
}

app/src/main/java/com/enaboapps/switchify/pc/PcPointerProfile.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ fun PcPointerMovementProfile.supportsTextStreams(): Boolean {
4747
)
4848
)
4949
}
50+
51+
fun PcPointerMovementProfile.supportsModifierToggle(): Boolean {
52+
return capabilities.supportedCommands.containsAll(
53+
setOf(
54+
"keyboard.modifierDown",
55+
"keyboard.modifierUp"
56+
)
57+
)
58+
}

app/src/main/java/com/enaboapps/switchify/pc/PcProtocol.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,44 @@ object PcProtocol {
386386
)
387387
}
388388

389+
fun keyboardModifierDown(
390+
id: String,
391+
deviceId: String,
392+
token: String,
393+
timestamp: Long,
394+
key: PcKeyboardModifierKey,
395+
responseMode: PcCommandResponseMode = PcCommandResponseMode.Ack
396+
): String {
397+
return authenticatedCommand(
398+
id,
399+
deviceId,
400+
token,
401+
timestamp,
402+
"keyboard.modifierDown",
403+
JSONObject().put("key", key.protocolValue),
404+
responseMode
405+
)
406+
}
407+
408+
fun keyboardModifierUp(
409+
id: String,
410+
deviceId: String,
411+
token: String,
412+
timestamp: Long,
413+
key: PcKeyboardModifierKey,
414+
responseMode: PcCommandResponseMode = PcCommandResponseMode.Ack
415+
): String {
416+
return authenticatedCommand(
417+
id,
418+
deviceId,
419+
token,
420+
timestamp,
421+
"keyboard.modifierUp",
422+
JSONObject().put("key", key.protocolValue),
423+
responseMode
424+
)
425+
}
426+
389427
fun windowControl(
390428
id: String,
391429
deviceId: String,
@@ -630,6 +668,8 @@ object PcProtocol {
630668
"mouse.dragEnd",
631669
"keyboard.key",
632670
"keyboard.shortcut",
671+
"keyboard.modifierDown",
672+
"keyboard.modifierUp",
633673
"keyboard.typeText",
634674
"keyboard.textStream.char",
635675
"keyboard.textStream.key",

app/src/main/java/com/enaboapps/switchify/pc/bluetooth/SwitchifyPcBleClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ class SwitchifyPcBleClient(
295295
is PcControlCommand.TextStreamClose -> PcProtocol.keyboardTextStreamClose(id, deviceId, token, timestamp, streamId, expectedCount, responseMode)
296296
is PcControlCommand.PressKey -> PcProtocol.keyboardKey(id, deviceId, token, timestamp, key, responseMode)
297297
is PcControlCommand.KeyboardShortcut -> PcProtocol.keyboardShortcut(id, deviceId, token, timestamp, keys, responseMode)
298+
is PcControlCommand.ModifierDown -> PcProtocol.keyboardModifierDown(id, deviceId, token, timestamp, key, responseMode)
299+
is PcControlCommand.ModifierUp -> PcProtocol.keyboardModifierUp(id, deviceId, token, timestamp, key, responseMode)
298300
is PcControlCommand.WindowControl -> PcProtocol.windowControl(id, deviceId, token, timestamp, action, responseMode)
299301
}
300302
}
@@ -402,6 +404,8 @@ private fun PcControlCommand.protocolType(): String {
402404
is PcControlCommand.TextStreamClose -> "keyboard.textStream.close"
403405
is PcControlCommand.PressKey -> "keyboard.key"
404406
is PcControlCommand.KeyboardShortcut -> "keyboard.shortcut"
407+
is PcControlCommand.ModifierDown -> "keyboard.modifierDown"
408+
is PcControlCommand.ModifierUp -> "keyboard.modifierUp"
405409
is PcControlCommand.WindowControl -> "window.control"
406410
}
407411
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ data class PcCompactCommandCell(
7474
val onClick: () -> Unit,
7575
val icon: ImageVector? = null,
7676
val iconRotationDegrees: Float = 0f,
77-
val tone: PcCommandTone = PcCommandTone.Neutral
77+
val tone: PcCommandTone = PcCommandTone.Neutral,
78+
val selected: Boolean = false
7879
)
7980

8081
/**
@@ -213,6 +214,7 @@ fun PcCompactCommandGrid(
213214
icon = cell.icon,
214215
iconRotationDegrees = cell.iconRotationDegrees,
215216
tone = cell.tone,
217+
selected = cell.selected,
216218
minHeightDp = minTileHeightDp,
217219
modifier = itemModifier
218220
)
@@ -240,6 +242,7 @@ fun PcScannedCommandTile(
240242
icon: ImageVector? = null,
241243
iconRotationDegrees: Float = 0f,
242244
tone: PcCommandTone = PcCommandTone.Neutral,
245+
selected: Boolean = false,
243246
minHeightDp: Int = 52,
244247
square: Boolean = false
245248
) {
@@ -248,24 +251,27 @@ fun PcScannedCommandTile(
248251
val backgroundColor = when {
249252
!enabled -> MaterialTheme.colorScheme.surfaceVariant
250253
pressed -> MaterialTheme.colorScheme.primaryContainer
254+
selected -> MaterialTheme.colorScheme.primaryContainer
251255
tone == PcCommandTone.Primary -> MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.56f)
252256
tone == PcCommandTone.Destructive -> MaterialTheme.colorScheme.errorContainer.copy(alpha = 0.48f)
253257
else -> MaterialTheme.colorScheme.surface
254258
}
255259
val borderColor = if (enabled) {
256-
when (tone) {
257-
PcCommandTone.Primary -> MaterialTheme.colorScheme.primary.copy(alpha = 0.62f)
258-
PcCommandTone.Destructive -> MaterialTheme.colorScheme.error.copy(alpha = 0.62f)
259-
PcCommandTone.Neutral -> MaterialTheme.colorScheme.outline
260+
when {
261+
selected -> MaterialTheme.colorScheme.primary
262+
tone == PcCommandTone.Primary -> MaterialTheme.colorScheme.primary.copy(alpha = 0.62f)
263+
tone == PcCommandTone.Destructive -> MaterialTheme.colorScheme.error.copy(alpha = 0.62f)
264+
else -> MaterialTheme.colorScheme.outline
260265
}
261266
} else {
262267
MaterialTheme.colorScheme.outlineVariant
263268
}
264269
val contentColor = if (enabled) {
265-
when (tone) {
266-
PcCommandTone.Primary -> MaterialTheme.colorScheme.onPrimaryContainer
267-
PcCommandTone.Destructive -> MaterialTheme.colorScheme.onErrorContainer
268-
PcCommandTone.Neutral -> MaterialTheme.colorScheme.onSurface
270+
when {
271+
selected -> MaterialTheme.colorScheme.onPrimaryContainer
272+
tone == PcCommandTone.Primary -> MaterialTheme.colorScheme.onPrimaryContainer
273+
tone == PcCommandTone.Destructive -> MaterialTheme.colorScheme.onErrorContainer
274+
else -> MaterialTheme.colorScheme.onSurface
269275
}
270276
} else {
271277
MaterialTheme.colorScheme.onSurfaceVariant

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ private fun PcMouseControlScreen(
182182
PcTransientMessage(message = uiState.message)
183183
PcWindowControlScreen(
184184
enabled = surfaceEnabled,
185+
activeModifiers = uiState.activeModifiers,
186+
onModifierSelected = viewModel::toggleModifier,
185187
onCommandSelected = viewModel::send
186188
)
187189
}

0 commit comments

Comments
 (0)