1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.taskbar.customization 18 19 import com.android.launcher3.taskbar.TaskbarActivityContext 20 21 /** Evaluates the taskbar specs based on the taskbar grid size and the taskbar icon size. */ 22 class TaskbarSpecsEvaluator( 23 private val taskbarActivityContext: TaskbarActivityContext, 24 private val taskbarFeatureEvaluator: TaskbarFeatureEvaluator, 25 numRows: Int = taskbarActivityContext.deviceProfile.inv.numRows, 26 numColumns: Int = taskbarActivityContext.deviceProfile.inv.numColumns, 27 ) { 28 var taskbarIconSize: TaskbarIconSize = getIconSizeByGrid(numColumns, numRows) 29 val numShownHotseatIcons 30 get() = taskbarActivityContext.deviceProfile.numShownHotseatIcons 31 32 // TODO(b/341146605) : initialize it to taskbar container in later cl. 33 private var taskbarContainer: List<TaskbarContainer> = emptyList() 34 35 val taskbarIconPadding: Int = 36 if ( 37 TaskbarIconSpecs.transientOrPinnedTaskbarIconPaddingSize.size > taskbarIconSize.size && 38 !taskbarFeatureEvaluator.hasNavButtons 39 ) { 40 (TaskbarIconSpecs.iconSize52dp.size - taskbarIconSize.size) / 2 41 } else { 42 0 43 } 44 45 val taskbarIconMargin: TaskbarIconMarginSize = 46 if (taskbarFeatureEvaluator.isTransient) { 47 TaskbarIconSpecs.defaultTransientIconMargin 48 } else { 49 TaskbarIconSpecs.defaultPersistentIconMargin 50 } 51 getIconSizeByGridnull52 fun getIconSizeByGrid(columns: Int, rows: Int): TaskbarIconSize { 53 return if (taskbarFeatureEvaluator.isTransient) { 54 TaskbarIconSpecs.transientTaskbarIconSizeByGridSize.getOrDefault( 55 TransientTaskbarIconSizeKey(columns, rows, taskbarFeatureEvaluator.isLandscape), 56 TaskbarIconSpecs.defaultTransientIconSize, 57 ) 58 } else { 59 TaskbarIconSpecs.defaultPersistentIconSize 60 } 61 } 62 getIconSizeStepDownnull63 fun getIconSizeStepDown(iconSize: TaskbarIconSize): TaskbarIconSize { 64 if (!taskbarFeatureEvaluator.isTransient) return TaskbarIconSpecs.defaultPersistentIconSize 65 66 val currentIconSizeIndex = TaskbarIconSpecs.transientTaskbarIconSizes.indexOf(iconSize) 67 // return the current icon size if supplied icon size is unknown or we have reached the 68 // min icon size. 69 return if (currentIconSizeIndex == -1 || currentIconSizeIndex == 0) { 70 iconSize 71 } else { 72 TaskbarIconSpecs.transientTaskbarIconSizes[currentIconSizeIndex - 1] 73 } 74 } 75 getIconSizeStepUpnull76 fun getIconSizeStepUp(iconSize: TaskbarIconSize): TaskbarIconSize { 77 if (!taskbarFeatureEvaluator.isTransient) return TaskbarIconSpecs.defaultPersistentIconSize 78 79 val currentIconSizeIndex = TaskbarIconSpecs.transientTaskbarIconSizes.indexOf(iconSize) 80 // return the current icon size if supplied icon size is unknown or we have reached the 81 // max icon size. 82 return if ( 83 currentIconSizeIndex == -1 || 84 currentIconSizeIndex == TaskbarIconSpecs.transientTaskbarIconSizes.size - 1 85 ) { 86 iconSize 87 } else { 88 TaskbarIconSpecs.transientTaskbarIconSizes[currentIconSizeIndex + 1] 89 } 90 } 91 92 // TODO(jagrutdesai) : Call this in init once the containers are ready. calculateTaskbarIconSizenull93 private fun calculateTaskbarIconSize() { 94 while ( 95 taskbarIconSize != TaskbarIconSpecs.minimumIconSize && 96 taskbarActivityContext.transientTaskbarBounds.width() < 97 calculateSpaceNeeded(taskbarContainer) 98 ) { 99 taskbarIconSize = getIconSizeStepDown(taskbarIconSize) 100 } 101 } 102 calculateSpaceNeedednull103 private fun calculateSpaceNeeded(containers: List<TaskbarContainer>): Int { 104 return containers.sumOf { it.spaceNeeded } 105 } 106 } 107 108 data class TaskbarIconSize(val size: Int) 109 110 data class TransientTaskbarIconSizeKey(val columns: Int, val rows: Int, val isLandscape: Boolean) 111 112 data class TaskbarIconMarginSize(val size: Int) 113