Skip to content

Commit

Permalink
Perform drawing only when size threshold is met
Browse files Browse the repository at this point in the history
Otherwise drawing operations such as `Canvas.drawRect` crash as the code passes rectangles with negative sizes. Closes #16
  • Loading branch information
kirill-grouchnikov committed Jan 31, 2022
1 parent 201b8f9 commit 43627f7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 79 deletions.
Expand Up @@ -196,6 +196,10 @@ internal fun AuroraIndeterminateLinearProgress(
) {
val valComplete = progress * (2 * size.height + 1)
val radius = 1.5f.dp.toPx()
if ((size.width <= radius) || (size.height <= radius)) {
// Size too small to do any meaningful painting
return@Canvas
}

withTransform({
clipPath(Path().also {
Expand Down Expand Up @@ -309,6 +313,10 @@ internal fun AuroraDeterminateLinearProgress(
)
) {
val radius = 1.5f.dp.toPx()
if ((size.width <= radius) || (size.height <= radius)) {
// Size too small to do any meaningful painting
return@Canvas
}

withTransform({
clipPath(Path().also {
Expand Down
Expand Up @@ -269,6 +269,10 @@ internal fun AuroraTextField(
Box {
Canvas(modifier = Modifier.matchParentSize()) {
val borderStrokeWidth = 1.0f
if ((size.width <= borderStrokeWidth) || (size.height <= borderStrokeWidth)) {
// Size too small to do any meaningful painting
return@Canvas
}

// Read-only text fields use the regular background fill. Editable text fields
// use text background fill (with rollover and focused transitions)
Expand Down
Expand Up @@ -325,86 +325,89 @@ internal fun Modifier.drawUndecoratedWindowBorder(
val width: Float = size.width
val height: Float = size.height
val thickness = WindowSizingConstants.DecoratedBorderThickness.toPx()
drawRect(
color = backgroundColorScheme.lightColor,
topLeft = Offset(thickness / 2.0f, thickness / 2.0f),
size = Size(width - thickness, height - thickness),
style = androidx.compose.ui.graphics.drawscope.Stroke(width = thickness)
)

val quarterThickness = thickness / 4.0f
// bottom and right in border ultra dark
drawLine(
color = borderColorScheme.ultraDarkColor,
start = Offset(x = 0f, y = height - quarterThickness / 2.0f),
end = Offset(x = width, y = height - quarterThickness / 2.0f),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.ultraDarkColor,
start = Offset(x = width - quarterThickness / 2.0f, y = 0f),
end = Offset(x = width - quarterThickness / 2.0f, y = height),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// top and left in border dark
drawLine(
color = borderColorScheme.darkColor,
start = Offset(x = 0f, y = quarterThickness / 2.0f),
end = Offset(x = width, y = quarterThickness / 2.0f),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.darkColor,
start = Offset(x = quarterThickness / 2.0f, y = 0f),
end = Offset(x = quarterThickness / 2.0f, y = height),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// inner bottom and right in background mid
drawLine(
color = borderColorScheme.midColor,
start = Offset(
x = quarterThickness,
y = height - 1.5f * quarterThickness
),
end = Offset(
x = width - quarterThickness,
y = height - 1.5f * quarterThickness
),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.midColor,
start = Offset(
x = width - 1.5f * quarterThickness,
y = quarterThickness
),
end = Offset(
x = width - 1.5f * quarterThickness,
y = height - quarterThickness
),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// inner top and left in background mid
drawLine(
color = borderColorScheme.midColor,
start = Offset(x = quarterThickness, y = 1.5f * quarterThickness),
end = Offset(x = width - quarterThickness, y = 1.5f * quarterThickness),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.midColor,
start = Offset(x = 1.5f * quarterThickness, y = quarterThickness),
end = Offset(x = 1.5f * quarterThickness, y = height - quarterThickness),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
if ((width > thickness) && (height > thickness)) {
drawRect(
color = backgroundColorScheme.lightColor,
topLeft = Offset(thickness / 2.0f, thickness / 2.0f),
size = Size(width - thickness, height - thickness),
style = androidx.compose.ui.graphics.drawscope.Stroke(width = thickness)
)

val quarterThickness = thickness / 4.0f
// bottom and right in border ultra dark
drawLine(
color = borderColorScheme.ultraDarkColor,
start = Offset(x = 0f, y = height - quarterThickness / 2.0f),
end = Offset(x = width, y = height - quarterThickness / 2.0f),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.ultraDarkColor,
start = Offset(x = width - quarterThickness / 2.0f, y = 0f),
end = Offset(x = width - quarterThickness / 2.0f, y = height),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// top and left in border dark
drawLine(
color = borderColorScheme.darkColor,
start = Offset(x = 0f, y = quarterThickness / 2.0f),
end = Offset(x = width, y = quarterThickness / 2.0f),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.darkColor,
start = Offset(x = quarterThickness / 2.0f, y = 0f),
end = Offset(x = quarterThickness / 2.0f, y = height),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// inner bottom and right in background mid
drawLine(
color = borderColorScheme.midColor,
start = Offset(
x = quarterThickness,
y = height - 1.5f * quarterThickness
),
end = Offset(
x = width - quarterThickness,
y = height - 1.5f * quarterThickness
),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.midColor,
start = Offset(
x = width - 1.5f * quarterThickness,
y = quarterThickness
),
end = Offset(
x = width - 1.5f * quarterThickness,
y = height - quarterThickness
),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
// inner top and left in background mid
drawLine(
color = borderColorScheme.midColor,
start = Offset(x = quarterThickness, y = 1.5f * quarterThickness),
end = Offset(x = width - quarterThickness, y = 1.5f * quarterThickness),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
drawLine(
color = borderColorScheme.midColor,
start = Offset(x = 1.5f * quarterThickness, y = quarterThickness),
end = Offset(x = 1.5f * quarterThickness, y = height - quarterThickness),
strokeWidth = quarterThickness,
cap = StrokeCap.Butt
)
}
}

@AuroraInternalApi
Expand Down

0 comments on commit 43627f7

Please sign in to comment.