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