1a3e0fd82Sopenharmony_ci/* 2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License. 5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at 6a3e0fd82Sopenharmony_ci * 7a3e0fd82Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8a3e0fd82Sopenharmony_ci * 9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and 13a3e0fd82Sopenharmony_ci * limitations under the License. 14a3e0fd82Sopenharmony_ci */ 15a3e0fd82Sopenharmony_ci 16a3e0fd82Sopenharmony_ci#include "dock/rotate_input_device.h" 17a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h" 18a3e0fd82Sopenharmony_ci#include "dock/focus_manager.h" 19a3e0fd82Sopenharmony_ci 20a3e0fd82Sopenharmony_ci 21a3e0fd82Sopenharmony_ci#if ENABLE_ROTATE_INPUT 22a3e0fd82Sopenharmony_cinamespace { 23a3e0fd82Sopenharmony_ci#ifdef _WIN32 24a3e0fd82Sopenharmony_ciconstexpr int16_t ROTATE_INPUT_THRESHOLD = 1; 25a3e0fd82Sopenharmony_ci#else 26a3e0fd82Sopenharmony_ciconstexpr int16_t ROTATE_INPUT_THRESHOLD = 10; 27a3e0fd82Sopenharmony_ci#endif 28a3e0fd82Sopenharmony_ciconstexpr uint8_t ROTATE_END_ZERO_COUNT = 2; 29a3e0fd82Sopenharmony_ci} 30a3e0fd82Sopenharmony_ci 31a3e0fd82Sopenharmony_cinamespace OHOS { 32a3e0fd82Sopenharmony_ciRotateInputDevice::RotateInputDevice() 33a3e0fd82Sopenharmony_ci : rotateStart_(false), threshold_(ROTATE_INPUT_THRESHOLD), cachedRotation_(0), zeroCount_(0) 34a3e0fd82Sopenharmony_ci{ 35a3e0fd82Sopenharmony_ci} 36a3e0fd82Sopenharmony_ci 37a3e0fd82Sopenharmony_civoid RotateInputDevice::DispatchEvent(const DeviceData& data) 38a3e0fd82Sopenharmony_ci{ 39a3e0fd82Sopenharmony_ci bool cachedToRotate = false; 40a3e0fd82Sopenharmony_ci if (data.rotate == 0 || rotateStart_) { 41a3e0fd82Sopenharmony_ci cachedRotation_ = 0; 42a3e0fd82Sopenharmony_ci } else { 43a3e0fd82Sopenharmony_ci cachedRotation_ += data.rotate; 44a3e0fd82Sopenharmony_ci if (MATH_ABS(cachedRotation_) >= threshold_) { 45a3e0fd82Sopenharmony_ci cachedToRotate = true; 46a3e0fd82Sopenharmony_ci } 47a3e0fd82Sopenharmony_ci } 48a3e0fd82Sopenharmony_ci 49a3e0fd82Sopenharmony_ci if (!cachedToRotate && !rotateStart_) { 50a3e0fd82Sopenharmony_ci return; 51a3e0fd82Sopenharmony_ci } 52a3e0fd82Sopenharmony_ci 53a3e0fd82Sopenharmony_ci UIView* view = FocusManager::GetInstance()->GetFocusedView(); 54a3e0fd82Sopenharmony_ci if (view == nullptr) { 55a3e0fd82Sopenharmony_ci GRAPHIC_LOGE("RotateInputDevice Failed to dispatch event without focused view!\n"); 56a3e0fd82Sopenharmony_ci return; 57a3e0fd82Sopenharmony_ci } 58a3e0fd82Sopenharmony_ci 59a3e0fd82Sopenharmony_ci UIView* par = view; 60a3e0fd82Sopenharmony_ci while (par->GetParent() != nullptr) { 61a3e0fd82Sopenharmony_ci if (!par->IsVisible()) { 62a3e0fd82Sopenharmony_ci return; 63a3e0fd82Sopenharmony_ci } 64a3e0fd82Sopenharmony_ci par = par->GetParent(); 65a3e0fd82Sopenharmony_ci } 66a3e0fd82Sopenharmony_ci if (par->GetViewType() != UI_ROOT_VIEW) { 67a3e0fd82Sopenharmony_ci GRAPHIC_LOGW("RotateInputDevice failed to dispatch event without target view attached!\n"); 68a3e0fd82Sopenharmony_ci return; 69a3e0fd82Sopenharmony_ci } 70a3e0fd82Sopenharmony_ci 71a3e0fd82Sopenharmony_ci if (data.rotate == 0 && rotateStart_) { 72a3e0fd82Sopenharmony_ci zeroCount_++; 73a3e0fd82Sopenharmony_ci if (zeroCount_ >= ROTATE_END_ZERO_COUNT) { 74a3e0fd82Sopenharmony_ci view->OnRotateEndEvent(0); 75a3e0fd82Sopenharmony_ci zeroCount_ = 0; 76a3e0fd82Sopenharmony_ci rotateStart_ = false; 77a3e0fd82Sopenharmony_ci GRAPHIC_LOGW("RotateInputDevice dispatched 0-value event!\n"); 78a3e0fd82Sopenharmony_ci } 79a3e0fd82Sopenharmony_ci return; 80a3e0fd82Sopenharmony_ci } 81a3e0fd82Sopenharmony_ci if (!rotateStart_) { 82a3e0fd82Sopenharmony_ci view->OnRotateStartEvent(data.rotate); 83a3e0fd82Sopenharmony_ci } 84a3e0fd82Sopenharmony_ci view->OnRotateEvent(data.rotate); 85a3e0fd82Sopenharmony_ci rotateStart_ = true; 86a3e0fd82Sopenharmony_ci GRAPHIC_LOGI("RotateInputDevice dispatched rotate event, targetView Type = %{public}d,\ 87a3e0fd82Sopenharmony_ci rotate value = %{public}d\n!", static_cast<uint8_t>(view->GetViewType()), data.rotate); 88a3e0fd82Sopenharmony_ci} 89a3e0fd82Sopenharmony_ci} // namespace OHOS 90a3e0fd82Sopenharmony_ci#endif 91