-
Notifications
You must be signed in to change notification settings - Fork 2
Fix keyboard dismissal when opening message sheets #1417
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
Draft
agent-p1p
wants to merge
1
commit into
master
Choose a base branch
from
pip/whitenoise-android-1396
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
app/src/main/java/dev/ipf/whitenoise/android/ui/design/AppSheets.kt
This file contains hidden or 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,88 @@ | ||
| package dev.ipf.whitenoise.android.ui.design | ||
|
|
||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.gestures.detectTapGestures | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.ColumnScope | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.navigationBarsPadding | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.widthIn | ||
| import androidx.compose.material3.BottomSheetDefaults | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.Surface | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.input.pointer.pointerInput | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.semantics.Role | ||
| import androidx.compose.ui.semantics.contentDescription | ||
| import androidx.compose.ui.semantics.dialog | ||
| import androidx.compose.ui.semantics.isTraversalGroup | ||
| import androidx.compose.ui.semantics.onClick | ||
| import androidx.compose.ui.semantics.paneTitle | ||
| import androidx.compose.ui.semantics.role | ||
| import androidx.compose.ui.semantics.semantics | ||
| import androidx.compose.ui.unit.dp | ||
| import dev.ipf.whitenoise.android.R | ||
| import dev.ipf.whitenoise.android.ui.theme.amoledSheetContainerColor | ||
|
|
||
| /** | ||
| * Bottom-anchored sheet that leaves focus in its host window. | ||
| * | ||
| * Material's [androidx.compose.material3.ModalBottomSheet] opens a focusable dialog window. From a | ||
| * focused conversation that steals window focus from the composer and Android closes the IME. | ||
| * [KeyboardSafePopup] supplies the shared non-focusable popup, outside-tap consumption, and | ||
| * overlay-priority Back dismissal. This wrapper adds the sheet styling and accessibility semantics. | ||
| */ | ||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| @Composable | ||
| internal fun KeyboardPreservingBottomSheet( | ||
| paneTitle: String, | ||
| onDismissRequest: () -> Unit, | ||
| modifier: Modifier = Modifier, | ||
| content: @Composable ColumnScope.() -> Unit, | ||
| ) { | ||
| val dismissLabel = stringResource(R.string.dismiss) | ||
| KeyboardSafePopup( | ||
| expanded = true, | ||
| onDismissRequest = onDismissRequest, | ||
| popupPositionProvider = BottomAnchoredPopupPositionProvider, | ||
| scrimModifier = | ||
| Modifier | ||
| .background(BottomSheetDefaults.ScrimColor) | ||
| .semantics { | ||
| contentDescription = dismissLabel | ||
| role = Role.Button | ||
| onClick(label = dismissLabel) { | ||
| onDismissRequest() | ||
| true | ||
| } | ||
| }, | ||
| ) { | ||
| Surface( | ||
| modifier = | ||
| Modifier | ||
| .then(modifier) | ||
| .widthIn(max = BottomSheetDefaults.SheetMaxWidth) | ||
| .fillMaxWidth() | ||
| .semantics { | ||
| isTraversalGroup = true | ||
| dialog() | ||
| this.paneTitle = paneTitle | ||
| }.pointerInput(Unit) { | ||
| detectTapGestures { /* Consume blank sheet taps without dismissing. */ } | ||
| }, | ||
| shape = BottomSheetDefaults.ExpandedShape, | ||
| color = amoledSheetContainerColor(), | ||
| ) { | ||
| Column( | ||
| modifier = | ||
| Modifier | ||
| .navigationBarsPadding() | ||
| .padding(top = 8.dp), | ||
| content = content, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
82 changes: 82 additions & 0 deletions
82
app/src/test/java/dev/ipf/whitenoise/android/ui/KeyboardPreservingBottomSheetCoverageTest.kt
This file contains hidden or 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,82 @@ | ||
| package dev.ipf.whitenoise.android.ui | ||
|
|
||
| import dev.ipf.whitenoise.android.functionBody | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import java.io.File | ||
|
|
||
| class KeyboardPreservingBottomSheetCoverageTest { | ||
| @Test | ||
| fun bottomSheetDelegatesOverlayPlumbingToKeyboardSafePopup() { | ||
| val source = appSheetsSource().readText() | ||
| val body = source.functionBody("KeyboardPreservingBottomSheet") | ||
|
|
||
| assertTrue("sheet must use the shared non-focusable overlay", "KeyboardSafePopup(" in body) | ||
| assertTrue( | ||
| "sheet must anchor above the visible-window bottom, including an open IME", | ||
| "popupPositionProvider = BottomAnchoredPopupPositionProvider" in body, | ||
| ) | ||
| assertFalse("sheet wrapper must not duplicate Popup plumbing", Regex("""\bPopup\(""").containsMatchIn(body)) | ||
| assertFalse("sheet wrapper must not duplicate Back handling", "BackHandler(" in body || "OnBackInvoked" in body) | ||
| } | ||
|
|
||
| @Test | ||
| fun bottomSheetAddsVisualAndAccessibleModalSemantics() { | ||
| val body = appSheetsSource().readText().functionBody("KeyboardPreservingBottomSheet") | ||
|
|
||
| assertTrue( | ||
| "shared dismissal scrim must remain visible and accessible", | ||
| "scrimModifier" in body && | ||
| ".background(BottomSheetDefaults.ScrimColor)" in body && | ||
| "contentDescription = dismissLabel" in body && | ||
| "role = Role.Button" in body && | ||
| "onClick(label = dismissLabel)" in body, | ||
| ) | ||
| assertTrue( | ||
| "sheet content must retain dialog, traversal, and pane semantics", | ||
| "isTraversalGroup = true" in body && | ||
| "dialog()" in body && | ||
| "this.paneTitle = paneTitle" in body, | ||
| ) | ||
| assertTrue( | ||
| "sheet must retain Material sizing, shape, and navigation-bar padding", | ||
| "BottomSheetDefaults.SheetMaxWidth" in body && | ||
| "BottomSheetDefaults.ExpandedShape" in body && | ||
| ".navigationBarsPadding()" in body, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun remainingMessageSheetsUseKeyboardPreservingContainer() { | ||
| listOf( | ||
| editHistorySource().readText().functionBody("EditHistorySheet"), | ||
| messageFullScreenSource().readText().functionBody("MessageInfoSheet"), | ||
| ).forEach { body -> | ||
| assertTrue("conversation sheet must use KeyboardPreservingBottomSheet", "KeyboardPreservingBottomSheet(" in body) | ||
| assertFalse("conversation sheet must not open a focus-stealing ModalBottomSheet", "ModalBottomSheet(" in body) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun forwardSheetKeepsItsFocusableModalForSearch() { | ||
| val body = messageActionsSource().readText().functionBody("ForwardMessageSheet") | ||
|
|
||
| assertTrue("the forward flow still needs a focusable ModalBottomSheet for its search field", "ModalBottomSheet(" in body) | ||
| } | ||
|
|
||
| private fun appSheetsSource(): File = source("ui/design/AppSheets.kt") | ||
|
|
||
| private fun editHistorySource(): File = source("ui/conversation/messages/EditHistory.kt") | ||
|
|
||
| private fun messageFullScreenSource(): File = source("ui/conversation/messages/MessageFullScreen.kt") | ||
|
|
||
| private fun messageActionsSource(): File = source("ui/conversation/messages/MessageActions.kt") | ||
|
|
||
| private fun source(relativePath: String): File = | ||
| listOf( | ||
| File("src/main/java/dev/ipf/whitenoise/android/$relativePath"), | ||
| File("app/src/main/java/dev/ipf/whitenoise/android/$relativePath"), | ||
| ).firstOrNull { it.exists() } | ||
| ?: error("Missing $relativePath source file") | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.