-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intial stab at this, I'm not sure all the code lives in the right place and looking for feedback on that. Additional issues/questions: - Is there a way to get the 'standard' system keyboard shortcuts for this? - How to detech ctrl/meta + key press? I never saw them detected though I'm testing with the android emulator which does werid things with external keybaords. - keyboard input enabled by default? note: view needs to be focused to start receiving events - configure zoom & pan steps? Fixes #78
- Loading branch information
Showing
6 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
zoomable/src/commonMain/kotlin/me/saket/telephoto/zoomable/internal/keyboardActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package me.saket.telephoto.zoomable.internal | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.input.key.KeyEvent | ||
import androidx.compose.ui.input.key.KeyEventType | ||
import androidx.compose.ui.input.key.KeyInputModifierNode | ||
import androidx.compose.ui.input.key.isCtrlPressed | ||
import androidx.compose.ui.input.key.isMetaPressed | ||
import androidx.compose.ui.input.key.key | ||
import androidx.compose.ui.input.key.type | ||
import androidx.compose.ui.node.ModifierNodeElement | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.DpOffset | ||
import androidx.compose.ui.unit.dp | ||
|
||
/** | ||
* Responds to keyboard events to zoom and pan | ||
*/ | ||
internal data class KeyboardActionsElement( | ||
private var zoomStep: Float, | ||
private var panStep: Dp, | ||
private var canPan: () -> Boolean, | ||
private var onZoom: (Float) -> Unit, | ||
private var onResetZoom: () -> Unit, | ||
private var onPan: (DpOffset) -> Unit, | ||
) : ModifierNodeElement<KeyboardActionsNode>() { | ||
override fun create(): KeyboardActionsNode { | ||
return KeyboardActionsNode( | ||
zoomStep = zoomStep, | ||
panStep = panStep, | ||
canPan = canPan, | ||
onZoom = onZoom, | ||
onResetZoom = onResetZoom, | ||
onPan = onPan, | ||
) | ||
} | ||
|
||
override fun update(node: KeyboardActionsNode) { | ||
node.update( | ||
zoomStep = zoomStep, | ||
panStep = panStep, | ||
canPan = canPan, | ||
onZoom = onZoom, | ||
onResetZoom = onResetZoom, | ||
onPan = onPan, | ||
) | ||
} | ||
} | ||
|
||
internal class KeyboardActionsNode( | ||
private var zoomStep: Float, | ||
private var panStep: Dp, | ||
private var canPan: () -> Boolean, | ||
private var onZoom: (Float) -> Unit, | ||
private var onResetZoom: () -> Unit, | ||
private var onPan: (DpOffset) -> Unit, | ||
) : Modifier.Node(), KeyInputModifierNode { | ||
override fun onKeyEvent(event: KeyEvent): Boolean { | ||
//TODO: how to detect the correct key combos? | ||
println("onKeyEvent: ${event.key} isCtrlPressed=${event.isCtrlPressed} isMetaPressed:${event.isMetaPressed}") | ||
when { | ||
event.key == Key.ZoomIn || event.key == Key.Plus || event.key == Key.Equals -> { | ||
if (event.type == KeyEventType.KeyDown) { | ||
onZoom(zoomStep) | ||
} | ||
return true | ||
} | ||
event.key == Key.ZoomOut || event.key == Key.Minus -> { | ||
if (event.type == KeyEventType.KeyDown) { | ||
onZoom(1 / zoomStep) | ||
} | ||
return true | ||
} | ||
event.key == Key.Zero -> { | ||
if (event.type == KeyEventType.KeyDown) { | ||
onResetZoom() | ||
} | ||
return true | ||
} | ||
} | ||
if (canPan()) { | ||
when (event.key) { | ||
Key.DirectionUp -> { | ||
val offset = DpOffset(x = 0.dp, y = -panStep) | ||
if (event.type == KeyEventType.KeyDown) { | ||
onPan(offset) | ||
} | ||
return true | ||
} | ||
Key.DirectionDown -> { | ||
val offset = DpOffset(x = 0.dp, y = panStep) | ||
if (event.type == KeyEventType.KeyDown) { | ||
onPan(offset) | ||
} | ||
return true | ||
} | ||
Key.DirectionLeft -> { | ||
val offset = DpOffset(x = -panStep, y = 0.dp) | ||
if (event.type == KeyEventType.KeyDown) { | ||
onPan(offset) | ||
} | ||
return true | ||
} | ||
Key.DirectionRight -> { | ||
val offset = DpOffset(x = panStep, y = 0.dp) | ||
if (event.type == KeyEventType.KeyDown) { | ||
onPan(offset) | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun onPreKeyEvent(event: KeyEvent): Boolean { | ||
return false | ||
} | ||
|
||
fun update( | ||
zoomStep: Float, | ||
panStep: Dp, | ||
canPan: () -> Boolean, | ||
onZoom: (Float) -> Unit, | ||
onResetZoom: () -> Unit, | ||
onPan: (DpOffset) -> Unit, | ||
) { | ||
this.zoomStep = zoomStep | ||
this.panStep = panStep | ||
this.canPan = canPan | ||
this.zoomStep = zoomStep | ||
this.onZoom = onZoom | ||
this.onResetZoom = onResetZoom | ||
this.onPan = onPan | ||
} | ||
} |