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 '../utils/Log'; 17import { AppName } from './AppName'; 18import { AppMenu } from './AppMenu'; 19import { FormModel } from '../model/FormModel'; 20import { StyleConstants } from '../constants/StyleConstants'; 21import { PresetStyleConstants } from '../constants/PresetStyleConstants'; 22import { CommonConstants } from '../constants/CommonConstants'; 23import { localEventManager } from '../manager/LocalEventManager'; 24import { EventConstants } from '../constants/EventConstants'; 25import { LauncherDragItemInfo } from '../bean/LauncherDragItemInfo'; 26import { CardItemInfo, MenuInfo } from '../bean'; 27 28const TAG = 'FormItemComponent'; 29 30@Component 31export struct FormItemComponent { 32 @State showFormName: boolean = true; 33 @State isAllowUpdate: boolean = true; 34 @State isShow: boolean = true; 35 @State isHover: boolean = false; 36 formNameHeight: number = 0; 37 formNameSize: number = 0; 38 nameFontColor: string = '#ffffff'; 39 iconNameMargin: number = PresetStyleConstants.DEFAULT_ICON_NAME_GAP; 40 private formItemWidth: number = 0; 41 private formItemHeight: number = 0; 42 private menuInfo: MenuInfo[] = []; 43 private formItem: CardItemInfo = new CardItemInfo(); 44 private formId = 0; 45 private mFormModel: FormModel = FormModel.getInstance(); 46 private nameLines: number = PresetStyleConstants.DEFAULT_APP_NAME_LINES; 47 mPaddingTop: number = StyleConstants.DEFAULT_10; 48 getFormId: (id: number) => void = (id: number) => {}; 49 clickForm: Function = () => {}; 50 dragStart: Function = () => {}; 51 52 aboutToAppear(): void { 53 this.mFormModel = FormModel.getInstance(); 54 } 55 56 aboutToDisappear(): void { 57 } 58 59 @Builder MenuBuilder() { 60 Column() { 61 AppMenu({ 62 menuInfoList: this.menuInfo, 63 }) 64 } 65 .alignItems(HorizontalAlign.Center) 66 .justifyContent(FlexAlign.Center) 67 .width(StyleConstants.CONTEXT_MENU_WIDTH) 68 } 69 70 build() { 71 Column() { 72 FormComponent({ 73 id: this.formItem.cardId as number, 74 name: this.formItem.cardName as string, 75 bundle: this.formItem.bundleName as string, 76 ability: this.formItem.abilityName as string, 77 module: this.formItem.moduleName as string, 78 dimension: this.formItem.cardDimension as number 79 }) 80 .clip(new Rect({ width: this.formItemWidth, height: this.formItemHeight, radius: 24})) 81 .size({ width: this.formItemWidth, height: this.formItemHeight }) 82 .allowUpdate(this.isAllowUpdate) 83 .visibility(this.isShow ? Visibility.Visible : Visibility.None) 84 .onAcquired((form) => { 85 Log.showInfo(TAG, `FormComponent card id is: ${form.id}`); 86 this.formId = form.id; 87 if (this.getFormId) { 88 this.getFormId(form.id); 89 } 90 }) 91 .onClick((event: ClickEvent) => { 92 Log.showInfo(TAG, `FormComponent onClick`); 93 }) 94 .onError((error) => { 95 Log.showInfo(TAG, `FormComponent error msg: ${error.msg}`); 96 this.mFormModel.deleteForm(this.formItem.cardId); 97 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_UPDATE, null); 98 }) 99 .onTouch(event => { 100 if (event.type === CommonConstants.TOUCH_TYPE_UP) { 101 this.clickForm(event, this.formItem); 102 } 103 }) 104 .bindContextMenu(this.MenuBuilder, ResponseType.LongPress) 105 .onDragStart((event: DragEvent, extraParams: string) => { 106 return this.dragStart(event); 107 }) 108 .onDragEnd((event: DragEvent, extraParams: string) => { 109 Log.showInfo(TAG, `onDragEnd event: [${event.getWindowX()}, ${event.getWindowY()}]` + event.getResult()); 110 AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo()); 111 }) 112 113 Column() { 114 AppName({ 115 bundleName: this.formItem.bundleName, 116 moduleName: this.formItem.moduleName, 117 labelId: this.formItem.appLabelId, 118 nameHeight: this.formNameHeight, 119 nameSize: this.formNameSize, 120 nameFontColor: this.nameFontColor, 121 appName: this.formItem.appName, 122 nameLines: this.nameLines, 123 marginTop: this.iconNameMargin 124 }) 125 } 126 .visibility(this.showFormName ? Visibility.Visible : Visibility.Hidden) 127 } 128 .bindContextMenu(this.MenuBuilder, ResponseType.RightClick) 129 .onHover((isHover: boolean) => { 130 Log.showInfo(TAG, `Form onHover isHover: ${isHover}`); 131 this.isHover = isHover; 132 }) 133 .onDisAppear(() => { 134 Log.showInfo(TAG, `formItemComponent onDisAppear: ${this.formId}`); 135 }) 136 .padding({top : this.mPaddingTop}) 137 .height(StyleConstants.PERCENTAGE_100) 138 .width(StyleConstants.PERCENTAGE_100) 139 } 140}