Skip to content

Commit

Permalink
Make HardwareShortcutsSpec and HardwareShortcutDetector public
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Jun 24, 2024
1 parent 7ab5d16 commit 2b6bf76
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ fun ZoomableImage(
// this image when a quick-zoom gesture is made before the image is fully loaded.
state = rememberZoomableState(
zoomSpec = ZoomSpec(maxZoomFactor = 1f, preventOverOrUnderZoom = false),
hardwareShortcutsSpec = HardwareShortcutsSpec.Disabled,
autoApplyTransformations = false,
).also {
(it as RealZoomableState).hardwareShortcutsSpec = HardwareShortcutsSpec.Disabled
},
),
onClick = onClick,
onLongClick = onLongClick,
clipToBounds = clipToBounds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import me.saket.telephoto.zoomable.internal.HardwareShortcutDetector

@Poko
@Immutable
internal class HardwareShortcutsSpec(
class HardwareShortcutsSpec(
val enabled: Boolean = true,
val detector: HardwareShortcutDetector = HardwareShortcutDetector.Platform,
val shortcutDetector: HardwareShortcutDetector = HardwareShortcutDetector.Default,
) {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import androidx.compose.ui.platform.LocalLayoutDirection
* @param autoApplyTransformations Determines whether the resulting scale and translation of pan and zoom
* gestures should be automatically applied by [Modifier.zoomable] to its content. This can be disabled
* if your content prefers applying the transformations in a bespoke manner.
*
* @param hardwareShortcutsSpec Spec used for handling keyboard and mouse shortcuts, or
* [HardwareShortcutsSpec.Disabled] for disabling them.
*/
@Composable
fun rememberZoomableState(
zoomSpec: ZoomSpec = ZoomSpec(),
autoApplyTransformations: Boolean = true,
hardwareShortcutsSpec: HardwareShortcutsSpec = HardwareShortcutsSpec(),
): ZoomableState {
val isLayoutPreview = LocalInspectionMode.current
val state = rememberSaveable(saver = RealZoomableState.Saver) {
Expand All @@ -35,7 +39,7 @@ fun rememberZoomableState(
)
}.also {
it.zoomSpec = zoomSpec
it.hardwareShortcutsSpec = HardwareShortcutsSpec()
it.hardwareShortcutsSpec = hardwareShortcutsSpec
it.layoutDirection = LocalLayoutDirection.current
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import me.saket.telephoto.zoomable.internal.HardwareShortcutDetector.ShortcutEve
import kotlin.math.absoluteValue

@Immutable
internal interface HardwareShortcutDetector {
interface HardwareShortcutDetector {
companion object {
val Platform: HardwareShortcutDetector get() = DefaultHardwareShortcutDetector
val Default: HardwareShortcutDetector get() = DefaultHardwareShortcutDetector
}

/** Detect a keyboard shortcut or return `null` to ignore. */
Expand Down Expand Up @@ -63,7 +63,6 @@ internal interface HardwareShortcutDetector {
val DefaultPanOffset = 50.dp
}
}

}

internal object DefaultHardwareShortcutDetector : HardwareShortcutDetector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal class HardwareShortcutsNode(

override fun onKeyEvent(event: KeyEvent): Boolean {
if (event.type == KeyEventType.KeyDown) {
val shortcut = spec.detector.detectKey(event)
val shortcut = spec.shortcutDetector.detectKey(event)
shortcut?.let(::handleShortcut)
return shortcut != null
} else {
Expand All @@ -82,7 +82,7 @@ internal class HardwareShortcutsNode(
&& pass == PointerEventPass.Main
&& pointerEvent.changes.fastAny { !it.isConsumed }
) {
val shortcut = spec.detector.detectScroll(pointerEvent)
val shortcut = spec.shortcutDetector.detectScroll(pointerEvent)
if (shortcut != null) {
pointerEvent.changes.fastForEach { it.consume() }
handleShortcut(shortcut)
Expand Down

0 comments on commit 2b6bf76

Please sign in to comment.