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

Fix ComposePanel.requestFocus #1352

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ class ComposePanel @ExperimentalComposeUiApi constructor(
FocusEvent.Cause.TRAVERSAL_FORWARD -> {
focusManager.moveFocus(FocusDirection.Next)
}

FocusEvent.Cause.TRAVERSAL_BACKWARD -> {
focusManager.moveFocus(FocusDirection.Previous)
}

FocusEvent.Cause.UNKNOWN, FocusEvent.Cause.ACTIVATION -> {
focusManager.moveFocus(FocusDirection.Enter)
}
else -> Unit
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package androidx.compose.ui.awt

import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
Expand All @@ -29,14 +31,14 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.layout.layout
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.sendMouseEvent
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
import androidx.compose.ui.util.ThrowUncaughtExceptionRule
Expand All @@ -47,6 +49,7 @@ import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.GraphicsEnvironment
import java.awt.event.MouseEvent
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import junit.framework.TestCase.assertTrue
Expand Down Expand Up @@ -459,4 +462,59 @@ class ComposePanelTest {
window.dispose()
}
}
}

@Test
fun `requestFocus assigns focus to first focusable element`() = runApplicationTest {
assumeFalse(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance)

var focusedElement: String? = null
val composePanel = ComposePanel()
composePanel.setBounds(0, 25, 100, 100)
composePanel.setContent {
Column {
Box(Modifier
.size(10.dp)
.onFocusChanged {
focusedElement = if (it.isFocused) "first" else null
}
.focusable()
)
Box(Modifier
.size(10.dp)
.onFocusChanged {
focusedElement = if (it.isFocused) "second" else null
}
.focusable()
)
}
}

val frame = JFrame()
try {
val button = JButton("Button")
frame.size = Dimension(500, 500)
frame.contentPane.add(button, BorderLayout.NORTH)
frame.contentPane.add(composePanel, BorderLayout.CENTER)
frame.isVisible = true

assertEquals(null, focusedElement)

// The first requestFocus sends a focusGained(Cause.ACTIVATION) event
composePanel.requestFocus()
awaitIdle()
assertEquals("first", focusedElement)

// Switch focus back to Swing
button.requestFocus()
awaitIdle()
assertEquals(null, focusedElement)

// The 2nd requestFocus sends a focusGained(Cause.UNKNOWN) event; we want to test both
composePanel.requestFocus()
awaitIdle()
assertEquals("first", focusedElement)
} finally {
frame.dispose()
}
}
}
Loading