adding some widget positioning and sizing improvements

This commit is contained in:
tibbi 2022-10-05 11:30:39 +02:00
parent 0bf2ccbcd0
commit 0f536defce
1 changed files with 27 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.launcher.views package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.appwidget.AppWidgetHostView
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo import android.appwidget.AppWidgetProviderInfo
import android.content.Context import android.content.Context
@ -11,6 +12,7 @@ import android.text.StaticLayout
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils import android.text.TextUtils
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Size
import android.util.SizeF import android.util.SizeF
import android.widget.RelativeLayout import android.widget.RelativeLayout
import androidx.core.graphics.drawable.toDrawable import androidx.core.graphics.drawable.toDrawable
@ -189,11 +191,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
resize_frame.onResizeListener = { cellsRect -> resize_frame.onResizeListener = { cellsRect ->
val minWidth = (cellsRect.width() + 1) * cellWidth item.left = cellsRect.left
val minHeight = (cellsRect.height() + 1) * cellHeight item.top = cellsRect.top
widgetView.updateAppWidgetSize(Bundle(), minWidth, minHeight, minWidth, minHeight) item.right = cellsRect.right
widgetView.layoutParams.width = minWidth item.bottom = cellsRect.bottom
widgetView.layoutParams.height = minHeight updateWidgetPositionAndSize(widgetView, item)
ensureBackgroundThread { ensureBackgroundThread {
context.homeScreenGridItemsDB.updateItemPosition(cellsRect.left, cellsRect.top, cellsRect.right, cellsRect.bottom, item.id!!) context.homeScreenGridItemsDB.updateItemPosition(cellsRect.left, cellsRect.top, cellsRect.right, cellsRect.bottom, item.id!!)
} }
@ -423,22 +425,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
hideResizeLines() hideResizeLines()
} }
widgetView.x = calculateWidgetX(item.left) val widgetSize = updateWidgetPositionAndSize(widgetView, item)
widgetView.y = calculateWidgetY(item.top) addView(widgetView, widgetSize.width, widgetSize.height)
val widgetWidth = item.getWidthInCells() * cellWidth
val widgetHeight = item.getHeightInCells() * cellHeight
// set initial sizes to avoid some glitches
if (isSPlus()) {
val sizes = listOf(SizeF(widgetWidth.toFloat(), widgetHeight.toFloat()))
widgetView.updateAppWidgetSize(Bundle(), sizes)
} else {
val minWidth = appWidgetProviderInfo.minWidth
val minHeight = appWidgetProviderInfo.minHeight
widgetView.updateAppWidgetSize(Bundle(), minWidth, minHeight, minWidth, minHeight)
}
addView(widgetView, widgetWidth, widgetHeight)
widgetViews.add(widgetView) widgetViews.add(widgetView)
// remove the drawable so that it gets refreshed on long press // remove the drawable so that it gets refreshed on long press
@ -446,6 +434,24 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
gridItems.add(item) gridItems.add(item)
} }
private fun updateWidgetPositionAndSize(widgetView: AppWidgetHostView, item: HomeScreenGridItem): Size {
widgetView.x = calculateWidgetX(item.left)
widgetView.y = calculateWidgetY(item.top)
val widgetWidth = item.getWidthInCells() * cellWidth
val widgetHeight = item.getHeightInCells() * cellHeight
if (isSPlus()) {
val sizes = listOf(SizeF(widgetWidth.toFloat(), widgetHeight.toFloat()))
widgetView.updateAppWidgetSize(Bundle(), sizes)
} else {
widgetView.updateAppWidgetSize(Bundle(), widgetWidth, widgetHeight, widgetWidth, widgetHeight)
widgetView.layoutParams?.width = widgetWidth
widgetView.layoutParams?.height = widgetHeight
}
return Size(widgetWidth, widgetHeight)
}
private fun calculateWidgetX(leftCell: Int) = leftCell * cellWidth + sideMargins.left.toFloat() private fun calculateWidgetX(leftCell: Int) = leftCell * cellWidth + sideMargins.left.toFloat()
private fun calculateWidgetY(topCell: Int) = topCell * cellHeight + sideMargins.top.toFloat() private fun calculateWidgetY(topCell: Int) = topCell * cellHeight + sideMargins.top.toFloat()