-
Notifications
You must be signed in to change notification settings - Fork 48
feat/pane-expansion : Add expansion anchors and enforce 250dp minimum… #81
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ import androidx.compose.material3.VerticalDragHandle | |
| import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi | ||
| import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold | ||
| import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole | ||
| import androidx.compose.material3.adaptive.layout.PaneExpansionAnchor | ||
| import androidx.compose.material3.adaptive.layout.PaneExpansionState | ||
| import androidx.compose.material3.adaptive.layout.rememberPaneExpansionState | ||
| import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldNavigator | ||
|
|
@@ -52,10 +53,15 @@ import androidx.compose.runtime.rememberCoroutineScope | |
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.clipToBounds | ||
| import androidx.compose.ui.layout.layout | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.platform.testTag | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.compose.ui.zIndex | ||
| import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel | ||
| import androidx.ink.strokes.Stroke | ||
| import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
|
|
@@ -70,6 +76,31 @@ import com.example.cahier.features.home.viewmodel.HomeScreenViewModel | |
| import com.example.cahier.features.home.viewmodel.NoteListUiState | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| private val MinPaneWidth = 250.dp | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid duplicating the complex Key Improvements:
private val MinPaneWidth = 250.dp
private fun Modifier.minimumWidthLayout(minWidth: androidx.compose.ui.unit.Dp): Modifier = this
.clipToBounds()
.layout { measurable, constraints ->
if (constraints.maxWidth == 0) {
layout(0, 0) {}
} else {
val fixedWidth = minWidth.roundToPx()
val placeable = measurable.measure(
constraints.copy(
minWidth = fixedWidth,
maxWidth = maxOf(fixedWidth, constraints.maxWidth)
)
)
val reportedWidth = constraints.maxWidth
layout(reportedWidth, placeable.height) {
placeable.placeRelative(0, 0)
}
}
} |
||
|
|
||
|
|
||
| // The layout modifier implements a minimum width for the pane while | ||
| // allowing it to report its original width to the scaffold, preventing | ||
| // content from being overly compressed during resizing. | ||
| private fun Modifier.minimumWidthLayout(minWidth: Dp): Modifier = this | ||
| .clipToBounds() | ||
| .layout { measurable, constraints -> | ||
| if (constraints.maxWidth == 0) { | ||
| layout(0, 0) {} | ||
| } else { | ||
| val fixedWidth = minWidth.roundToPx() | ||
| val placeable = measurable.measure( | ||
| constraints.copy( | ||
| minWidth = fixedWidth, | ||
| maxWidth = maxOf(fixedWidth, constraints.maxWidth) | ||
| ) | ||
| ) | ||
| val reportedWidth = constraints.maxWidth | ||
| layout(reportedWidth, placeable.height) { | ||
| placeable.placeRelative(0, 0) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object HomeDestination : NavigationDestination { | ||
| override val route = "home" | ||
|
|
@@ -108,16 +139,31 @@ fun HomePane( | |
| modifier: Modifier = Modifier, | ||
| forceCompact: Boolean? = null, | ||
| homeScreenViewModel: HomeScreenViewModel = hiltViewModel(), | ||
| ) { | ||
| ) { | ||
| var currentDestination by rememberSaveable { mutableStateOf(AppDestinations.Home) } | ||
| val navigator = rememberListDetailPaneScaffoldNavigator<Note>() | ||
| val noteList by homeScreenViewModel.noteList.collectAsStateWithLifecycle() | ||
| val selectedNoteUIState by homeScreenViewModel.uiState.collectAsStateWithLifecycle() | ||
| val coroutineScope = rememberCoroutineScope() | ||
| val activity = LocalActivity.current | ||
| val windowSizeClass = activity?.let { calculateWindowSizeClass(it) } | ||
| val paneExpansionState = rememberPaneExpansionState() | ||
| var hasSetInitialProportion by remember { | ||
| val paneExpansionState = rememberPaneExpansionState( | ||
| keyProvider = navigator.scaffoldValue, | ||
| anchors = listOf( | ||
| // 1. Fully collapsed state (0% screen fraction) | ||
| PaneExpansionAnchor.Proportion(proportion = 0f), | ||
|
|
||
| // 2. The 25 % minimum width snapping boundary anchor | ||
| PaneExpansionAnchor.Proportion(proportion = 0.25f), | ||
|
|
||
| // 3. Middle split state (50% screen fraction) | ||
| PaneExpansionAnchor.Proportion(proportion = 0.5f), | ||
|
|
||
| // 4. Fully expanded list state (100% screen fraction, detail collapsed) | ||
| PaneExpansionAnchor.Proportion(proportion = 1f) | ||
| ) | ||
| ) | ||
|
Comment on lines
+150
to
+165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a discrepancy between the defined anchors ( Because |
||
| var hasSetInitialProportion by rememberSaveable { | ||
| mutableStateOf(false) | ||
| } | ||
| val isCompact = forceCompact | ||
|
|
@@ -153,7 +199,7 @@ fun HomePane( | |
|
|
||
| LaunchedEffect(isCompact, hasSetInitialProportion) { | ||
| if (!isCompact && !hasSetInitialProportion) { | ||
| paneExpansionState.setFirstPaneProportion(0.3f) | ||
| paneExpansionState.setFirstPaneProportion(0.5f) | ||
| hasSetInitialProportion = true | ||
| } | ||
| } | ||
|
|
@@ -236,23 +282,29 @@ private fun CahierNavigationSuite( | |
| when (currentDestination) { | ||
| AppDestinations.Home -> { | ||
| ListDetailPaneScaffold( | ||
| modifier = Modifier.fillMaxSize(), | ||
| directive = navigator.scaffoldDirective, | ||
| value = navigator.scaffoldValue, | ||
| paneExpansionState = paneExpansionState, | ||
| paneExpansionDragHandle = { state -> | ||
| val interactionSource = remember { MutableInteractionSource() } | ||
| VerticalDragHandle( | ||
| modifier = | ||
| Modifier.paneExpansionDraggable( | ||
| state, | ||
| LocalMinimumInteractiveComponentSize | ||
| .current, | ||
| interactionSource, | ||
| ), | ||
| Modifier | ||
| .paneExpansionDraggable( | ||
| state, | ||
| LocalMinimumInteractiveComponentSize | ||
| .current, | ||
| interactionSource, | ||
| ) | ||
| .zIndex(2f), // Specify z-index to ensure the drag handle is drawn on top of the panes | ||
| ) | ||
| }, | ||
| listPane = { | ||
| ListPaneContent( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| .minimumWidthLayout(MinPaneWidth), | ||
| noteList = noteList.noteList, | ||
| isCompact = isCompact, | ||
| selectedNoteId = if (isCompact) null | ||
|
|
@@ -294,6 +346,11 @@ private fun CahierNavigationSuite( | |
| if (!isCompact) { | ||
| selectedNoteUIState.note.let { note -> | ||
| DetailPaneContent( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| // Specify z-index to ensure the detail pane content is correctly layered | ||
| .zIndex(1f) | ||
| .minimumWidthLayout(MinPaneWidth), | ||
| note = note, | ||
| strokes = selectedNoteUIState.strokes, | ||
| onClickToEdit = { | ||
|
|
||
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.
Add the import for
clipToBoundsto support the custom minimum width layout modifier.