Created
May 23, 2021 15:40
-
-
Save Skaldebane/4a8e1499bf0b00834f421bea8111d77f to your computer and use it in GitHub Desktop.
Initial testing for a font viewer for desktop, using Jetpack Compose - Skaldebane.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.desktop.Window | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.material.* | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.text.font.FontFamily | |
import androidx.compose.ui.text.platform.Font | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
import java.io.File | |
import kotlin.math.pow | |
fun main() = Window { | |
var text by remember { mutableStateOf("Hello, World!") } | |
var cwd by remember { mutableStateOf(File(System.getProperty("user.home"))) } | |
var fontFamily: FontFamily by remember { mutableStateOf(FontFamily.Default) } | |
var fontSize by remember { mutableStateOf(14.sp) } | |
MaterialTheme { | |
Row { | |
Column(modifier = Modifier.weight(1f, true)) { | |
OutlinedTextField( | |
value = text, | |
onValueChange = { | |
text = it | |
}, | |
label = { Text("Test your font:") }, | |
textStyle = TextStyle( | |
fontFamily = fontFamily, | |
fontSize = fontSize | |
), | |
modifier = Modifier | |
.padding(12.dp), | |
maxLines = 5 | |
) | |
Slider( | |
value = fontSize.value, | |
onValueChange = { | |
fontSize = it.sp | |
}, | |
valueRange = 1f..100f, | |
modifier = Modifier | |
) | |
} | |
Divider( | |
modifier = Modifier | |
.padding(12.dp) | |
.fillMaxHeight() | |
.width(1.dp), | |
color = Color.LightGray | |
) | |
Column(modifier = Modifier.weight(1f, true)) { | |
Text(cwd.path) | |
LazyColumn(Modifier.fillMaxWidth()) { | |
item { | |
Text( | |
text = "..", | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(8.dp) | |
.clickable { | |
cwd = cwd.parentFile | |
} | |
) | |
} | |
items(cwd.listFiles()!!.requireNoNulls()) { file -> | |
Row( | |
modifier = Modifier.clickable { | |
if (file.isDirectory) { | |
cwd = file | |
} else { | |
fontFamily = FontFamily(Font(file)) | |
} | |
} | |
) { | |
Text( | |
text = file.name, | |
modifier = Modifier | |
.padding(8.dp) | |
.weight(1f) | |
) | |
Text( | |
text = if (file.isFile) file.size() else "", | |
modifier = Modifier.padding(8.dp) | |
) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
fun File.size(): String { | |
val bytes = this.length(); | |
return when { | |
bytes <= 1024 -> | |
"$bytes b" | |
bytes <= 1024f.pow(2) -> | |
"${(bytes/1024f)} Mb" | |
bytes <= 1024f.pow(3) -> | |
"${bytes/1024f.pow(2)} Gb" | |
else -> | |
"${bytes/1024f.pow(3)} Tb" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment