Skip to content

Commit 8d48e20

Browse files
committed
✨ added scrollbar and improved dialog peek
Signed-off-by: shub39 <[email protected]>
1 parent 6564123 commit 8d48e20

File tree

9 files changed

+108
-23
lines changed

9 files changed

+108
-23
lines changed

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/home/components/ChaptersSection.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.padding
88
import androidx.compose.foundation.layout.size
99
import androidx.compose.foundation.lazy.LazyColumn
1010
import androidx.compose.foundation.lazy.items
11+
import androidx.compose.foundation.lazy.rememberLazyListState
1112
import androidx.compose.material3.Icon
1213
import androidx.compose.material3.IconButton
1314
import androidx.compose.material3.ListItem
@@ -18,6 +19,7 @@ import androidx.compose.ui.Modifier
1819
import androidx.compose.ui.unit.dp
1920
import com.shub39.dharmik.bhagvad_gita.presentation.home.HomeAction
2021
import com.shub39.dharmik.bhagvad_gita.presentation.home.HomeState
22+
import com.shub39.dharmik.core.presentation.components.simpleVerticalScrollbar
2123
import compose.icons.FontAwesomeIcons
2224
import compose.icons.fontawesomeicons.Solid
2325
import compose.icons.fontawesomeicons.solid.ArrowRight
@@ -32,8 +34,12 @@ fun ChaptersSection(
3234
state: HomeState,
3335
onAction: (HomeAction) -> Unit
3436
) {
37+
val listState = rememberLazyListState()
38+
3539
LazyColumn(
40+
state = listState,
3641
modifier = Modifier
42+
.simpleVerticalScrollbar(listState)
3743
.animateContentSize()
3844
.fillMaxSize(),
3945
contentPadding = PaddingValues(horizontal = 16.dp),

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/home/components/HomeSection.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.padding
1010
import androidx.compose.foundation.layout.size
1111
import androidx.compose.foundation.lazy.LazyColumn
1212
import androidx.compose.foundation.lazy.items
13+
import androidx.compose.foundation.lazy.rememberLazyListState
1314
import androidx.compose.material3.HorizontalDivider
1415
import androidx.compose.material3.Icon
1516
import androidx.compose.material3.IconButton
@@ -24,6 +25,7 @@ import androidx.compose.ui.unit.dp
2425
import com.shub39.dharmik.bhagvad_gita.presentation.components.VerseCard
2526
import com.shub39.dharmik.bhagvad_gita.presentation.home.HomeAction
2627
import com.shub39.dharmik.bhagvad_gita.presentation.home.HomeState
28+
import com.shub39.dharmik.core.presentation.components.simpleVerticalScrollbar
2729
import com.shub39.dharmik.core.presentation.copyToClipboard
2830
import compose.icons.FontAwesomeIcons
2931
import compose.icons.fontawesomeicons.Solid
@@ -42,11 +44,14 @@ fun HomeSection(
4244
homeState: HomeState,
4345
onAction: (HomeAction) -> Unit
4446
) = Box {
47+
val listState = rememberLazyListState()
4548
val coroutineScope = rememberCoroutineScope()
4649
val clipboardManager = LocalClipboard.current
4750

4851
LazyColumn(
52+
state = listState,
4953
modifier = Modifier
54+
.simpleVerticalScrollbar(listState)
5055
.animateContentSize()
5156
.fillMaxSize(),
5257
contentPadding = PaddingValues(horizontal = 16.dp),

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/home/components/SettingsSection.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
1010
import androidx.compose.foundation.layout.padding
1111
import androidx.compose.foundation.layout.size
1212
import androidx.compose.foundation.lazy.LazyColumn
13+
import androidx.compose.foundation.lazy.rememberLazyListState
1314
import androidx.compose.material.icons.Icons
1415
import androidx.compose.material.icons.filled.Create
1516
import androidx.compose.material3.FilledTonalIconButton
@@ -34,6 +35,7 @@ import com.shub39.dharmik.core.domain.AppTheme
3435
import com.shub39.dharmik.core.domain.VerseCardState
3536
import com.shub39.dharmik.core.presentation.components.DharmikDialog
3637
import com.shub39.dharmik.core.presentation.components.SettingSlider
38+
import com.shub39.dharmik.core.presentation.components.simpleVerticalScrollbar
3739
import compose.icons.FontAwesomeIcons
3840
import compose.icons.fontawesomeicons.Brands
3941
import compose.icons.fontawesomeicons.Solid
@@ -53,13 +55,17 @@ fun SettingsSection(
5355
state: HomeState,
5456
onAction: (HomeAction) -> Unit
5557
) {
58+
val listState = rememberLazyListState()
5659
val uriHandler = LocalUriHandler.current
5760

5861
var showThemePicker by remember { mutableStateOf(false) }
5962
var showLanguagePicker by remember { mutableStateOf(false) }
6063

6164
LazyColumn(
62-
modifier = Modifier.fillMaxSize(),
65+
state = listState,
66+
modifier = Modifier
67+
.simpleVerticalScrollbar(listState)
68+
.fillMaxSize(),
6369
horizontalAlignment = Alignment.CenterHorizontally,
6470
) {
6571
item {

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/verses/Verses.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import com.shub39.dharmik.bhagvad_gita.presentation.verses.components.Commentari
4949
import com.shub39.dharmik.bhagvad_gita.presentation.verses.components.TranslationsDisplay
5050
import com.shub39.dharmik.core.domain.VerseCardState
5151
import com.shub39.dharmik.core.presentation.components.PageFill
52+
import com.shub39.dharmik.core.presentation.components.simpleVerticalScrollbar
5253
import com.shub39.dharmik.core.presentation.copyToClipboard
5354
import compose.icons.FontAwesomeIcons
5455
import compose.icons.fontawesomeicons.Solid
@@ -204,6 +205,7 @@ fun Verses(
204205
LazyColumn(
205206
state = scrollState,
206207
modifier = Modifier
208+
.simpleVerticalScrollbar(scrollState)
207209
.fillMaxSize(),
208210
contentPadding = PaddingValues(16.dp),
209211
verticalArrangement = Arrangement.spacedBy(16.dp)

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/verses/components/CommentariesDisplay.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fun CommentariesDisplay(
6565
if (!value.isNullOrEmpty()) {
6666
DialogPeek(
6767
title = key,
68-
translation = value,
68+
content = value,
6969
onCopy = onCopy,
7070
fontSize = fontSize
7171
)

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/verses/components/DialogPeek.kt

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import androidx.compose.foundation.layout.padding
99
import androidx.compose.foundation.layout.size
1010
import androidx.compose.foundation.rememberScrollState
1111
import androidx.compose.foundation.verticalScroll
12+
import androidx.compose.material3.ExperimentalMaterial3Api
13+
import androidx.compose.material3.FilledTonalIconButton
1214
import androidx.compose.material3.Icon
1315
import androidx.compose.material3.IconButton
1416
import androidx.compose.material3.MaterialTheme
17+
import androidx.compose.material3.ModalBottomSheet
1518
import androidx.compose.material3.Text
1619
import androidx.compose.runtime.Composable
1720
import androidx.compose.runtime.getValue
@@ -23,16 +26,16 @@ import androidx.compose.ui.Modifier
2326
import androidx.compose.ui.text.font.FontWeight
2427
import androidx.compose.ui.unit.dp
2528
import androidx.compose.ui.unit.sp
26-
import com.shub39.dharmik.core.presentation.components.DharmikDialog
2729
import compose.icons.FontAwesomeIcons
2830
import compose.icons.fontawesomeicons.Solid
2931
import compose.icons.fontawesomeicons.solid.Clipboard
3032
import compose.icons.fontawesomeicons.solid.Eye
3133

34+
@OptIn(ExperimentalMaterial3Api::class)
3235
@Composable
3336
fun DialogPeek(
3437
title: String,
35-
translation: String,
38+
content: String,
3639
onCopy: (String) -> Unit,
3740
fontSize: Float = 16f
3841
) {
@@ -65,28 +68,27 @@ fun DialogPeek(
6568
}
6669

6770
if (showComplete) {
68-
DharmikDialog(
71+
ModalBottomSheet (
6972
onDismissRequest = { showComplete = false }
7073
) {
71-
val scrollState = rememberScrollState()
72-
7374
Column(
74-
modifier = Modifier
75-
.verticalScroll(scrollState)
76-
.padding(16.dp)
75+
modifier = Modifier.verticalScroll(rememberScrollState())
7776
) {
7877
Row(
79-
modifier = Modifier.fillMaxWidth(),
80-
verticalAlignment = Alignment.CenterVertically,
81-
horizontalArrangement = Arrangement.SpaceBetween
78+
modifier = Modifier
79+
.padding(horizontal = 32.dp)
80+
.fillMaxWidth(),
81+
verticalAlignment = Alignment.CenterVertically
8282
) {
8383
Text(
8484
text = title,
85-
style = MaterialTheme.typography.titleMedium
85+
color = MaterialTheme.colorScheme.primary,
86+
style = MaterialTheme.typography.titleLarge,
87+
modifier = Modifier.weight(1f)
8688
)
8789

88-
IconButton(
89-
onClick = { onCopy(translation) }
90+
FilledTonalIconButton (
91+
onClick = { onCopy(content) }
9092
) {
9193
Icon(
9294
imageVector = FontAwesomeIcons.Solid.Clipboard,
@@ -96,14 +98,13 @@ fun DialogPeek(
9698
}
9799
}
98100

99-
Spacer(modifier = Modifier.padding(8.dp))
100-
101101
Text(
102-
text = translation,
102+
text = content,
103103
fontSize = fontSize.sp,
104-
style = MaterialTheme.typography.bodyLarge,
105-
fontWeight = FontWeight.Bold
104+
modifier = Modifier.padding(horizontal = 32.dp, vertical = 16.dp)
106105
)
106+
107+
Spacer(modifier = Modifier.padding(16.dp))
107108
}
108109
}
109110
}

composeApp/src/commonMain/kotlin/com/shub39/dharmik/bhagvad_gita/presentation/verses/components/TranslationsDisplay.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fun TranslationsDisplay(
5353
if (value.isNotEmpty()) {
5454
DialogPeek(
5555
title = key,
56-
translation = value,
56+
content = value,
5757
onCopy = onCopy,
5858
fontSize = fontSize
5959
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.shub39.dharmik.core.presentation.components
2+
3+
import androidx.compose.animation.core.Spring.StiffnessMediumLow
4+
import androidx.compose.animation.core.animateFloatAsState
5+
import androidx.compose.animation.core.spring
6+
import androidx.compose.animation.core.tween
7+
import androidx.compose.foundation.lazy.LazyListState
8+
import androidx.compose.material3.MaterialTheme
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.runtime.getValue
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.draw.drawWithContent
13+
import androidx.compose.ui.geometry.CornerRadius
14+
import androidx.compose.ui.geometry.Offset
15+
import androidx.compose.ui.geometry.Size
16+
import androidx.compose.ui.unit.Dp
17+
import androidx.compose.ui.unit.dp
18+
19+
@Composable
20+
fun Modifier.simpleVerticalScrollbar(
21+
state: LazyListState,
22+
width: Dp = 4.dp,
23+
): Modifier {
24+
25+
val targetAlpha = if (state.isScrollInProgress) .7f else 0f
26+
val duration = if (state.isScrollInProgress) 150 else 1000
27+
28+
val alpha by animateFloatAsState(
29+
targetValue = targetAlpha,
30+
animationSpec = tween(durationMillis = duration)
31+
)
32+
33+
val firstIndex by animateFloatAsState(
34+
targetValue = state.layoutInfo.visibleItemsInfo.firstOrNull()?.index?.toFloat() ?: 0f,
35+
animationSpec = spring(stiffness = StiffnessMediumLow)
36+
)
37+
38+
val lastIndex by animateFloatAsState(
39+
targetValue = state.layoutInfo.visibleItemsInfo.lastOrNull()?.index?.toFloat() ?: 0f,
40+
animationSpec = spring(stiffness = StiffnessMediumLow)
41+
)
42+
43+
val color = MaterialTheme.colorScheme.primary
44+
45+
return drawWithContent {
46+
drawContent()
47+
48+
val itemsCount = state.layoutInfo.totalItemsCount
49+
50+
if (itemsCount > 0 && alpha > 0f) {
51+
val scrollbarTop = firstIndex / itemsCount * size.height
52+
val scrollBottom = (lastIndex + 1f) / itemsCount * size.height
53+
val scrollbarHeight = scrollBottom - scrollbarTop
54+
drawRoundRect(
55+
cornerRadius = CornerRadius(width.toPx() / 2, width.toPx() / 2),
56+
color = color,
57+
topLeft = Offset(size.width - width.toPx(), scrollbarTop),
58+
size = Size(width.toPx(), scrollbarHeight),
59+
alpha = alpha
60+
)
61+
}
62+
}
63+
}

composeApp/src/desktopMain/kotlin/com/shub39/dharmik/main.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.shub39.dharmik
22

3+
import androidx.compose.ui.unit.DpSize
4+
import androidx.compose.ui.unit.dp
35
import androidx.compose.ui.window.WindowState
46
import androidx.compose.ui.window.singleWindowApplication
57
import com.shub39.dharmik.app.App
@@ -9,7 +11,7 @@ fun main() {
911
initKoin()
1012

1113
singleWindowApplication(
12-
state = WindowState(),
14+
state = WindowState(size = DpSize(width = 700.dp, height = 700.dp)),
1315
title = "Dharmik"
1416
) {
1517
App()

0 commit comments

Comments
 (0)