1/*
2 * Copyright (c) 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 { ColumnSize, ScreenManager } from '../../model/common/ScreenManager';
17import { ToolBarButton } from './ToolBarButton';
18import { Constants } from '../../model/common/Constants';
19import { ActionBarProp } from '../browserOperation/ActionBarProp';
20import { Action } from '../browserOperation/Action';
21import { ActionBarMode } from '../browserOperation/ActionBarMode';
22
23export class MenuItem {
24  public value?: string;
25  public action?: () => void;
26}
27
28@Component
29export struct ToolBar {
30  @Consume isShowBar: boolean;
31  @StorageLink('isSplitMode') isSplitMode: boolean = ScreenManager.getInstance().isSplitMode();
32  @StorageLink('leftBlank') leftBlank: number[] =
33    [0, ScreenManager.getInstance().getStatusBarHeight(), 0, ScreenManager.getInstance().getNaviBarHeight()];
34  @Consume toolMenuList: Action[];
35  @Consume moreMenuList: Action[];
36  @StorageLink('isHorizontal') isHorizontal: boolean = ScreenManager.getInstance().isHorizontal();
37  @State showPopup: boolean = false;
38  onMenuClicked: Function = (): void => {};
39  @State actionBarProp: ActionBarProp = new ActionBarProp();
40  @Consume @Watch('isNeedHidePopup') hidePopup: boolean;
41  private isFromPhotoBrowser = false;
42
43  aboutToAppear(): void {
44    this.actionBarProp = this.createActionBar();
45  }
46
47  isNeedHidePopup(): void {
48    if (this.hidePopup) {
49      this.showPopup = false;
50    }
51  }
52
53  @Builder
54  PopupBuilder() {
55    Column() {
56      ForEach(this.moreMenuList, (menu: Action, index?: number) => {
57        Text(menu.textRes)
58          .key('ToolBarPopupBuilderTextOf' + menu.componentKey)
59          .width(Constants.PERCENT_100)
60          .height($r('app.float.menu_height'))
61          .fontColor(menu.fillColor)
62          .fontSize($r('sys.float.ohos_id_text_size_body1'))
63          .fontWeight(FontWeight.Regular)
64          .onClick(() => {
65            this.onMenuClicked && this.onMenuClicked(menu);
66          })
67        if (this.moreMenuList.indexOf(menu) != this.moreMenuList.length - 1) {
68          Divider()
69            .width(Constants.PERCENT_100)
70            .strokeWidth(Constants.MENU_DIVIDER_STROKE_WIDTH)
71            .color($r('sys.color.ohos_id_color_list_separator'))
72        }
73      }, (menu: Action) => menu.actionID.toString())
74    }
75    .width(ScreenManager.getInstance().getColumnsWidth(ColumnSize.COLUMN_TWO))
76    .borderRadius($r('sys.float.ohos_id_corner_radius_card'))
77    .padding({
78      top: $r('app.float.menu_padding_vertical'),
79      bottom: $r('app.float.menu_padding_vertical'),
80      left: $r('app.float.menu_padding_horizontal'),
81      right: $r('app.float.menu_padding_horizontal')
82    })
83    .backgroundColor(Color.White)
84  }
85
86  build() {
87    Row() {
88      Row() {
89        ForEach(this.toolMenuList, (menu: Action, index?: number) => {
90          if (menu.actionID == Action.MORE.actionID) {
91            Column() {
92              Row() {
93                ToolBarButton({
94                  res: menu.iconRes,
95                  action: menu,
96                  isLeft: true,
97                  isAutoTint: menu.isAutoTint,
98                  colorMode: this.actionBarProp.getColorMode()
99                })
100              }.margin({ top: $r('app.float.id_icon_margin_vertical') })
101
102              Row() {
103                Text(menu.textRes)
104                  .key('ToolBar_Text' + menu.componentKey)
105                  .fontSize($r('sys.float.ohos_id_text_size_caption'))
106                  .fontFamily($r('app.string.id_text_font_family_regular'))
107                  .fontColor(menu.fillColor)
108              }.margin({ top: $r('app.float.tab_bar_image_bottom') })
109            }
110            .key('ToolBarButton' + menu.componentKey)
111            .width(`${100 / this.toolMenuList.length}%`)
112            .height(Constants.PERCENT_100)
113            .onClick(() => {
114              this.showPopup = !this.showPopup
115            })
116            .bindPopup(this.showPopup, {
117              builder: this.PopupBuilder,
118              placement: Placement.Top,
119              popupColor: '#00FFFFFF',
120              enableArrow: false,
121              onStateChange: (e) => {
122                if (!e.isVisible) {
123                  this.showPopup = false;
124                } else {
125                  this.hidePopup = false;
126                }
127              }
128            })
129
130          } else {
131            Column() {
132              Row() {
133                ToolBarButton({
134                  res: menu.iconRes,
135                  action: menu,
136                  isLeft: true,
137                  isAutoTint: menu.isAutoTint,
138                  colorMode: this.actionBarProp.getColorMode()
139                })
140              }.margin({ top: $r('app.float.id_icon_margin_vertical') })
141
142              Row() {
143                Text(menu.textRes)
144                  .fontSize($r('sys.float.ohos_id_text_size_caption'))
145                  .fontFamily($r('app.string.id_text_font_family_regular'))
146                  .fontColor(menu.fillColor)
147              }
148              .margin({ top: $r('sys.float.ohos_id_text_margin_vertical') })
149            }
150            .key('ToolBarButton' + menu.componentKey)
151            .onClick(() => {
152              this.onMenuClicked && this.onMenuClicked(menu)
153            })
154            .width(`${100 / this.toolMenuList.length}%`)
155            .height(Constants.PERCENT_100)
156          }
157        }, (menu: Action) => menu.actionID.toString())
158      }
159      .width(Constants.PERCENT_100)
160      .height(Constants.ActionBarHeight)
161      .padding(this.toolMenuList.length >= 4 ? {} : { left: $r('app.float.actionbar_margin_horizontal'),
162                                                      right: $r('app.float.actionbar_margin_horizontal') })
163    }
164    .width(Constants.PERCENT_100)
165    .backgroundColor(this.isFromPhotoBrowser ? $r('app.color.transparent') : this.actionBarProp.getBackgroundColor())
166    .opacity(this.actionBarProp.getAlpha())
167    .visibility((this.isShowBar || this.isFromPhotoBrowser) &&
168      !this.isHorizontal ? Visibility.Visible : Visibility.Hidden)
169    .markAnchor({ x: Constants.PERCENT_0, y: this.isFromPhotoBrowser ? Constants.PERCENT_0 : Constants.PERCENT_100 })
170    .position({ x: Constants.PERCENT_0, y: this.isFromPhotoBrowser ? Constants.PERCENT_0 : Constants.PERCENT_100 })
171  }
172
173  private createActionBar(): ActionBarProp {
174    let actionBarProp: ActionBarProp = new ActionBarProp();
175    actionBarProp
176      .setMode(ActionBarMode.DETAIL_MODE)
177      .setAlpha(ActionBarProp.PHOTO_BROWSER_ACTIONBAR_ALPHA)
178    return actionBarProp;
179  }
180}