forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listen to browser clipboard events and bind them with Compose TextFieldSelectionManager and SelectionManager #1206
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93bce9d
Listen to browser clipboard events and bind them with Compose TextFie…
eymar 763e588
Update the KeyMapping for web to skip key events associated with the …
eymar cd17460
Minor refactoring of ClipboardEventsHandler
Schahen 79d3fa0
Add copy event handling in SelectionContainer
eymar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...undation/src/commonMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.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,27 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
internal expect inline fun rememberClipboardEventsHandler( | ||
crossinline onPaste: (String) -> Unit = {}, | ||
crossinline onCopy: () -> String? = { null }, | ||
crossinline onCut: () -> String? = { null }, | ||
isEnabled: Boolean | ||
) |
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
72 changes: 72 additions & 0 deletions
72
...oundation/src/jsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.js.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,72 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.NonRestartableComposable | ||
import kotlinx.browser.document | ||
import org.w3c.dom.clipboard.ClipboardEvent | ||
import org.w3c.dom.events.EventListener | ||
|
||
@Composable | ||
@NonRestartableComposable | ||
internal actual inline fun rememberClipboardEventsHandler( | ||
crossinline onPaste: (String) -> Unit, | ||
crossinline onCopy: () -> String?, | ||
crossinline onCut: () -> String?, | ||
isEnabled: Boolean | ||
) { | ||
if (isEnabled) { | ||
DisposableEffect(Unit) { | ||
|
||
val copyListener = EventListener { event -> | ||
val textToCopy = onCopy() | ||
if (textToCopy != null && event is ClipboardEvent) { | ||
event.clipboardData?.setData("text/plain", textToCopy) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
val pasteListener = EventListener { event -> | ||
if (event is ClipboardEvent) { | ||
val textToPaste = event.clipboardData?.getData("text/plain") ?: "" | ||
onPaste(textToPaste) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
val cutListener = EventListener { event -> | ||
val cutText = onCut() | ||
if (cutText != null && event is ClipboardEvent) { | ||
event.clipboardData?.setData("text/plain", cutText) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
document.addEventListener("copy", copyListener) | ||
document.addEventListener("paste", pasteListener) | ||
document.addEventListener("cut", cutListener) | ||
|
||
onDispose { | ||
document.removeEventListener("copy", copyListener) | ||
document.removeEventListener("paste", pasteListener) | ||
document.removeEventListener("cut", cutListener) | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ation/foundation/src/jsWasmMain/kotlin/androidx/compose/foundation/text/KeyMapping.web.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,46 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.input.key.KeyEvent | ||
import androidx.compose.ui.input.key.isCtrlPressed | ||
import androidx.compose.ui.input.key.isMetaPressed | ||
import androidx.compose.ui.input.key.key | ||
import org.jetbrains.skiko.OS | ||
import org.jetbrains.skiko.hostOs | ||
|
||
internal actual val platformDefaultKeyMapping: KeyMapping = createPlatformDefaultKeyMapping(hostOs) | ||
|
||
internal fun createPlatformDefaultKeyMapping(platform: OS): KeyMapping { | ||
val keyMapping = when (platform) { | ||
OS.MacOS -> createMacosDefaultKeyMapping() | ||
else -> defaultKeyMapping | ||
} | ||
return object : KeyMapping { | ||
private val clipboardKeys = setOf(Key.C, Key.V, Key.X) | ||
|
||
override fun map(event: KeyEvent): KeyCommand? { | ||
val isCtrlOrCmd = if (hostOs.isMacOS) event.isMetaPressed else event.isCtrlPressed | ||
if (isCtrlOrCmd && event.key in clipboardKeys) { | ||
// we let a browser dispatch a clipboard event | ||
return null | ||
} | ||
return keyMapping.map(event) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,14 +16,14 @@ | |
|
||
package androidx.compose.foundation.text | ||
|
||
import org.jetbrains.skiko.OS | ||
import org.jetbrains.skiko.hostOs | ||
import androidx.compose.runtime.Composable | ||
|
||
internal actual val platformDefaultKeyMapping: KeyMapping = createPlatformDefaultKeyMapping(hostOs) | ||
|
||
internal fun createPlatformDefaultKeyMapping(platform: OS): KeyMapping { | ||
return when (platform) { | ||
OS.MacOS -> createMacosDefaultKeyMapping() | ||
else -> defaultKeyMapping | ||
} | ||
@Composable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: github is displaying this incorrectly. File But file |
||
internal actual inline fun rememberClipboardEventsHandler( | ||
crossinline onPaste: (String) -> Unit, | ||
crossinline onCopy: () -> String?, | ||
crossinline onCut: () -> String?, | ||
isEnabled: Boolean | ||
) { | ||
// nothing to do | ||
} |
20 changes: 20 additions & 0 deletions
20
...ion/src/jvmMain/kotlin/androidx/compose/foundation/text/selection/SelectionManager.jvm.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,20 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text.selection | ||
|
||
internal actual val SelectionManager.skipCopyKeyEvent: Boolean | ||
get() = false |
29 changes: 29 additions & 0 deletions
29
...ation/foundation/src/nativeMain/kotlin/androidx/compose/foundation/text/Actuals.native.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,29 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
internal actual inline fun rememberClipboardEventsHandler( | ||
crossinline onPaste: (String) -> Unit, | ||
crossinline onCopy: () -> String?, | ||
crossinline onCut: () -> String?, | ||
isEnabled: Boolean | ||
) { | ||
// nothing to do | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a new file. It was renamed from
KeyMapping.js.kt
+ updated for clipboard related key events