1/** 2 * Copyright (c) 2021-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 { Log } from '@ohos/common'; 17import { Trace } from '@ohos/common'; 18import { StyleConstants } from '@ohos/common'; 19import { CommonConstants } from '@ohos/common'; 20import { FolderComponent } from '@ohos/common' 21import { PresetStyleConstants } from '@ohos/common'; 22import { BigFolderViewModel } from '@ohos/bigfolder'; 23import { BigFolderStyleConfig } from '@ohos/bigfolder'; 24import { PageDesktopDragHandler } from '../PageDesktopDragHandler'; 25import PageDesktopViewModel from '../../viewmodel/PageDesktopViewModel' 26import { PageDesktopStartAppHandler } from '../PageDesktopStartAppHandler'; 27import { BigFolderConstants } from '@ohos/bigfolder'; 28 29const TAG = 'FolderItem'; 30 31/** 32 * Folder item, which display on desktop workspace. 33 */ 34@Component 35export default struct FolderItem { 36 @StorageLink('dragItemInfo') pageDesktopDragItemInfo: any = {}; 37 @StorageLink('dragItemType') dragItemType: number = CommonConstants.DRAG_FROM_DESKTOP; 38 @StorageLink('openFolderStatus') openFolderStatus: number = BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE; 39 @StorageLink('selectDesktopAppItem') selectDesktopAppItem: string = ''; 40 private mAppNameHeight: number = StyleConstants.DEFAULT_APP_NAME_HEIGHT; 41 private mAppNameSize: number = StyleConstants.DEFAULT_APP_NAME_SIZE; 42 private folderItem: any; 43 private isSwappingPage = false; 44 private mAppItemWidth: number = 0; 45 private mPageDesktopViewModel: PageDesktopViewModel; 46 private mBigFolderViewModel: BigFolderViewModel; 47 private mMargin: number = 0; 48 private mGridSpaceWidth: number = 0; 49 private mGridSpaceHeight: number = 0; 50 private mIconMarginVertical: number = StyleConstants.DEFAULT_10; 51 private mPageDesktopDragHandler: PageDesktopDragHandler; 52 private mPageDesktopStartAppHandler: PageDesktopStartAppHandler; 53 private mFolderStyleConfig: BigFolderStyleConfig; 54 mNameLines: number = PresetStyleConstants.DEFAULT_APP_NAME_LINES; 55 56 @Builder dragLayerBuilder() { 57 Column() { 58 FolderComponent({ 59 showFolderName: false, 60 nameFontColor: this.mPageDesktopViewModel.getPageDesktopStyleConfig().mNameFontColor, 61 folderNameHeight: 0, 62 folderNameSize: 0, 63 mFolderItem: this.folderItem, 64 folderGridSize: this.mFolderStyleConfig.mGridSize * 1.05, 65 appIconSize: this.mFolderStyleConfig.mFolderAppSize * 1.05, 66 gridMargin: this.mFolderStyleConfig.mGridMargin * 1.05, 67 gridGap: this.mFolderStyleConfig.mFolderGridGap * 1.05, 68 buildMenu: (folderItem) => { 69 return []; 70 } 71 }) 72 } 73 .height(this.mFolderStyleConfig.mGridSize * 1.05 + StyleConstants.DEFAULT_40) 74 .width(this.mFolderStyleConfig.mGridSize * 1.05) 75 } 76 77 aboutToAppear(): void { 78 this.mPageDesktopDragHandler = PageDesktopDragHandler.getInstance(); 79 this.mPageDesktopStartAppHandler = PageDesktopStartAppHandler.getInstance(); 80 this.mBigFolderViewModel = BigFolderViewModel.getInstance(); 81 this.mFolderStyleConfig = this.mBigFolderViewModel.getFolderStyleConfig(); 82 this.mPageDesktopViewModel = PageDesktopViewModel.getInstance(); 83 let mGridConfig = this.mPageDesktopViewModel.getGridConfig(); 84 let styleConfig = this.mPageDesktopViewModel.getPageDesktopStyleConfig(); 85 this.mAppItemWidth = styleConfig.mIconSize; 86 this.mAppNameHeight = styleConfig.mNameHeight; 87 this.mAppNameSize = styleConfig.mNameSize; 88 this.mIconMarginVertical = styleConfig.mIconMarginVertical; 89 this.mMargin = styleConfig.mMargin; 90 this.mGridSpaceWidth = Number(this.mPageDesktopViewModel.getWorkSpaceWidth()) - this.mMargin; 91 this.mGridSpaceHeight = Number(this.mPageDesktopViewModel.getWorkSpaceHeight()); 92 this.openFolderStatus = AppStorage.Get('openFolderStatus') != 'undefined' ? AppStorage.Get('openFolderStatus') : BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE; 93 this.formatFolderInfo(); 94 } 95 96 /** 97 * format FolderInfo 98 */ 99 private formatFolderInfo() { 100 let column = this.mBigFolderViewModel.getFolderLayoutConfig().column; 101 let row = this.mBigFolderViewModel.getFolderLayoutConfig().row; 102 const allCount = column * row; 103 if (this.folderItem.layoutInfo[0].length > allCount) { 104 let folderLayoutInfoList = []; 105 let integer = Math.floor(this.folderItem.layoutInfo[0].length / allCount); 106 let remainder = this.folderItem.layoutInfo[0].length % allCount; 107 for (let i = 0; i < integer; i++) { 108 folderLayoutInfoList.push(this.folderItem.layoutInfo[0].slice(i * allCount, (i + 1) * allCount)); 109 } 110 if (remainder != 0) { 111 folderLayoutInfoList.push(this.folderItem.layoutInfo[0].slice(integer * allCount, integer * allCount + remainder)); 112 } 113 this.folderItem.layoutInfo.splice(0, 1); 114 this.folderItem.layoutInfo = folderLayoutInfoList; 115 } 116 } 117 118 /** 119 * When rename is clicked, call this function to change folder state. 120 */ 121 private renameClick() { 122 Log.showInfo(TAG, 'click menu folder rename'); 123 AppStorage.SetOrCreate('overlayMode', CommonConstants.OVERLAY_TYPE_HIDE); 124 this.mBigFolderViewModel.openFolder(true, this.folderItem); 125 } 126 127 private getOpenFolder(): string { 128 let openFolderData: { 129 folderId: string, 130 layoutInfo: any 131 } = AppStorage.Get('openFolderData'); 132 133 return openFolderData.folderId; 134 } 135 136 dragStart(event: DragEvent): CustomBuilder { 137 AppStorage.SetOrCreate('isDrag', true); 138 ContextMenu.close(); 139 this.dragItemType = CommonConstants.DRAG_FROM_DESKTOP; 140 this.pageDesktopDragItemInfo = this.folderItem; 141 const selectAppIndex = globalThis.PageDesktopDragHandler.getItemIndex(event.getX(), event.getY()); 142 const startPosition = globalThis.PageDesktopDragHandler.getTouchPosition(event.getX(), event.getY()) 143 globalThis.PageDesktopDragHandler.mStartPosition = startPosition; 144 AppStorage.SetOrCreate('selectAppIndex', selectAppIndex); 145 Log.showInfo(TAG, `onDragStart event: [${event.getX()}, ${event.getY()}], selectAppIndex: ${selectAppIndex}`); 146 const rowOffset = startPosition.row - this.pageDesktopDragItemInfo.row; 147 const columnOffset = startPosition.column - this.pageDesktopDragItemInfo.column; 148 const positionOffset = [columnOffset, rowOffset]; 149 AppStorage.SetOrCreate('positionOffset', positionOffset); 150 return this.dragLayerBuilder(); 151 } 152 153 build() { 154 Flex({ 155 direction: FlexDirection.Column, 156 alignItems: ItemAlign.Center, 157 justifyContent: FlexAlign.SpaceAround 158 }) { 159 Column() { 160 FolderComponent({ 161 showFolderName: true, 162 mFolderItem: this.folderItem, 163 badgeNumber: this.folderItem.badgeNumber, 164 isSelect: this.selectDesktopAppItem === this.folderItem.folderId, 165 folderNameHeight: this.mAppNameHeight, 166 folderNameLines: this.mNameLines, 167 folderNameSize: this.mAppNameSize, 168 folderGridSize:this.mFolderStyleConfig.mGridSize, 169 appIconSize: this.mFolderStyleConfig.mFolderAppSize, 170 gridMargin:this.mFolderStyleConfig.mGridMargin, 171 mPaddingTop: this.mIconMarginVertical, 172 gridGap:this.mFolderStyleConfig.mFolderGridGap, 173 iconNameMargin: this.mFolderStyleConfig.mIconNameMargin, 174 nameFontColor: this.mPageDesktopViewModel.getPageDesktopStyleConfig().mNameFontColor, 175 onAppIconClick: (event, appItem) => { 176 Log.showInfo(TAG, "onAppIconClick"); 177 Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION); 178 this.setStartAppInfo(appItem); 179 this.mPageDesktopViewModel.openApplication(appItem.abilityName, appItem.bundleName, appItem.moduleName); 180 }, 181 onOpenFolderClick: (event, folderItem) => { 182 Log.showInfo(TAG, "onOpenFolderClick"); 183 Trace.start(Trace.CORE_METHOD_OPEN_FOLDER); 184 this.mBigFolderViewModel.openFolder(false, folderItem); 185 }, 186 onFolderTouch: (event, folderItem) => { 187 if (event.type === CommonConstants.TOUCH_TYPE_UP && AppStorage.Get('isDrag')) { 188 let mIsDragEffectArea = globalThis.PageDesktopDragHandler.isDragEffectArea(event.touches[0].screenX, event.touches[0].screenY); 189 Log.showInfo(TAG, `onTouch mIsDragEffectArea: ${mIsDragEffectArea}`); 190 if (!mIsDragEffectArea) { 191 globalThis.PageDesktopDragHandler.deleteBlankPageOutsideEffect(); 192 AppStorage.SetOrCreate('dragItemInfo', {}); 193 AppStorage.SetOrCreate('selectAppIndex', null); 194 AppStorage.SetOrCreate('isDrag', false); 195 } 196 } 197 }, 198 onGetPosition: (getPosition: Function) => { 199 Log.showInfo(TAG, "onGetPosition"); 200 let styleConfig = this.mPageDesktopViewModel.getPageDesktopStyleConfig(); 201 let row = this.folderItem.row; 202 let column = this.folderItem.column; 203 Log.showInfo(TAG, `onGetPosition currentFolderPosition row: ${row}, column: ${column}`); 204 let x = styleConfig.mAppItemSize * column 205 + styleConfig.mColumnsGap * column + styleConfig.mMargin + styleConfig.mIconMarginHorizontal; 206 let y = styleConfig.mAppItemSize * row 207 + styleConfig.mRowsGap * row + styleConfig.mDesktopMarginTop + styleConfig.mIconMarginVertical; 208 Log.showInfo(TAG, `onGetPosition currentFolderPosition x: ${x}, y: ${y}`); 209 getPosition(x, y); 210 }, 211 buildMenu: (folderItem) => 212 this.mPageDesktopViewModel.buildRenameMenuInfoList(folderItem, this.renameClick.bind(this)), 213 dragStart: this.dragStart.bind(this) 214 }) 215 } 216 .visibility((this.pageDesktopDragItemInfo.folderId === this.folderItem.folderId || (this.openFolderStatus != 0 && this.getOpenFolder() == this.folderItem.folderId)) ? Visibility.Hidden : Visibility.Visible) 217 .onMouse((event: MouseEvent) => { 218 if (event.button == MouseButton.Right) { 219 event.stopPropagation(); 220 Log.showInfo(TAG, 'onMouse mouse button right'); 221 AppStorage.SetOrCreate('selectDesktopAppItem', this.folderItem.folderId); 222 } 223 }) 224 .gesture( 225 GestureGroup(GestureMode.Exclusive, 226 TapGesture({ count: 2 }) 227 .onAction((event: GestureEvent) => { 228 Log.showInfo(TAG, 'TapGesture double click'); 229 this.mBigFolderViewModel.openFolder(false, this.folderItem); 230 }) 231 ) 232 ) 233 } 234 .width(StyleConstants.PERCENTAGE_100) 235 .height(StyleConstants.PERCENTAGE_100) 236 } 237 238 /** 239 * set start app info 240 */ 241 private setStartAppInfo(appItem) { 242 AppStorage.SetOrCreate('startAppItemInfo', appItem); 243 AppStorage.SetOrCreate('startAppFromFolderItemInfo', this.folderItem); 244 AppStorage.SetOrCreate('startAppTypeFromPageDesktop', CommonConstants.OVERLAY_TYPE_FOLDER); 245 this.mPageDesktopStartAppHandler.setAppIconSize(this.mFolderStyleConfig.mFolderAppSize); 246 this.mPageDesktopStartAppHandler.setAppIconInfo(); 247 } 248}