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 #ifndef FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H 18 19 #include <memory> 20 #include <mutex> 21 22 #include "adapter/ohos/entrance/ace_container.h" 23 #include "base/resource/asset_manager.h" 24 #include "base/thread/task_executor.h" 25 #include "base/utils/noncopyable.h" 26 #include "core/common/ace_view.h" 27 #include "core/common/container.h" 28 #include "core/common/js_message_dispatcher.h" 29 #include "core/common/window.h" 30 #include "core/components/dialog/dialog_properties.h" 31 #include "core/components_ng/render/adapter/rosen_window.h" 32 33 namespace OHOS::Ace::Platform { 34 class DialogContainer : public Container, public JsMessageDispatcher { 35 DECLARE_ACE_TYPE(DialogContainer, Container, JsMessageDispatcher); 36 37 public: 38 explicit DialogContainer(int32_t instanceId, FrontendType type = FrontendType::DECLARATIVE_JS); 39 ~DialogContainer() override = default; 40 41 void Initialize() override {}; 42 void Destroy() override; 43 void DestroyView() override; 44 45 int32_t GetInstanceId() const override 46 { 47 if (aceView_) { 48 return aceView_->GetInstanceId(); 49 } 50 return -1; 51 } 52 53 RefPtr<Frontend> GetFrontend() const override 54 { 55 return frontend_; 56 } 57 GetResourceConfiguration() const58 ResourceConfiguration GetResourceConfiguration() const 59 { 60 return resourceInfo_.GetResourceConfiguration(); 61 } 62 SetResourceConfiguration(const ResourceConfiguration& config)63 void SetResourceConfiguration(const ResourceConfiguration& config) 64 { 65 resourceInfo_.SetResourceConfiguration(config); 66 } 67 68 RefPtr<PlatformResRegister> GetPlatformResRegister() const override 69 { 70 return resRegister_; 71 } 72 73 RefPtr<PipelineBase> GetPipelineContext() const override 74 { 75 return pipelineContext_; 76 } 77 78 int32_t GetViewPosX() const override 79 { 80 return aceView_ ? aceView_->GetPosX() : 0; 81 } 82 83 int32_t GetViewPosY() const override 84 { 85 return aceView_ ? aceView_->GetPosY() : 0; 86 } 87 88 void SetWindowId(uint32_t windowId) override 89 { 90 windowId_ = windowId; 91 } 92 93 uint32_t GetWindowId() const override 94 { 95 return windowId_; 96 } 97 98 int32_t GetViewWidth() const override 99 { 100 return aceView_ ? aceView_->GetWidth() : 0; 101 } 102 103 int32_t GetViewHeight() const override 104 { 105 return aceView_ ? aceView_->GetHeight() : 0; 106 } 107 108 RefPtr<AceView> GetAceView() const override 109 { 110 std::lock_guard<std::mutex> lock(viewMutex_); 111 return aceView_; 112 } 113 114 void* GetView() const override 115 { 116 std::lock_guard<std::mutex> lock(viewMutex_); 117 return static_cast<void*>(AceType::RawPtr(aceView_)); 118 } 119 120 RefPtr<TaskExecutor> GetTaskExecutor() const override 121 { 122 return taskExecutor_; 123 } 124 125 void Dispatch( 126 const std::string& group, std::vector<uint8_t>&& data, int32_t id, bool replyToComponent) const override {}; 127 128 void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override {}; 129 130 void DispatchSync( 131 const std::string& group, std::vector<uint8_t>&& data, uint8_t** resData, int64_t& position) const override 132 {} 133 134 std::string GetHostClassName() const override 135 { 136 return ""; 137 } 138 139 void DumpHeapSnapshot(bool isPrivate) override; 140 SetAssetManager(const RefPtr<AssetManager>& assetManager)141 void SetAssetManager(const RefPtr<AssetManager>& assetManager) 142 { 143 assetManager_ = assetManager; 144 if (frontend_) { 145 frontend_->SetAssetManager(assetManager); 146 } 147 } 148 149 RefPtr<AssetManager> GetAssetManager() const override 150 { 151 return assetManager_; 152 } 153 154 bool IsSubContainer() const override 155 { 156 return true; 157 } 158 159 bool IsDialogContainer() const override 160 { 161 return true; 162 } 163 164 static void ShowToast(int32_t instanceId, const std::string& message, int32_t duration, const std::string& bottom, 165 std::function<void(int32_t)>&& callback); 166 static void CloseToast(int32_t instanceId, const int32_t toastId, std::function<void(int32_t)>&& callback); 167 static void ShowDialog(int32_t instanceId, const std::string& title, const std::string& message, 168 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, 169 const std::set<std::string>& callbacks); 170 static void ShowDialog(int32_t instanceId, const PromptDialogAttr& dialogAttr, 171 const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback, 172 const std::set<std::string>& callbacks); 173 static void ShowActionMenu(int32_t instanceId, const std::string& title, const std::vector<ButtonInfo>& button, 174 std::function<void(int32_t, int32_t)>&& callback); 175 176 static bool ShowToastDialogWindow( 177 int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast = false); 178 static bool CloseWindow(int32_t instanceId); 179 static bool HideWindow(int32_t instanceId); 180 181 static void SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window>& uiWindow); 182 static sptr<OHOS::Rosen::Window> GetUIWindow(int32_t instanceId); 183 184 static void DestroyContainer(int32_t instanceId, const std::function<void()>& destroyCallback = nullptr); 185 static RefPtr<DialogContainer> GetContainer(int32_t instanceId); 186 static void SetView(const RefPtr<AceView>& view, double density, int32_t width, int32_t height, 187 sptr<OHOS::Rosen::Window>& rsWindow); 188 static void SetViewNew(const RefPtr<AceView>& view, double density, int32_t width, int32_t height, 189 sptr<OHOS::Rosen::Window>& rsWindow); 190 static bool OnBackPressed(int32_t instanceId); 191 192 void SetFontScaleAndWeightScale(int32_t instanceId); 193 void UpdateConfiguration(const ParsedConfig& parsedConfig); 194 void CheckAndSetFontFamily() override; 195 196 private: 197 void InitPipelineContext(std::shared_ptr<Window> window, int32_t instanceId, double density, int32_t width, 198 int32_t height, uint32_t windowId); 199 void InitializeFrontend(); 200 void InitializeCallback(); 201 void InitializeTouchEventCallback(); 202 void InitializeMouseEventCallback(); 203 void InitializeAxisEventCallback(); 204 void InitializeKeyEventCallback(); 205 void InitializeRotationEventCallback(); 206 void InitializeViewChangeCallback(); 207 void InitializeDensityChangeCallback(); 208 void InitializeSystemBarHeightChangeCallback(); 209 void InitializeSurfaceDestroyCallback(); 210 void InitializeDragEventCallback(); 211 212 void AttachView(std::shared_ptr<Window> window, const RefPtr<AceView>& view, double density, int32_t width, 213 int32_t height, uint32_t windowId); 214 void SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow); 215 sptr<OHOS::Rosen::Window> GetUIWindowInner() const; 216 217 uint32_t windowId_ = OHOS::Rosen::INVALID_WINDOW_ID; 218 int32_t instanceId_ = -1; 219 RefPtr<AceView> aceView_; 220 RefPtr<TaskExecutor> taskExecutor_; 221 RefPtr<AssetManager> assetManager_; 222 RefPtr<PlatformResRegister> resRegister_; 223 RefPtr<PipelineBase> pipelineContext_; 224 RefPtr<Frontend> frontend_; 225 FrontendType type_ = FrontendType::DECLARATIVE_JS; 226 ResourceInfo resourceInfo_; 227 sptr<OHOS::Rosen::Window> uiWindow_ = nullptr; 228 std::string windowName_; 229 WindowModal windowModal_ { WindowModal::NORMAL }; 230 ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE }; 231 mutable std::mutex viewMutex_; 232 233 ACE_DISALLOW_COPY_AND_MOVE(DialogContainer); 234 }; 235 236 } // namespace OHOS::Ace::Platform 237 238 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_DIALOG_CONTAINER_H 239