• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
26
27const TAG = 'FormItemComponent';
28
29@Component
30export struct FormItemComponent {
31  @State showFormName: boolean = true;
32  @State isAllowUpdate: boolean = true;
33  @State isShow: boolean = true;
34  @State isHover: boolean = false;
35  formNameHeight: number;
36  formNameSize: number;
37  nameFontColor: string;
38  iconNameMargin: number = PresetStyleConstants.DEFAULT_ICON_NAME_GAP;
39  private formItemWidth;
40  private formItemHeight;
41  private menuInfo;
42  private formItem;
43  private formId;
44  private mFormModel: FormModel;
45  private nameLines: number = PresetStyleConstants.DEFAULT_APP_NAME_LINES;
46  mPaddingTop: number = StyleConstants.DEFAULT_10;
47  getFormId: (id: number) => void;
48  clickForm?: Function = null;
49  dragStart: Function;
50
51  aboutToAppear(): void  {
52    this.mFormModel = FormModel.getInstance();
53  }
54
55  aboutToDisappear(): void {
56    this.getFormId = null;
57    this.clickForm = null;
58    this.dragStart = null;
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,
76        name: this.formItem.cardName,
77        bundle: this.formItem.bundleName,
78        ability: this.formItem.abilityName,
79        module: this.formItem.moduleName,
80        dimension: this.formItem.cardDimension
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        .bindContextMenu(this.MenuBuilder, ResponseType.LongPress)
107        .onDragStart((event: DragEvent, extraParams: string) => {
108          return this.dragStart(event);
109        })
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}