1 /* 2 * Copyright (c) 2023 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 JSAPPIMPL_H 17 #define JSAPPIMPL_H 18 19 #include <atomic> 20 #include <memory> 21 #include <thread> 22 23 #include "JsApp.h" 24 #include "ace_ability.h" 25 #include "ace_run_args.h" 26 #include "device_config.h" 27 #include "device_type.h" 28 #include "window_animation_config.h" 29 30 namespace OHOS { 31 namespace Previewer { 32 struct PreviewerWindowModel; 33 class PreviewerDisplay; 34 } 35 namespace Rosen { 36 class GlfwRenderContext; 37 enum class FoldStatus: uint32_t; 38 enum class WindowType : uint32_t; 39 struct SystemBarProperty; 40 struct Rect; 41 } 42 #if defined(__APPLE__) || defined(_WIN32) 43 namespace AbilityRuntime { 44 class Simulator; 45 struct Options; 46 struct DependencyHspInfo; 47 } 48 namespace AppExecFwk { 49 class Configuration; 50 } 51 #endif 52 } 53 54 class JsAppImpl : public JsApp { 55 public: 56 JsAppImpl() noexcept; 57 ~JsAppImpl(); 58 JsAppImpl& operator=(const JsAppImpl&) = delete; 59 JsAppImpl(const JsAppImpl&) = delete; 60 61 static JsAppImpl& GetInstance(); 62 void Start() override; 63 void Restart() override; 64 void Interrupt() override; 65 std::string GetJSONTree() override; 66 std::string GetDefaultJSONTree() override; 67 void OrientationChanged(std::string commandOrientation) override; 68 void ResolutionChanged(ResolutionParam& param, int32_t screenDensity, std::string reason) override; 69 OHOS::Ace::WindowSizeChangeReason ConvertResizeReason(std::string reason); 70 void SetResolutionParams(int32_t changedOriginWidth, int32_t changedOriginHeight, 71 int32_t changedWidth, int32_t changedHeight, int32_t screenDensity); 72 void SetArgsColorMode(const std::string& value) override; 73 void SetArgsAceVersion(const std::string& value) override; 74 void SetDeviceOrentation(const std::string& value); 75 std::string GetOrientation() const override; 76 std::string GetColorMode() const override; 77 void ColorModeChanged(const std::string commandColorMode) override; 78 void ReloadRuntimePage(const std::string currentPage) override; 79 void SetScreenDensity(const std::string value) override; 80 void SetConfigChanges(const std::string value) override; 81 bool MemoryRefresh(const std::string memoryRefreshArgs) const override; 82 void LoadDocument(const std::string, const std::string, const Json2::Value&) override; 83 void FoldStatusChanged(const std::string commandFoldStatus, 84 int32_t currentWidth, int32_t currentHeight) override; 85 void SetAvoidArea(const AvoidAreas& areas) override; 86 void UpdateAvoidArea2Ide(const std::string& key, const OHOS::Rosen::Rect& value); 87 OHOS::Rosen::Window* GetWindow() const; 88 89 void DispatchBackPressedEvent() const; 90 void DispatchKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent) const; 91 void DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) const; 92 void DispatchAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) const; 93 void DispatchInputMethodEvent(const unsigned int codePoint) const; 94 #ifdef COMPONENT_TEST_ENABLED 95 void SetComponentTestModeConfig(const std::string value); 96 #endif // COMPONENT_TEST_ENABLED 97 void InitGlfwEnv(); 98 void CalculateAvoidAreaByType(OHOS::Rosen::WindowType type, 99 const OHOS::Rosen::SystemBarProperty& property); 100 void InitAvoidAreas(OHOS::Rosen::Window* window); 101 void InitJsApp() override; 102 void StopAbility(); 103 void InitCommandInfo(); 104 void InitScreenInfo(); 105 106 protected: 107 void SetJsAppArgs(OHOS::Ace::Platform::AceRunArgs& args); 108 void RunJsApp(); 109 void RunNormalAbility(); 110 void RunDebugAbility(); 111 double watchScreenDensity = 320; // Watch Screen Density 112 double phoneScreenDensity = 480; // Phone Screen Density 113 double tvScreenDensity = 320; // TV Screen Density 114 double tabletScreenDensity = 400; // Tablet Screen Density 115 double carScreenDensity = 320; // Car Screen Density 116 double twoInOneScreenDensity = 240; // Car Screen Density 117 118 private: 119 void SetPkgContextInfo(); 120 void SetMockJsonInfo(); 121 void SetAssetPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 122 void SetProjectModel(OHOS::Ace::Platform::AceRunArgs& args) const; 123 void SetPageProfile(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 124 void SetDeviceWidth(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const; 125 void SetDeviceHeight(OHOS::Ace::Platform::AceRunArgs& args, const int32_t) const; 126 void SetWindowTitle(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 127 void SetUrl(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 128 void SetConfigChanges(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 129 #ifdef COMPONENT_TEST_ENABLED 130 void SetComponentTestConfig(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 131 #endif // COMPONENT_TEST_ENABLED 132 void SetColorMode(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 133 void SetOrientation(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 134 void SetAceVersionArgs(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 135 void SetLanguage(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 136 void SetRegion(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 137 void SetScript(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 138 void SetSystemResourcesPath(OHOS::Ace::Platform::AceRunArgs& args) const; 139 void SetAppResourcesPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 140 void SetFormsEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool formsEnabled) const; 141 void SetContainerSdkPath(OHOS::Ace::Platform::AceRunArgs& args, const std::string) const; 142 void SetOnRouterChange(OHOS::Ace::Platform::AceRunArgs& args) const; 143 void SetOnError(OHOS::Ace::Platform::AceRunArgs& args) const; 144 void SetComponentModeEnabled(OHOS::Ace::Platform::AceRunArgs& args, bool isComponentMode) const; 145 void AssignValueForWidthAndHeight(const int32_t origWidth, const int32_t origHeight, 146 const int32_t compWidth, const int32_t compHeight); 147 void AdaptDeviceType(OHOS::Ace::Platform::AceRunArgs& args, const std::string, 148 const int32_t, double screenDendity = 0) const; 149 void ParseSystemParams(OHOS::Ace::Platform::AceRunArgs& args, const Json2::Value& paramObj); 150 void SetSystemParams(OHOS::Ace::Platform::SystemParams& args, const Json2::Value& paramObj); 151 void SetDeviceScreenDensity(const int32_t screenDensity, const std::string type); 152 std::string GetDeviceTypeName(const OHOS::Ace::DeviceType) const; 153 OHOS::Rosen::FoldStatus ConvertFoldStatus(std::string value) const; 154 const double BASE_SCREEN_DENSITY = 160; // Device Baseline Screen Density 155 std::unique_ptr<OHOS::Ace::Platform::AceAbility> ability; 156 std::atomic<bool> isStop; 157 int32_t width = 0; 158 int32_t height = 0; 159 int32_t orignalWidth = 0; 160 int32_t orignalHeight = 0; 161 AvoidAreas avoidInitialAreas; 162 OHOS::Ace::Platform::AceRunArgs aceRunArgs; 163 std::shared_ptr<OHOS::Rosen::GlfwRenderContext> glfwRenderContext; 164 #ifdef COMPONENT_TEST_ENABLED 165 std::string componentTestModeConfig; 166 #endif // COMPONENT_TEST_ENABLED 167 #if defined(__APPLE__) || defined(_WIN32) 168 std::shared_ptr<OHOS::AbilityRuntime::Simulator> simulator; 169 int64_t debugAbilityId = -1; 170 void SetSimulatorParams(OHOS::AbilityRuntime::Options& options); 171 void SetSimulatorCommonParams(OHOS::AbilityRuntime::Options& options); 172 std::shared_ptr<OHOS::AppExecFwk::Configuration> UpdateConfiguration(OHOS::Ace::Platform::AceRunArgs& args); 173 174 std::shared_ptr<OHOS::Previewer::PreviewerWindowModel> windowModel; 175 void SetWindowParams() const; 176 template<typename T> SetDevice(const OHOS::Ace::DeviceType & deviceType)177 T SetDevice(const OHOS::Ace::DeviceType& deviceType) const 178 { 179 static_assert(std::is_enum_v<T>, "T must be an enum type"); 180 switch (deviceType) { 181 case OHOS::Ace::DeviceType::WATCH: 182 return T::WATCH; 183 case OHOS::Ace::DeviceType::TV: 184 return T::TV; 185 case OHOS::Ace::DeviceType::CAR: 186 return T::CAR; 187 case OHOS::Ace::DeviceType::TABLET: 188 return T::TABLET; 189 case OHOS::Ace::DeviceType::PHONE: 190 return T::PHONE; 191 case OHOS::Ace::DeviceType::TWO_IN_ONE: 192 return T::TWO_IN_ONE; 193 default: 194 return T::UNKNOWN; 195 } 196 } 197 template<typename T> SetColorMode(const OHOS::Ace::ColorMode & colorMode)198 T SetColorMode(const OHOS::Ace::ColorMode& colorMode) const 199 { 200 static_assert(std::is_enum_v<T>, "T must be an enum type"); 201 if (colorMode == OHOS::Ace::ColorMode::LIGHT) { 202 return T::LIGHT; 203 } else if (colorMode == OHOS::Ace::ColorMode::DARK) { 204 return T::DARK; 205 } else { 206 return T::COLOR_MODE_UNDEFINED; 207 } 208 } 209 template<typename T> SetOrientation(const OHOS::Ace::DeviceOrientation & orientation)210 T SetOrientation(const OHOS::Ace::DeviceOrientation& orientation) const 211 { 212 static_assert(std::is_enum_v<T>, "T must be an enum type"); 213 if (orientation == OHOS::Ace::DeviceOrientation::PORTRAIT) { 214 return T::PORTRAIT; 215 } else if (orientation == OHOS::Ace::DeviceOrientation::LANDSCAPE) { 216 return T::LANDSCAPE; 217 } else { 218 return T::ORIENTATION_UNDEFINED; 219 } 220 } 221 #endif 222 }; 223 224 #endif // JSAPPIMPL_H 225