Skip to content

Commit

Permalink
feat: Add gestures to the code viewer
Browse files Browse the repository at this point in the history
- 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
wingio committed Nov 7, 2023
1 parent c77835c commit 3e13725
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.materiiapps.gloom.ui.components.BackButton

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun SmallToolbar(
title: String,
modifier: Modifier = Modifier,
actions: @Composable RowScope.() -> Unit = {},
scrollBehavior: TopAppBarScrollBehavior? = null
) {
TopAppBar(
title = { Text(text = title) },
navigationIcon = { BackButton() },
actions = actions,
scrollBehavior = scrollBehavior
scrollBehavior = scrollBehavior,
modifier = modifier
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.materiiapps.gloom.ui.screens.explorer

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
Expand All @@ -18,9 +20,14 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.nestedscroll.nestedScroll
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import com.materiiapps.gloom.Res
Expand All @@ -34,6 +41,7 @@ import com.materiiapps.gloom.ui.screens.explorer.viewers.ImageFileViewer
import com.materiiapps.gloom.ui.screens.explorer.viewers.MarkdownFileViewer
import com.materiiapps.gloom.ui.screens.explorer.viewers.PdfFileViewer
import com.materiiapps.gloom.ui.screens.explorer.viewers.TextFileViewer
import com.materiiapps.gloom.ui.utils.thenIf
import com.materiiapps.gloom.ui.viewmodels.explorer.FileViewerViewModel
import dev.icerock.moko.resources.compose.stringResource
import org.koin.androidx.compose.get
Expand All @@ -56,8 +64,12 @@ class FileViewerScreen(
rememberPullRefreshState(viewModel.isLoading, onRefresh = { viewModel.getRepoFile() })
val file = viewModel.file?.gitObject?.onCommit?.file

var topBarHidden by remember {
mutableStateOf(false)
}

Scaffold(
topBar = { Toolbar(scrollBehavior, file) },
topBar = { Toolbar(scrollBehavior, file, topBarHidden) },
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
contentWindowInsets = WindowInsets(0, 0, 0, 0)
) { pv ->
Expand All @@ -76,7 +88,12 @@ class FileViewerScreen(
.fillMaxWidth()
)

false -> FileContent(file)
false -> FileContent(
file = file,
onHideToggled = {
topBarHidden = !topBarHidden
}
)
}

RefreshIndicator(pullRefreshState, viewModel.isLoading)
Expand All @@ -85,12 +102,19 @@ class FileViewerScreen(
}

@Composable
private fun FileContent(file: RepoFile.File?) {
private fun FileContent(
file: RepoFile.File?,
onHideToggled: () -> Unit
) {
when (file?.fileType?.__typename) {
"MarkdownFileType" -> MarkdownFileViewer(file.fileType?.onMarkdownFileType!!)
"ImageFileType" -> ImageFileViewer(file.fileType?.onImageFileType!!)
"PdfFileType" -> PdfFileViewer(file.fileType?.onPdfFileType!!)
"TextFileType" -> TextFileViewer(file.fileType?.onTextFileType!!, file.extension ?: "")
"TextFileType" -> TextFileViewer(
file.fileType?.onTextFileType!!,
file.extension ?: "",
onHideToggled
)
else -> {}
}
}
Expand All @@ -117,11 +141,17 @@ class FileViewerScreen(
private fun Toolbar(
scrollBehavior: TopAppBarScrollBehavior,
file: RepoFile.File?,
hidden: Boolean
) {
SmallToolbar(
title = path.split("/").lastOrNull() ?: "File",
actions = { FileActions(file) },
scrollBehavior = scrollBehavior
scrollBehavior = scrollBehavior,
modifier = Modifier
.animateContentSize()
.thenIf(hidden) {
height(0.dp)
}
)
}

Expand Down
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)
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data class CodeTheme(
val background: Color,
val linesBackground: Color,
val linesContent: Color,
val selectedHighlight: Color,
val code: Color,
val keyword: Color,
val string: Color,
Expand All @@ -33,11 +34,13 @@ data class CodeTheme(
background: Color,
linesBackground: Color,
linesContent: Color,
selectedHighlight: Color,
syntaxTheme: SyntaxTheme
): this(
background = background,
linesBackground = linesBackground,
linesContent = linesContent,
selectedHighlight = selectedHighlight,
code = Color(syntaxTheme.code),
keyword = Color(syntaxTheme.keyword),
string = Color(syntaxTheme.string),
Expand Down Expand Up @@ -77,6 +80,7 @@ data class CodeTheme(
background = MaterialTheme.colorScheme.surfaceColorAtElevation(3.dp).copy(alpha = 0.2f),
linesBackground = MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp),
linesContent = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f),
selectedHighlight = Color(0xFFFF9800), // Orange
syntaxTheme = syntaxTheme
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package com.materiiapps.gloom.ui.utils

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection

object DimenUtils {

val navBarPadding: Dp
@Composable get() = WindowInsets.systemBars.asPaddingValues().calculateBottomPadding()

fun PaddingValues.multiply(multiplier: Float, layoutDirection: LayoutDirection): PaddingValues {
return PaddingValues(
start = calculateStartPadding(layoutDirection) * multiplier,
end = calculateEndPadding(layoutDirection) * multiplier,
top = calculateTopPadding() * multiplier,
bottom = calculateBottomPadding() * multiplier
)
}

}

@Composable
Expand Down
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)
}
}

}
Loading

0 comments on commit 3e13725

Please sign in to comment.