GridMenu.kt (3859B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2024 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.compose 18 19 import androidx.annotation.DrawableRes 20 import androidx.annotation.StringRes 21 import androidx.compose.foundation.clickable 22 import androidx.compose.foundation.layout.Box 23 import androidx.compose.foundation.layout.BoxScope 24 import androidx.compose.foundation.layout.Column 25 import androidx.compose.foundation.layout.PaddingValues 26 import androidx.compose.foundation.layout.fillMaxWidth 27 import androidx.compose.foundation.layout.height 28 import androidx.compose.foundation.layout.padding 29 import androidx.compose.foundation.lazy.grid.GridCells 30 import androidx.compose.foundation.lazy.grid.LazyGridScope 31 import androidx.compose.foundation.lazy.grid.LazyVerticalGrid 32 import androidx.compose.material3.Icon 33 import androidx.compose.material3.MaterialTheme 34 import androidx.compose.material3.ShapeDefaults 35 import androidx.compose.material3.Text 36 import androidx.compose.runtime.Composable 37 import androidx.compose.ui.Alignment 38 import androidx.compose.ui.Modifier 39 import androidx.compose.ui.draw.alpha 40 import androidx.compose.ui.draw.clip 41 import androidx.compose.ui.res.painterResource 42 import androidx.compose.ui.res.stringResource 43 import androidx.compose.ui.text.style.TextAlign 44 import androidx.compose.ui.unit.dp 45 46 // Source: https://github.com/z-huang/InnerTune 47 48 val GridMenuItemHeight = 96.dp 49 50 @Composable 51 fun GridMenu( 52 modifier: Modifier = Modifier, 53 contentPadding: PaddingValues = PaddingValues(0.dp), 54 content: LazyGridScope.() -> Unit, 55 ) { 56 LazyVerticalGrid( 57 columns = GridCells.Adaptive(minSize = 100.dp), 58 modifier = modifier, 59 contentPadding = contentPadding, 60 content = content, 61 ) 62 } 63 64 fun LazyGridScope.GridMenuItem( 65 modifier: Modifier = Modifier, 66 @DrawableRes icon: Int, 67 @StringRes title: Int, 68 enabled: Boolean = true, 69 onClick: () -> Unit, 70 ) = GridMenuItem( 71 modifier = modifier, 72 icon = { 73 Icon( 74 painter = painterResource(icon), 75 contentDescription = null, 76 ) 77 }, 78 title = title, 79 enabled = enabled, 80 onClick = onClick, 81 ) 82 83 fun LazyGridScope.GridMenuItem( 84 modifier: Modifier = Modifier, 85 icon: @Composable BoxScope.() -> Unit, 86 @StringRes title: Int, 87 enabled: Boolean = true, 88 onClick: () -> Unit, 89 ) { 90 item { 91 Column( 92 modifier = 93 modifier 94 .clip(ShapeDefaults.Large) 95 .height(GridMenuItemHeight) 96 .clickable( 97 enabled = enabled, 98 onClick = onClick, 99 ).alpha(if (enabled) 1f else 0.5f) 100 .padding(12.dp), 101 ) { 102 Box( 103 modifier = 104 Modifier 105 .fillMaxWidth() 106 .weight(1f), 107 contentAlignment = Alignment.Center, 108 content = icon, 109 ) 110 Text( 111 text = stringResource(title), 112 style = MaterialTheme.typography.labelLarge, 113 textAlign = TextAlign.Center, 114 maxLines = 2, 115 modifier = Modifier.fillMaxWidth(), 116 ) 117 } 118 } 119 }