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 { StyleConstants } from '@ohos/common'; 18import { CommonConstants } from '@ohos/common'; 19import { windowManager } from '@ohos/common'; 20import { LayoutViewModel } from '@ohos/common'; 21import { SmartDock } from '@ohos/smartdock/component'; 22import { AppGridLayout } from '@ohos/appcenter/component'; 23import { AppGridViewModel } from '@ohos/appcenter'; 24 25const TAG = 'AppCenterView'; 26 27interface AnimationInfo { 28 appScaleX: number; 29 appScaleY: number; 30} 31 32@Entry 33@Component 34struct AppCenterView { 35 @State workSpaceHeight: number = 0; 36 @State dockHeight: number = 0; 37 @State device: string = CommonConstants.PAD_DEVICE_TYPE; 38 private mLayoutViewModel?: LayoutViewModel; 39 mAppGridViewModel: AppGridViewModel = AppGridViewModel.getInstance(); 40 @StorageLink('animationInfo_alpha') mAppAlpha: number = 1.0; 41 @StorageLink('animationInfo_scale') mAnimationInfo: AnimationInfo = { 42 appScaleX: 1.0, 43 appScaleY: 1.0, 44 } 45 46 aboutToAppear(): void { 47 Log.showInfo(TAG, 'aboutToAppear'); 48 AppStorage.setOrCreate('deviceType', this.device); 49 this.mLayoutViewModel = LayoutViewModel.getInstance(); 50 } 51 52 onPageShow(): void { 53 Log.showInfo(TAG, 'onPageShow'); 54 // move appCenter startup front when power on 55 this.updateScreenSize(); 56 this.mAppGridViewModel = AppGridViewModel.getInstance(); 57 this.mAppGridViewModel.registerAppListChange(); 58 this.mAppGridViewModel.registerEventListener(); 59 } 60 61 onPageHide(): void { 62 Log.showInfo(TAG, 'onPageHide'); 63 } 64 65 aboutToDisappear(): void { 66 Log.showInfo(TAG, 'aboutToDisappear'); 67 this.mAppGridViewModel.unregisterAppListChange(); 68 this.mAppGridViewModel.unregisterEventListener(); 69 } 70 71 private async updateScreenSize(): Promise<void> { 72 this.workSpaceHeight = LayoutViewModel.getInstance().getWorkSpaceHeight() as number; 73 this.dockHeight = LayoutViewModel.getInstance().getDockHeight() as number; 74 } 75 76 onBackPress(): boolean { 77 Log.showInfo(TAG, 'onBackPress'); 78 AppStorage.setOrCreate('selectDesktopAppItem', ''); 79 windowManager.destroyWindow(windowManager.APP_CENTER_WINDOW_NAME); 80 return true; 81 } 82 83 private buildLog(): boolean { 84 Log.showDebug(TAG, 'AppCenterView buildLog'); 85 return true; 86 } 87 88 build() { 89 Stack() { 90 if (this.buildLog()) {} 91 Column() { 92 Column() { 93 AppGridLayout(); 94 } 95 .padding({ 96 top: StyleConstants.DEFAULT_28 97 }) 98 .alignItems(HorizontalAlign.Center) 99 .justifyContent(FlexAlign.Center) 100 101 Column() { 102 SmartDock(); 103 } 104 .height(this.dockHeight) 105 } 106 .width('100%') 107 .height('100%') 108 .scale({ x: this.mAnimationInfo.appScaleX, y: this.mAnimationInfo.appScaleY }) 109 .opacity(this.mAppAlpha) 110 } 111 .backgroundImage('/common/pics/ic_wallpaper_recent.jpg') 112 .backgroundImageSize(ImageSize.Cover) 113 .width('100%') 114 .height('100%') 115 .onClick(() => { 116 Log.showInfo(TAG, 'click appcenter area'); 117 const contextFlag: boolean = AppStorage.get('contextMenuState') as boolean; 118 Log.showInfo(TAG, 'onClick contextFlag: ' + contextFlag); 119 if (contextFlag && !ContextMenu.close()) { 120 AppStorage.setOrCreate('contextMenuState', false); 121 } else { 122 this.onBackPress(); 123 } 124 }) 125 } 126} 127