-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add gestures to the code viewer
- Double tap to show/hide ui - Long press to start selection - Single press while in a selection to select more - Pinch to zoom (min 70%, max 200%)
- Loading branch information
Showing
8 changed files
with
320 additions
and
38 deletions.
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
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
102 changes: 98 additions & 4 deletions
102
ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/screens/explorer/viewers/TextFileViewer.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 |
---|---|---|
@@ -1,18 +1,112 @@ | ||
package com.materiiapps.gloom.ui.screens.explorer.viewers | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.scaleIn | ||
import androidx.compose.animation.scaleOut | ||
import androidx.compose.foundation.gestures.detectTransformGestures | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.systemBarsPadding | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.SearchOff | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.LargeFloatingActionButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableFloatStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import com.materiiapps.gloom.Res | ||
import com.materiiapps.gloom.gql.fragment.RepoFile | ||
import com.materiiapps.gloom.ui.utils.DimenUtils.multiply | ||
import com.materiiapps.gloom.ui.widgets.code.CodeViewer | ||
import dev.icerock.moko.resources.compose.stringResource | ||
|
||
@Composable | ||
fun TextFileViewer( | ||
textFile: RepoFile.OnTextFileType, | ||
extension: String, | ||
onHideToggled: () -> Unit | ||
) { | ||
val content = textFile.contentRaw ?: return | ||
|
||
CodeViewer( | ||
code = content, | ||
extension = extension | ||
) | ||
val lazyListState = rememberLazyListState() | ||
val scrollState = rememberScrollState() | ||
val layoutDirection = LocalLayoutDirection.current | ||
val defaultLineNumberPadding = PaddingValues(horizontal = 8.dp, vertical = 3.dp) | ||
|
||
var scale by remember { mutableFloatStateOf(1f) } | ||
var lineNumberPadding by remember { mutableStateOf(defaultLineNumberPadding) } | ||
var hideFAB by remember { mutableStateOf(false) } | ||
var linesSelected by remember { mutableStateOf(null as IntRange?) } | ||
|
||
Box( | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
CodeViewer( | ||
code = content, | ||
extension = extension, | ||
fontSize = 13.sp * scale, | ||
codePadding = 16.dp * scale, | ||
lineNumberPadding = lineNumberPadding, | ||
linesSelected = linesSelected, | ||
onLinesSelected = { | ||
linesSelected = it | ||
}, | ||
onDoubleClick = { // Double tap to hide the ui | ||
onHideToggled() | ||
hideFAB = !hideFAB | ||
}, | ||
modifier = Modifier | ||
.pointerInput(Unit) { | ||
// Pinch to zoom | ||
detectTransformGestures { _, pan, zoom, _ -> | ||
if (scale * zoom in 0.7f..2f) { | ||
scale *= zoom | ||
lineNumberPadding = lineNumberPadding.multiply(zoom, layoutDirection) | ||
} | ||
scrollState.dispatchRawDelta(-(pan.x.toDp().toPx())) | ||
lazyListState.dispatchRawDelta(-(pan.y.toDp().toPx())) | ||
} | ||
} | ||
) | ||
|
||
AnimatedVisibility( | ||
visible = scale != 1f && !hideFAB, | ||
enter = scaleIn(), | ||
exit = scaleOut(), | ||
modifier = Modifier | ||
.align(Alignment.BottomEnd) | ||
.systemBarsPadding() | ||
.padding(16.dp) | ||
) { | ||
LargeFloatingActionButton( | ||
shape = RoundedCornerShape(25), | ||
onClick = { | ||
scale = 1f | ||
lineNumberPadding = defaultLineNumberPadding | ||
}, | ||
modifier = Modifier | ||
.size(56.dp) | ||
) { | ||
Icon( | ||
Icons.Outlined.SearchOff, | ||
contentDescription = stringResource(Res.strings.action_reset_zoom) | ||
) | ||
} | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/utils/DimenUtils.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
19 changes: 19 additions & 0 deletions
19
ui/src/commonMain/kotlin/com/materiiapps/gloom/ui/utils/LazyUtil.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,19 @@ | ||
package com.materiiapps.gloom.ui.utils | ||
|
||
import androidx.compose.foundation.lazy.LazyListItemInfo | ||
import androidx.compose.foundation.lazy.LazyListState | ||
import androidx.compose.ui.geometry.Offset | ||
import kotlin.math.roundToInt | ||
|
||
object LazyUtil { | ||
|
||
fun LazyListState.getItemAtOffset( | ||
offset: Offset, | ||
horizontal: Boolean = false | ||
): LazyListItemInfo? { | ||
return layoutInfo.visibleItemsInfo.firstOrNull { itemInfo -> | ||
(if (horizontal) offset.x else offset.y).roundToInt() in (itemInfo.offset..itemInfo.offset + itemInfo.size) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.