1 /*
2 * Copyright (c) 2020-2021 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
16 #include "dock/rotate_input_device.h"
17 #include "gfx_utils/graphic_log.h"
18 #include "dock/focus_manager.h"
19
20
21 #if ENABLE_ROTATE_INPUT
22 namespace {
23 #ifdef _WIN32
24 constexpr int16_t ROTATE_INPUT_THRESHOLD = 1;
25 #else
26 constexpr int16_t ROTATE_INPUT_THRESHOLD = 10;
27 #endif
28 constexpr uint8_t ROTATE_END_ZERO_COUNT = 2;
29 }
30
31 namespace OHOS {
RotateInputDevice()32 RotateInputDevice::RotateInputDevice()
33 : rotateStart_(false), threshold_(ROTATE_INPUT_THRESHOLD), cachedRotation_(0), zeroCount_(0)
34 {
35 }
36
DispatchEvent(const DeviceData & data)37 void RotateInputDevice::DispatchEvent(const DeviceData& data)
38 {
39 bool cachedToRotate = false;
40 if (data.rotate == 0 || rotateStart_) {
41 cachedRotation_ = 0;
42 } else {
43 cachedRotation_ += data.rotate;
44 if (MATH_ABS(cachedRotation_) >= threshold_) {
45 cachedToRotate = true;
46 }
47 }
48
49 if (!cachedToRotate && !rotateStart_) {
50 return;
51 }
52
53 UIView* view = FocusManager::GetInstance()->GetFocusedView();
54 if (view == nullptr) {
55 GRAPHIC_LOGE("RotateInputDevice Failed to dispatch event without focused view!\n");
56 return;
57 }
58
59 UIView* par = view;
60 while (par->GetParent() != nullptr) {
61 if (!par->IsVisible()) {
62 return;
63 }
64 par = par->GetParent();
65 }
66 if (par->GetViewType() != UI_ROOT_VIEW) {
67 GRAPHIC_LOGW("RotateInputDevice failed to dispatch event without target view attached!\n");
68 return;
69 }
70
71 if (data.rotate == 0 && rotateStart_) {
72 zeroCount_++;
73 if (zeroCount_ >= ROTATE_END_ZERO_COUNT) {
74 view->OnRotateEndEvent(0);
75 zeroCount_ = 0;
76 rotateStart_ = false;
77 GRAPHIC_LOGW("RotateInputDevice dispatched 0-value event!\n");
78 }
79 return;
80 }
81 if (!rotateStart_) {
82 view->OnRotateStartEvent(data.rotate);
83 }
84 view->OnRotateEvent(data.rotate);
85 rotateStart_ = true;
86 GRAPHIC_LOGI("RotateInputDevice dispatched rotate event, targetView Type = %{public}d,\
87 rotate value = %{public}d\n!", static_cast<uint8_t>(view->GetViewType()), data.rotate);
88 }
89 } // namespace OHOS
90 #endif
91