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