just renaming rows to cells

This commit is contained in:
tibbi 2022-10-04 10:08:37 +02:00
parent a159465361
commit 4921159242
3 changed files with 52 additions and 52 deletions

View File

@ -291,7 +291,7 @@ class MainActivity : SimpleActivity(), FlingListener {
main_holder.performHapticFeedback() main_holder.performHapticFeedback()
} }
val anchorY = home_screen_grid.sideMargins.top + (clickedGridItem.top * home_screen_grid.rowHeight.toFloat()) val anchorY = home_screen_grid.sideMargins.top + (clickedGridItem.top * home_screen_grid.cellHeight.toFloat())
showHomeIconMenu(x, anchorY, clickedGridItem, false) showHomeIconMenu(x, anchorY, clickedGridItem, false)
return return
} }
@ -314,9 +314,9 @@ class MainActivity : SimpleActivity(), FlingListener {
val anchorY = if (isOnAllAppsFragment || gridItem.type == ITEM_TYPE_WIDGET) { val anchorY = if (isOnAllAppsFragment || gridItem.type == ITEM_TYPE_WIDGET) {
y y
} else if (gridItem.top == ROW_COUNT - 1) { } else if (gridItem.top == ROW_COUNT - 1) {
home_screen_grid.sideMargins.top + (gridItem.top * home_screen_grid.rowHeight.toFloat()) home_screen_grid.sideMargins.top + (gridItem.top * home_screen_grid.cellHeight.toFloat())
} else { } else {
(gridItem.top * home_screen_grid.rowHeight.toFloat()) (gridItem.top * home_screen_grid.cellHeight.toFloat())
} }
home_screen_popup_menu_anchor.x = x home_screen_popup_menu_anchor.x = x

View File

@ -39,10 +39,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
private var iconSize = 0 private var iconSize = 0
// let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps // let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps
private var rowXCoords = ArrayList<Int>(COLUMN_COUNT) private var cellXCoords = ArrayList<Int>(COLUMN_COUNT)
private var rowYCoords = ArrayList<Int>(ROW_COUNT) private var cellYCoords = ArrayList<Int>(ROW_COUNT)
var rowWidth = 0 var cellWidth = 0
var rowHeight = 0 var cellHeight = 0
// apply fake margins at the home screen. Real ones would cause the icons be cut at dragging at screen sides // apply fake margins at the home screen. Real ones would cause the icons be cut at dragging at screen sides
var sideMargins = Rect() var sideMargins = Rect()
@ -181,7 +181,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val viewX = widgetView.x.toInt() val viewX = widgetView.x.toInt()
val viewY = widgetView.y.toInt() val viewY = widgetView.y.toInt()
val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height) val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height)
resize_frame.updateFrameCoords(frameRect, rowWidth, rowHeight, sideMargins, appWidgetProviderInfo) resize_frame.updateFrameCoords(frameRect, cellWidth, cellHeight, sideMargins, appWidgetProviderInfo)
resize_frame.beVisible() resize_frame.beVisible()
resize_frame.z = 1f // make sure the frame isnt behind the widget itself resize_frame.z = 1f // make sure the frame isnt behind the widget itself
resize_frame.onClickListener = { resize_frame.onClickListener = {
@ -416,8 +416,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
widgetView.x = calculateWidgetX(item.left) widgetView.x = calculateWidgetX(item.left)
widgetView.y = calculateWidgetY(item.top) widgetView.y = calculateWidgetY(item.top)
val widgetWidth = item.widthCells * rowWidth val widgetWidth = item.widthCells * cellWidth
val widgetHeight = item.heightCells * rowHeight val widgetHeight = item.heightCells * cellHeight
// set initial sizes to avoid some glitches // set initial sizes to avoid some glitches
if (isSPlus()) { if (isSPlus()) {
@ -437,15 +437,15 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
gridItems.add(item) gridItems.add(item)
} }
private fun calculateWidgetX(leftCell: Int) = leftCell * rowWidth + sideMargins.left.toFloat() private fun calculateWidgetX(leftCell: Int) = leftCell * cellWidth + sideMargins.left.toFloat()
private fun calculateWidgetY(topCell: Int) = topCell * rowHeight + sideMargins.top.toFloat() private fun calculateWidgetY(topCell: Int) = topCell * cellHeight + sideMargins.top.toFloat()
// 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>? {
rowXCoords.forEachIndexed { xIndex, xCell -> cellXCoords.forEachIndexed { xIndex, xCell ->
rowYCoords.forEachIndexed { yIndex, yCell -> cellYCoords.forEachIndexed { yIndex, yCell ->
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) { if (xCell + cellWidth / 2 == center.first && yCell + cellHeight / 2 == center.second) {
return Pair(xIndex, yIndex) return Pair(xIndex, yIndex)
} }
} }
@ -468,42 +468,42 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
@SuppressLint("DrawAllocation") @SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
if (rowXCoords.isEmpty()) { if (cellXCoords.isEmpty()) {
rowWidth = getFakeWidth() / COLUMN_COUNT cellWidth = getFakeWidth() / COLUMN_COUNT
rowHeight = getFakeHeight() / ROW_COUNT cellHeight = getFakeHeight() / ROW_COUNT
iconSize = rowWidth - 2 * iconMargin iconSize = cellWidth - 2 * iconMargin
for (i in 0 until COLUMN_COUNT) { for (i in 0 until COLUMN_COUNT) {
rowXCoords.add(i, i * rowWidth) cellXCoords.add(i, i * cellWidth)
} }
for (i in 0 until ROW_COUNT) { for (i in 0 until ROW_COUNT) {
rowYCoords.add(i, i * rowHeight) cellYCoords.add(i, i * cellHeight)
} }
rowXCoords.forEach { x -> cellXCoords.forEach { x ->
rowYCoords.forEach { y -> cellYCoords.forEach { y ->
gridCenters.add(Pair(x + rowWidth / 2, y + rowHeight / 2)) gridCenters.add(Pair(x + cellWidth / 2, y + cellHeight / 2))
} }
} }
} }
gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON }.forEach { item -> gridItems.filter { it.drawable != null && it.type == ITEM_TYPE_ICON }.forEach { item ->
if (item.id != draggedItem?.id) { if (item.id != draggedItem?.id) {
val drawableX = rowXCoords[item.left] + iconMargin + sideMargins.left val drawableX = cellXCoords[item.left] + iconMargin + sideMargins.left
// icons at the bottom are drawn at the bottom of the grid and they have no label // icons at the bottom are drawn at the bottom of the grid and they have no label
if (item.top == ROW_COUNT - 1) { if (item.top == ROW_COUNT - 1) {
val drawableY = rowYCoords[item.top] + rowHeight - iconSize - iconMargin * 2 + sideMargins.top val drawableY = cellYCoords[item.top] + cellHeight - iconSize - iconMargin * 2 + sideMargins.top
item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
} else { } else {
val drawableY = rowYCoords[item.top] + iconSize / 2 + sideMargins.top val drawableY = cellYCoords[item.top] + iconSize / 2 + sideMargins.top
item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) item.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
if (item.id != draggedItem?.id && item.title.isNotEmpty()) { if (item.id != draggedItem?.id && item.title.isNotEmpty()) {
val textX = rowXCoords[item.left].toFloat() + labelSideMargin + sideMargins.left val textX = cellXCoords[item.left].toFloat() + labelSideMargin + sideMargins.left
val textY = rowYCoords[item.top] + iconSize * 1.5f + labelSideMargin + sideMargins.top val textY = cellYCoords[item.top] + iconSize * 1.5f + labelSideMargin + sideMargins.top
val staticLayout = StaticLayout.Builder val staticLayout = StaticLayout.Builder
.obtain(item.title, 0, item.title.length, textPaint, rowWidth - 2 * labelSideMargin) .obtain(item.title, 0, item.title.length, textPaint, cellWidth - 2 * labelSideMargin)
.setMaxLines(2) .setMaxLines(2)
.setEllipsize(TextUtils.TruncateAt.END) .setEllipsize(TextUtils.TruncateAt.END)
.setAlignment(Layout.Alignment.ALIGN_CENTER) .setAlignment(Layout.Alignment.ALIGN_CENTER)
@ -535,11 +535,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val gridCells = getClosestGridCells(center) val gridCells = getClosestGridCells(center)
if (gridCells != null) { if (gridCells != null) {
val shadowX = rowXCoords[gridCells.first] + iconMargin.toFloat() + iconSize / 2 + sideMargins.left val shadowX = cellXCoords[gridCells.first] + iconMargin.toFloat() + iconSize / 2 + sideMargins.left
val shadowY = if (gridCells.second == ROW_COUNT - 1) { val shadowY = if (gridCells.second == ROW_COUNT - 1) {
rowYCoords[gridCells.second] + rowHeight - iconSize / 2 - iconMargin * 2 cellYCoords[gridCells.second] + cellHeight - iconSize / 2 - iconMargin * 2
} else { } else {
rowYCoords[gridCells.second] + iconSize cellYCoords[gridCells.second] + iconSize
} + sideMargins.top } + sideMargins.top
canvas.drawCircle(shadowX, shadowY.toFloat(), iconSize / 2f, dragShadowCirclePaint) canvas.drawCircle(shadowX, shadowY.toFloat(), iconSize / 2f, dragShadowCirclePaint)
@ -560,10 +560,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 * rowWidth + sideMargins.left + iconMargin.toFloat() val leftSide = widgetRect.left * cellWidth + sideMargins.left + iconMargin.toFloat()
val topSide = widgetRect.top * rowHeight + sideMargins.top + iconMargin.toFloat() val topSide = widgetRect.top * cellHeight + sideMargins.top + iconMargin.toFloat()
val rightSide = leftSide + draggedItem!!.widthCells * rowWidth - sideMargins.right - iconMargin.toFloat() val rightSide = leftSide + draggedItem!!.widthCells * cellWidth - sideMargins.right - iconMargin.toFloat()
val bottomSide = topSide + draggedItem!!.heightCells * rowHeight - sideMargins.top val bottomSide = topSide + draggedItem!!.heightCells * cellHeight - sideMargins.top
canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint) canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint)
} }
@ -572,7 +572,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
val aspectRatio = drawable.minimumHeight / drawable.minimumWidth.toFloat() val aspectRatio = drawable.minimumHeight / drawable.minimumWidth.toFloat()
val drawableX = (draggedItemCurrentCoords.first - drawable.minimumWidth / 2f).toInt() val drawableX = (draggedItemCurrentCoords.first - drawable.minimumWidth / 2f).toInt()
val drawableY = (draggedItemCurrentCoords.second - drawable.minimumHeight / 3f).toInt() val drawableY = (draggedItemCurrentCoords.second - drawable.minimumHeight / 3f).toInt()
val drawableWidth = draggedItem!!.widthCells * rowWidth - iconMargin * (draggedItem!!.widthCells - 1) val drawableWidth = draggedItem!!.widthCells * cellWidth - iconMargin * (draggedItem!!.widthCells - 1)
drawable.setBounds( drawable.setBounds(
drawableX, drawableX,
drawableY, drawableY,
@ -601,9 +601,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
// get the clickable area around the icon, it includes text too // get the clickable area around the icon, it includes text too
private fun getClickableRect(item: HomeScreenGridItem): Rect { private fun getClickableRect(item: HomeScreenGridItem): Rect {
val clickableLeft = item.left * rowWidth + sideMargins.left val clickableLeft = item.left * cellWidth + sideMargins.left
val clickableTop = rowYCoords[item.top] + iconSize / 3 + sideMargins.top val clickableTop = cellYCoords[item.top] + iconSize / 3 + sideMargins.top
return Rect(clickableLeft, clickableTop, clickableLeft + rowWidth, clickableTop + iconSize * 2) return Rect(clickableLeft, clickableTop, clickableLeft + cellWidth, clickableTop + iconSize * 2)
} }
// drag the center of the widget, not the top left corner // drag the center of the widget, not the top left corner
@ -639,8 +639,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} else if (gridItem.type == ITEM_TYPE_WIDGET) { } else if (gridItem.type == ITEM_TYPE_WIDGET) {
val left = calculateWidgetX(gridItem.left) val left = calculateWidgetX(gridItem.left)
val top = calculateWidgetY(gridItem.top) val top = calculateWidgetY(gridItem.top)
val right = left + gridItem.widthCells * rowWidth val right = left + gridItem.widthCells * cellWidth
val bottom = top + gridItem.heightCells * rowHeight val bottom = top + gridItem.heightCells * cellHeight
if (x >= left && x <= right && y >= top && y <= bottom) { if (x >= left && x <= right && y >= top && y <= bottom) {
return gridItem return gridItem

View File

@ -20,8 +20,8 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
private var actionDownCoords = PointF() private var actionDownCoords = PointF()
private var actionDownMS = 0L private var actionDownMS = 0L
private var frameRect = Rect(0, 0, 0, 0) private var frameRect = Rect(0, 0, 0, 0)
private var rowWidth = 0 private var cellWidth = 0
private var rowHeight = 0 private var cellHeight = 0
private var providerInfo: AppWidgetProviderInfo? = null private var providerInfo: AppWidgetProviderInfo? = null
private var sideMargins = Rect() private var sideMargins = Rect()
private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius) private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius)
@ -49,10 +49,10 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
} }
} }
fun updateFrameCoords(coords: Rect, rowWidth: Int, rowHeight: Int, sideMargins: Rect, providerInfo: AppWidgetProviderInfo?) { fun updateFrameCoords(coords: Rect, cellWidth: Int, cellHeight: Int, sideMargins: Rect, providerInfo: AppWidgetProviderInfo?) {
frameRect = coords frameRect = coords
this.rowWidth = rowWidth this.cellWidth = cellWidth
this.rowHeight = rowHeight this.cellHeight = cellHeight
this.sideMargins = sideMargins this.sideMargins = sideMargins
this.providerInfo = providerInfo this.providerInfo = providerInfo
redrawFrame() redrawFrame()
@ -107,10 +107,10 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
dragDirection = DRAGGING_NONE dragDirection = DRAGGING_NONE
} else { } else {
when (dragDirection) { when (dragDirection) {
DRAGGING_LEFT -> frameRect.left = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, rowWidth) + sideMargins.left DRAGGING_LEFT -> frameRect.left = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth) + sideMargins.left
DRAGGING_TOP -> frameRect.top = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, rowHeight) + sideMargins.top DRAGGING_TOP -> frameRect.top = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight) + sideMargins.top
DRAGGING_RIGHT -> frameRect.right = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, rowWidth) + sideMargins.left DRAGGING_RIGHT -> frameRect.right = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth) + sideMargins.left
DRAGGING_BOTTOM -> frameRect.bottom = roundToClosestMultiplyOfNumber(frameRect.bottom - sideMargins.top, rowHeight) + sideMargins.top DRAGGING_BOTTOM -> frameRect.bottom = roundToClosestMultiplyOfNumber(frameRect.bottom - sideMargins.top, cellHeight) + sideMargins.top
} }
redrawFrame() redrawFrame()
} }