Skip to content

Commit

Permalink
fix: better overlap calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
agronick committed Dec 17, 2023
1 parent 8a9f060 commit 5768500
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
18 changes: 6 additions & 12 deletions app/src/main/java/com/agronick/launcher/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,15 @@ class App(val pkgInfo: PInfo, var size: Int) {
return Vector2(aX, aY)
}

fun intersects(point: Vector2): Boolean {
val circle = lastCircle
if (circle != null) {
return CircleCircleIntersection(
Circle(point, 3.0f),
circle,
).type.let {
it == CircleCircleIntersection.Type.ECCENTRIC_CONTAINED
}
}
return false
fun distance(point: Vector2): Float {
return lastCircle?.c?.distance(point) ?: Float.POSITIVE_INFINITY
}

fun radius(): Float {
return lastCircle?.r ?: (size * 0.5f)
}

override fun toString(): String {
return pkgInfo.pname ?: "Unmapped"
}

}
13 changes: 8 additions & 5 deletions app/src/main/java/com/agronick/launcher/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ class Container(val appList: AppListProvider, density: Float) {
}

fun getAppAtPoint(point: Vector2, toIgnore: HashSet<App>? = null): App? {
return flatAppList.find {
(if (toIgnore != null) !toIgnore.contains(it) else true) && it.intersects(
point
)
}
return flatAppList.mapNotNull mapping@{
if (toIgnore?.contains(it) == true) return@mapping null
val distance = it.distance(point)
if (distance > it.radius()) return@mapping null
return@mapping it to distance
}.minByOrNull {
it.second
}?.first
}

fun prepare(offsetLeft: Float, offsetTop: Float, size: Float) {
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/agronick/launcher/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MainView(context: Context, appList: List<PInfo>) : View(context) {
private var canvasSize: Float = 0f
private var edgeLimit = 100000f
var allHidden = false
private var lastInvalidate = 0L

private val STATE_NONE = 0
private val STATE_REORDERING = 1
Expand Down Expand Up @@ -223,12 +224,13 @@ class MainView(context: Context, appList: List<PInfo>) : View(context) {
}

fun prepareInvalidate() {
if (canvasSize == 0f) return
Runnable {
container.prepare(offsetLeft, offsetTop, canvasSize)
reorderer?.prepare()
invalidate()
}.run()
// Prevent flickering / allow vsync
val now = System.currentTimeMillis()
if (canvasSize == 0f || now - lastInvalidate < 33) return
lastInvalidate = now
container.prepare(offsetLeft, offsetTop, canvasSize)
reorderer?.prepare()
invalidate()
}

override fun onDraw(canvas: Canvas) {
Expand Down

0 comments on commit 5768500

Please sign in to comment.