-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from skiptools/redacted
Support .redacted(reason: .placeholder)
- Loading branch information
Showing
6 changed files
with
162 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 Skip | ||
// | ||
// This is free software: you can redistribute and/or modify it | ||
// under the terms of the GNU Lesser General Public License 3.0 | ||
// as published by the Free Software Foundation https://fsf.org | ||
|
||
package skip.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.drawWithContent | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.ColorFilter | ||
import androidx.compose.ui.graphics.ColorMatrix | ||
import androidx.compose.ui.graphics.Paint | ||
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.Dp | ||
import kotlin.math.ceil | ||
|
||
/// Compose the given content with opaque redaction treatment. | ||
@Composable fun Redacted(context: ComposeContext, color: Color, content: @Composable (ComposeContext) -> Unit) { | ||
val contentContext = context.content() | ||
val redactedContext = context.content(modifier = Modifier | ||
.drawWithContent { | ||
val matrix = redactedColorMatrix(color) | ||
val filter = ColorFilter.colorMatrix(matrix) | ||
val paint = Paint().apply { | ||
colorFilter = filter | ||
} | ||
drawIntoCanvas { canvas -> | ||
canvas.saveLayer(Rect(0f, 0f, size.width, size.height), paint) | ||
drawContent() | ||
canvas.restore() | ||
} | ||
} | ||
) | ||
content(redactedContext) | ||
} | ||
|
||
private fun redactedColorMatrix(color: Color): ColorMatrix { | ||
return ColorMatrix().apply { | ||
set(0, 0, 0f) // Do not preserve original R | ||
set(1, 1, 0f) // Do not preserve original G | ||
set(2, 2, 0f) // Do not preserve original B | ||
|
||
set(0, 4, color.red * 255) // Use given color's R | ||
set(1, 4, color.green * 255) // Use given color's G | ||
set(2, 4, color.blue * 255) // Use given color's B | ||
set(3, 3, color.alpha) // Multiply original alpha by shadow color alpha | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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