1/** 2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { BaseStartAppHandler } from '@ohos/common'; 17import { CommonConstants } from '@ohos/common'; 18import { BigFolderViewModel } from '@ohos/bigfolder'; 19import PageDesktopViewModel from '../viewmodel/PageDesktopViewModel'; 20import { Log } from '@ohos/common'; 21import { CheckEmptyUtils } from '@ohos/common'; 22 23const TAG = 'PageDesktopStartAppHandler'; 24 25/** 26 * Desktop workspace start app processing class 27 */ 28export class PageDesktopStartAppHandler extends BaseStartAppHandler { 29 private mGridConfig; 30 private mPageDesktopStyleConfig; 31 private mFolderStyleConfig; 32 33 private constructor() { 34 super(); 35 this.mGridConfig = PageDesktopViewModel.getInstance().getGridConfig(); 36 this.mPageDesktopStyleConfig = PageDesktopViewModel.getInstance().getPageDesktopStyleConfig(); 37 this.mFolderStyleConfig = BigFolderViewModel.getInstance().getFolderStyleConfig(); 38 } 39 40 static getInstance(): PageDesktopStartAppHandler { 41 Log.showInfo(TAG, `PageDesktopStartAppHandler getInstance called!`); 42 if (globalThis.PageDesktopStartAppHandler == null) { 43 Log.showInfo(TAG, `PageDesktopStartAppHandler constructor`); 44 globalThis.PageDesktopStartAppHandler = new PageDesktopStartAppHandler(); 45 } 46 return globalThis.PageDesktopStartAppHandler; 47 } 48 49 protected calculateAppIconPosition(): void { 50 Log.showInfo(TAG, `calculateAppIconPosition called!!`); 51 if (CheckEmptyUtils.isEmpty(this.mGridConfig) || CheckEmptyUtils.isEmpty(this.mPageDesktopStyleConfig)) { 52 Log.showError(TAG, `calculateAppIconPosition with invalid config`); 53 return; 54 } 55 56 const gridWidth: number = this.mPageDesktopStyleConfig.mGridWidth; 57 const gridHeight: number = this.mPageDesktopStyleConfig.mGridHeight; 58 const column: number = this.mGridConfig.column; 59 const row: number = this.mGridConfig.row; 60 const columnsGap: number = this.mPageDesktopStyleConfig.mColumnsGap; 61 const rowGap: number = this.mPageDesktopStyleConfig.mRowsGap; 62 const gridItemHeight: number = row > 0 ? (gridHeight + rowGap) / row : 0; 63 const gridItemWidth: number = column > 0 ? (gridWidth + columnsGap) / column : 0; 64 let appItem: any = AppStorage.Get('startAppItemInfo'); 65 const startAppTypeFromPageDesktop: number = AppStorage.Get('startAppTypeFromPageDesktop'); 66 if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_APP_ICON) { 67 let paddingTop = Math.floor(gridHeight / row) - this.mPageDesktopStyleConfig.mAppItemSize; 68 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + paddingTop + appItem.row * (gridItemHeight); 69 70 let columnSize: number = (this.mPageDesktopStyleConfig.mGridWidth - (column - 1) * columnsGap) / column; 71 let iconLeftMargin: number = (columnSize - this.mPageDesktopStyleConfig.mIconSize) / 2; 72 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + iconLeftMargin + appItem.column * (gridItemWidth); 73 74 } else if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_CARD) { 75 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + this.mPageDesktopStyleConfig.mPaddingTop 76 + this.mPageDesktopStyleConfig.mIconMarginVertical + appItem.row * (gridItemHeight); 77 78 let columnSize: number = (this.mPageDesktopStyleConfig.mGridWidth - (column - 1) * columnsGap) / column; 79 let cardSize = this.mFolderStyleConfig.mGridSize; 80 let iconLeftMargin: number = (columnSize * 2 + columnsGap - cardSize) / 2 81 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + iconLeftMargin + appItem.column * (gridItemWidth); 82 83 } else if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_FOLDER) { 84 let folderItem: any = AppStorage.Get('startAppFromFolderItemInfo'); 85 const folderGridSize: number = this.mFolderStyleConfig.mGridSize; 86 const folderGridGap: number = this.mFolderStyleConfig.mFolderGridGap; 87 const folderAppSize: number = this.mFolderStyleConfig.mFolderAppSize; 88 let folderLeftMargin: number = (gridItemWidth * folderItem.area[0] - folderGridSize - columnsGap) / 2; 89 let folderRow: number = Math.floor((folderGridSize) / (folderGridGap + folderAppSize)); 90 let folderColumn: number = folderRow; 91 let folderLeftPadding: number = (folderGridSize - folderRow * (folderGridGap + folderAppSize) + folderGridGap) / 2; 92 let folderTopPadding: number = (folderGridSize - folderColumn * (folderGridGap + folderAppSize) + folderGridGap) / 2; 93 94 let index: number = this.getIndexInFolderAppList(appItem, folderItem); 95 if (index >= folderRow * folderColumn) { 96 index = 1; 97 } 98 let row: number = Math.floor(index / folderColumn); 99 let column: number = index % folderColumn; 100 if (column != 0) { 101 row += 1; 102 } else { 103 column = folderColumn; 104 } 105 Log.showInfo(TAG, `calculateAppIconPosition index ${index} row ${row} column ${column}`); 106 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + folderItem.row * (gridItemHeight) + this.mPageDesktopStyleConfig.mPaddingTop 107 + this.mPageDesktopStyleConfig.mIconMarginVertical + folderTopPadding + (row - 1) * (folderGridGap + folderAppSize); 108 109 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + folderItem.column * (gridItemWidth) + folderLeftMargin 110 + folderLeftPadding + (column - 1) * (folderGridGap + folderAppSize); 111 } 112 } 113 114 private getIndexInFolderAppList(appItem, folderItem): number { 115 let index: number = 0; 116 if (CheckEmptyUtils.isEmpty(appItem) || CheckEmptyUtils.isEmpty(folderItem) 117 || CheckEmptyUtils.isEmpty(folderItem.layoutInfo[0])) { 118 Log.showError(TAG, `getIndexInFolderAppList with invalid appItem or folderItem`); 119 } 120 121 for (var i = 0; i < folderItem.layoutInfo[0].length; i++) { 122 if (folderItem.layoutInfo[0][i]?.bundleName === appItem.bundleName) { 123 index = i; 124 break; 125 } 126 } 127 128 return index + 1; 129 } 130} 131