• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_VIEW_ABSTRACT_H
17 #define FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_VIEW_ABSTRACT_H
18 
19 #include <functional>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 #include "base/geometry/dimension.h"
25 #include "base/geometry/dimension_rect.h"
26 #include "base/json/json_util.h"
27 #include "base/log/ace_scoring_log.h"
28 #include "base/log/ace_trace.h"
29 #include "base/log/log.h"
30 #include "base/memory/ace_type.h"
31 #include "base/utils/macros.h"
32 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_common_ffi.h"
33 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
34 #include "core/common/container.h"
35 #include "core/common/resource/resource_manager.h"
36 #include "core/common/resource/resource_object.h"
37 #include "core/common/resource/resource_wrapper.h"
38 #include "core/components/common/properties/decoration.h"
39 #include "core/components/common/properties/placement.h"
40 #include "core/components_ng/pattern/text/text_menu_extension.h"
41 #include "core/components_ng/pattern/text/text_model.h"
42 #ifndef __OHOS_NG__
43 #include "core/components/box/box_component.h"
44 #include "core/components/display/display_component.h"
45 #include "core/components/menu/menu_component.h"
46 #include "core/components/theme/theme_manager.h"
47 #include "core/components/transform/transform_component.h"
48 #endif
49 #include "ffi_remote_data.h"
50 
51 #include "core/gestures/tap_gesture.h"
52 #include "core/pipeline/base/component.h"
53 
54 using VectorTextMenuItemHandle = void*;
55 
56 extern "C" {
57 struct NativeResourceObject {
58     const char* bundleName;
59     const char* moduleName;
60     int32_t id;
61     int32_t type;
62     const char* paramsJsonStr;
63 };
64 
65 struct FfiTextMenuItem {
66     ExternalString content;
67     ExternalString icon;
68     ExternalString id;
69 };
70 }
71 
72 typedef VectorTextMenuItemHandle (*CjOnCreateMenu)(VectorTextMenuItemHandle);
73 typedef bool (*CjOnMenuItemClick)(FfiTextMenuItem, int32_t, int32_t);
74 
75 namespace OHOS::Ace::Framework {
76 RefPtr<ResourceObject> GetResourceObject(const NativeResourceObject& obj);
77 RefPtr<ResourceObject> GetResourceObjectByBundleAndModule(const NativeResourceObject& obj);
78 RefPtr<ResourceWrapper> CreateResourceWrapper(const NativeResourceObject& obj, RefPtr<ResourceObject>& resourceObject);
79 RefPtr<ResourceWrapper> CreateResourceWrapper();
80 
81 enum class Align {
82     TOP_LEFT,
83     TOP_CENTER,
84     TOP_RIGHT,
85     CENTER_LEFT,
86     CENTER,
87     CENTER_RIGHT,
88     BOTTOM_LEFT,
89     BOTTOM_CENTER,
90     BOTTOM_RIGHT,
91 };
92 
93 enum class CJResponseType : int32_t {
94     RIGHT_CLICK = 0,
95     LONGPRESS,
96 };
97 
98 class ACE_EXPORT ViewAbstract : public OHOS::FFI::FFIData {
DECL_TYPE(ViewAbstract,OHOS::FFI::FFIData)99     DECL_TYPE(ViewAbstract, OHOS::FFI::FFIData)
100 public:
101     ViewAbstract() : FFIData() {}
102 
103     template<typename T>
GetTheme()104     static RefPtr<T> GetTheme()
105     {
106         auto currentObj = Container::Current();
107         if (!currentObj) {
108             LOGW("container is null");
109             return nullptr;
110         }
111         auto pipelineContext = currentObj->GetPipelineContext();
112         if (!pipelineContext) {
113             LOGE("pipelineContext is null!");
114             return nullptr;
115         }
116         auto themeManager = pipelineContext->GetThemeManager();
117         if (!themeManager) {
118             LOGE("themeManager is null!");
119             return nullptr;
120         }
121         return themeManager->GetTheme<T>();
122     }
123 
124     static RefPtr<ThemeConstants> GetThemeConstants(
125         const std::string& bundleName = "", const std::string& moduleName = "");
126     static void CjEnabled(bool enabled);
127 
128     static void CompleteResourceObject(NativeResourceObject& obj, std::string& bundleName, std::string& moduleName);
129     static void CompleteResourceObjectWithBundleName(
130         NativeResourceObject& obj, std::string& bundleName, std::string& moduleName, int32_t& resId);
131     static bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
132     static bool ParseDollarResource(const std::string& resPath, std::string& targetModule, ResourceType& resType,
133         std::string& resName, bool isParseType);
134 
135     static bool ParseCjString(NativeResourceObject& obj, std::string& result);
136     static bool ParseCjMedia(NativeResourceObject& obj, std::string& result);
137     static bool ParseCjSymbolId(NativeResourceObject& obj, uint32_t& result);
138     static bool ParseCjColor(NativeResourceObject& obj, Color& result);
139     static bool ParseCjDimension(
140         NativeResourceObject& obj, CalcDimension& result, DimensionUnit defaultUnit, bool isSupportPercent = true);
141     static bool ParseCjDimensionVP(NativeResourceObject& obj, CalcDimension& result, bool isSupportPercent = true);
142     static bool ParseCjDimensionFP(NativeResourceObject& obj, CalcDimension& result, bool isSupportPercent = true);
143     static bool ParseCjDouble(NativeResourceObject& obj, double& result);
144     static bool ParseCjInteger(NativeResourceObject& obj, uint32_t& result);
145     static bool ParseCjInteger(NativeResourceObject& obj, int32_t& result);
146     static bool ParseCjBool(NativeResourceObject& obj, bool& result);
147     static bool ParseCjIntegerArray(NativeResourceObject& obj, std::vector<uint32_t>& result);
148     static bool ParseCjStringArray(NativeResourceObject& obj, std::vector<std::string>& result);
149 
150     template<typename T>
ParseCjInteger(NativeResourceObject & obj,T & result)151     static bool ParseCjInteger(NativeResourceObject& obj, T& result)
152     {
153         std::string bundleName;
154         std::string moduleName;
155         CompleteResourceObject(obj, bundleName, moduleName);
156         if (obj.type == -1) {
157             return false;
158         }
159         auto resourceObject = GetResourceObjectByBundleAndModule(obj);
160         auto resourceWrapper = CreateResourceWrapper(obj, resourceObject);
161         if (!resourceWrapper) {
162             return false;
163         }
164         if (obj.id == -1) {
165             if (!obj.paramsJsonStr) {
166                 return false;
167             }
168             auto params = JsonUtil::ParseJsonString(obj.paramsJsonStr);
169             if (!params->IsArray()) {
170                 return false;
171             }
172             auto param = params->GetArrayItem(0);
173             if (obj.type == static_cast<int32_t>(ResourceType::INTEGER)) {
174                 result = static_cast<T>(resourceWrapper->GetIntByName(param->GetString()));
175                 return true;
176             }
177             return false;
178         }
179         if (obj.type == static_cast<int32_t>(ResourceType::INTEGER)) {
180             result = static_cast<T>(resourceWrapper->GetInt(static_cast<uint32_t>(obj.id)));
181             return true;
182         }
183         return false;
184     }
185 
186     static bool ParseEditMenuOptions(CjOnCreateMenu& cjOnCreateMenu, CjOnMenuItemClick& cjOnMenuItemClick,
187         NG::OnCreateMenuCallback& onCreateMenuCallback, NG::OnMenuItemClickCallback& onMenuItemClick);
188 
189 private:
190     static void CompleteResourceObjectInner(
191         NativeResourceObject& obj, std::string& bundleName, std::string& moduleName, int32_t& resIdValue);
192     static bool ParseCjMediaInternal(NativeResourceObject& obj, std::string& result);
193     static void ParseOnCreateMenu(CjOnCreateMenu& cjOnCreateMenu, NG::OnCreateMenuCallback& onCreateMenuCallback);
194     static void ParseOnMenuItemClick(
195         CjOnMenuItemClick& cjOnMenuItemClick, NG::OnMenuItemClickCallback& onMenuItemClick);
196 };
197 } // namespace OHOS::Ace::Framework
198 #endif // FRAMEWORKS_BRIDGE_CJ_FRONTEND_CPP_VIEW_VIEW_ABSTRACT_H
199