Skip to content
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 4 commits into from
Mar 21, 2024

Conversation

eymar
Copy link
Collaborator

@eymar eymar commented Mar 19, 2024

Notes:
I had to almost copy/paste the code for k/js and k/wasm because of k/wasm-js and k/js-js interop differences. The function is not too big, so I consider it as a compromise.

Test in mpp:demo:

  • Components -> Selection : select some text and press Cmd + C or Ctrl + C, try to paste it then somewhere
  • Components -> TextFields -> Almost FullScreen: Try all cobminations

@eymar eymar requested a review from Schahen March 19, 2024 17:33
addTypedEvent<KeyboardEvent>("keydown") { event ->
val skikoEvent: SkikoKeyboardEvent = event.toSkikoEvent(SkikoKeyboardEventKind.DOWN)
if (skikoEvent.key in setOfClipboardKeys && skikoEvent.modifiers != SkikoInputModifiers.EMPTY) {
Copy link
Collaborator

@Schahen Schahen Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things I wanted to discuss regarding this fragment. First, we already have a mechanism for passing events to the browser - that's onKeyboardEventWithResult what if we'll move this logic of whether we are dispatching clipboard event over there?

Second, it seems to be the right way to filter out some specific keyboard event is to have a helper function that have actual access to the keymappings . Check out, for instance, this code

internal actual fun isCopyKeyEvent(keyEvent: KeyEvent) =
    platformDefaultKeyMapping.map(keyEvent) == KeyCommand.COPY

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this one by updating KeyMapping for web. Please have a look at the latest commit.

@@ -1191,3 +1193,9 @@ private fun notifyFocusedRect(
internal const val USE_WINDOW_FOCUS_ENABLED = false
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

@@ -304,6 +304,8 @@ internal fun CoreTextField(
val coroutineScope = rememberCoroutineScope()
val bringIntoViewRequester = remember { BringIntoViewRequester() }

rememberClipboardEventsHandler(manager, state.hasFocus)
Copy link
Collaborator

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/

Copy link
Collaborator Author

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:

  • the events handler needs to "know" some textfield state (what text field is active, what text is selected, etc.). In this case the necesary state is provided by TextFieldSelectionManager (it's internal in foundation module)
  • there is no global state of text fields. TextFieldState and TextFieldSelectionManager are created (and remembered) for every CoreTextField during its initialization. It makse CoreTextField a convenient place to initialize our event handler too.
  • An alternative approach to find a focused node and to dispatch an event to it would require changes in common too, because currently the nodes can handle just key events, but not higher-level clipboard events.

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.

OS.MacOS -> createMacosDefaultKeyMapping()
else -> defaultKeyMapping
}
@Composable
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: github is displaying this incorrectly. File KeyMapping.jsMain.kt was renamed into KeyMapping.web.kt.

But file Actuals.jvm.kt is actually a new file.

@@ -0,0 +1,46 @@
/*
Copy link
Collaborator Author

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

@eymar eymar requested a review from igordmn March 20, 2024 10:46
@eymar eymar changed the title Listen to browser clipboard events and bind them with Compose TextFieldSelectionManager Listen to browser clipboard events and bind them with Compose TextFieldSelectionManager and SelectionManager Mar 20, 2024
@igordmn igordmn removed their request for review March 20, 2024 18:00
@eymar eymar merged commit be54485 into jb-main Mar 21, 2024
6 checks passed
@eymar eymar deleted the ok/handle_browser_clipboard_events branch March 21, 2024 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants