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 { 17 BaseStartAppHandler, 18 CommonConstants, 19 Log, 20 CheckEmptyUtils 21} from '@ohos/common'; 22import { BigFolderViewModel } from '@ohos/bigfolder'; 23import { PageDesktopViewModel } from '../viewmodel/PageDesktopViewModel'; 24 25const TAG = 'PageDesktopStartAppHandler'; 26 27/** 28 * Desktop workspace start app processing class 29 */ 30export class PageDesktopStartAppHandler extends BaseStartAppHandler { 31 private mGridConfig; 32 private mPageDesktopStyleConfig; 33 private mFolderStyleConfig; 34 35 private constructor() { 36 super(); 37 this.mGridConfig = PageDesktopViewModel.getInstance().getGridConfig(); 38 this.mPageDesktopStyleConfig = PageDesktopViewModel.getInstance().getPageDesktopStyleConfig(); 39 this.mFolderStyleConfig = BigFolderViewModel.getInstance().getFolderStyleConfig(); 40 } 41 42 static getInstance(): PageDesktopStartAppHandler { 43 Log.showInfo(TAG, `PageDesktopStartAppHandler getInstance called!`); 44 if (globalThis.PageDesktopStartAppHandler == null) { 45 Log.showInfo(TAG, `PageDesktopStartAppHandler constructor`); 46 globalThis.PageDesktopStartAppHandler = new PageDesktopStartAppHandler(); 47 } 48 return globalThis.PageDesktopStartAppHandler; 49 } 50 51 protected calculateAppIconPosition(): void { 52 Log.showInfo(TAG, `calculateAppIconPosition called!!`); 53 if (CheckEmptyUtils.isEmpty(this.mGridConfig) || CheckEmptyUtils.isEmpty(this.mPageDesktopStyleConfig)) { 54 Log.showError(TAG, `calculateAppIconPosition with invalid config`); 55 return; 56 } 57 58 const gridWidth: number = this.mPageDesktopStyleConfig.mGridWidth; 59 const gridHeight: number = this.mPageDesktopStyleConfig.mGridHeight; 60 const column: number = this.mGridConfig.column; 61 const row: number = this.mGridConfig.row; 62 const columnsGap: number = this.mPageDesktopStyleConfig.mColumnsGap; 63 const rowGap: number = this.mPageDesktopStyleConfig.mRowsGap; 64 const gridItemHeight: number = row > 0 ? (gridHeight + rowGap) / row : 0; 65 const gridItemWidth: number = column > 0 ? (gridWidth + columnsGap) / column : 0; 66 let appItem: any = AppStorage.get('startAppItemInfo'); 67 const startAppTypeFromPageDesktop: number = AppStorage.get('startAppTypeFromPageDesktop'); 68 if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_APP_ICON) { 69 let paddingTop = Math.floor(gridHeight / row) - this.mPageDesktopStyleConfig.mAppItemSize; 70 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + paddingTop + appItem.row * (gridItemHeight); 71 72 let columnSize: number = (this.mPageDesktopStyleConfig.mGridWidth - (column - 1) * columnsGap) / column; 73 let iconLeftMargin: number = (columnSize - this.mPageDesktopStyleConfig.mIconSize) / 2; 74 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + iconLeftMargin + appItem.column * (gridItemWidth); 75 76 } else if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_CARD) { 77 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + this.mPageDesktopStyleConfig.mPaddingTop 78 + this.mPageDesktopStyleConfig.mIconMarginVertical + appItem.row * (gridItemHeight); 79 80 let columnSize: number = (this.mPageDesktopStyleConfig.mGridWidth - (column - 1) * columnsGap) / column; 81 let cardSize = this.mFolderStyleConfig.mGridSize; 82 let iconLeftMargin: number = (columnSize * 2 + columnsGap - cardSize) / 2 83 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + iconLeftMargin + appItem.column * (gridItemWidth); 84 85 } else if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_FOLDER) { 86 let folderItem: any = AppStorage.get('startAppFromFolderItemInfo'); 87 const folderGridSize: number = this.mFolderStyleConfig.mGridSize; 88 const folderGridGap: number = this.mFolderStyleConfig.mFolderGridGap; 89 const folderAppSize: number = this.mFolderStyleConfig.mFolderAppSize; 90 let folderLeftMargin: number = (gridItemWidth * folderItem.area[0] - folderGridSize - columnsGap) / 2; 91 let folderRow: number = Math.floor((folderGridSize) / (folderGridGap + folderAppSize)); 92 let folderColumn: number = folderRow; 93 let folderLeftPadding: number = (folderGridSize - folderRow * (folderGridGap + folderAppSize) + folderGridGap) / 2; 94 let folderTopPadding: number = (folderGridSize - folderColumn * (folderGridGap + folderAppSize) + folderGridGap) / 2; 95 96 let index: number = this.getIndexInFolderAppList(appItem, folderItem); 97 if (index >= folderRow * folderColumn) { 98 index = 1; 99 } 100 let row: number = Math.floor(index / folderColumn); 101 let column: number = index % folderColumn; 102 if (column != 0) { 103 row += 1; 104 } else { 105 column = folderColumn; 106 } 107 Log.showInfo(TAG, `calculateAppIconPosition index ${index} row ${row} column ${column}`); 108 this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + folderItem.row * (gridItemHeight) + this.mPageDesktopStyleConfig.mPaddingTop 109 + this.mPageDesktopStyleConfig.mIconMarginVertical + folderTopPadding + (row - 1) * (folderGridGap + folderAppSize); 110 111 this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + folderItem.column * (gridItemWidth) + folderLeftMargin 112 + folderLeftPadding + (column - 1) * (folderGridGap + folderAppSize); 113 } 114 } 115 116 private getIndexInFolderAppList(appItem, folderItem): number { 117 let index: number = 0; 118 if (CheckEmptyUtils.isEmpty(appItem) || CheckEmptyUtils.isEmpty(folderItem) 119 || CheckEmptyUtils.isEmpty(folderItem.layoutInfo[0])) { 120 Log.showError(TAG, 'getIndexInFolderAppList with invalid appItem or folderItem'); 121 } 122 123 for (var i = 0; i < folderItem.layoutInfo[0].length; i++) { 124 if (folderItem.layoutInfo[0][i]?.bundleName === appItem.bundleName) { 125 index = i; 126 break; 127 } 128 } 129 130 return index + 1; 131 } 132} 133