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 { CommonConstants } from '@ohos/common'; 18import { EventConstants } from '@ohos/common'; 19import { windowManager } from '@ohos/common'; 20import { localEventManager } from '@ohos/common'; 21import { SettingsModel } from '@ohos/common'; 22import { LayoutViewModel } from '@ohos/common'; 23import { SmartDock } from '@ohos/smartdock/component'; 24import { PageDesktopLayout } from '@ohos/pagedesktop/component'; 25import { FolderOpenComponent } from '@ohos/bigfolder/component'; 26import { BigFolderConstants, BigFolderStyleConfig } from '@ohos/bigfolder'; 27import PhoneStage from '../common/PhoneStage'; 28import StyleConstants from '../common/constants/StyleConstants'; 29import { SmartDockStyleConfig } from '@ohos/smartdock'; 30import PhonePageDesktopGridStyleConfig from '../common/PhonePageDesktopGridStyleConfig'; 31import { FormStyleConfig } from '@ohos/form'; 32import { GestureNavigationManager } from '../../../../../../feature/gesturenavigation/src/main/ets/default/common/GestureNavigationManager' 33import display from '@ohos.display'; 34import mediaquery from '@ohos.mediaquery'; 35 36const TAG = "EntryView"; 37 38interface LocalEventListener { 39 onReceiveEvent: (event: string, params: string) => void; 40} 41 42interface disPlay { 43 id: number, 44 width: number, 45 height: number, 46 refreshRate: number, 47}; 48 49@Entry 50@Component 51struct EntryView { 52 @StorageLink('screenWidth') screenWidth: number = 0; 53 @StorageLink('screenHeight') @Watch('updateScreenInfo') screenHeight: number = 0; 54 @StorageLink('deviceType') deviceType: string = CommonConstants.DEFAULT_DEVICE_TYPE; 55 @State workSpaceWidth: number = 0; 56 @State workSpaceHeight: number = 0; 57 @State dockHeight: number = 0; 58 private mStage: PhoneStage = new PhoneStage(); 59 private navigationBarStatus: string | undefined; 60 private mOrientationListener = mediaquery.matchMediaSync('(orientation: landscape)'); 61 62 onPageShow(): void { 63 Log.showInfo(TAG, 'onPageShow'); 64 } 65 66 onPageHide(): void { 67 // register orientation listener 68 this.mOrientationListener.on('change', this.onPortrait); 69 Log.showInfo(TAG, 'onPageHide'); 70 } 71 72 aboutToAppear(): void { 73 Log.showInfo(TAG, 'aboutToAppear'); 74 this.mStage.onCreate(); 75 this.navigationBarStatus = SettingsModel.getInstance().getValue(); 76 this.getWindowSize(); 77 this.updateScreenSize(); 78 79 this.registerPageDesktopNavigatorStatusChangeEvent(this.mLocalEventListener); 80 //register orientation listener 81 this.mOrientationListener.on('change', this.onPortrait); 82 } 83 84 registerPageDesktopNavigatorStatusChangeEvent(listener: LocalEventListener): void { 85 localEventManager.registerEventListener(listener, [EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE]); 86 } 87 88 private onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { 89 if (mediaQueryResult.matches) { 90 Log.showInfo(TAG, 'screen change to portrait'); 91 } else { 92 Log.showInfo(TAG, 'screen change to landscape'); 93 } 94 const gestureNavigationManager =GestureNavigationManager.getInstance(); 95 display.getDefaultDisplay() 96 .then((dis:disPlay)=>{ 97 Log.showInfo(TAG, `change to display: ${JSON.stringify(dis)}`); 98 gestureNavigationManager.updateWindowSize(dis); 99 }); 100 } 101 102 private readonly mLocalEventListener: LocalEventListener = { 103 onReceiveEvent: (event: string, params: string) => { 104 Log.showDebug(TAG, `receive event: ${event}, params: ${params}`); 105 if (event === EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE) { 106 this.navigationBarStatus = params; 107 this.updateScreenInfo(); 108 } 109 } 110 }; 111 112 aboutToDisappear(): void { 113 this.mOrientationListener.off('change', this.onPortrait); 114 this.mStage.onDestroy(); 115 Log.showInfo(TAG, 'aboutToDisappear'); 116 } 117 118 onBackPress(): boolean { 119 Log.showInfo(TAG, 'onBackPress'); 120 ContextMenu.close(); 121 AppStorage.setOrCreate('overlayMode', CommonConstants.OVERLAY_TYPE_HIDE); 122 AppStorage.setOrCreate('openFolderStatus', BigFolderConstants.OPEN_FOLDER_STATUS_CLOSE); 123 return true; 124 } 125 126 private updateScreenInfo(): void { 127 Log.showDebug(TAG, 'updateScreenInfo'); 128 if (this.screenWidth != 0 && this.screenHeight != 0) { 129 LayoutViewModel.getInstance().initScreen(this.navigationBarStatus); 130 SmartDockStyleConfig.getInstance(); 131 PhonePageDesktopGridStyleConfig.getInstance(); 132 BigFolderStyleConfig.getInstance(); 133 FormStyleConfig.getInstance(); 134 this.updateScreenSize(); 135 } 136 } 137 138 private getWindowSize(): void { 139 try { 140 this.screenWidth = px2vp(windowManager.getWindowWidth()); 141 this.screenHeight = px2vp(windowManager.getWindowHeight()); 142 AppStorage.setOrCreate('screenWidth', this.screenWidth); 143 AppStorage.setOrCreate('screenHeight', this.screenHeight); 144 } catch (error) { 145 Log.showError(TAG, `getWindowWidth or getWindowHeight error: ${error}`); 146 } 147 } 148 149 private updateScreenSize(): void { 150 this.workSpaceWidth = this.screenWidth; 151 this.workSpaceHeight = LayoutViewModel.getInstance().getWorkSpaceHeight() as number; 152 this.dockHeight = LayoutViewModel.getInstance().getDockHeight() as number; 153 AppStorage.setOrCreate('workSpaceWidth', this.workSpaceWidth); 154 AppStorage.setOrCreate('workSpaceHeight', this.workSpaceHeight); 155 AppStorage.setOrCreate('dockHeight', this.dockHeight); 156 Log.showDebug(TAG, `updateScreenSize product: ${this.deviceType}, screenWidth: ${this.screenWidth}, screenHeight: ${this.screenHeight}, 157 workSpaceWidth: ${this.workSpaceWidth}, workSpaceHeight: ${this.workSpaceHeight}, dockHeight: ${this.dockHeight}`); 158 } 159 160 build() { 161 Stack() { 162 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) { 163 Column() { 164 PageDesktopLayout(); 165 } 166 .height(this.workSpaceHeight) 167 .onAreaChange((oldValue: Area, newValue: Area) => { 168 Log.showDebug(TAG, `onAreaChange navigationBarStatus: ${this.navigationBarStatus}`); 169 if (JSON.stringify(oldValue) == JSON.stringify(newValue)) return; 170 if (this.navigationBarStatus == "1") { 171 setTimeout(() => { 172 SettingsModel.getInstance().setValue(this.navigationBarStatus); 173 }, 50) 174 } 175 }) 176 177 Column() { 178 SmartDock(); 179 } 180 .height(this.dockHeight) 181 } 182 183 FolderOpenComponent(); 184 } 185 .backgroundImage(StyleConstants.DEFAULT_BACKGROUND_IMAGE) 186 .backgroundImageSize(ImageSize.Cover) 187 .backgroundImagePosition(Alignment.Center) 188 .width('100%') 189 .height('100%') 190 } 191} 192