Skip to content

Commit d69cb4e

Browse files
authored
Merge pull request #145 from esensar/fix/remove-out-of-bound-widgets
Remove widgets that are out of bounds if they are blocking dropped icon
2 parents 2349fa0 + 9e9d305 commit d69cb4e

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
245245
}
246246
}
247247

248+
fun removeWidget(item: HomeScreenGridItem) {
249+
ensureBackgroundThread {
250+
removeItemFromHomeScreen(item)
251+
post {
252+
removeView(widgetViews.firstOrNull { it.tag == item.widgetId })
253+
widgetViews.removeIf { it.tag == item.widgetId }
254+
}
255+
gridItems.removeIf { it.id == item.id }
256+
}
257+
}
258+
248259
private fun removeItemFromHomeScreen(item: HomeScreenGridItem) {
249260
ensureBackgroundThread {
250261
if (item.id != null) {
@@ -381,7 +392,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
381392
val viewX = widgetView.x.toInt()
382393
val viewY = widgetView.y.toInt()
383394
val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
384-
val otherGridItems = gridItems.filterVisibleOnCurrentPageOnly().filter { it.widgetId != item.widgetId }.toMutableList() as ArrayList<HomeScreenGridItem>
395+
val otherGridItems = gridItems.filterVisibleOnCurrentPageOnly()
396+
.filter { !it.outOfBounds() }
397+
.filter { it.widgetId != item.widgetId }.toMutableList() as ArrayList<HomeScreenGridItem>
385398
binding.resizeFrame.updateFrameCoords(frameRect, cellWidth, cellHeight, sideMargins, item, otherGridItems)
386399
binding.resizeFrame.beVisible()
387400
binding.resizeFrame.z = 1f // make sure the frame isnt behind the widget itself
@@ -399,6 +412,28 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
399412
cellsRect.bottom
400413
}
401414
updateWidgetPositionAndSize(widgetView, item)
415+
416+
val widgetTargetCells = ArrayList<Pair<Int, Int>>()
417+
for (xCell in item.left..item.right) {
418+
for (yCell in item.top..item.bottom) {
419+
widgetTargetCells.add(Pair(xCell, yCell))
420+
}
421+
}
422+
423+
gridItems.filterVisibleOnCurrentPageOnly().filter { it.id != item.id }.forEach { gridItem ->
424+
for (xCell in gridItem.left..gridItem.right) {
425+
for (yCell in gridItem.getDockAdjustedTop(rowCount)..gridItem.getDockAdjustedBottom(rowCount)) {
426+
val cell = Pair(xCell, yCell)
427+
val isAnyCellOccupied = widgetTargetCells.contains(cell)
428+
if (isAnyCellOccupied) {
429+
if (gridItem.type == ITEM_TYPE_WIDGET && gridItem.outOfBounds()) {
430+
removeWidget(gridItem)
431+
}
432+
}
433+
}
434+
}
435+
}
436+
402437
ensureBackgroundThread {
403438
context.homeScreenGridItemsDB.updateItemPosition(item.left, item.top, item.right, item.bottom, item.page, false, null, item.id!!)
404439
}
@@ -450,7 +485,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
450485
val cell = Pair(xCell, yCell)
451486
val isAnyCellOccupied = wantedCell == cell
452487
if (isAnyCellOccupied) {
453-
isDroppingPositionValid = false
488+
if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
489+
removeWidget(item)
490+
} else {
491+
isDroppingPositionValid = false
492+
}
454493
return@forEach
455494
}
456495
}
@@ -532,7 +571,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
532571
if (item.type != ITEM_TYPE_WIDGET && !item.docked) {
533572
potentialParent = item
534573
} else {
535-
isDroppingPositionValid = false
574+
if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
575+
removeWidget(item)
576+
} else {
577+
isDroppingPositionValid = false
578+
}
536579
}
537580
return@forEach
538581
}
@@ -760,8 +803,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
760803
val cell = Pair(xCell, yCell)
761804
val isAnyCellOccupied = widgetTargetCells.contains(cell)
762805
if (isAnyCellOccupied) {
763-
areAllCellsEmpty = false
764-
return@forEach
806+
if (item.type == ITEM_TYPE_WIDGET && item.outOfBounds()) {
807+
removeWidget(item)
808+
} else {
809+
areAllCellsEmpty = false
810+
return@forEach
811+
}
765812
}
766813
}
767814
}

0 commit comments

Comments
 (0)