-
Notifications
You must be signed in to change notification settings - Fork 166
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
[Rich text editor] Add full screen mode #1447
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16d0ff1
Add RTE full screen mode
jonnyandrew 2152511
Fix scrolling when content overflows
jonnyandrew 8c0f258
Fix lint issues
jonnyandrew ac76393
Update previews
jonnyandrew fb7d73b
Update screenshots
5b702c1
Add changelog
jonnyandrew 2d31c8f
Add note about subcomposition
jonnyandrew 8a188a6
Merge branch 'develop' of github.com:vector-im/element-x-android into…
jonnyandrew 9356c8b
Merge branch 'develop' of github.com:vector-im/element-x-android into…
jonnyandrew 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 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 @@ | ||
[Rich text editor] Add full screen mode |
177 changes: 177 additions & 0 deletions
177
...rc/main/kotlin/io/element/android/features/messages/impl/ExpandableBottomSheetScaffold.kt
This file contains 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,177 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* 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. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalMaterial3Api::class) | ||
|
||
package io.element.android.features.messages.impl | ||
|
||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.material3.BottomSheetScaffold | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.SheetState | ||
import androidx.compose.material3.SheetValue | ||
import androidx.compose.material3.rememberBottomSheetScaffoldState | ||
import androidx.compose.material3.rememberStandardBottomSheetState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.derivedStateOf | ||
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.graphics.Shape | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.layout.SubcomposeLayout | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.min | ||
import kotlin.math.roundToInt | ||
|
||
/** | ||
* A [BottomSheetScaffold] that allows the sheet to be expanded the screen height | ||
* of the sheet contents. | ||
* | ||
* @param content The main content. | ||
* @param sheetContent The sheet content. | ||
* @param sheetDragHandle The drag handle for the sheet. | ||
* @param sheetSwipeEnabled Whether the sheet can be swiped. This value is ignored and swipe is disabled if the sheet content overflows. | ||
* @param sheetShape The shape of the sheet. | ||
* @param sheetTonalElevation The tonal elevation of the sheet. | ||
* @param sheetShadowElevation The shadow elevation of the sheet. | ||
* @param modifier The modifier for the layout. | ||
* @param sheetContentKey The key for the sheet content. If the key changes, the sheet will be remeasured. | ||
*/ | ||
@Composable | ||
internal fun ExpandableBottomSheetScaffold( | ||
content: @Composable (padding: PaddingValues) -> Unit, | ||
sheetContent: @Composable (subcomposing: Boolean) -> Unit, | ||
sheetDragHandle: @Composable () -> Unit, | ||
sheetSwipeEnabled: Boolean, | ||
sheetShape: Shape, | ||
sheetTonalElevation: Dp, | ||
sheetShadowElevation: Dp, | ||
modifier: Modifier = Modifier, | ||
sheetContentKey: Int? = null, | ||
) { | ||
val scaffoldState = rememberBottomSheetScaffoldState( | ||
bottomSheetState = rememberStandardBottomSheetState( | ||
initialValue = SheetValue.PartiallyExpanded, | ||
skipHiddenState = true, | ||
) | ||
) | ||
|
||
// If the content overflows, we disable swipe to prevent the sheet from intercepting | ||
// scroll events of the sheet content. | ||
var contentOverflows by remember { mutableStateOf(false) } | ||
val sheetSwipeEnabledIfPossible by remember(contentOverflows, sheetSwipeEnabled) { | ||
derivedStateOf { | ||
sheetSwipeEnabled && !contentOverflows | ||
} | ||
} | ||
|
||
LaunchedEffect(sheetSwipeEnabledIfPossible) { | ||
if (!sheetSwipeEnabledIfPossible) { | ||
scaffoldState.bottomSheetState.partialExpand() | ||
} | ||
} | ||
|
||
@Composable | ||
fun Scaffold( | ||
sheetContent: @Composable () -> Unit, | ||
dragHandle: @Composable () -> Unit, | ||
peekHeight: Dp, | ||
) { | ||
BottomSheetScaffold( | ||
modifier = Modifier, | ||
scaffoldState = scaffoldState, | ||
sheetPeekHeight = peekHeight, | ||
sheetSwipeEnabled = sheetSwipeEnabledIfPossible, | ||
sheetDragHandle = dragHandle, | ||
sheetShape = sheetShape, | ||
content = content, | ||
sheetContent = { sheetContent() }, | ||
sheetTonalElevation = sheetTonalElevation, | ||
sheetShadowElevation = sheetShadowElevation, | ||
) | ||
} | ||
|
||
SubcomposeLayout( | ||
modifier = modifier, | ||
measurePolicy = { constraints: Constraints -> | ||
val sheetContentSub = subcompose(Slot.SheetContent(sheetContentKey)) { sheetContent(true) }.map { | ||
it.measure(Constraints(maxWidth = constraints.maxWidth)) | ||
}.first() | ||
val dragHandleSub = subcompose(Slot.DragHandle) { sheetDragHandle() }.map { | ||
it.measure(Constraints(maxWidth = constraints.maxWidth)) | ||
}.firstOrNull() | ||
val dragHandleHeight = dragHandleSub?.height?.toDp() ?: 0.dp | ||
|
||
val maxHeight = constraints.maxHeight.toDp() | ||
val contentHeight = sheetContentSub.height.toDp() + dragHandleHeight | ||
|
||
contentOverflows = contentHeight > maxHeight | ||
|
||
val peekHeight = min( | ||
maxHeight, // prevent the sheet from expanding beyond the screen | ||
contentHeight | ||
) | ||
|
||
val scaffoldPlaceables = subcompose(Slot.Scaffold) { | ||
Scaffold({ | ||
Layout( | ||
modifier = Modifier.fillMaxHeight(), | ||
measurePolicy = { measurables, constraints -> | ||
val constraintHeight = constraints.maxHeight | ||
val offset = scaffoldState.bottomSheetState.getOffset() ?: 0 | ||
val height = Integer.max(0, constraintHeight - offset) | ||
val top = measurables[0].measure( | ||
constraints.copy( | ||
minHeight = height, | ||
maxHeight = height | ||
) | ||
) | ||
layout(constraints.maxWidth, constraints.maxHeight) { | ||
top.place(x = 0, y = 0) | ||
} | ||
}, | ||
content = { sheetContent(false) }) | ||
}, sheetDragHandle, peekHeight) | ||
}.map { measurable: Measurable -> | ||
measurable.measure(constraints) | ||
} | ||
val scaffoldPlaceable = scaffoldPlaceables.first() | ||
layout(constraints.maxWidth, constraints.maxHeight) { | ||
scaffoldPlaceable.place(0, 0) | ||
} | ||
}) | ||
} | ||
|
||
private fun SheetState.getOffset(): Int? = try { | ||
requireOffset().roundToInt() | ||
} catch (e: IllegalStateException) { | ||
null | ||
} | ||
|
||
private sealed class Slot { | ||
data class SheetContent(val key: Int?) : Slot() | ||
data object DragHandle : Slot() | ||
data object Scaffold : Slot() | ||
} | ||
|
This file contains 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 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 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 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 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.
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.
richTextEditorState
is mutable