Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/src/main/java/dev/cluvex/strata/core/util/FileKind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.InsertDriveFile
import androidx.compose.material.icons.automirrored.rounded.TextSnippet
import androidx.compose.material.icons.rounded.Android
import androidx.compose.material.icons.rounded.Article
import androidx.compose.material.icons.rounded.Audiotrack
import androidx.compose.material.icons.rounded.Code
import androidx.compose.material.icons.rounded.DataObject
import androidx.compose.material.icons.rounded.Description
import androidx.compose.material.icons.rounded.Difference
import androidx.compose.material.icons.rounded.Folder
import androidx.compose.material.icons.rounded.FolderZip
import androidx.compose.material.icons.rounded.FontDownload
import androidx.compose.material.icons.rounded.Image
import androidx.compose.material.icons.rounded.Lock
import androidx.compose.material.icons.rounded.Movie
import androidx.compose.material.icons.rounded.Notes
import androidx.compose.material.icons.rounded.PictureAsPdf
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material.icons.rounded.TableChart
import androidx.compose.material.icons.rounded.Tune
import androidx.compose.ui.graphics.vector.ImageVector
import dev.cluvex.strata.core.model.FileItem

Expand Down Expand Up @@ -58,6 +65,22 @@ enum class FileKind {
}
}

/**
* Finer-grained flavours of [FileKind.TEXT]: the kind decides how the file is opened, while the
* variant only changes how it looks in a listing. Everything in the text family used to share one
* glyph and one hue, which made `notes.txt`, `README.md` and `config.json` indistinguishable.
*/
enum class FileVariant(val icon: ImageVector) {
MARKDOWN(Icons.Rounded.Article),
JSON(Icons.Rounded.DataObject),
XML(Icons.Rounded.Code),
YAML(Icons.Rounded.Settings),
CSV(Icons.Rounded.TableChart),
LOG(Icons.Rounded.Notes),
CONFIG(Icons.Rounded.Tune),
DIFF(Icons.Rounded.Difference),
}

object FileKinds {

private val image = setOf("png", "jpg", "jpeg", "gif", "webp", "bmp", "heic", "heif", "ico", "svg")
Expand All @@ -75,10 +98,30 @@ object FileKinds {
private val text = setOf(
"txt", "md", "json", "xml", "yml", "yaml", "toml", "ini", "cfg", "conf",
"properties", "prop", "log", "csv", "tsv", "mtl", "mtsx", "diff", "patch",
"markdown", "mdown", "mkd", "mdx", "json5", "jsonc", "ndjson", "plist",
)
private val font = setOf("ttf", "otf", "woff", "woff2", "ttc")
private val document = setOf("doc", "docx", "xls", "xlsx", "ppt", "pptx", "odt", "rtf", "epub")

private val variants: Map<String, FileVariant> = buildMap {
fun putAll(variant: FileVariant, vararg extensions: String) = extensions.forEach { put(it, variant) }
putAll(FileVariant.MARKDOWN, "md", "markdown", "mdown", "mkd", "mdx")
putAll(FileVariant.JSON, "json", "json5", "jsonc", "ndjson")
putAll(FileVariant.XML, "xml", "xsd", "xsl", "xslt", "plist")
putAll(FileVariant.YAML, "yml", "yaml")
putAll(FileVariant.CSV, "csv", "tsv")
putAll(FileVariant.LOG, "log")
putAll(FileVariant.CONFIG, "ini", "cfg", "conf", "properties", "prop", "toml")
putAll(FileVariant.DIFF, "diff", "patch")
}

/** The look of a file inside [FileKind.TEXT], or null when the generic text glyph is right. */
fun variantOf(item: FileItem): FileVariant? {
if (item.isDirectory) return null
if (of(item) != FileKind.TEXT) return null
return variants[item.extension]
}

fun of(item: FileItem): FileKind {
if (item.isDirectory) return FileKind.FOLDER
val ext = item.extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,14 @@ private fun KindIcon(item: FileItem, iconSize: Dp, shape: Shape) {
val colors = MaterialTheme.colorScheme
val settings = LocalSettings.current
val kind = FileKinds.of(item)
val tile = KindPalette.colors(kind, settings.appearance.colorfulIcons, colors, colors.background.luminance() < 0.5f)
val variant = FileKinds.variantOf(item)
val tile = KindPalette.colors(
kind,
settings.appearance.colorfulIcons,
colors,
colors.background.luminance() < 0.5f,
variant?.let { KindPalette.hue(it) },
)
Box(
Modifier
.size(iconSize)
Expand All @@ -407,7 +414,7 @@ private fun KindIcon(item: FileItem, iconSize: Dp, shape: Shape) {
contentAlignment = Alignment.Center,
) {
Icon(
kind.icon,
variant?.icon ?: kind.icon,
contentDescription = stringResource(kind.label),
tint = tile.content,
modifier = Modifier.padding(iconSize * 0.22f),
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/dev/cluvex/strata/ui/theme/KindColors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.ui.graphics.Color
import com.materialkolor.palettes.TonalPalette
import dev.cluvex.strata.core.util.FileKind
import dev.cluvex.strata.core.util.FileVariant

data class KindColors(val container: Color, val content: Color)

Expand All @@ -24,15 +25,26 @@ object KindPalette {
FileKind.ENCRYPTED -> 320.0
}

private val cache = HashMap<Pair<FileKind, Boolean>, KindColors>()
/** Hues for the text flavours, spread out so neighbouring rows never share a colour. */
fun hue(variant: FileVariant): Double = when (variant) {
FileVariant.MARKDOWN -> 210.0
FileVariant.JSON -> 45.0
FileVariant.XML -> 250.0
FileVariant.YAML -> 172.0
FileVariant.CSV -> 120.0
FileVariant.LOG -> 30.0
FileVariant.CONFIG -> 280.0
FileVariant.DIFF -> 350.0
}

private val cache = HashMap<Triple<FileKind, Double, Boolean>, KindColors>()

fun colors(kind: FileKind, colorful: Boolean, scheme: ColorScheme, dark: Boolean): KindColors {
fun colors(kind: FileKind, colorful: Boolean, scheme: ColorScheme, dark: Boolean, hueOverride: Double? = hue(kind)): KindColors {
if (kind == FileKind.FOLDER) return KindColors(scheme.primaryContainer, scheme.onPrimaryContainer)
val hue = hue(kind)
if (!colorful || hue == null) return KindColors(scheme.surfaceContainerHighest, scheme.onSurfaceVariant)
if (!colorful || hueOverride == null) return KindColors(scheme.surfaceContainerHighest, scheme.onSurfaceVariant)
return synchronized(cache) {
cache.getOrPut(kind to dark) {
val palette = TonalPalette.fromHueAndChroma(hue, 40.0)
cache.getOrPut(Triple(kind, hueOverride, dark)) {
val palette = TonalPalette.fromHueAndChroma(hueOverride, 40.0)
if (dark) KindColors(Color(palette.tone(30)), Color(palette.tone(90)))
else KindColors(Color(palette.tone(90)), Color(palette.tone(30)))
}
Expand Down