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 value?: string; 25 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 PopupBuilder() { 54 Column() { 55 ForEach(this.moreMenuList, (menu: Action, index?: number) => { 56 Text(menu.textRes) 57 .key('ToolBarPopupBuilderTextOf' + menu.componentKey) 58 .width(Constants.PERCENT_100) 59 .height($r('app.float.menu_height')) 60 .fontColor(menu.fillColor) 61 .fontSize($r('sys.float.ohos_id_text_size_body1')) 62 .fontWeight(FontWeight.Regular) 63 .onClick(() => { 64 this.onMenuClicked && this.onMenuClicked(menu); 65 }) 66 if (this.moreMenuList.indexOf(menu) != this.moreMenuList.length - 1) { 67 Divider() 68 .width(Constants.PERCENT_100) 69 .strokeWidth(Constants.MENU_DIVIDER_STROKE_WIDTH) 70 .color($r('sys.color.ohos_id_color_list_separator')) 71 } 72 }, (menu: Action) => menu.actionID.toString()) 73 } 74 .width(ScreenManager.getInstance().getColumnsWidth(ColumnSize.COLUMN_TWO)) 75 .borderRadius($r('sys.float.ohos_id_corner_radius_card')) 76 .padding({ 77 top: $r('app.float.menu_padding_vertical'), 78 bottom: $r('app.float.menu_padding_vertical'), 79 left: $r('app.float.menu_padding_horizontal'), 80 right: $r('app.float.menu_padding_horizontal') 81 }) 82 .backgroundColor(Color.White) 83 .margin({ 84 right: $r('app.float.pop_window_margin_rihgt'), 85 bottom: $r('app.float.menu_margin_bottom') 86 }) 87 } 88 89 build() { 90 Row() { 91 Row() { 92 ForEach(this.toolMenuList, (menu: Action, index?: number) => { 93 if (menu.actionID == Action.MORE.actionID) { 94 Column() { 95 Row() { 96 ToolBarButton({ 97 res: menu.iconRes, 98 action: menu, 99 isLeft: true, 100 isAutoTint: menu.isAutoTint, 101 colorMode: this.actionBarProp.getColorMode() 102 }) 103 }.margin({ top: $r('app.float.id_icon_margin_vertical') }) 104 105 Row() { 106 Text(menu.textRes) 107 .key('ToolBar_Text' + menu.componentKey) 108 .fontSize($r('sys.float.ohos_id_text_size_caption')) 109 .fontFamily($r('app.string.id_text_font_family_regular')) 110 .fontColor(menu.fillColor) 111 }.margin({ top: $r('app.float.tab_bar_image_bottom') }) 112 } 113 .key('ToolBarButton' + menu.componentKey) 114 .width(`${100 / this.toolMenuList.length}%`) 115 .height(Constants.PERCENT_100) 116 .onClick(() => { 117 this.showPopup = !this.showPopup 118 }) 119 .bindPopup(this.showPopup, { 120 builder: this.PopupBuilder, 121 placement: Placement.Top, 122 popupColor: '#00FFFFFF', 123 enableArrow: false, 124 onStateChange: (e) => { 125 if (!e.isVisible) { 126 this.showPopup = false; 127 } else { 128 this.hidePopup = false; 129 } 130 } 131 }) 132 133 } else { 134 Column() { 135 Row() { 136 ToolBarButton({ 137 res: menu.iconRes, 138 action: menu, 139 isLeft: true, 140 isAutoTint: menu.isAutoTint, 141 colorMode: this.actionBarProp.getColorMode() 142 }) 143 }.margin({ top: $r('app.float.id_icon_margin_vertical') }) 144 145 Row() { 146 Text(menu.textRes) 147 .fontSize($r('sys.float.ohos_id_text_size_caption')) 148 .fontFamily($r('app.string.id_text_font_family_regular')) 149 .fontColor(menu.fillColor) 150 } 151 .margin({ top: $r('sys.float.ohos_id_text_margin_vertical') }) 152 } 153 .key('ToolBarButton' + menu.componentKey) 154 .onClick(() => { 155 this.onMenuClicked && this.onMenuClicked(menu) 156 }) 157 .width(`${100 / this.toolMenuList.length}%`) 158 .height(Constants.PERCENT_100) 159 } 160 }, (menu: Action) => menu.actionID.toString()) 161 } 162 .width(Constants.PERCENT_100) 163 .height(Constants.ActionBarHeight) 164 .padding(this.toolMenuList.length >= 4 ? {} : { left: $r('app.float.actionbar_margin_horizontal'), 165 right: $r('app.float.actionbar_margin_horizontal') }) 166 } 167 .width(Constants.PERCENT_100) 168 .backgroundColor(this.isFromPhotoBrowser ? $r('app.color.transparent') : this.actionBarProp.getBackgroundColor()) 169 .opacity(this.actionBarProp.getAlpha()) 170 .visibility((this.isShowBar || this.isFromPhotoBrowser) && !this.isHorizontal ? Visibility.Visible : Visibility.Hidden) 171 .markAnchor({ x: Constants.PERCENT_0, y: this.isFromPhotoBrowser ? Constants.PERCENT_0 : Constants.PERCENT_100 }) 172 .position({ x: Constants.PERCENT_0, y: this.isFromPhotoBrowser ? Constants.PERCENT_0 : Constants.PERCENT_100 }) 173 } 174 175 private createActionBar(): ActionBarProp { 176 let actionBarProp: ActionBarProp = new ActionBarProp(); 177 actionBarProp 178 .setMode(ActionBarMode.DETAIL_MODE) 179 .setAlpha(ActionBarProp.PHOTO_BROWSER_ACTIONBAR_ALPHA) 180 return actionBarProp; 181 } 182}