-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from 2 commits
93bce9d
763e588
cd17460
79d3fa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,6 +304,8 @@ internal fun CoreTextField( | |
val coroutineScope = rememberCoroutineScope() | ||
val bringIntoViewRequester = remember { BringIntoViewRequester() } | ||
|
||
rememberClipboardEventsHandler(manager, state.hasFocus) | ||
|
||
// Focus | ||
val focusModifier = Modifier.textFieldFocusModifier( | ||
enabled = enabled, | ||
|
@@ -1191,3 +1193,9 @@ private fun notifyFocusedRect( | |
internal const val USE_WINDOW_FOCUS_ENABLED = false | ||
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. is this a flag of a kind? Is this supposed to be set externally somehow? 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. To be honest, I don't know. The logic I added is not related (at least directly) by this flag. It was there before. |
||
internal fun isWindowFocusedBehindFlag(windowInfo: WindowInfo) = | ||
if (USE_WINDOW_FOCUS_ENABLED) windowInfo.isWindowFocused else true | ||
|
||
@Composable | ||
internal expect inline fun rememberClipboardEventsHandler( | ||
textFieldSelectionManager: TextFieldSelectionManager, | ||
isFocused: Boolean | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.NonRestartableComposable | ||
import androidx.compose.ui.text.AnnotatedString | ||
import kotlinx.browser.document | ||
import org.w3c.dom.clipboard.ClipboardEvent | ||
import org.w3c.dom.events.Event | ||
import org.w3c.dom.events.EventListener | ||
|
||
@Composable | ||
@NonRestartableComposable | ||
internal actual inline fun rememberClipboardEventsHandler( | ||
textFieldSelectionManager: TextFieldSelectionManager, | ||
isFocused: Boolean | ||
) { | ||
if (isFocused) { | ||
DisposableEffect(textFieldSelectionManager) { | ||
|
||
val onCopy = EventListenerWrapper { event -> | ||
val textToCopy = textFieldSelectionManager.onCopyWithResult() | ||
if (textToCopy != null && event is ClipboardEvent) { | ||
event.clipboardData?.setData("text/plain", textToCopy) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
val onPaste = EventListenerWrapper { event -> | ||
if (event is ClipboardEvent) { | ||
val textToPaste = event.clipboardData?.getData("text/plain") ?: "" | ||
event.preventDefault() | ||
textFieldSelectionManager.paste(AnnotatedString(textToPaste)) | ||
} | ||
} | ||
|
||
val onCut = EventListenerWrapper { event -> | ||
if (event is ClipboardEvent) { | ||
val cutText = textFieldSelectionManager.onCutWithResult() | ||
event.clipboardData?.setData("text/plain", cutText ?: "") | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
document.addEventListener("copy", onCopy) | ||
document.addEventListener("paste", onPaste) | ||
document.addEventListener("cut", onCut) | ||
|
||
return@DisposableEffect onDispose { | ||
document.removeEventListener("copy", onCopy) | ||
document.removeEventListener("paste", onPaste) | ||
document.removeEventListener("cut", onCut) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun EventListenerWrapper(handler: (Event) -> Unit): EventListener = | ||
EventListener { handler(it) } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
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. This is not a new file. It was renamed from |
||
* 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) | ||
} | ||
} | ||
} |
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,13 @@ | |
|
||
package androidx.compose.foundation.text | ||
|
||
import org.jetbrains.skiko.OS | ||
import org.jetbrains.skiko.hostOs | ||
import androidx.compose.foundation.text.selection.TextFieldSelectionManager | ||
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( | ||
textFieldSelectionManager: TextFieldSelectionManager, | ||
isFocused: Boolean | ||
) { | ||
// nothing to do | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
internal actual inline fun rememberClipboardEventsHandler( | ||
textFieldSelectionManager: TextFieldSelectionManager, | ||
isFocused: Boolean | ||
) { | ||
// nothing to do | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.NonRestartableComposable | ||
import androidx.compose.ui.text.AnnotatedString | ||
import kotlinx.browser.document | ||
import org.w3c.dom.clipboard.ClipboardEvent | ||
import org.w3c.dom.events.Event | ||
import org.w3c.dom.events.EventListener | ||
|
||
@Composable | ||
@NonRestartableComposable | ||
internal actual inline fun rememberClipboardEventsHandler( | ||
textFieldSelectionManager: TextFieldSelectionManager, | ||
isFocused: Boolean | ||
) { | ||
if (isFocused) { | ||
DisposableEffect(textFieldSelectionManager) { | ||
|
||
val onCopy = EventListenerWrapper { event -> | ||
val textToCopy = textFieldSelectionManager.onCopyWithResult() | ||
if (textToCopy != null && event is ClipboardEvent) { | ||
event.clipboardData?.setData("text/plain", textToCopy) | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
val onPaste = EventListenerWrapper { event -> | ||
if (event is ClipboardEvent) { | ||
val textToPaste = event.clipboardData?.getData("text/plain") ?: "" | ||
event.preventDefault() | ||
textFieldSelectionManager.paste(AnnotatedString(textToPaste)) | ||
} | ||
} | ||
|
||
val onCut = EventListenerWrapper { event -> | ||
if (event is ClipboardEvent) { | ||
val cutText = textFieldSelectionManager.onCutWithResult() | ||
event.clipboardData?.setData("text/plain", cutText ?: "") | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
document.addEventListener("copy", onCopy) | ||
document.addEventListener("paste", onPaste) | ||
document.addEventListener("cut", onCut) | ||
|
||
return@DisposableEffect onDispose { | ||
document.removeEventListener("copy", onCopy) | ||
document.removeEventListener("paste", onPaste) | ||
document.removeEventListener("cut", onCut) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun EventListenerWrapper(handler: (Event) -> Unit): EventListener = | ||
js("(event) => { handler(event) }") |
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.
what worries me that we are introducing a platform-specific hook to the common so that we do something on one platform. I do realise that this is sometimes unavoidable, all I'm trying to say that before it's unavoidable it worth to invest into investigating any non-invasiva alternative/
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.
I looked once again and I reach a conclusion that to implement this feature, we can't avoid changing common. Here is what I base my conclusion on:
There're similar cases when one platform has a feature/function implemented, while all other platfroms have just empty function body.
The fact that we change the common code here, doesn't require us to urgently upstream this change, because it's an internal implementation detail which doesn't affect android behaviour.