• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_ohos.h"
17 
18 #include "input_manager.h"
19 #include "struct_multimodal.h"
20 
21 #include "core/common/container.h"
22 
23 namespace OHOS::Ace {
24 
CreateMouseStyle()25 RefPtr<MouseStyle> MouseStyle::CreateMouseStyle()
26 {
27     return AceType::MakeRefPtr<MouseStyleOhos>();
28 }
29 
SetPointerStyle(int32_t windowId,MouseFormat pointerStyle) const30 bool MouseStyleOhos::SetPointerStyle(int32_t windowId, MouseFormat pointerStyle) const
31 {
32     auto container = Container::Current();
33     if (!container) {
34         TAG_LOGW(AceLogTag::ACE_MOUSE, "SetPointerStyle container is null!");
35         return false;
36     }
37     auto isUIExtension = container->IsUIExtensionWindow() && pointerStyle != MouseFormat::DEFAULT;
38     auto inputManager = MMI::InputManager::GetInstance();
39     if (!inputManager) {
40         TAG_LOGW(AceLogTag::ACE_MOUSE, "SetPointerStyle inputManager is null!");
41         return false;
42     }
43     static const LinearEnumMapNode<MouseFormat, int32_t> mouseFormatMap[] = {
44         { MouseFormat::DEFAULT, MMI::DEFAULT },
45         { MouseFormat::EAST, MMI::EAST },
46         { MouseFormat::WEST, MMI::WEST },
47         { MouseFormat::SOUTH, MMI::SOUTH },
48         { MouseFormat::NORTH, MMI::NORTH },
49         { MouseFormat::WEST_EAST, MMI::WEST_EAST },
50         { MouseFormat::NORTH_SOUTH, MMI::NORTH_SOUTH },
51         { MouseFormat::NORTH_EAST, MMI::NORTH_EAST },
52         { MouseFormat::NORTH_WEST, MMI::NORTH_WEST },
53         { MouseFormat::SOUTH_EAST, MMI::SOUTH_EAST },
54         { MouseFormat::SOUTH_WEST, MMI::SOUTH_WEST },
55         { MouseFormat::NORTH_EAST_SOUTH_WEST, MMI::NORTH_EAST_SOUTH_WEST },
56         { MouseFormat::NORTH_WEST_SOUTH_EAST, MMI::NORTH_WEST_SOUTH_EAST },
57         { MouseFormat::CROSS, MMI::CROSS },
58         { MouseFormat::CURSOR_COPY, MMI::CURSOR_COPY },
59         { MouseFormat::CURSOR_FORBID, MMI::CURSOR_FORBID },
60         { MouseFormat::COLOR_SUCKER, MMI::COLOR_SUCKER },
61         { MouseFormat::HAND_GRABBING, MMI::HAND_GRABBING },
62         { MouseFormat::HAND_OPEN, MMI::HAND_OPEN },
63         { MouseFormat::HAND_POINTING, MMI::HAND_POINTING },
64         { MouseFormat::HELP, MMI::HELP },
65         { MouseFormat::CURSOR_MOVE, MMI::CURSOR_MOVE },
66         { MouseFormat::RESIZE_LEFT_RIGHT, MMI::RESIZE_LEFT_RIGHT },
67         { MouseFormat::RESIZE_UP_DOWN, MMI::RESIZE_UP_DOWN },
68         { MouseFormat::SCREENSHOT_CHOOSE, MMI::SCREENSHOT_CHOOSE },
69         { MouseFormat::SCREENSHOT_CURSOR, MMI::SCREENSHOT_CURSOR },
70         { MouseFormat::TEXT_CURSOR, MMI::TEXT_CURSOR },
71         { MouseFormat::ZOOM_IN, MMI::ZOOM_IN },
72         { MouseFormat::ZOOM_OUT, MMI::ZOOM_OUT },
73         { MouseFormat::MIDDLE_BTN_EAST, MMI::MIDDLE_BTN_EAST },
74         { MouseFormat::MIDDLE_BTN_WEST, MMI::MIDDLE_BTN_WEST },
75         { MouseFormat::MIDDLE_BTN_SOUTH, MMI::MIDDLE_BTN_SOUTH },
76         { MouseFormat::MIDDLE_BTN_NORTH, MMI::MIDDLE_BTN_NORTH },
77         { MouseFormat::MIDDLE_BTN_NORTH_SOUTH, MMI::MIDDLE_BTN_NORTH_SOUTH },
78         { MouseFormat::MIDDLE_BTN_NORTH_EAST, MMI::MIDDLE_BTN_NORTH_EAST },
79         { MouseFormat::MIDDLE_BTN_NORTH_WEST, MMI::MIDDLE_BTN_NORTH_WEST },
80         { MouseFormat::MIDDLE_BTN_SOUTH_EAST, MMI::MIDDLE_BTN_SOUTH_EAST },
81         { MouseFormat::MIDDLE_BTN_SOUTH_WEST, MMI::MIDDLE_BTN_SOUTH_WEST },
82         { MouseFormat::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST, MMI::MIDDLE_BTN_NORTH_SOUTH_WEST_EAST },
83         { MouseFormat::HORIZONTAL_TEXT_CURSOR, MMI::HORIZONTAL_TEXT_CURSOR },
84         { MouseFormat::CURSOR_CROSS, MMI::CURSOR_CROSS },
85         { MouseFormat::CURSOR_CIRCLE, MMI::CURSOR_CIRCLE },
86         { MouseFormat::LOADING, MMI::LOADING },
87         { MouseFormat::RUNNING, MMI::RUNNING },
88     };
89     int32_t MMIPointStyle = MMI::DEFAULT;
90     int64_t idx = BinarySearchFindIndex(mouseFormatMap, ArraySize(mouseFormatMap), pointerStyle);
91     if (idx >= 0) {
92         MMIPointStyle = mouseFormatMap[idx].value;
93     }
94     MMI::PointerStyle style;
95     style.id = MMIPointStyle;
96     TAG_LOGD(AceLogTag::ACE_MOUSE, "SetPointerStyle windowId=%{public}d style=%{public}d isUIExtension=%{public}d",
97         windowId, static_cast<int32_t>(pointerStyle), isUIExtension);
98     int32_t setResult = inputManager->SetPointerStyle(windowId, style, isUIExtension);
99     if (setResult == -1) {
100         TAG_LOGW(AceLogTag::ACE_MOUSE, "SetPointerStyle result is false");
101         return false;
102     }
103     return true;
104 }
105 
GetPointerStyle(int32_t windowId,int32_t & pointerStyle) const106 int32_t MouseStyleOhos::GetPointerStyle(int32_t windowId, int32_t& pointerStyle) const
107 {
108     auto container = Container::Current();
109     CHECK_NULL_RETURN(container, -1);
110     auto isUIExtension = container->IsUIExtensionWindow();
111     auto inputManager = MMI::InputManager::GetInstance();
112     CHECK_NULL_RETURN(inputManager, -1);
113     MMI::PointerStyle style;
114     int32_t getResult = inputManager->GetPointerStyle(windowId, style, isUIExtension);
115     if (getResult == -1) {
116         TAG_LOGW(AceLogTag::ACE_MOUSE, "GetPointerStyle result is false");
117         return -1;
118     }
119     pointerStyle = style.id;
120     return getResult;
121 }
122 
SetMouseIcon(int32_t windowId,MouseFormat pointerStyle,std::shared_ptr<Media::PixelMap> pixelMap) const123 void MouseStyleOhos::SetMouseIcon(
124     int32_t windowId, MouseFormat pointerStyle, std::shared_ptr<Media::PixelMap> pixelMap) const
125 {
126     auto inputManager = MMI::InputManager::GetInstance();
127     if (pointerStyle == MouseFormat::CONTEXT_MENU) {
128         inputManager->SetPointerVisible(true);
129         inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
130     } else if (pointerStyle == MouseFormat::ALIAS) {
131         inputManager->SetMouseIcon(windowId, static_cast<void*>(pixelMap.get()));
132         inputManager->SetPointerVisible(true);
133     }
134 }
135 
SetCustomCursor(int32_t windowId,int32_t focusX,int32_t focusY,std::shared_ptr<Media::PixelMap> pixelMap) const136 void MouseStyleOhos::SetCustomCursor(
137     int32_t windowId, int32_t focusX, int32_t focusY, std::shared_ptr<Media::PixelMap> pixelMap) const
138 {
139     auto inputManager = MMI::InputManager::GetInstance();
140     CHECK_NULL_VOID(inputManager);
141     CHECK_NULL_VOID(pixelMap);
142 
143     int32_t status = inputManager->SetCustomCursor(windowId, static_cast<void*>(pixelMap.get()), focusX, focusY);
144     if (status != 0) {
145         TAG_LOGE(AceLogTag::ACE_WEB, "set custom cursor failed %{public}u", status);
146         return;
147     }
148     inputManager->SetPointerVisible(true);
149 }
150 
SetPointerVisible(MouseFormat pointerStyle) const151 void MouseStyleOhos::SetPointerVisible(MouseFormat pointerStyle) const
152 {
153     auto inputManager = MMI::InputManager::GetInstance();
154     if (pointerStyle != MouseFormat::CURSOR_NONE) {
155         inputManager->SetPointerVisible(true);
156     } else {
157         inputManager->SetPointerVisible(false);
158     }
159 }
160 } // namespace OHOS::Ace