mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2025-02-16 19:40:41 +01:00
Clean up widget placement
This commit is contained in:
parent
1097159b8d
commit
209c74e99b
@ -29,6 +29,7 @@ import com.simplemobiletools.launcher.helpers.*
|
|||||||
import com.simplemobiletools.launcher.models.HomeScreenGridItem
|
import com.simplemobiletools.launcher.models.HomeScreenGridItem
|
||||||
import kotlinx.android.synthetic.main.activity_main.view.*
|
import kotlinx.android.synthetic.main.activity_main.view.*
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
import kotlin.math.floor
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) {
|
class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : RelativeLayout(context, attrs, defStyle) {
|
||||||
@ -40,6 +41,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
private var cellYCoords = ArrayList<Int>(rowCount)
|
private var cellYCoords = ArrayList<Int>(rowCount)
|
||||||
var cellWidth = 0
|
var cellWidth = 0
|
||||||
var cellHeight = 0
|
var cellHeight = 0
|
||||||
|
private var extraXMargin = 0
|
||||||
|
private var extraYMargin = 0
|
||||||
|
|
||||||
private var iconMargin = (context.resources.getDimension(R.dimen.icon_side_margin) * 5 / columnCount).toInt()
|
private var iconMargin = (context.resources.getDimension(R.dimen.icon_side_margin) * 5 / columnCount).toInt()
|
||||||
private var labelSideMargin = context.resources.getDimension(R.dimen.small_margin).toInt()
|
private var labelSideMargin = context.resources.getDimension(R.dimen.small_margin).toInt()
|
||||||
@ -112,7 +115,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun resizeGrid(newRowCount: Int, newColumnCount: Int) {
|
fun resizeGrid(newRowCount: Int, newColumnCount: Int) {
|
||||||
if (columnCount != newColumnCount || rowCount != newRowCount){
|
if (columnCount != newColumnCount || rowCount != newRowCount) {
|
||||||
rowCount = newRowCount
|
rowCount = newRowCount
|
||||||
columnCount = newColumnCount
|
columnCount = newColumnCount
|
||||||
cellXCoords = ArrayList(columnCount)
|
cellXCoords = ArrayList(columnCount)
|
||||||
@ -393,7 +396,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
} else {
|
} else {
|
||||||
context.homeScreenGridItemsDB.updateItemPosition(widgetItem.left, widgetItem.top, widgetItem.right, widgetItem.bottom, widgetItem.id!!)
|
context.homeScreenGridItemsDB.updateItemPosition(widgetItem.left, widgetItem.top, widgetItem.right, widgetItem.bottom, widgetItem.id!!)
|
||||||
val widgetView = widgetViews.firstOrNull { it.tag == widgetItem.widgetId }
|
val widgetView = widgetViews.firstOrNull { it.tag == widgetItem.widgetId }
|
||||||
if (widgetView != null) {
|
if (widgetView != null && !widgetItem.outOfBounds()) {
|
||||||
post {
|
post {
|
||||||
widgetView.x = calculateWidgetX(widgetItem.left)
|
widgetView.x = calculateWidgetX(widgetItem.left)
|
||||||
widgetView.y = calculateWidgetY(widgetItem.top)
|
widgetView.y = calculateWidgetY(widgetItem.top)
|
||||||
@ -498,9 +501,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
return Size(widgetWidth, widgetHeight)
|
return Size(widgetWidth, widgetHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateWidgetX(leftCell: Int) = leftCell * cellWidth + sideMargins.left.toFloat()
|
private fun calculateWidgetX(leftCell: Int) = cellXCoords[leftCell] + sideMargins.left.toFloat() + extraXMargin
|
||||||
|
|
||||||
private fun calculateWidgetY(topCell: Int) = topCell * cellHeight + sideMargins.top.toFloat()
|
private fun calculateWidgetY(topCell: Int) = cellYCoords[topCell] + sideMargins.top.toFloat() + extraYMargin
|
||||||
|
|
||||||
// convert stuff like 102x192 to grid cells like 0x1
|
// convert stuff like 102x192 to grid cells like 0x1
|
||||||
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
|
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
|
||||||
@ -537,16 +540,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
fillCellSizes()
|
fillCellSizes()
|
||||||
}
|
}
|
||||||
|
|
||||||
val extraXMargin = if (cellWidth > cellHeight) {
|
|
||||||
(cellWidth - cellHeight) / 2
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
val extraYMargin = if (cellHeight > cellWidth) {
|
|
||||||
(cellHeight - cellWidth) / 2
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON || it.type == ITEM_TYPE_SHORTCUT }.forEach { item ->
|
gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON || it.type == ITEM_TYPE_SHORTCUT }.forEach { item ->
|
||||||
if (item.outOfBounds()) {
|
if (item.outOfBounds()) {
|
||||||
return@forEach
|
return@forEach
|
||||||
@ -585,7 +578,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isFirstDraw || redrawWidgets) {
|
if (isFirstDraw || redrawWidgets) {
|
||||||
gridItems.filter { it.type == ITEM_TYPE_WIDGET }.forEach { item ->
|
gridItems.filter { it.type == ITEM_TYPE_WIDGET && !it.outOfBounds() }.forEach { item ->
|
||||||
bindWidget(item, isFirstDraw)
|
bindWidget(item, isFirstDraw)
|
||||||
}
|
}
|
||||||
redrawWidgets = false
|
redrawWidgets = false
|
||||||
@ -625,10 +618,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
val gridCells = getClosestGridCells(center)
|
val gridCells = getClosestGridCells(center)
|
||||||
if (gridCells != null) {
|
if (gridCells != null) {
|
||||||
val widgetRect = getWidgetOccupiedRect(gridCells)
|
val widgetRect = getWidgetOccupiedRect(gridCells)
|
||||||
val leftSide = widgetRect.left * cellWidth + sideMargins.left + iconMargin.toFloat()
|
val leftSide = calculateWidgetX(widgetRect.left)
|
||||||
val topSide = widgetRect.top * cellHeight + sideMargins.top + iconMargin.toFloat()
|
val topSide = calculateWidgetY(widgetRect.top)
|
||||||
val rightSide = leftSide + draggedItem!!.getWidthInCells() * cellWidth - sideMargins.right - iconMargin.toFloat()
|
val rightSide = leftSide + draggedItem!!.getWidthInCells() * cellWidth
|
||||||
val bottomSide = topSide + draggedItem!!.getHeightInCells() * cellHeight - sideMargins.top
|
val bottomSide = topSide + draggedItem!!.getHeightInCells() * cellHeight
|
||||||
canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint)
|
canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -655,6 +648,16 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
private fun fillCellSizes() {
|
private fun fillCellSizes() {
|
||||||
cellWidth = getFakeWidth() / context.config.homeColumnCount
|
cellWidth = getFakeWidth() / context.config.homeColumnCount
|
||||||
cellHeight = getFakeHeight() / context.config.homeRowCount
|
cellHeight = getFakeHeight() / context.config.homeRowCount
|
||||||
|
extraXMargin = if (cellWidth > cellHeight) {
|
||||||
|
(cellWidth - cellHeight) / 2
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
extraYMargin = if (cellHeight > cellWidth) {
|
||||||
|
(cellHeight - cellWidth) / 2
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
iconSize = min(cellWidth, cellHeight) - 2 * iconMargin
|
iconSize = min(cellWidth, cellHeight) - 2 * iconMargin
|
||||||
for (i in 0 until context.config.homeColumnCount) {
|
for (i in 0 until context.config.homeColumnCount) {
|
||||||
cellXCoords.add(i, i * cellWidth)
|
cellXCoords.add(i, i * cellWidth)
|
||||||
@ -689,17 +692,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
fillCellSizes()
|
fillCellSizes()
|
||||||
}
|
}
|
||||||
|
|
||||||
val extraXMargin = if (cellWidth > cellHeight) {
|
|
||||||
(cellWidth - cellHeight) / 2
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
val extraYMargin = if (cellHeight > cellWidth) {
|
|
||||||
(cellHeight - cellWidth) / 2
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
val clickableLeft = cellXCoords[item.left] + sideMargins.left + extraXMargin
|
val clickableLeft = cellXCoords[item.left] + sideMargins.left + extraXMargin
|
||||||
val clickableTop = if (item.top == rowCount - 1) {
|
val clickableTop = if (item.top == rowCount - 1) {
|
||||||
cellYCoords[item.top] + cellHeight - iconSize - iconMargin
|
cellYCoords[item.top] + cellHeight - iconSize - iconMargin
|
||||||
@ -711,7 +703,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
|
|
||||||
// drag the center of the widget, not the top left corner
|
// drag the center of the widget, not the top left corner
|
||||||
private fun getWidgetOccupiedRect(item: Pair<Int, Int>): Rect {
|
private fun getWidgetOccupiedRect(item: Pair<Int, Int>): Rect {
|
||||||
val left = item.first - Math.floor((draggedItem!!.getWidthInCells() - 1) / 2.0).toInt()
|
val left = item.first - floor((draggedItem!!.getWidthInCells() - 1) / 2.0).toInt()
|
||||||
val rect = Rect(left, item.second, left + draggedItem!!.getWidthInCells() - 1, item.second + draggedItem!!.getHeightInCells() - 1)
|
val rect = Rect(left, item.second, left + draggedItem!!.getWidthInCells() - 1, item.second + draggedItem!!.getHeightInCells() - 1)
|
||||||
if (rect.left < 0) {
|
if (rect.left < 0) {
|
||||||
rect.right -= rect.left
|
rect.right -= rect.left
|
||||||
|
Loading…
x
Reference in New Issue
Block a user