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

ComposePanel. Focus moves to child after focusing/unfocusing the main window #4919

Closed
igordmn opened this issue Jun 3, 2024 · 5 comments
Closed
Assignees
Labels
bug Something isn't working focus management p:high High priority regression swing interop Swing interop issue

Comments

@igordmn
Copy link
Collaborator

igordmn commented Jun 3, 2024

Compose 1.6.20-dev1663

  1. Run
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.TextField
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposePanel
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import java.awt.Dimension
import javax.swing.JFrame
import javax.swing.SwingUtilities

fun main() = SwingUtilities.invokeLater {
    val panel = ComposePanel()
    panel.setContent {
        var isFocused by remember { mutableStateOf(false) }
        Box(Modifier
            .onFocusChanged { isFocused = it.isFocused }
            .focusTarget()
            .border(1.dp, if (isFocused) Color.Red else Color.Transparent)
            .padding(8.dp)
        ) {
            TextField("", {}, singleLine = true)
        }
    }

    val frame = JFrame()
    frame.contentPane.add(panel)
    frame.size = Dimension(500, 500)
    frame.isVisible = true
}

  1. Focus the box
    image
  2. Deactivate/Activate the window
  3. See that focus moved to the textfield
    image

This happens, because Swing calls focusGained with FocusEvent.Cause.ACTIVATION when activating the main window, and Enter moves the focus to the child

Regression after JetBrains/compose-multiplatform-core#1352

@igordmn igordmn added bug Something isn't working p:high High priority swing interop Swing interop issue focus management regression labels Jun 3, 2024
@igordmn
Copy link
Collaborator Author

igordmn commented Jun 3, 2024

Will be fixed in integration branch

@igordmn igordmn self-assigned this Jun 3, 2024
@m-sasha
Copy link
Contributor

m-sasha commented Jun 3, 2024

I guess the fix will be to focus the first focusable element instead of "Enter"?

@igordmn
Copy link
Collaborator Author

igordmn commented Jun 3, 2024

The fix will be to skip requesting focus if it is already in focus. It is the only way to behave when we activate/deactivate window.

@m-sasha
Copy link
Contributor

m-sasha commented Jun 3, 2024

I couldn't immediately find a way to check if something is already focused. When "nothing" is focused, the root node has the focus.

igordmn added a commit to JetBrains/compose-multiplatform-core that referenced this issue Jun 7, 2024
No need to apply them anymore.

```
git diff androidx/compose-ui/1.6.7 origin/jb-main -- compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/focus/FocusOwnerImpl.kt > 1.patch
```

Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

JetBrains/compose-multiplatform#4917

Make the first ComposeComponent focused initially

Apply jb-main FocusOwnerImpl.kt changes

No need to apply them anymore.

```
git diff androidx/compose-ui/1.6.7 origin/jb-main -- compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/focus/FocusOwnerImpl.kt > 1.patch
```

Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

Fixes JetBrains/compose-multiplatform#4919

JetBrains/compose-multiplatform#4917

JetBrains/compose-multiplatform#2944
igordmn added a commit to JetBrains/compose-multiplatform-core that referenced this issue Jun 10, 2024
Fixes JetBrains/compose-multiplatform#2944
Fixes JetBrains/compose-multiplatform#4919
Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

After https://android-review.googlesource.com/c/platform/frameworks/support/+/2813125, there are some changes that affected our code for focus interop with ComposePanel/SwingPanel:
- the root node no longer focusable
- FocusOwnerImpl now has iterop callbacks and our own modification of this file (in jb-main, integration was reset) doesn't work anymore

## Testing
- added new tests
- ComposeFocusTest now pass

## Release notes
### Fixes - Multiple platforms
- Fix "ComposePanel. Focus moves to child after focusing/unfocusing the main window"
igordmn added a commit to JetBrains/compose-multiplatform-core that referenced this issue Jun 10, 2024
Fixes JetBrains/compose-multiplatform#2944
Fixes JetBrains/compose-multiplatform#4919
Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

After https://android-review.googlesource.com/c/platform/frameworks/support/+/2813125, there are some changes that affected our code for focus interop with ComposePanel/SwingPanel:
- the root node no longer focusable
- FocusOwnerImpl now has iterop callbacks and our own modification of this file (in jb-main, integration was reset) doesn't work anymore

## Testing
- added new tests
- ComposeFocusTest now pass

## Release notes
### Fixes - Multiple platforms
- Fix "ComposePanel. Focus moves to child after focusing/unfocusing the main window"
igordmn added a commit to JetBrains/compose-multiplatform-core that referenced this issue Jun 10, 2024
Fixes JetBrains/compose-multiplatform#2944
Fixes JetBrains/compose-multiplatform#4919
Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

After https://android-review.googlesource.com/c/platform/frameworks/support/+/2813125, there are some changes that affected our code for focus interop with ComposePanel/SwingPanel:
- the root node no longer focusable
- FocusOwnerImpl now has iterop callbacks and our own modification of this file (in jb-main, integration was reset) doesn't work anymore

## Testing
- added new tests
- ComposeFocusTest now pass

## Release notes
### Fixes - Multiple platforms
- Fix "ComposePanel. Focus moves to child after focusing/unfocusing the main window"
igordmn added a commit to JetBrains/compose-multiplatform-core that referenced this issue Jun 10, 2024
Fixes JetBrains/compose-multiplatform#2944
Fixes JetBrains/compose-multiplatform#4919
Fixes https://youtrack.jetbrains.com/issue/COMPOSE-1212/Integration.-Check-changes-in-FocusOwnerImpl

After https://android-review.googlesource.com/c/platform/frameworks/support/+/2813125, there are some changes that affected our code for focus interop with ComposePanel/SwingPanel:
- the root node no longer focusable
- FocusOwnerImpl now has iterop callbacks and our own modification of this file (in jb-main, integration was reset) doesn't work anymore

## Testing
- added new tests
- ComposeFocusTest now pass

## Release notes
### Fixes - Multiple platforms
- Fix "ComposePanel. Focus moves to child after focusing/unfocusing the main window"
@okushnikov
Copy link

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working focus management p:high High priority regression swing interop Swing interop issue
Projects
None yet
Development

No branches or pull requests

3 participants