• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Fujian Newland Auto-ID Tech.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 */
15import TouchManagerUtil from '../util/InputEventUtil'
16import { injectBackEvent } from '../util/InputEventUtil'
17/**
18 * 输入事件管理
19 */
20@Preview
21@Component
22export struct InputEventManager {
23  @State mIsShowTouchData: boolean = false // 是否显示触屏事件详情
24  @State mTouchData: string = '' // 触屏数据
25  @State mBtnBg: Resource = $r('app.color.btn_normal_bg')
26
27  aboutToDisappear() {
28    TouchManagerUtil.unregisterTouch()
29  }
30
31  touchCallback(touchData: string) {
32    if (this.mIsShowTouchData) {
33      this.mTouchData = touchData
34    }
35  }
36
37  build() {
38    Scroll() {
39      Column({ space: 12 }) {
40        Column() {
41          Row() {
42            Text($r('app.string.input_touch_event_listener'))
43              .fontColor($r('sys.color.ohos_id_color_text_primary'))
44              .fontSize($r('sys.float.ohos_id_text_size_body1'))
45              .fontWeight(FontWeight.Medium)
46            Blank()
47            Toggle({
48              type: ToggleType.Switch,
49              isOn: false
50            })
51              .width(42)
52              .onChange((isOn) => {
53                this.mIsShowTouchData = isOn
54                if (isOn) {
55                  TouchManagerUtil.registerTouch((touchData: string): void => this.touchCallback(touchData));
56                } else {
57                  this.mTouchData = ''
58                  TouchManagerUtil.unregisterTouch()
59                }
60              })
61          }
62          .width('100%')
63
64          Text($r('app.string.input_touch_event_hint'))
65            .fontColor($r('app.color.battery_info_value_text'))
66            .fontSize($r('sys.float.ohos_id_text_size_body2'))
67            .width('100%')
68        }
69        .padding(px2vp(24))
70        .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
71        .border({
72          radius: $r('sys.float.ohos_id_corner_radius_default_l')
73        })
74        .id('columnInputListener')
75
76        if (this.mIsShowTouchData) {
77          Text($r('app.string.input_touch_event_data_title'))
78            .fontColor($r('sys.color.ohos_id_color_text_secondary'))
79            .fontSize($r('sys.float.ohos_id_text_size_body2'))
80            .fontWeight(FontWeight.Regular)
81            .fontFamily('HarmonyHeiTi')
82          Column() {
83            Text(this.mTouchData)
84              .fontColor($r('app.color.input_consumer_hint'))
85              .fontSize(14)
86              .fontWeight(FontWeight.Regular)
87              .width('100%')
88          }
89          .padding(px2vp(24))
90          .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
91          .border({
92            radius: $r('sys.float.ohos_id_corner_radius_default_l')
93          })
94        }
95
96        Column() {
97          Text($r('app.string.input_inject_back_event'))
98            .fontColor($r('sys.color.ohos_id_color_text_primary'))
99            .fontSize($r('sys.float.ohos_id_text_size_body1'))
100            .fontWeight(FontWeight.Medium)
101            .width('100%')
102            .margin({
103              bottom: px2vp(10)
104            })
105
106          Text($r('app.string.input_inject_back_event_hint'))
107            .fontColor($r('app.color.battery_info_value_text'))
108            .fontSize($r('sys.float.ohos_id_text_size_body2'))
109            .width('100%')
110
111          Column() {
112            Text($r('app.string.input_inject_back_text'))
113              .fontColor(Color.White)
114              .fontSize($r('sys.float.ohos_id_text_size_body2'))
115          }
116          .width('60%')
117          .height(35)
118          .justifyContent(FlexAlign.Center)
119          .alignItems(HorizontalAlign.Center)
120          .backgroundColor(this.mBtnBg)
121          .margin({
122            top: px2vp(24)
123          })
124          .onTouch((event) => {
125            if (event !== undefined && event !== null) {
126              console.log(`TouchManagerUtil onTouch event ${JSON.stringify(event)}`);
127              if (event.type === TouchType.Down) {
128                this.mBtnBg = $r('app.color.btn_press_bg');
129              }
130              if (event.type === TouchType.Up) {
131                this.mBtnBg = $r('app.color.btn_normal_bg');
132              }
133            }
134          })
135          .onClick(() => {
136            injectBackEvent()
137          })
138          .border({
139            radius: $r('sys.float.ohos_id_corner_radius_default_l')
140          })
141          .id('columnBackBtn')
142        }
143        .padding(px2vp(24))
144        .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
145        .border({
146          radius: $r('sys.float.ohos_id_corner_radius_default_l')
147        })
148        .id('columnBackEvent')
149
150      }.justifyContent(FlexAlign.Center)
151      .alignItems(HorizontalAlign.Start)
152      .margin(px2vp(24))
153    }
154    .width('100%')
155    .scrollBar(BarState.Off)
156    .id('scrollInputEvent')
157  }
158}