1 /*
2 * Copyright (c) 2024 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 "base/mousestyle/mouse_style.h"
17
18 #include "base/log/log_wrapper.h"
19 #include "core/components_ng/base/frame_node.h"
20
21 namespace OHOS::Ace {
22 namespace {
23 constexpr int32_t MOUSESTYLE_LOG_LIMIT = 10;
24 }
SetMouseFormat(int32_t windowId,int32_t nodeId,MouseFormat mouseFormat,bool isByPass,MouseStyleChangeReason reason)25 bool MouseStyleManager::SetMouseFormat(int32_t windowId, int32_t nodeId, MouseFormat mouseFormat,
26 bool isByPass, MouseStyleChangeReason reason)
27 {
28 TAG_LOGD(AceLogTag::ACE_MOUSE, "SetMouseFormat windowId = %{public}d, "
29 "nodeId = " SEC_PLD(%{public}d) ", "
30 "mouseFormat = %{public}d, reason = %{public}d", windowId,
31 SEC_PARAM(nodeId), mouseFormat, reason);
32 auto pipelineContext = PipelineContext::GetCurrentContext();
33 CHECK_NULL_RETURN(pipelineContext, false);
34 if (isByPass) {
35 return false;
36 }
37 if (userSetCursor_ && reason != MouseStyleChangeReason::USER_SET_MOUSESTYLE) {
38 return false;
39 }
40 if (!userSetCursor_ && (!mouseStyleNodeId_.has_value() ||
41 mouseStyleNodeId_.value() != nodeId)) {
42 return false;
43 }
44 if (!windowId) {
45 windowId = static_cast<int32_t>(pipelineContext->GetFocusWindowId());
46 }
47
48 MouseStyleChangeLog mouseStyleChangeLog;
49 mouseStyleChangeLog.windowId = windowId;
50 mouseStyleChangeLog.changeNodeId = nodeId;
51 if (vsyncMouseStyleChanges_.empty()) {
52 mouseStyleChangeLog.beforeMouseStyle = mouseFormat_;
53 } else {
54 mouseStyleChangeLog.beforeMouseStyle = vsyncMouseStyleChanges_.back().afterMouseStyle;
55 }
56 mouseStyleChangeLog.afterMouseStyle = mouseFormat;
57 mouseStyleChangeLog.reason = reason;
58 vsyncMouseStyleChanges_.push_back(mouseStyleChangeLog);
59 return true;
60 }
61
VsyncMouseFormat()62 void MouseStyleManager::VsyncMouseFormat()
63 {
64 if (vsyncMouseStyleChanges_.empty()) {
65 return;
66 }
67
68 MouseStyleChangeLog mouseStyleChangeLog;
69 mouseStyleChangeLog.beforeMouseStyle = lastVsyncMouseFormat_;
70 lastVsyncMouseFormat_ = mouseFormat_;
71 MouseStyleChangeReason changeReason = MouseStyleChangeReason::INNER_SET_MOUSESTYLE;
72 int32_t changeNodeId = -1;
73 int32_t windowId = -1;
74 for (const auto& iter : vsyncMouseStyleChanges_) {
75 if (iter.reason >= changeReason) {
76 mouseFormat_ = iter.afterMouseStyle;
77 windowId = iter.windowId;
78 changeNodeId = iter.changeNodeId;
79 changeReason = iter.reason;
80 }
81 }
82 mouseStyleChangeLog.afterMouseStyle = mouseFormat_;
83 mouseStyleChangeLog.changeNodeId = changeNodeId;
84 mouseStyleChangeLog.reason = changeReason;
85
86 if (mouseFormat_ == lastVsyncMouseFormat_) {
87 TAG_LOGI(AceLogTag::ACE_MOUSE, "VsyncMouseFormat mouseFormat_ and "
88 "lastVsyncMouseFormat_ = %{public}d is same.", mouseFormat_);
89 vsyncMouseStyleChanges_.clear();
90 return;
91 }
92
93 TAG_LOGD(AceLogTag::ACE_MOUSE, "VsyncMouseFormat setPointerStyle, windowId is %{public}d, "
94 "nodeId is %{public}d, last vsync mouseFormat is %{public}d, set mouseFormat is %{public}d",
95 windowId, changeNodeId, lastVsyncMouseFormat_, mouseFormat_);
96 auto mouseStyle = MouseStyle::CreateMouseStyle();
97 mouseStyle->SetPointerStyle(static_cast<int32_t>(windowId), mouseFormat_);
98
99 mouseStyleChangeLogs_.push_back(mouseStyleChangeLog);
100 if (mouseStyleChangeLogs_.size() > MOUSESTYLE_LOG_LIMIT) {
101 mouseStyleChangeLogs_.pop_front();
102 }
103 vsyncMouseStyleChanges_.clear();
104 }
105
DumpMouseStyleChangeLog()106 void MouseStyleManager::DumpMouseStyleChangeLog()
107 {
108 for (const auto& iter : mouseStyleChangeLogs_) {
109 TAG_LOGI(AceLogTag::ACE_MOUSE, "MouseStyleChangeLog: windowId is %{public}d, "
110 "nodeId is %{public}d, beforeMouseStyle is %{public}d, afterMouseStyle is %{public}d",
111 iter.windowId, iter.changeNodeId,
112 iter.beforeMouseStyle, iter.afterMouseStyle);
113 }
114 }
115
116 } // namespace OHOS::Ace