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 { 17 Log, 18 Trace, 19 StyleConstants, 20 CommonConstants, 21 CloseAppManager } from '@ohos/common'; 22import GridSwiper from '../common/components/GridSwiper'; 23import { PageDesktopDragHandler } from '../common/PageDesktopDragHandler'; 24import PageDesktopViewModel from '../viewmodel/PageDesktopViewModel'; 25import { PageDesktopCloseAppHandler } from '../common/PageDesktopCloseAppHandler'; 26 27let mPageDesktopViewModel: PageDesktopViewModel = null; 28const TAG = "PageDesktopLayout"; 29 30@Component 31export struct PageDesktopLayout { 32 @StorageLink('workSpaceWidth') @Watch('updateDeskTopParams') workSpaceWidth: number = 0; 33 @StorageLink('workSpaceHeight') @Watch('updateDeskTopParams') workSpaceHeight: number = 0; 34 @State @Watch('updateDeskTopParams') mMargin: number = 0; 35 @State mTop: number = 0; 36 @State @Watch('changeGridConfig') gridConfig: string = ''; 37 @StorageLink('menuId') menuId: number = 0; 38 private mPageDesktopDragHandler: PageDesktopDragHandler = null; 39 private isPad: boolean = false; 40 private deviceType: string = CommonConstants.DEFAULT_DEVICE_TYPE; 41 dialogController: CustomDialogController = new CustomDialogController({ 42 builder: settingDialog({ cancel: this.onCancel, confirm: this.confirm, onAccept: this.onAccept }), 43 cancel: this.onCancel, 44 autoCancel: true, 45 alignment: DialogAlignment.Bottom, 46 customStyle: true 47 }) 48 49 onCancel() { 50 } 51 52 onAccept() { 53 mPageDesktopViewModel.addOrDeleteBlankPage(); 54 } 55 56 confirm() { 57 Trace.start(Trace.CORE_METHOD_START_SETTINGS); 58 mPageDesktopViewModel.intoSetting(); 59 } 60 61 aboutToAppear(): void { 62 this.deviceType = AppStorage.Get('deviceType'); 63 this.mPageDesktopDragHandler = PageDesktopDragHandler.getInstance(); 64 mPageDesktopViewModel = PageDesktopViewModel.getInstance(); 65 this.gridConfig = mPageDesktopViewModel.getGridConfig().layout; 66 this.updateStyle(); 67 if (this.deviceType != CommonConstants.PAD_DEVICE_TYPE) { 68 mPageDesktopViewModel.registerAppListChangeCallback(); 69 } 70 CloseAppManager.getInstance().registerCloseAppHandler(new PageDesktopCloseAppHandler()); 71 } 72 73 aboutToDisappear(): void { 74 delete this.dialogController; 75 this.dialogController = null; 76 } 77 78 private updateStyle() { 79 mPageDesktopViewModel.setDevice(this.deviceType); 80 this.isPad = mPageDesktopViewModel.getDevice(); 81 Log.showDebug(TAG, `updateStyle isPad: ${this.isPad}`); 82 } 83 84 private updateDeskTopParams() { 85 this.mMargin = PageDesktopViewModel.getInstance().getPageDesktopStyleConfig().mMargin; 86 this.mTop = PageDesktopViewModel.getInstance().getPageDesktopStyleConfig().mDesktopMarginTop; 87 Log.showDebug(TAG, `updateDeskTopParams mMargin: ${this.mMargin}, this.mTop: ${this.mTop}`); 88 if (this.mPageDesktopDragHandler != null) { 89 this.mPageDesktopDragHandler.setDragEffectArea({ 90 left: this.mMargin, 91 top: this.mTop, 92 right: this.workSpaceWidth - this.mMargin, 93 bottom: PageDesktopViewModel.getInstance().getPageDesktopStyleConfig().mGridHeight + this.mTop 94 }); 95 } 96 } 97 98 private changeGridConfig(): void { 99 Log.showDebug(TAG, `changeGridConfig GridConfig: ${this.gridConfig}`); 100 this.updateDeskTopParams(); 101 } 102 103 private buildLog(): boolean { 104 Log.showDebug(TAG, 'build start'); 105 return true; 106 } 107 108 build() { 109 GridSwiper({ 110 gridConfig: this.gridConfig, 111 mPageDesktopViewModel: mPageDesktopViewModel, 112 dialogController: this.deviceType == CommonConstants.PAD_DEVICE_TYPE ? null : this.dialogController 113 }) 114 .width(StyleConstants.PERCENTAGE_100) 115 .height(StyleConstants.PERCENTAGE_100) 116 } 117} 118 119@CustomDialog 120struct settingDialog { 121 @StorageLink('NavigationBarStatusValue') navigationBarStatusValue: boolean = false; 122 controller: CustomDialogController 123 cancel: () => void 124 confirm: () => void 125 onAccept: () => void 126 127 aboutToDisappear(): void { 128 this.controller = null; 129 this.cancel = null; 130 this.confirm = null; 131 this.onAccept = null; 132 } 133 134 build() { 135 Stack() { 136 Column() { 137 138 } 139 .blur(40) 140 .width('100%') 141 .height(120) 142 .border({ 143 radius: StyleConstants.DEFAULT_24 144 }) 145 146 Column() { 147 Row() { 148 Text($r('app.string.into_settings')) 149 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 150 .fontColor(StyleConstants.TEXT_COLOR_PRIMARY) 151 }.margin({ top: StyleConstants.DEFAULT_24, bottom: StyleConstants.DEFAULT_16 }) 152 153 Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceEvenly }) { 154 Button() { 155 Text($r('app.string.cancel')) 156 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 157 .fontColor(StyleConstants.BUTTON_FONT_COLOR) 158 } 159 .backgroundColor(StyleConstants.DEFAULT_BG_COLOR) 160 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 161 .width(StyleConstants.DEFAULT_BUTTON_WIDTH) 162 .onClick(() => { 163 this.cancel() 164 this.controller.close(); 165 }) 166 167 Divider() 168 .width(5) 169 .vertical(true) 170 .color(StyleConstants.DEFAULT_DIVIDER_COLOR) 171 .height(StyleConstants.DEFAULT_DIVIDER_HEIGHT) 172 173 Button() { 174 Text(mPageDesktopViewModel.isBlankPage() ? $r('app.string.delete_blank_page') : $r('app.string.add_blank_page')) 175 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 176 .fontColor(StyleConstants.BUTTON_FONT_COLOR) 177 } 178 .backgroundColor(StyleConstants.DEFAULT_BG_COLOR) 179 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 180 .width(StyleConstants.DEFAULT_BUTTON_WIDTH) 181 .onClick(() => { 182 this.controller.close(); 183 this.onAccept() 184 }) 185 186 Divider() 187 .width(5) 188 .vertical(true) 189 .color(StyleConstants.DEFAULT_DIVIDER_COLOR) 190 .height(StyleConstants.DEFAULT_DIVIDER_HEIGHT) 191 192 Button() { 193 Text($r('app.string.launcher_edit')) 194 .fontSize(StyleConstants.DEFAULT_BADGE_FONT_SIZE) 195 .fontColor(StyleConstants.BUTTON_FONT_COLOR) 196 } 197 .backgroundColor(StyleConstants.DEFAULT_BG_COLOR) 198 .height(StyleConstants.DEFAULT_BUTTON_HEIGHT) 199 .width(StyleConstants.DEFAULT_BUTTON_WIDTH) 200 .onClick(() => { 201 this.confirm() 202 this.controller.close(); 203 204 }) 205 } 206 } 207 .backgroundColor(Color.White) 208 .opacity(0.85) 209 .width('100%') 210 .padding({ 211 bottom: StyleConstants.DEFAULT_24 212 }) 213 .border({ 214 radius: StyleConstants.DEFAULT_24 215 }) 216 } 217 .margin({ right: StyleConstants.DEFAULT_12, left: StyleConstants.DEFAULT_12, 218 bottom: this.navigationBarStatusValue ? StyleConstants.DEFAULT_12 : StyleConstants.DEFAULT_40 }) 219 } 220}