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 '../../../../../../../../common/src/main/ets/default/Log' 17import EventManager, { unsubscribe } from '../../../../../../../../common/src/main/ets/default/event/EventManager' 18import CheckEmptyUtils from '../../../../../../../../common/src/main/ets/default/CheckEmptyUtils' 19import { WINDOW_SHOW_HIDE_EVENT } from '../../../../../../../../common/src/main/ets/default/WindowManager' 20import { PluginType,itemData } from "../../../../../../../../common/src/main/ets/plugindatasource/common/Constants" 21import ViewModel from '../viewmodel/StatusBarVM' 22import StyleConfigurationCommon, { CommonStyle 23} from '../../../../../../../../common/src/main/ets/default/StyleConfiguration' 24import StyleConfiguration, { IconItemComponentStyle } from '../common/StyleConfiguration' 25import MetaIconItemComponent from './MetaIconItemComponent' 26import PluginIconItemComponent from './PluginIconItemComponent' 27 28const TAG = 'IconItemComponent' 29 30class itemStatusData{ 31 selected: boolean 32} 33 34@Component 35export default struct IconItemComponent { 36 private keyId: string 37 @State mItemData: itemData = new itemData() 38 @State mItemStatus: itemStatusData = { 39 selected: false 40 } 41 mClearCallback: unsubscribe | undefined 42 @State style: IconItemComponentStyle = StyleConfiguration.getIconItemComponentStyle() 43 @State styleCommon: CommonStyle = StyleConfigurationCommon.getCommonStyle() 44 @State mHasMarginLR: boolean = false 45 46 aboutToAppear() { 47 Log.showInfo(TAG, `aboutToAppear Start, keyId: ${this.keyId} `); 48 this.linkItemData() 49 if (this.mItemData?.relationWindowId) { 50 this.mClearCallback = EventManager.subscribe(WINDOW_SHOW_HIDE_EVENT, (args) => this.onWindowChange(args)) 51 } 52 } 53 54 aboutToDisappear() { 55 Log.showInfo(TAG, `aboutToDisappear`); 56 this.mClearCallback && this.mClearCallback() 57 this.mClearCallback = undefined 58 } 59 60 linkItemData() { 61 this.mItemData = AppStorage.Link('StatusBar_' + this.keyId).get() 62 Log.showInfo(TAG, `linkItemData, mItemData: ${this.keyId} ${this.mItemData.label} ${ 63 this.mItemData 64 .iconUrl}`) 65 this.mItemStatus = AppStorage.SetAndLink('StatusBar_Status_' + this.keyId, { selected: false }).get() 66 } 67 68 build() { 69 Row() { 70 if (this.mHasMarginLR) { 71 Row().width(this.style.marginLeft).height('100%') 72 } 73 Row() { 74 if (this.mItemData.pluginType == PluginType.META) { 75 MetaIconItemComponent({ 76 keyId: this.keyId 77 }) 78 } else if (this.mItemData.pluginType == PluginType.DATA_ABILITY) { 79 // TODO: 80 } else if (this.mItemData.pluginType == PluginType.PLUGIN_COMPONENT) { 81 PluginIconItemComponent({ 82 keyId: this.keyId 83 }) 84 } 85 } 86 .height(this.style.stackHeight) 87 .borderRadius(this.style.stackBorderRadius) 88 .backgroundColor(this.mItemStatus.selected ? this.style.stackBgColorSelected : this.style.stackBgColorUnSelected) 89 .onClick(this.onIconItemClick.bind(this)) 90 .gesture(LongPressGesture({ repeat: false }).onAction(this.onIconItemLongPressGesture.bind(this))) 91 .onTouch(this.onIconItemTouch.bind(this)) 92 .onAreaChange((e, e2) => { 93 this.mHasMarginLR = e2.width > 0 94 }) 95 96 if (this.mHasMarginLR) { 97 Row().width(this.style.marginRight).height('100%') 98 } 99 }.height('100%') 100 } 101 102 onWindowChange(args) { 103 if (this.mItemData.canSelect && args?.windowName == this.mItemData.relationWindowId) { 104 this.mItemStatus.selected = args?.isShow 105 } 106 } 107 108 onIconItemClick(event: ClickEvent) { 109 Log.showInfo(TAG, `onIconItemClick`); 110 if (!this.mItemStatus.selected) { 111 this.execClickAction() 112 } else { 113 this.execSelectedClickAction() 114 } 115 } 116 117 execClickAction() { 118 Log.showInfo(TAG, `execClickAction`); 119 if (this.mItemData?.actionData?.clickAction) { 120 EventManager.publish(this.mItemData.actionData.clickAction) 121 } 122 } 123 124 execSelectedClickAction() { 125 Log.showInfo(TAG, `execSelectedClickAction ${this.mItemData?.actionData?.selectedClickAction}`); 126 if (this.mItemData?.actionData?.selectedClickAction) { 127 EventManager.publish(this.mItemData.actionData.selectedClickAction) 128 } 129 } 130 131 onIconItemLongPressGesture(event: GestureEvent) { 132 Log.showInfo(TAG, `onIconItemLongPressGesture, event: ${JSON.stringify(event)}`); 133 let longClickEvent = this.mItemData.actionData.longClickAction; 134 longClickEvent && EventManager.publish(longClickEvent); 135 } 136 137 onIconItemTouch(event: TouchEvent) { 138 Log.showInfo(TAG, `onIconItemTouch, event: ${JSON.stringify(event)}`); 139 if (this.mItemData?.actionData?.clickAction || this.mItemData?.actionData?.longClickAction) { 140 event.stopPropagation() 141 } 142 } 143}