1e75ebbc8Sopenharmony_ci/*
2e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
6e75ebbc8Sopenharmony_ci *
7e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e75ebbc8Sopenharmony_ci *
9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
13e75ebbc8Sopenharmony_ci * limitations under the License.
14e75ebbc8Sopenharmony_ci */
15e75ebbc8Sopenharmony_ci
16e75ebbc8Sopenharmony_ciimport Log from '../default/Log'
17e75ebbc8Sopenharmony_ciimport StyleConfiguration, { SimpleToggleBaseStyle } from './common/StyleConfiguration'
18e75ebbc8Sopenharmony_ci
19e75ebbc8Sopenharmony_ciconst TAG = 'SimpleToggleBase'
20e75ebbc8Sopenharmony_ci
21e75ebbc8Sopenharmony_ci@Component
22e75ebbc8Sopenharmony_ciexport default struct SimpleToggleBase {
23e75ebbc8Sopenharmony_ci  private mToggleId: string
24e75ebbc8Sopenharmony_ci  @Link mIcon: Resource
25e75ebbc8Sopenharmony_ci  @State mIconStr: string = ''
26e75ebbc8Sopenharmony_ci  private mUseIconStr: boolean = false
27e75ebbc8Sopenharmony_ci  private mAutoIconColor: boolean = false
28e75ebbc8Sopenharmony_ci  @Link mChangeSwitch: boolean
29e75ebbc8Sopenharmony_ci  @Link mLabel: Resource
30e75ebbc8Sopenharmony_ci  private mLabelStr: string | Resource
31e75ebbc8Sopenharmony_ci  private mUseLabelStr: boolean = false
32e75ebbc8Sopenharmony_ci  private mIconOnBG: string | Resource
33e75ebbc8Sopenharmony_ci  private mEditMode: boolean = false
34e75ebbc8Sopenharmony_ci  private mDragMode: boolean = false
35e75ebbc8Sopenharmony_ci  private mClickEvent: Function
36e75ebbc8Sopenharmony_ci  private mLongClickEvent: Function
37e75ebbc8Sopenharmony_ci  @State mIconIsHover: boolean = false
38e75ebbc8Sopenharmony_ci  @State mTextIsHover: boolean = false
39e75ebbc8Sopenharmony_ci  @State style: SimpleToggleBaseStyle = StyleConfiguration.getSimpleToggleBaseStyle()
40e75ebbc8Sopenharmony_ci
41e75ebbc8Sopenharmony_ci  aboutToAppear() {
42e75ebbc8Sopenharmony_ci    Log.showInfo(TAG, `aboutToAppear, mToggleId: ${this.mToggleId}, mLabel: ${JSON.stringify(this.mLabel)}, mEditMode: ${this.mEditMode} mDragMode: ${this.mDragMode} `)
43e75ebbc8Sopenharmony_ci  }
44e75ebbc8Sopenharmony_ci
45e75ebbc8Sopenharmony_ci  aboutToDisappear() {
46e75ebbc8Sopenharmony_ci    Log.showInfo(TAG, 'aboutToDisappear')
47e75ebbc8Sopenharmony_ci  }
48e75ebbc8Sopenharmony_ci
49e75ebbc8Sopenharmony_ci  build() {
50e75ebbc8Sopenharmony_ci    Column() {
51e75ebbc8Sopenharmony_ci      Stack() {
52e75ebbc8Sopenharmony_ci        Flex()
53e75ebbc8Sopenharmony_ci          .backgroundColor(this.mChangeSwitch ? (this.mIconOnBG ? this.mIconOnBG : this.style.iconOnBG) : this.style.iconOffBG)
54e75ebbc8Sopenharmony_ci          .clip(new Circle({
55e75ebbc8Sopenharmony_ci            width: this.mDragMode ? this.style.dragCircleWidth : this.style.circleWidth,
56e75ebbc8Sopenharmony_ci            height: this.mDragMode ? this.style.dragCircleHeight : this.style.circleHeight
57e75ebbc8Sopenharmony_ci          }))
58e75ebbc8Sopenharmony_ci          .width(this.mDragMode ? this.style.dragCircleWidth : this.style.circleWidth)
59e75ebbc8Sopenharmony_ci          .height(this.mDragMode ? this.style.dragCircleHeight : this.style.circleHeight)
60e75ebbc8Sopenharmony_ci        if (!this.mDragMode) {
61e75ebbc8Sopenharmony_ci          Flex()
62e75ebbc8Sopenharmony_ci            .backgroundColor(this.mIconIsHover ? this.style.hoverColor : this.style.transparentColor)
63e75ebbc8Sopenharmony_ci            .clip(new Circle({ width: this.style.circleWidth, height: this.style.circleHeight }))
64e75ebbc8Sopenharmony_ci            .width(this.style.circleWidth)
65e75ebbc8Sopenharmony_ci            .height(this.style.circleHeight)
66e75ebbc8Sopenharmony_ci        }
67e75ebbc8Sopenharmony_ci        Image(this.mUseIconStr ? this.mIconStr : this.mIcon)
68e75ebbc8Sopenharmony_ci          .size({
69e75ebbc8Sopenharmony_ci            width: this.mDragMode ? this.style.dragIconWidth : this.style.iconWidth,
70e75ebbc8Sopenharmony_ci            height: this.mDragMode ? this.style.dragIconHeight : this.style.iconHeight })
71e75ebbc8Sopenharmony_ci          .objectFit(ImageFit.Contain)
72e75ebbc8Sopenharmony_ci          .fillColor(this.mChangeSwitch ? this.style.iconOnColor : this.style.iconOffColor)
73e75ebbc8Sopenharmony_ci          .onHover((isHover) => {
74e75ebbc8Sopenharmony_ci            this.mIconIsHover = isHover;
75e75ebbc8Sopenharmony_ci          })
76e75ebbc8Sopenharmony_ci      }
77e75ebbc8Sopenharmony_ci      .width(this.mDragMode ? this.style.dragCircleWidth : this.style.circleWidth)
78e75ebbc8Sopenharmony_ci      .height(this.mDragMode ? this.style.dragCircleHeight : this.style.circleHeight)
79e75ebbc8Sopenharmony_ci      .onClick(this.onIconItemClick.bind(this))
80e75ebbc8Sopenharmony_ci
81e75ebbc8Sopenharmony_ci      if (!this.mDragMode) {
82e75ebbc8Sopenharmony_ci        Stack() {
83e75ebbc8Sopenharmony_ci          Flex()
84e75ebbc8Sopenharmony_ci            .backgroundColor(this.mTextIsHover ? this.style.hoverColor : this.style.transparentColor)
85e75ebbc8Sopenharmony_ci            .clip(new Rect({
86e75ebbc8Sopenharmony_ci              width: this.style.textHoverWidth,
87e75ebbc8Sopenharmony_ci              height: this.style.textHoverHeight
88e75ebbc8Sopenharmony_ci            }).radius(this.style.textHoverRadius))
89e75ebbc8Sopenharmony_ci            .width(this.style.textHoverWidth)
90e75ebbc8Sopenharmony_ci            .height(this.style.textHoverHeight)
91e75ebbc8Sopenharmony_ci          Text(this.mUseLabelStr ? this.mLabelStr : this.mLabel)
92e75ebbc8Sopenharmony_ci            .fontSize(this.style.titleSize)
93e75ebbc8Sopenharmony_ci            .fontColor(this.style.titleColor)
94e75ebbc8Sopenharmony_ci            .textAlign(TextAlign.Center)
95e75ebbc8Sopenharmony_ci            .maxLines(2)
96e75ebbc8Sopenharmony_ci            .onHover((isHover) => {
97e75ebbc8Sopenharmony_ci              this.mTextIsHover = isHover;
98e75ebbc8Sopenharmony_ci            })
99e75ebbc8Sopenharmony_ci        }.margin({top: this.style.componentGap})
100e75ebbc8Sopenharmony_ci      }
101e75ebbc8Sopenharmony_ci    }
102e75ebbc8Sopenharmony_ci    .width('100%')
103e75ebbc8Sopenharmony_ci    .height('100%')
104e75ebbc8Sopenharmony_ci  }
105e75ebbc8Sopenharmony_ci
106e75ebbc8Sopenharmony_ci  onIconItemClick(event: ClickEvent) {
107e75ebbc8Sopenharmony_ci    if (this.mDragMode) {
108e75ebbc8Sopenharmony_ci      return
109e75ebbc8Sopenharmony_ci    }
110e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `onIconItemClick`)
111e75ebbc8Sopenharmony_ci    if (this.mClickEvent) {
112e75ebbc8Sopenharmony_ci      this.mClickEvent()
113e75ebbc8Sopenharmony_ci    }
114e75ebbc8Sopenharmony_ci  }
115e75ebbc8Sopenharmony_ci
116e75ebbc8Sopenharmony_ci  onIconItemLongPressGesture(event: GestureEvent) {
117e75ebbc8Sopenharmony_ci    if (this.mEditMode || this.mDragMode) {
118e75ebbc8Sopenharmony_ci      return
119e75ebbc8Sopenharmony_ci    }
120e75ebbc8Sopenharmony_ci    Log.showDebug(TAG, `onIconItemLongPressGesture, event: ${JSON.stringify(event)}`)
121e75ebbc8Sopenharmony_ci    if (this.mLongClickEvent) {
122e75ebbc8Sopenharmony_ci      this.mLongClickEvent()
123e75ebbc8Sopenharmony_ci    }
124e75ebbc8Sopenharmony_ci  }
125e75ebbc8Sopenharmony_ci}
126