Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD/#28] CustomShadow 구현 #29

Merged
merged 4 commits into from
Jul 10, 2024
Merged
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
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {

// Compose Preview
debugImplementation(libs.compose.ui.tooling)
implementation(libs.androidx.ui.tooling.preview)

// Test Dependency
testImplementation(libs.junit)
Expand Down
63 changes: 63 additions & 0 deletions core/src/main/java/com/terning/core/extension/Modifier.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package com.terning.core.extension

import android.annotation.SuppressLint
import android.graphics.BlurMaskFilter
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.terning.core.designsystem.theme.Grey200

@SuppressLint("ModifierFactoryUnreferencedReceiver")
inline fun Modifier.noRippleClickable(crossinline onClick: () -> Unit): Modifier = composed {
Expand All @@ -25,3 +36,55 @@ fun Modifier.addFocusCleaner(focusManager: FocusManager): Modifier {
})
}
}

@Composable
fun Modifier.customShadow(
color: Color = Grey200,
shadowRadius: Dp = 0.dp,
shadowWidth: Dp = 2.dp,
offsetX: Dp = 0.dp,
offsetY: Dp = 0.dp,
) = composed {
val paint: Paint = remember { Paint() }
val density = LocalDensity.current
val radius = shadowRadius + shadowWidth
val blurRadiusPx = with(density) { radius.toPx() }
val maskFilter = remember {
BlurMaskFilter(blurRadiusPx, BlurMaskFilter.Blur.NORMAL)
}
drawBehind {
drawIntoCanvas { canvas ->
val frameworkPaint = paint.asFrameworkPaint()
if (radius != 0.dp) {
frameworkPaint.maskFilter = maskFilter
}
frameworkPaint.color = color.toArgb()

val leftPixel = offsetX.toPx()
val topPixel = offsetY.toPx()
val rightPixel = size.width + leftPixel
val bottomPixel = size.height + topPixel

if (radius > 0.dp) {
val radiusPx = radius.toPx()
canvas.drawRoundRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
radiusX = radiusPx,
radiusY = radiusPx,
paint = paint,
)
} else {
canvas.drawRect(
left = leftPixel,
top = topPixel,
right = rightPixel,
bottom = bottomPixel,
paint = paint,
)
}
}
}
}
Loading