• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_ABILITY_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_ABILITY_H
18 
19 #include <atomic>
20 
21 #include "flutter/shell/platform/glfw/public/flutter_glfw.h"
22 #ifdef ENABLE_ROSEN_BACKEND
23 #include "glfw_render_context.h"
24 #endif
25 
26 #include "adapter/preview/entrance/ace_run_args.h"
27 #include "base/utils/macros.h"
28 #include "core/event/key_event.h"
29 #include "core/event/touch_event.h"
30 
31 namespace OHOS::Ace::Platform {
32 
33 struct ConfigChanges {
34     bool watchLocale = false;
35     bool watchLayout = false;
36     bool watchFontSize = false;
37     bool watchOrientation = false;
38     bool watchDensity = false;
39 };
40 
41 struct SystemParams {
42     int32_t deviceWidth { 0 };
43     int32_t deviceHeight { 0 };
44     bool isRound = false;
45     double density { 1.0 };
46     std::string language = "zh";
47     std::string region = "CN";
48     std::string script = "";
49     OHOS::Ace::DeviceType deviceType { DeviceType::PHONE };
50     OHOS::Ace::ColorMode colorMode { ColorMode::LIGHT };
51     OHOS::Ace::DeviceOrientation orientation { DeviceOrientation::PORTRAIT };
52 };
53 
54 #ifndef ENABLE_ROSEN_BACKEND
55 using GlfwController = FlutterDesktopWindowControllerRef;
56 #else
57 using GlfwController = std::shared_ptr<OHOS::Rosen::GlfwRenderContext>;
58 #endif
59 
60 class ACE_FORCE_EXPORT_WITH_PREVIEW AceAbility {
61 public:
62     explicit AceAbility(const AceRunArgs& runArgs);
63     ~AceAbility();
64 
65     // Be called in Previewer frontend thread, which is not ACE platform thread.
66     static std::unique_ptr<AceAbility> CreateInstance(AceRunArgs& runArgs);
67     void InitEnv();
68     void Start();
69     static void Stop();
70     void OnConfigurationChanged(const DeviceConfig& newConfig);
71     void SurfaceChanged(
72         const DeviceOrientation& orientation, const double& resolution, int32_t& width, int32_t& height);
73     void ReplacePage(const std::string& url, const std::string& params);
74     void LoadDocument(const std::string& url, const std::string& componentName, SystemParams& systemParams);
75 
76     std::string GetJSONTree();
77     std::string GetDefaultJSONTree();
78     bool OperateComponent(const std::string& attrsJson);
GetGlfwWindowController()79     GlfwController GetGlfwWindowController()
80     {
81         return controller_;
82     }
83 
84 private:
85     void RunEventLoop();
86 
87     void SetConfigChanges(const std::string& configChanges);
88 
SetGlfwWindowController(const GlfwController & controller)89     void SetGlfwWindowController(const GlfwController &controller)
90     {
91         controller_ = controller;
92     }
93 
94 #ifdef ENABLE_ROSEN_BACKEND
SetFlutterWindowControllerRef(const FlutterDesktopWindowControllerRef & controller)95     void SetFlutterWindowControllerRef(const FlutterDesktopWindowControllerRef &controller)
96     {
97         windowControllerRef_ = controller;
98     }
99 
100     FlutterDesktopWindowControllerRef windowControllerRef_ = nullptr;
101 #endif
102 
103     // flag indicating if the glfw message loop should be running.
104     static std::atomic<bool> loopRunning_;
105 
106     AceRunArgs runArgs_;
107     ConfigChanges configChanges_;
108     GlfwController controller_ = nullptr;
109 };
110 
111 } // namespace OHOS::Ace::Platform
112 
113 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ACE_ABILITY_H
114