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    Log.showInfo(TAG, `aboutToAppear begin height: ${this.formItemHeight}, width: ${this.formItemWidth}`);
55  }
56
57  aboutToDisappear(): void {
58    Log.showInfo(TAG, 'aboutToDisappear begin');
59  }
60
61  @Builder MenuBuilder() {
62    Column() {
63      AppMenu({
64        menuInfoList: this.menuInfo,
65      })
66    }
67    .alignItems(HorizontalAlign.Center)
68    .justifyContent(FlexAlign.Center)
69    .width(StyleConstants.CONTEXT_MENU_WIDTH)
70  }
71
72  build() {
73    Column() {
74      FormComponent({
75        id: this.formItem.cardId as number,
76        name: this.formItem.cardName as string,
77        bundle: this.formItem.bundleName as string,
78        ability: this.formItem.abilityName as string,
79        module: this.formItem.moduleName as string,
80        dimension: this.formItem.cardDimension as number
81      })
82        .clip(new Rect({ width: this.formItemWidth, height: this.formItemHeight, radius: 24}))
83        .size({ width: this.formItemWidth, height: this.formItemHeight })
84        .allowUpdate(this.isAllowUpdate)
85        .visibility(this.isShow ? Visibility.Visible : Visibility.None)
86        .onAcquired((form) => {
87          Log.showInfo(TAG, `FormComponent card id is: ${form.id}`);
88          this.formId = form.id;
89          if (this.getFormId) {
90            this.getFormId(form.id);
91          }
92        })
93        .onClick((event: ClickEvent) => {
94          Log.showInfo(TAG, 'FormComponent onClick');
95        })
96        .onError((error) => {
97          Log.showInfo(TAG, `FormComponent error msg: ${error.msg}`);
98          this.mFormModel.deleteForm(this.formItem.cardId);
99          localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_UPDATE, null);
100        })
101        .onTouch(event => {
102          if (event.type === CommonConstants.TOUCH_TYPE_UP) {
103            this.clickForm(event, this.formItem);
104          }
105        })
106        .onDragStart((event: DragEvent, extraParams: string) => {
107          return this.dragStart(event);
108        })
109        .bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
110        .onDragEnd((event: DragEvent, extraParams: string) => {
111          Log.showInfo(TAG, `onDragEnd event: [${event.getWindowX()}, ${event.getWindowY()}]` + event.getResult());
112          AppStorage.setOrCreate<LauncherDragItemInfo>('dragItemInfo', new LauncherDragItemInfo());
113        })
114
115      Column() {
116        AppName({
117          bundleName: this.formItem.bundleName,
118          moduleName: this.formItem.moduleName,
119          labelId: this.formItem.appLabelId,
120          nameHeight: this.formNameHeight,
121          nameSize: this.formNameSize,
122          nameFontColor: this.nameFontColor,
123          appName: this.formItem.appName,
124          nameLines: this.nameLines,
125          marginTop: this.iconNameMargin
126        })
127      }
128      .visibility(this.showFormName ? Visibility.Visible : Visibility.Hidden)
129    }
130    .bindContextMenu(this.MenuBuilder, ResponseType.RightClick)
131    .onHover((isHover: boolean) => {
132      Log.showInfo(TAG, `Form onHover isHover: ${isHover}`);
133      this.isHover = isHover;
134    })
135    .onDisAppear(() => {
136      Log.showInfo(TAG, `formItemComponent onDisAppear: ${this.formId}`);
137    })
138    .padding({top : this.mPaddingTop})
139    .height(StyleConstants.PERCENTAGE_100)
140    .width(StyleConstants.PERCENTAGE_100)
141  }
142}