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 AppMenu, 18} from '@ohos/common/component' 19import { 20 Log, 21 Trace, 22 StyleConstants, 23 CommonConstants, 24 MenuInfo, 25 localEventManager, 26 EventConstants, 27 LauncherDragItemInfo 28} from '@ohos/common'; 29import SwiperPage from './SwiperPage'; 30import { PageDesktopViewModel } from '../../viewmodel/PageDesktopViewModel'; 31 32const TAG = 'GridSwiper'; 33 34interface AppListInfo { 35 appGridInfo: LauncherDragItemInfo[][] 36} 37 38@Component 39export default struct GridSwiper { 40 @StorageLink('NavigationBarStatusValue') navigationBarStatusValue: boolean = false; 41 @StorageLink('isDesktopLoadFinished') desktopLoadFinished: boolean = false; 42 @Prop gridConfig: string; 43 @StorageLink('pageIndex') pageIndex: number = 0; 44 private mPageDesktopViewModel?: PageDesktopViewModel; 45 @StorageLink('appListInfo') @Watch('updatePageList') appListInfo: AppListInfo = { appGridInfo: [[]] }; 46 @State pageList: number[] = [0]; 47 private swiperController: SwiperController = new SwiperController(); 48 dialogController?: CustomDialogController | null; 49 @StorageLink('desktopEventResponse') desktopEventResponse: boolean = true; 50 @State desktopIndicator: DotIndicator | DigitIndicator = 51 Indicator.dot().selectedColor(StyleConstants.DEFAULT_FONT_COLOR); 52 53 updatePageList(): void { 54 if (this.pageList.length !== this.appListInfo.appGridInfo.length) { 55 if (this.pageList.length < this.appListInfo.appGridInfo.length) { 56 for (let i = this.pageList.length; i < this.appListInfo.appGridInfo.length; i++) { 57 if (this.pageList.indexOf(i) === -1) { 58 this.pageList.push(i); 59 } 60 } 61 } else { 62 for (let i = this.pageList.length - 1; i >= this.appListInfo.appGridInfo.length; i--) { 63 if (this.pageList.indexOf(i) !== -1) { 64 this.pageList.splice(i, 1); 65 } 66 } 67 } 68 } 69 const NINE = 9; 70 if (this.pageList?.length > NINE) { 71 this.desktopIndicator = Indicator.digit().fontColor(StyleConstants.DEFAULT_FORM_MGR_FONT_COLOR) 72 .selectedFontColor(StyleConstants.DEFAULT_FONT_COLOR); 73 } else { 74 this.desktopIndicator = Indicator.dot().selectedColor(StyleConstants.DEFAULT_FONT_COLOR); 75 } 76 } 77 78 aboutToAppear(): void { 79 this.mPageDesktopViewModel = PageDesktopViewModel.getInstance(); 80 this.mPageDesktopViewModel.setSwiperController(this.swiperController); 81 } 82 83 aboutToDisappear(): void { 84 Log.showInfo(TAG, 'aboutToDisappear'); 85 } 86 87 private buildLog(): boolean { 88 AppStorage.setOrCreate('dockAddedPage', this.pageList); 89 let isDesktopLoadFinished = AppStorage.get('isDesktopLoadFinished') as boolean; 90 Log.showDebug(TAG, `build start ${isDesktopLoadFinished} page ${this.pageIndex}`); 91 return isDesktopLoadFinished == true; 92 } 93 94 @Builder MenuBuilder() { 95 Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 96 AppMenu({ 97 getMenuInfoList: () => this.getMenu() 98 }) 99 } 100 .width(StyleConstants.CONTEXT_MENU_WIDTH) 101 .borderRadius(StyleConstants.DEFAULT_12) 102 } 103 104 private getMenu(): MenuInfo[] { 105 let menuInfoList: MenuInfo[] = []; 106 let setting = new MenuInfo(); 107 setting.menuType = CommonConstants.MENU_TYPE_FIXED 108 setting.menuImgSrc = '/common/pics/ic_public_settings.svg' 109 setting.menuText = $r('app.string.into_settings') 110 setting.onMenuClick = () => { 111 Trace.start(Trace.CORE_METHOD_START_SETTINGS); 112 this.mPageDesktopViewModel?.intoSetting(); 113 } 114 menuInfoList.push(setting); 115 116 let addOrDeleteBlankPage = new MenuInfo(); 117 addOrDeleteBlankPage.menuType = CommonConstants.MENU_TYPE_FIXED 118 addOrDeleteBlankPage.menuImgSrc = this.mPageDesktopViewModel?.getBlankPageBtnIcon() 119 addOrDeleteBlankPage.menuText = this.mPageDesktopViewModel?.getBlankPageBtnStr() 120 addOrDeleteBlankPage.onMenuClick = () => { 121 this.mPageDesktopViewModel?.addOrDeleteBlankPage(); 122 } 123 menuInfoList.push(addOrDeleteBlankPage); 124 return menuInfoList; 125 } 126 127 build() { 128 Column() { 129 if (this.buildLog()) {} 130 if (this.desktopLoadFinished) { 131 Swiper(this.swiperController) { 132 ForEach(this.pageList, (item: number, index: number) => { 133 if (AppStorage.get('deviceType') == CommonConstants.DEFAULT_DEVICE_TYPE) { 134 Column() { 135 SwiperPage({ 136 appListInfo: $appListInfo, 137 swiperPage: index.valueOf(), 138 gridConfig: this.gridConfig, 139 mPageDesktopViewModel: this.mPageDesktopViewModel 140 }).id(`SwiperPage_${item}${index}`) 141 } 142 .gesture( 143 LongPressGesture({ repeat: false }) 144 .onAction((event: GestureEvent) => { 145 this.dialogController?.open(); 146 }) 147 ) 148 .bindContextMenu(this.MenuBuilder, ResponseType.RightClick) 149 } else { 150 SwiperPage({ 151 appListInfo: $appListInfo, 152 swiperPage: index.valueOf(), 153 gridConfig: this.gridConfig, 154 mPageDesktopViewModel: this.mPageDesktopViewModel 155 }).id(`SwiperPage_${item}${index}`) 156 .bindContextMenu(this.MenuBuilder, ResponseType.LongPress) 157 .bindContextMenu(this.MenuBuilder, ResponseType.RightClick) 158 } 159 }, (item: number, index: number) => { 160 return `${item}${index}`; 161 }) 162 } 163 .id(`${TAG}_Swiper`) 164 .padding({ 165 top: this.navigationBarStatusValue ? 166 this.mPageDesktopViewModel!.getPageDesktopStyleConfig().mDesktopMarginTop : 167 this.mPageDesktopViewModel!.getPageDesktopStyleConfig().mDesktopMarginTop + 0 168 }) 169 .height(StyleConstants.PERCENTAGE_100) 170 .width(StyleConstants.PERCENTAGE_100) 171 .indicator(this.desktopIndicator) 172 .loop(false) 173 .index(this.pageIndex) 174 .onChange((index) => { 175 if (typeof index !== 'undefined' && this.pageIndex !== index) { 176 this.pageIndex = index; 177 Log.showInfo(TAG, `swiper change to page ${index}`); 178 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null); 179 } 180 }) 181 .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => { 182 this.desktopEventResponse = false; 183 }) 184 .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => { 185 this.desktopEventResponse = false; 186 }) 187 .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => { 188 this.desktopEventResponse = true; 189 if (typeof index !== 'undefined') { 190 this.pageIndex = index; 191 Log.showInfo(TAG, `swiper animation end to page ${index}`); 192 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_FORM_ITEM_VISIBLE, null); 193 } 194 }) 195 .hitTestBehavior(this.desktopEventResponse ? HitTestMode.Default : HitTestMode.Block) 196 } 197 } 198 .id(`${TAG}`) 199 .alignItems(HorizontalAlign.Center) 200 .justifyContent(FlexAlign.Center) 201 .height(StyleConstants.PERCENTAGE_100) 202 .width(StyleConstants.PERCENTAGE_100) 203 } 204}