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 }
29
30 namespace OHOS {
RotateInputDevice()31 RotateInputDevice::RotateInputDevice() : rotateStart_(false), threshold_(ROTATE_INPUT_THRESHOLD), cachedRotation_(0) {}
32
DispatchEvent(const DeviceData & data)33 void RotateInputDevice::DispatchEvent(const DeviceData& data)
34 {
35 bool cachedToRotate = false;
36 if (data.rotate == 0 || rotateStart_) {
37 cachedRotation_ = 0;
38 } else {
39 cachedRotation_ += data.rotate;
40 if (MATH_ABS(cachedRotation_) >= threshold_) {
41 cachedToRotate = true;
42 }
43 }
44
45 if (!cachedToRotate && !rotateStart_) {
46 return;
47 }
48
49 UIView* view = FocusManager::GetInstance()->GetFocusedView();
50 if (view == nullptr) {
51 GRAPHIC_LOGE("RotateInputDevice Failed to dispatch event without focused view!\n");
52 return;
53 }
54
55 UIView* par = view;
56 while (par->GetParent() != nullptr) {
57 if (!par->IsVisible()) {
58 return;
59 }
60 par = par->GetParent();
61 }
62 if (par->GetViewType() != UI_ROOT_VIEW) {
63 GRAPHIC_LOGW("RotateInputDevice failed to dispatch event without target view attached!\n");
64 return;
65 }
66
67 if (data.rotate == 0 && rotateStart_) {
68 view->OnRotateEndEvent(0);
69 rotateStart_ = false;
70 GRAPHIC_LOGW("RotateInputDevice dispatched 0-value event!\n");
71 return;
72 }
73 if (!rotateStart_) {
74 view->OnRotateStartEvent(data.rotate);
75 }
76 view->OnRotateEvent(data.rotate);
77 rotateStart_ = true;
78 GRAPHIC_LOGI("RotateInputDevice dispatched rotate event, targetView Type = %{public}d,\
79 rotate value = %{public}d\n!", static_cast<uint8_t>(view->GetViewType()), data.rotate);
80 }
81 } // namespace OHOS
82 #endif
83