|
| 1 | +package io.github.composegears.valkyrie.ui.common.picker |
| 2 | + |
| 3 | +import androidx.compose.animation.core.animateDpAsState |
| 4 | +import androidx.compose.desktop.ui.tooling.preview.Preview |
| 5 | +import androidx.compose.foundation.background |
| 6 | +import androidx.compose.foundation.layout.Arrangement |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.BoxScope |
| 9 | +import androidx.compose.foundation.layout.Column |
| 10 | +import androidx.compose.foundation.layout.ExperimentalLayoutApi |
| 11 | +import androidx.compose.foundation.layout.FlowRow |
| 12 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 13 | +import androidx.compose.foundation.layout.heightIn |
| 14 | +import androidx.compose.foundation.layout.padding |
| 15 | +import androidx.compose.material3.Icon |
| 16 | +import androidx.compose.material3.LocalContentColor |
| 17 | +import androidx.compose.material3.MaterialTheme |
| 18 | +import androidx.compose.material3.Text |
| 19 | +import androidx.compose.material3.TextButton |
| 20 | +import androidx.compose.runtime.Composable |
| 21 | +import androidx.compose.runtime.getValue |
| 22 | +import androidx.compose.runtime.rememberCoroutineScope |
| 23 | +import androidx.compose.ui.Alignment |
| 24 | +import androidx.compose.ui.Modifier |
| 25 | +import androidx.compose.ui.draw.clip |
| 26 | +import androidx.compose.ui.graphics.Color |
| 27 | +import androidx.compose.ui.text.style.TextAlign |
| 28 | +import androidx.compose.ui.unit.dp |
| 29 | +import io.github.composegears.valkyrie.ui.common.picker.PickerEvent.PickDirectory |
| 30 | +import io.github.composegears.valkyrie.ui.common.picker.PickerEvent.PickFiles |
| 31 | +import io.github.composegears.valkyrie.ui.foundation.CenterVerticalRow |
| 32 | +import io.github.composegears.valkyrie.ui.foundation.VerticalSpacer |
| 33 | +import io.github.composegears.valkyrie.ui.foundation.WeightSpacer |
| 34 | +import io.github.composegears.valkyrie.ui.foundation.dashedBorder |
| 35 | +import io.github.composegears.valkyrie.ui.foundation.disabled |
| 36 | +import io.github.composegears.valkyrie.ui.foundation.icons.AddFile |
| 37 | +import io.github.composegears.valkyrie.ui.foundation.icons.ValkyrieIcons |
| 38 | +import io.github.composegears.valkyrie.ui.foundation.rememberMutableState |
| 39 | +import io.github.composegears.valkyrie.ui.foundation.theme.PreviewTheme |
| 40 | +import io.github.composegears.valkyrie.ui.platform.ClipboardDataType |
| 41 | +import io.github.composegears.valkyrie.ui.platform.Os |
| 42 | +import io.github.composegears.valkyrie.ui.platform.picker.rememberDirectoryPicker |
| 43 | +import io.github.composegears.valkyrie.ui.platform.picker.rememberMultipleFilesPicker |
| 44 | +import io.github.composegears.valkyrie.ui.platform.rememberCurrentOs |
| 45 | +import io.github.composegears.valkyrie.ui.platform.rememberMultiSelectDragAndDropHandler |
| 46 | +import io.github.composegears.valkyrie.ui.screen.mode.iconpack.conversion.ui.ClipboardEventColumn |
| 47 | +import io.github.composegears.valkyrie.util.stringResource |
| 48 | +import java.nio.file.Path |
| 49 | +import kotlin.io.path.isDirectory |
| 50 | +import kotlin.io.path.isRegularFile |
| 51 | +import kotlinx.coroutines.launch |
| 52 | + |
| 53 | +@Composable |
| 54 | +fun UniversalPicker( |
| 55 | + onPickerEvent: (PickerEvent) -> Unit, |
| 56 | + modifier: Modifier = Modifier, |
| 57 | + headerSection: @Composable () -> Unit, |
| 58 | +) { |
| 59 | + val scope = rememberCoroutineScope() |
| 60 | + |
| 61 | + val multipleFilePicker = rememberMultipleFilesPicker() |
| 62 | + val directoryPicker = rememberDirectoryPicker() |
| 63 | + |
| 64 | + ClipboardEventColumn( |
| 65 | + modifier = modifier, |
| 66 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 67 | + onPaste = { dataType -> |
| 68 | + when (dataType) { |
| 69 | + is ClipboardDataType.Files -> onPickerEvent(PickFiles(paths = dataType.paths)) |
| 70 | + is ClipboardDataType.Text -> onPickerEvent(PickerEvent.ClipboardText(dataType.text)) |
| 71 | + } |
| 72 | + }, |
| 73 | + ) { |
| 74 | + headerSection() |
| 75 | + WeightSpacer(weight = 0.3f) |
| 76 | + SelectableState( |
| 77 | + onSelectPath = { paths -> |
| 78 | + when { |
| 79 | + paths.size == 1 -> { |
| 80 | + val path = paths.first() |
| 81 | + |
| 82 | + when { |
| 83 | + path.isDirectory() -> onPickerEvent(PickDirectory(path = path)) |
| 84 | + path.isRegularFile() -> onPickerEvent(PickFiles(paths = paths)) |
| 85 | + } |
| 86 | + } |
| 87 | + else -> onPickerEvent(PickFiles(paths = paths)) |
| 88 | + } |
| 89 | + }, |
| 90 | + onPickDirectory = { |
| 91 | + scope.launch { |
| 92 | + val path = directoryPicker.launch() |
| 93 | + |
| 94 | + if (path != null) { |
| 95 | + onPickerEvent(PickDirectory(path = path)) |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + onPickFiles = { |
| 100 | + scope.launch { |
| 101 | + val paths = multipleFilePicker.launch() |
| 102 | + |
| 103 | + if (paths.isNotEmpty()) { |
| 104 | + onPickerEvent(PickFiles(paths = paths)) |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + ) |
| 109 | + WeightSpacer(weight = 0.7f) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +@OptIn(ExperimentalLayoutApi::class) |
| 114 | +@Composable |
| 115 | +private fun SelectableState( |
| 116 | + onPickDirectory: () -> Unit, |
| 117 | + onPickFiles: () -> Unit, |
| 118 | + onSelectPath: (List<Path>) -> Unit, |
| 119 | +) { |
| 120 | + val dragAndDropHandler = rememberMultiSelectDragAndDropHandler(onDrop = onSelectPath) |
| 121 | + val isDragging by rememberMutableState(dragAndDropHandler.isDragging) { dragAndDropHandler.isDragging } |
| 122 | + |
| 123 | + val os = rememberCurrentOs() |
| 124 | + |
| 125 | + DragAndDropBox(isDragging = isDragging) { |
| 126 | + Column(horizontalAlignment = Alignment.CenterHorizontally) { |
| 127 | + CenterVerticalRow { |
| 128 | + Icon( |
| 129 | + imageVector = ValkyrieIcons.AddFile, |
| 130 | + contentDescription = null, |
| 131 | + ) |
| 132 | + Text( |
| 133 | + modifier = Modifier.padding(8.dp), |
| 134 | + text = stringResource("picker.dnd"), |
| 135 | + textAlign = TextAlign.Center, |
| 136 | + style = MaterialTheme.typography.titleSmall, |
| 137 | + ) |
| 138 | + } |
| 139 | + Text( |
| 140 | + text = when (os) { |
| 141 | + Os.MacOS -> stringResource("picker.clipboard.mac") |
| 142 | + else -> stringResource("picker.clipboard.other") |
| 143 | + }, |
| 144 | + textAlign = TextAlign.Center, |
| 145 | + color = LocalContentColor.current.disabled(), |
| 146 | + style = MaterialTheme.typography.labelSmall, |
| 147 | + ) |
| 148 | + VerticalSpacer(16.dp) |
| 149 | + Text( |
| 150 | + text = stringResource("picker.dnd.or"), |
| 151 | + style = MaterialTheme.typography.labelMedium, |
| 152 | + ) |
| 153 | + FlowRow( |
| 154 | + modifier = Modifier.fillMaxWidth(), |
| 155 | + horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally), |
| 156 | + ) { |
| 157 | + TextButton(onClick = onPickDirectory) { |
| 158 | + Text(text = stringResource("picker.pick.directory")) |
| 159 | + } |
| 160 | + TextButton(onClick = onPickFiles) { |
| 161 | + Text(text = stringResource("picker.pick.files")) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +@Composable |
| 169 | +private fun DragAndDropBox( |
| 170 | + isDragging: Boolean, |
| 171 | + modifier: Modifier = Modifier, |
| 172 | + content: @Composable BoxScope.() -> Unit, |
| 173 | +) { |
| 174 | + val dashColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.5f) |
| 175 | + val border by animateDpAsState(if (isDragging) 4.dp else 1.dp) |
| 176 | + |
| 177 | + Box( |
| 178 | + modifier = modifier |
| 179 | + .fillMaxWidth(0.8f) |
| 180 | + .heightIn(min = 300.dp) |
| 181 | + .clip(MaterialTheme.shapes.small) |
| 182 | + .dashedBorder( |
| 183 | + strokeWidth = border, |
| 184 | + gapWidth = 8.dp, |
| 185 | + dashWidth = 8.dp, |
| 186 | + color = dashColor, |
| 187 | + shape = MaterialTheme.shapes.small, |
| 188 | + ) |
| 189 | + .padding(2.dp) |
| 190 | + .background( |
| 191 | + color = when { |
| 192 | + isDragging -> MaterialTheme.colorScheme.primary.copy(alpha = 0.05f) |
| 193 | + else -> Color.Transparent |
| 194 | + }, |
| 195 | + shape = MaterialTheme.shapes.small, |
| 196 | + ), |
| 197 | + contentAlignment = Alignment.Center, |
| 198 | + content = content, |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +@Preview |
| 203 | +@Composable |
| 204 | +private fun UniversalPickerPreview() = PreviewTheme { |
| 205 | + UniversalPicker( |
| 206 | + onPickerEvent = {}, |
| 207 | + headerSection = {}, |
| 208 | + ) |
| 209 | +} |
0 commit comments