Skip to content

Instantly share code, notes, and snippets.

Avatar

Zach Klippenstein zach-klippenstein

View GitHub Profile
@zach-klippenstein
zach-klippenstein / ComicBookApp.kt
Created Apr 12, 2022
Demo app with a modifier that renders its node as comic book-style dots.
View ComicBookApp.kt
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Checkbox
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
@zach-klippenstein
zach-klippenstein / comic-book-shader.sksl
Last active Apr 12, 2022
SkSL shader program to render a comic-book style effect.
View comic-book-shader.sksl
// Paste this code into shaders.skia.org
float tileSize = 12;
float dotMinDiameter = 9;
vec2 maxRedShift = vec2(-2, 3);
vec2 maxGreenShift = vec2(2.5, 0);
vec2 maxBlueShift = vec2(1, -2);
half3 baseColor = half3(0, 0, 0);
half4 main(vec2 fragcoord) {
View SaturateModifier.kt
fun Modifier.saturate(saturation: Float): Modifier =
drawWithCache {
// Cache the paint object so it can be reused between draw calls.
val contentPaint = Paint().apply {
colorFilter = ColorFilter.saturate(saturation)
}
onDrawWithContent {
drawIntoCanvas {
it.saveLayer(Rect(Offset.Zero, size), contentPaint)
@zach-klippenstein
zach-klippenstein / ChipInput.kt
Last active Jan 25, 2022
Sketch of a possible chip input implementation
View ChipInput.kt
@Composable fun <C> ChipField(
chips: List<C>,
chipContent: @Composable (C) -> Unit.
modifier: Modifier = Modifier
) {
SelectionContainer(
allowCursor = true,
// some sort of callback or hoisted state for reading/mutating selection
modifier.textFieldFocusable()
) {
@zach-klippenstein
zach-klippenstein / Blockify.kt
Last active Feb 5, 2022
A Compose modifier to turn your apps into blocks (no, this has nothing to do with NFTs)
View Blockify.kt
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.integration.demos.BlockFilter.Companion.Lighting
@zach-klippenstein
zach-klippenstein / ContextConfigThemeWrapper.kt
Created May 13, 2021
Custom fork of ContextThemeWrapper that does some extra stuff to support overriding configurations better.
View ContextConfigThemeWrapper.kt
/**
* Fork of AppCompat ContextThemeWrapper (barely) to correctly support applying override
* configurations – all the features we don't need are dropped, it always extends the base context's
* theme, the theme can't be set explicitly, and it adds one critical piece of functionality: the new
* theme is [rebased][Theme.rebase] after being cloned from the base context's theme.
*/
open class ContextConfigThemeWrapper(
base: Context,
private val overrideConfiguration: Configuration
) : ContextWrapper(base) {
@zach-klippenstein
zach-klippenstein / LICENSE
Last active Jun 15, 2022
iOS-style segmented control in Compose
View LICENSE
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
@zach-klippenstein
zach-klippenstein / DebugComposeBounds.kt
Last active Jun 28, 2021
Proof-of-concept of a composable that draws information about all its children on top of them.
View DebugComposeBounds.kt
@Composable fun App() {
DebugBounds {
Column(Modifier.background(Color.White).fillMaxSize()) {
BasicText("Some text")
Spacer(Modifier.size(10.dp))
BasicText("More text")
Spacer(Modifier.size(5.dp))
BasicText("Button", Modifier
.clickable { }
.background(Color.Blue, RoundedCornerShape(3.dp))
@zach-klippenstein
zach-klippenstein / ComposableDumper.kt
Last active Feb 1, 2021
Simple utility to dump the current Composition tree to logcat.
View ComposableDumper.kt
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.IntBounds
import androidx.ui.tooling.CallGroup
import androidx.ui.tooling.Group
import androidx.ui.tooling.NodeGroup
import androidx.ui.tooling.asTree