Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / launcher / src / main / java / com / vayunmathur / launcher / domain / GridReorder.kt
GridReorder.kt95 lines · 4.2 KiBRaw
| 1 | package com.vayunmathur.launcher.domain |
| 2 | |
| 3 | import kotlin.math.abs |
| 4 | |
| 5 | /** One of the four ways a drag can push its neighbours. */ |
| 6 | enum class PushDirection { Left, Up, Right, Down } |
| 7 | |
| 8 | /** |
| 9 | * Pushing a page's items *along* a drag rather than into the nearest hole. |
| 10 | * |
| 11 | * The difference is the whole feel of a reorder. [GridPlacer.findNearestVacant] answers "where is |
| 12 | * there room", which is right for an install but wrong under a finger: dragging an icon leftwards |
| 13 | * across a row makes each icon it passes jump to whatever gap happens to be closest, which reads |
| 14 | * as the page scattering. Launcher3 instead shoves the occupant one cell further in the direction |
| 15 | * the finger is already travelling, and shoves whatever *that* lands on, and so on — so a row |
| 16 | * shuffles along like beads on a wire. |
| 17 | * |
| 18 | * All-or-nothing on purpose. A cascade that runs out of grid halfway would leave the page |
| 19 | * rearranged for a move that then does not happen, or worse, two items in one cell. When it cannot |
| 20 | * be done, [pushAlong] says so and the caller falls back. |
| 21 | */ |
| 22 | object GridReorder { |
| 23 | |
| 24 | /** |
| 25 | * The direction [to] lies in from [from], or null when the two are the same cell. |
| 26 | * |
| 27 | * Quantised to the dominant axis, ties going horizontal, because a cascade needs one direction |
| 28 | * rather than a vector: pushing diagonally would mean moving each occupant twice. The caller is |
| 29 | * expected to keep the last non-null answer while the finger holds still, which is what stops |
| 30 | * the cascade flip-flopping between two directions frame to frame. |
| 31 | */ |
| 32 | fun directionOf(from: CellRect, to: CellRect): PushDirection? { |
| 33 | val dx = to.cellX - from.cellX |
| 34 | val dy = to.cellY - from.cellY |
| 35 | if (dx == 0 && dy == 0) return null |
| 36 | return if (abs(dx) >= abs(dy)) { |
| 37 | if (dx > 0) PushDirection.Right else PushDirection.Left |
| 38 | } else { |
| 39 | if (dy > 0) PushDirection.Down else PushDirection.Up |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Where each item has to go for [wanted] to be free, pushing along [direction], or null when |
| 45 | * some item in the chain would end up off the grid. |
| 46 | * |
| 47 | * Breadth-first over the chain rather than recursive descent, so the cascade is bounded by the |
| 48 | * number of items on the page: each one may move at most once, and a second push of the same |
| 49 | * item aborts the whole plan. Without that, two items shoved into each other's paths would |
| 50 | * loop. |
| 51 | */ |
| 52 | fun pushAlong( |
| 53 | spec: GridSpec, |
| 54 | others: Map<Long, CellRect>, |
| 55 | wanted: CellRect, |
| 56 | direction: PushDirection, |
| 57 | ): Map<Long, CellRect>? { |
| 58 | if (!fits(spec, wanted)) return null |
| 59 | |
| 60 | val layout = others.toMutableMap() |
| 61 | val moved = LinkedHashMap<Long, CellRect>() |
| 62 | // The item now sitting in each region, so a region does not count as blocking itself. |
| 63 | val pending = ArrayDeque<Pair<Long?, CellRect>>() |
| 64 | pending += null to wanted |
| 65 | |
| 66 | while (pending.isNotEmpty()) { |
| 67 | val (owner, region) = pending.removeFirst() |
| 68 | val blocking = layout |
| 69 | .filter { it.key != owner && it.value.overlaps(region) } |
| 70 | .map { it.key to it.value } |
| 71 | for ((id, rect) in blocking) { |
| 72 | if (id in moved) return null |
| 73 | val to = shiftedClear(rect, region, direction) |
| 74 | if (!fits(spec, to)) return null |
| 75 | layout[id] = to |
| 76 | moved[id] = to |
| 77 | pending += id to to |
| 78 | } |
| 79 | } |
| 80 | return moved |
| 81 | } |
| 82 | |
| 83 | /** [rect] moved the fewest whole cells along [direction] that takes it clear of [region]. */ |
| 84 | private fun shiftedClear(rect: CellRect, region: CellRect, direction: PushDirection): CellRect = |
| 85 | when (direction) { |
| 86 | PushDirection.Right -> rect.movedTo(region.right, rect.cellY) |
| 87 | PushDirection.Left -> rect.movedTo(region.cellX - rect.spanX, rect.cellY) |
| 88 | PushDirection.Down -> rect.movedTo(rect.cellX, region.bottom) |
| 89 | PushDirection.Up -> rect.movedTo(rect.cellX, region.cellY - rect.spanY) |
| 90 | } |
| 91 | |
| 92 | private fun fits(spec: GridSpec, rect: CellRect): Boolean = |
| 93 | rect.cellX >= 0 && rect.cellY >= 0 && |
| 94 | rect.right <= spec.columns && rect.bottom <= spec.rows |
| 95 | } |