From f99ad578703b67ce6734c1ea7ee972172b1d22d9 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Mon, 16 Nov 2026 13:42:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=99=88=20=ED=99=94=EB=A9=B4=20?= =?UTF-8?q?=EC=9C=84=EC=A0=AF=20=EC=9E=90=EC=9C=A0=20=EB=A6=AC=EC=82=AC?= =?UTF-8?q?=EC=9D=B4=EC=A6=88=20=EB=B0=8F=20=EC=9D=B4=EB=8F=99=20=EC=98=A4?= =?UTF-8?q?=EB=B2=84=EB=A0=88=EC=9D=B4(WidgetResizeOverlay)=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - feeds/WidgetResizeOverlay.kt 신규 생성: * 모서리 4곳 + 상하좌우 변 4곳 총 8개 핸들 기반의 픽셀 단위 자유 크기 조절(최소 48dp, 컨테이너 경계 클램핑) * 실시간 WxH dp 크기 배지 표시 및 프레임 내부 드래그 이동, 외부 탭 시 편집 모드 해제 - LauncherActivity.kt: * 위젯 롱프레스 시 WidgetResizeOverlay 활성화(onMoveStart/onMoving/onMoveEnd/onResizeEnd) * 리사이즈 완료 시 updateWidgetOptions() 호출로 AppWidgetHostView에 새 dp 크기 동기화 및 WidgetData Realm 저장 * 저장된 위젯 복구(createWidget) 시 커스텀 크기 옵션 즉시 반영 및 onPause() 시 오버레이 자동 해제 - 검증: ./gradlew :app:compileDebugKotlin 빌드 성공 확인 (4s, 133 actionable tasks) --- .../bums/lunatic/launcher/LauncherActivity.kt | 92 ++++++-- .../launcher/feeds/WidgetResizeOverlay.kt | 222 ++++++++++++++++++ 2 files changed, 300 insertions(+), 14 deletions(-) create mode 100644 app/src/main/kotlin/bums/lunatic/launcher/feeds/WidgetResizeOverlay.kt diff --git a/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt b/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt index 26dd638d..26af4f49 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/LauncherActivity.kt @@ -510,6 +510,7 @@ open class LauncherActivity : CommonActivity() { } false } + setupWidgetResizeOverlay() HeadsetActionButtonReceiver.register(this) ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) @@ -676,6 +677,7 @@ open class LauncherActivity : CommonActivity() { override fun onPause() { super.onPause() + widgetResizeOverlay?.detach() Wearable.getMessageClient(this).removeListener(messageListener) } // 위젯 선택기 실행 @@ -753,6 +755,72 @@ open class LauncherActivity : CommonActivity() { } } + private var widgetResizeOverlay: bums.lunatic.launcher.feeds.WidgetResizeOverlay? = null + + private fun setupWidgetResizeOverlay() { + val overlay = bums.lunatic.launcher.feeds.WidgetResizeOverlay(this).apply { + visibility = View.GONE + layoutParams = FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, + FrameLayout.LayoutParams.MATCH_PARENT + ) + listener = object : bums.lunatic.launcher.feeds.WidgetResizeOverlay.Listener { + override fun onMoveStart(view: AppWidgetHostView) { + binding.deleteZone.visibility = View.VISIBLE + } + + override fun onMoving(view: AppWidgetHostView) { + if (isViewOverlapping(view, binding.deleteZone)) { + binding.deleteZone.setBackgroundResource(R.drawable.bg_circle_emoji_red) + } else { + binding.deleteZone.setBackgroundResource(R.drawable.bg_circle_emoji) + } + } + + override fun onMoveEnd(view: AppWidgetHostView) { + if (isViewOverlapping(view, binding.deleteZone)) { + detach() + deleteWidget(view) + } else { + val widgetId = view.tag as? Int ?: -1 + if (widgetId != -1) { + saveWidgetState( + widgetId, + view.x, + view.y, + view.layoutParams.width, + view.layoutParams.height, + "" + ) + } + } + binding.deleteZone.visibility = View.GONE + } + + override fun onResizeEnd(view: AppWidgetHostView) { + val widgetId = view.tag as? Int ?: -1 + if (widgetId != -1) { + updateWidgetOptions(widgetId, view) + saveWidgetState( + widgetId, + view.x, + view.y, + view.layoutParams.width, + view.layoutParams.height, + "" + ) + } + } + + override fun onDismiss() { + binding.deleteZone.visibility = View.GONE + } + } + } + widgetResizeOverlay = overlay + binding.widgetContainer.addView(overlay) + } + // 위젯 뷰 생성 및 화면 배치 private fun createWidget(data: Intent?, savedWidgetData: WidgetData? = null) { val appWidgetId = savedWidgetData?.appWidgetId ?: data?.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) ?: return @@ -777,27 +845,23 @@ open class LauncherActivity : CommonActivity() { } hostView?.tag = appWidgetId - // 드래그 리스너 등록 + // 롱프레스 시 자유 리사이즈/이동 오버레이 활성화 hostView?.setOnLongClickListener { view -> - isDraggingWidget = true - currentDragView = view - lastTouchX = 0f - lastTouchY = 0f - - // [추가] 드래그 시작 시 삭제 영역 표시 - binding.deleteZone.visibility = View.VISIBLE - - // 시각적 피드백 - view.animate().scaleX(1.05f).scaleY(1.05f).setDuration(100).start() - true + (view as? AppWidgetHostView)?.let { host -> + widgetResizeOverlay?.attachTo(host) + true + } ?: false } binding.widgetContainer.addView(hostView) + // 저장된 위젯 복구 시 저장된 커스텀 크기로 옵션 동기화 + if (savedWidgetData != null && hostView != null) { + hostView.post { updateWidgetOptions(appWidgetId, hostView) } + } + // 새로 추가된 위젯이라면 DB에 저장 if (savedWidgetData == null) { saveWidgetState(appWidgetId, hostView!!.x, hostView.y, width, height, appWidgetInfo.provider.className) - } else { - } } diff --git a/app/src/main/kotlin/bums/lunatic/launcher/feeds/WidgetResizeOverlay.kt b/app/src/main/kotlin/bums/lunatic/launcher/feeds/WidgetResizeOverlay.kt new file mode 100644 index 00000000..eacb389b --- /dev/null +++ b/app/src/main/kotlin/bums/lunatic/launcher/feeds/WidgetResizeOverlay.kt @@ -0,0 +1,222 @@ +package bums.lunatic.launcher.feeds + +import android.annotation.SuppressLint +import android.appwidget.AppWidgetHostView +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.RectF +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import kotlin.math.hypot +import kotlin.math.max +import kotlin.math.min + +/** + * Free-form 8-handle resize & move overlay for AppWidgetHostView. + * Allows independent width/height resizing via corner & edge handles, + * free dragging inside the frame, and dismisses when tapping outside. + */ +class WidgetResizeOverlay(context: Context) : View(context) { + + interface Listener { + fun onMoveStart(view: AppWidgetHostView) + fun onMoving(view: AppWidgetHostView) + fun onMoveEnd(view: AppWidgetHostView) + fun onResizeEnd(view: AppWidgetHostView) + fun onDismiss() + } + + private enum class Handle { + NONE, BODY, + TOP_LEFT, TOP, TOP_RIGHT, + RIGHT, BOTTOM_RIGHT, BOTTOM, BOTTOM_LEFT, LEFT + } + + var listener: Listener? = null + private var targetView: AppWidgetHostView? = null + + private val density = context.resources.displayMetrics.density + private val minSizePx = (48 * density).toInt() + private val handleRadius = 8f * density + private val handleTouchRadius = 28f * density + + private val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.parseColor("#4FC3F7") + style = Paint.Style.STROKE + strokeWidth = 2.5f * density + } + private val handleFillPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.WHITE + style = Paint.Style.FILL + } + private val handleStrokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.parseColor("#0288D1") + style = Paint.Style.STROKE + strokeWidth = 2f * density + } + private val badgeBgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.parseColor("#CC000000") + style = Paint.Style.FILL + } + private val badgeTextPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.WHITE + textSize = 11f * density + } + + private var activeHandle = Handle.NONE + private var lastRawX = 0f + private var lastRawY = 0f + private var startX = 0f + private var startY = 0f + private var startW = 0 + private var startH = 0 + private var isResizing = false + + fun attachTo(widgetView: AppWidgetHostView) { + targetView = widgetView + visibility = VISIBLE + bringToFront() + invalidate() + } + + fun detach() { + targetView = null + activeHandle = Handle.NONE + isResizing = false + visibility = GONE + listener?.onDismiss() + } + + private fun getTargetRect(): RectF? { + val v = targetView ?: return null + val w = v.layoutParams?.width?.takeIf { it > 0 } ?: v.width + val h = v.layoutParams?.height?.takeIf { it > 0 } ?: v.height + return RectF(v.x, v.y, v.x + w, v.y + h) + } + + private fun getHandlePoints(r: RectF): List>> = listOf( + Handle.TOP_LEFT to (r.left to r.top), + Handle.TOP to (r.centerX() to r.top), + Handle.TOP_RIGHT to (r.right to r.top), + Handle.RIGHT to (r.right to r.centerY()), + Handle.BOTTOM_RIGHT to (r.right to r.bottom), + Handle.BOTTOM to (r.centerX() to r.bottom), + Handle.BOTTOM_LEFT to (r.left to r.bottom), + Handle.LEFT to (r.left to r.centerY()) + ) + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + val r = getTargetRect() ?: return + + canvas.drawRect(r, borderPaint) + + for ((_, pt) in getHandlePoints(r)) { + canvas.drawCircle(pt.first, pt.second, handleRadius, handleFillPaint) + canvas.drawCircle(pt.first, pt.second, handleRadius, handleStrokePaint) + } + + val wDp = (r.width() / density).toInt() + val hDp = (r.height() / density).toInt() + val label = "${wDp}×${hDp} dp" + val textW = badgeTextPaint.measureText(label) + val padX = 6f * density + val padY = 4f * density + val badgeTop = max(0f, r.top - 24f * density) + val badgeRect = RectF(r.left, badgeTop, r.left + textW + padX * 2, badgeTop + 18f * density) + canvas.drawRoundRect(badgeRect, 4f * density, 4f * density, badgeBgPaint) + canvas.drawText(label, badgeRect.left + padX, badgeRect.bottom - padY, badgeTextPaint) + } + + @SuppressLint("ClickableViewAccessibility") + override fun onTouchEvent(event: MotionEvent): Boolean { + val v = targetView ?: return false + val r = getTargetRect() ?: return false + + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + val x = event.x + val y = event.y + lastRawX = event.rawX + lastRawY = event.rawY + startX = v.x + startY = v.y + startW = v.layoutParams?.width?.takeIf { it > 0 } ?: v.width + startH = v.layoutParams?.height?.takeIf { it > 0 } ?: v.height + + activeHandle = getHandlePoints(r) + .minByOrNull { hypot((x - it.second.first).toDouble(), (y - it.second.second).toDouble()) } + ?.takeIf { hypot((x - it.second.first).toDouble(), (y - it.second.second).toDouble()) <= handleTouchRadius } + ?.first ?: if (r.contains(x, y)) Handle.BODY else Handle.NONE + + if (activeHandle == Handle.NONE) { + detach() + return true + } + isResizing = activeHandle != Handle.BODY + if (!isResizing) { + listener?.onMoveStart(v) + } + return true + } + + MotionEvent.ACTION_MOVE -> { + val dx = event.rawX - lastRawX + val dy = event.rawY - lastRawY + val parentW = (parent as? ViewGroup)?.width ?: width + val parentH = (parent as? ViewGroup)?.height ?: height + + if (activeHandle == Handle.BODY) { + v.x = (startX + dx).coerceIn(0f, max(0f, parentW - startW.toFloat())) + v.y = (startY + dy).coerceIn(0f, max(0f, parentH - startH.toFloat())) + listener?.onMoving(v) + } else if (activeHandle != Handle.NONE) { + var newLeft = startX + var newTop = startY + var newRight = startX + startW + var newBottom = startY + startH + + when (activeHandle) { + Handle.LEFT, Handle.TOP_LEFT, Handle.BOTTOM_LEFT -> + newLeft = min(newRight - minSizePx, max(0f, startX + dx)) + Handle.RIGHT, Handle.TOP_RIGHT, Handle.BOTTOM_RIGHT -> + newRight = max(newLeft + minSizePx, min(parentW.toFloat(), startX + startW + dx)) + else -> {} + } + when (activeHandle) { + Handle.TOP, Handle.TOP_LEFT, Handle.TOP_RIGHT -> + newTop = min(newBottom - minSizePx, max(0f, startY + dy)) + Handle.BOTTOM, Handle.BOTTOM_LEFT, Handle.BOTTOM_RIGHT -> + newBottom = max(newTop + minSizePx, min(parentH.toFloat(), startY + startH + dy)) + else -> {} + } + + v.x = newLeft + v.y = newTop + val lp = v.layoutParams + lp.width = (newRight - newLeft).toInt().coerceAtLeast(minSizePx) + lp.height = (newBottom - newTop).toInt().coerceAtLeast(minSizePx) + v.layoutParams = lp + } + invalidate() + return true + } + + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + if (activeHandle == Handle.BODY) { + listener?.onMoveEnd(v) + } else if (activeHandle != Handle.NONE) { + listener?.onResizeEnd(v) + } + activeHandle = Handle.NONE + isResizing = false + invalidate() + return true + } + } + return super.onTouchEvent(event) + } +}