From fdd2e9fc09ed8bbeb4d0cff48816b34c6bdb5e3e Mon Sep 17 00:00:00 2001 From: Syer10 Date: Mon, 1 Aug 2022 17:53:30 -0400 Subject: [PATCH] Use newer desktop click APIs --- .../library/components/DesktopLibraryGrid.kt | 16 +-- .../ui/manga/components/DesktopChapterItem.kt | 26 +++-- .../sources/components/DesktopSourcesMenu.kt | 21 ++-- .../jui/uicore/components/MouseClickable.kt | 105 ++---------------- 4 files changed, 46 insertions(+), 122 deletions(-) diff --git a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/library/components/DesktopLibraryGrid.kt b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/library/components/DesktopLibraryGrid.kt index dde51151..129b9ad4 100644 --- a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/library/components/DesktopLibraryGrid.kt +++ b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/library/components/DesktopLibraryGrid.kt @@ -7,22 +7,24 @@ package ca.gosyer.jui.ui.library.components import androidx.compose.foundation.ContextMenuItem +import androidx.compose.foundation.onClick import androidx.compose.runtime.Composable import androidx.compose.runtime.Stable import androidx.compose.ui.Modifier import ca.gosyer.jui.i18n.MR -import ca.gosyer.jui.uicore.components.contextMenuClickable +import ca.gosyer.jui.uicore.components.onRightClickContextMenu import ca.gosyer.jui.uicore.resources.stringResource actual fun Modifier.libraryMangaModifier( onClickManga: () -> Unit, onClickRemoveManga: () -> Unit -): Modifier = contextMenuClickable( - { - getContextItems(onClickRemoveManga) - }, - onClick = { onClickManga() } -) +): Modifier = this + .onClick(onClick = onClickManga) + .onRightClickContextMenu( + items = { + getContextItems(onClickRemoveManga) + } + ) @Composable @Stable diff --git a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/manga/components/DesktopChapterItem.kt b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/manga/components/DesktopChapterItem.kt index cdffa5ee..13c6d6e4 100644 --- a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/manga/components/DesktopChapterItem.kt +++ b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/manga/components/DesktopChapterItem.kt @@ -7,11 +7,12 @@ package ca.gosyer.jui.ui.manga.components import androidx.compose.foundation.ContextMenuItem +import androidx.compose.foundation.onClick import androidx.compose.runtime.Composable import androidx.compose.runtime.Stable import androidx.compose.ui.Modifier import ca.gosyer.jui.i18n.MR -import ca.gosyer.jui.uicore.components.contextMenuClickable +import ca.gosyer.jui.uicore.components.onRightClickContextMenu import ca.gosyer.jui.uicore.resources.stringResource actual fun Modifier.chapterItemModifier( @@ -19,16 +20,19 @@ actual fun Modifier.chapterItemModifier( toggleRead: () -> Unit, toggleBookmarked: () -> Unit, markPreviousAsRead: () -> Unit -): Modifier = contextMenuClickable( - { - getContextItems( - toggleRead, - toggleBookmarked, - markPreviousAsRead - ) - }, - onClick = { onClick() } -) +): Modifier = this + .onClick( + onClick = onClick + ) + .onRightClickContextMenu( + items = { + getContextItems( + toggleRead, + toggleBookmarked, + markPreviousAsRead + ) + } + ) @Composable @Stable diff --git a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/sources/components/DesktopSourcesMenu.kt b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/sources/components/DesktopSourcesMenu.kt index 88f53cd6..2e6c2d47 100644 --- a/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/sources/components/DesktopSourcesMenu.kt +++ b/presentation/src/desktopMain/kotlin/ca/gosyer/jui/ui/sources/components/DesktopSourcesMenu.kt @@ -6,17 +6,20 @@ package ca.gosyer.jui.ui.sources.components +import androidx.compose.foundation.PointerMatcher +import androidx.compose.foundation.onClick import androidx.compose.ui.Modifier -import ca.gosyer.jui.uicore.components.combinedMouseClickable +import androidx.compose.ui.input.pointer.PointerButton actual fun Modifier.sourceSideMenuItem( onSourceTabClick: () -> Unit, onSourceCloseTabClick: () -> Unit -): Modifier = combinedMouseClickable( - onClick = { - onSourceTabClick() - }, - onMiddleClick = { - onSourceCloseTabClick() - } -) +): Modifier = this + .onClick( + matcher = PointerMatcher.mouse(PointerButton.Primary), + onClick = onSourceTabClick + ) + .onClick( + matcher = PointerMatcher.mouse(PointerButton.Tertiary), + onClick = onSourceCloseTabClick + ) diff --git a/ui-core/src/desktopMain/kotlin/ca/gosyer/jui/uicore/components/MouseClickable.kt b/ui-core/src/desktopMain/kotlin/ca/gosyer/jui/uicore/components/MouseClickable.kt index ff881909..f0829cf0 100644 --- a/ui-core/src/desktopMain/kotlin/ca/gosyer/jui/uicore/components/MouseClickable.kt +++ b/ui-core/src/desktopMain/kotlin/ca/gosyer/jui/uicore/components/MouseClickable.kt @@ -7,8 +7,8 @@ package ca.gosyer.jui.uicore.components import androidx.compose.foundation.ContextMenuItem -import androidx.compose.foundation.MouseClickScope -import androidx.compose.foundation.mouseClickable +import androidx.compose.foundation.PointerMatcher +import androidx.compose.foundation.onClick import androidx.compose.material.CursorDropdownMenu import androidx.compose.material.DropdownMenuItem import androidx.compose.material.Text @@ -19,34 +19,12 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.composed -import androidx.compose.ui.input.pointer.isPrimaryPressed -import androidx.compose.ui.input.pointer.isSecondaryPressed -import androidx.compose.ui.input.pointer.isTertiaryPressed -import androidx.compose.ui.platform.debugInspectorInfo -import androidx.compose.ui.semantics.AccessibilityAction -import androidx.compose.ui.semantics.Role -import androidx.compose.ui.semantics.SemanticsPropertyKey -import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.input.pointer.PointerButton -fun Modifier.contextMenuClickable( +fun Modifier.onRightClickContextMenu( items: @Composable () -> List, - onClickLabel: String? = null, - onMiddleClickLabel: String? = null, - onRightClickLabel: String? = null, - onClick: MouseClickScope.() -> Unit = {}, - onMiddleClick: MouseClickScope.() -> Unit = {}, - enabled: Boolean = true -) = composed( - inspectorInfo = debugInspectorInfo { - name = "contextMenuClickable" - properties["onClick"] = onClick - properties["onMiddleClick"] = onMiddleClick - properties["enabled"] = enabled - properties["onClickLabel"] = onClickLabel - properties["onMiddleClickLabel"] = onMiddleClickLabel - properties["onRightClickLabel"] = onRightClickLabel - } -) { + enabled: Boolean = true, +) = composed { var expanded by remember { mutableStateOf(false) } CursorDropdownMenu( expanded, @@ -63,72 +41,9 @@ fun Modifier.contextMenuClickable( } } } - Modifier.combinedMouseClickable( - onClick = onClick, - onMiddleClick = onMiddleClick, - onClickLabel = onClickLabel, - onMiddleClickLabel = onMiddleClickLabel, - onRightClickLabel = onRightClickLabel, - onRightClick = { expanded = true }, - enabled = enabled - ) -} - -fun Modifier.combinedMouseClickable( - enabled: Boolean = true, - onClickLabel: String? = null, - onMiddleClickLabel: String? = null, - onRightClickLabel: String? = null, - role: Role? = null, - onClick: MouseClickScope.() -> Unit = {}, - onMiddleClick: MouseClickScope.() -> Unit = {}, - onRightClick: MouseClickScope.() -> Unit = {} -) = composed( - inspectorInfo = debugInspectorInfo { - name = "combinedMouseClickable" - properties["enabled"] = enabled - properties["onClickLabel"] = onClickLabel - properties["onMiddleClickLabel"] = onMiddleClickLabel - properties["onRightClickLabel"] = onRightClickLabel - properties["role"] = role - properties["onClick"] = onClick - properties["onMiddleClick"] = onMiddleClick - properties["onRightClick"] = onRightClick - } -) { - Modifier - .mouseClickable( - enabled, - onClickLabel, - role - ) { - when { - buttons.isPrimaryPressed -> onClick() - buttons.isSecondaryPressed -> onRightClick() - buttons.isTertiaryPressed -> onMiddleClick() - } - } - .semantics(mergeDescendants = true) { - this[DesktopSemanticsActions.onMiddleClick] = AccessibilityAction(onMiddleClickLabel) { onMiddleClick(); true } - this[DesktopSemanticsActions.onRightClick] = AccessibilityAction(onRightClickLabel) { onRightClick(); true } - } -} - -private object DesktopSemanticsActions { - val onRightClick = ActionPropertyKey Boolean>("OnRightClick") - val onMiddleClick = ActionPropertyKey Boolean>("OnMiddleClick") -} - -private fun > ActionPropertyKey( - name: String -): SemanticsPropertyKey> { - return SemanticsPropertyKey( - name = name, - mergePolicy = { parentValue, childValue -> - AccessibilityAction( - parentValue?.label ?: childValue.label, - parentValue?.action ?: childValue.action - ) - } + Modifier.onClick( + enabled = enabled, + matcher = PointerMatcher.mouse(PointerButton.Secondary), + onClick = { expanded = true }, ) }