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