• 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 { MenuInfo } from '../bean/MenuInfo';
18import { StyleConstants } from '../constants/StyleConstants';
19import { CommonConstants } from '../constants/CommonConstants';
20import { ResourceManager } from '../manager/ResourceManager';
21
22const TAG = 'AppMenu';
23
24@Component
25export struct AppMenu {
26  menuInfoList: Array<MenuInfo> = new Array<MenuInfo>();
27  getMenuInfoList: Function;
28  menuMode: number = CommonConstants.MENU_UI_MODE_LIGHT;
29  fixedMenuList: Array<MenuInfo> = null;
30  dynamicMenuList: Array<MenuInfo> = null;
31  isBothTypeMenuExist: boolean = true;
32  closeMenu: Function = () => {};
33
34  aboutToAppear(): void {
35    Log.showInfo(TAG, 'aboutToAppear start');
36    if (this.getMenuInfoList) {
37      this.menuInfoList = this.getMenuInfoList();
38    }
39    this.fixedMenuList = new Array<MenuInfo>();
40    this.dynamicMenuList = new Array<MenuInfo>();
41    for (let menuInfo of this.menuInfoList) {
42      if (menuInfo.menuType == CommonConstants.MENU_TYPE_FIXED) {
43        this.fixedMenuList.push(menuInfo);
44      } else {
45        this.dynamicMenuList.push(menuInfo);
46      }
47    }
48    this.isBothTypeMenuExist = this.fixedMenuList.length > 0 && this.dynamicMenuList.length > 0;
49    AppStorage.SetOrCreate('contextMenuState', true);
50  }
51
52  aboutToDisappear(): void  {
53    Log.showInfo(TAG, 'aboutToDisappear start');
54    this.fixedMenuList = null;
55    this.dynamicMenuList = null;
56    this.getMenuInfoList = null;
57    this.closeMenu = null;
58  }
59
60  build() {
61    Column() {
62      Column() {
63        ForEach(this.dynamicMenuList, (item) => {
64          Column() {
65            HorizontalMenuItem({
66              menuInfo: item,
67              menuMode: this.menuMode,
68              closeMenu: this.closeMenu
69            })
70          }
71        }, item => JSON.stringify(item))
72      }
73      .visibility(this.dynamicMenuList.length > 0 ? Visibility.Visible : Visibility.None)
74
75      if (this.isBothTypeMenuExist) {
76        Divider()
77          .vertical(false)
78          .color((this.menuMode == CommonConstants.MENU_UI_MODE_LIGHT) ? "#33000000" : "#33ffffff")
79          .strokeWidth(1)
80      }
81
82      Column() {
83        ForEach(this.fixedMenuList, (item) => {
84          Column() {
85            HorizontalMenuItem({
86              menuInfo: item,
87              menuMode: this.menuMode,
88              closeMenu: this.closeMenu
89            })
90          }
91        }, item => JSON.stringify(item))
92      }
93      .visibility(this.fixedMenuList.length > 0 ? Visibility.Visible : Visibility.None)
94    }
95    .padding({
96      top: 4,
97      bottom: 4,
98      left: 4,
99      right: 4
100    })
101    .borderRadius(StyleConstants.DEFAULT_12)
102  }
103}
104
105@Component
106struct HorizontalMenuItem {
107  @State shortcutIcon: string = StyleConstants.DEFAULT_ICON;
108  @State shortcutName: string = "";
109  private mResourceManager;
110  menuInfo: MenuInfo;
111  menuMode: number = CommonConstants.MENU_UI_MODE_LIGHT;
112  closeMenu: Function = () => {};
113
114  aboutToAppear(): void  {
115    this.mResourceManager = ResourceManager.getInstance();
116    this.updateIcon();
117    this.updateName();
118  }
119
120  aboutToDisappear(): void {
121    this.closeMenu = null;
122  }
123
124  public shortcutIconLoadCallback(image) {
125    this.shortcutIcon = image;
126  }
127
128  public shortcutNameLoadCallback(name) {
129    this.shortcutName = name;
130  }
131
132  public updateIcon() {
133    if (this.menuInfo.shortcutIconId != -1 && this.menuInfo.menuImgSrc != "" && this.menuInfo.menuImgSrc != null) {
134      this.mResourceManager.getAppIconWithCache(this.menuInfo.shortcutIconId, this.menuInfo.bundleName,
135        this.menuInfo.moduleName, this.shortcutIconLoadCallback.bind(this), StyleConstants.DEFAULT_ICON);
136    } else {
137      this.shortcutIconLoadCallback(this.menuInfo.menuImgSrc);
138    }
139  }
140
141  public updateName() {
142    if (this.menuInfo.shortcutLabelId != -1 && this.menuInfo.menuText != "" && this.menuInfo.menuText != null && this.mResourceManager) {
143      this.mResourceManager.getAppNameWithCache(this.menuInfo.shortcutLabelId, this.menuInfo.bundleName,
144        this.menuInfo.moduleName, this.shortcutName, this.shortcutNameLoadCallback.bind(this));
145    } else {
146      this.shortcutNameLoadCallback(this.menuInfo.menuText);
147    }
148  }
149
150  build() {
151    Row() {
152      if (this.shortcutIcon != null && this.shortcutIcon != "") {
153        Image(this.shortcutIcon)
154          .objectFit(ImageFit.Contain)
155          .height(StyleConstants.DEFAULT_20)
156          .width(StyleConstants.DEFAULT_20)
157          .margin({
158            left: 12
159          })
160      } else {
161        Image("")
162          .objectFit(ImageFit.Contain)
163          .height(StyleConstants.DEFAULT_20)
164          .width(StyleConstants.DEFAULT_20)
165          .backgroundColor('#33ffffFF')
166          .margin({
167            left: 12
168          })
169      }
170      Text(this.shortcutName)
171        .fontColor((this.menuMode == CommonConstants.MENU_UI_MODE_LIGHT) ? "#e5000000" : "#e5ffffff")
172        .fontSize(14)
173        .height(StyleConstants.DEFAULT_20)
174        .margin({
175          left: StyleConstants.DEFAULT_8
176        })
177        .textOverflow({overflow: TextOverflow.Ellipsis})
178    }
179    .alignItems(VerticalAlign.Center)
180    .borderRadius(StyleConstants.DEFAULT_ITEM_RADIUS)
181    .height(StyleConstants.DEFAULT_40)
182    .width(235)
183    .onClick(() => {
184      this.menuInfo.onMenuClick();
185      this.closeMenu();
186    })
187  }
188}