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_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H
18
19 #include <array>
20 #include <map>
21 #include <mutex>
22 #include <string>
23
24
25 #include "base/memory/referenced.h"
26 #include "base/geometry/dimension.h"
27 #include "base/log/log.h"
28 #include "base/utils/noncopyable.h"
29
30 namespace OHOS::Ace {
31
32 class PipelineBase;
33
34 enum class ScreenSizeType {
35 UNDEFINED = 0,
36 XS,
37 SM,
38 MD,
39 LG,
40 XL,
41 };
42
43 constexpr size_t SCREEN_SIZE_COUNT = static_cast<size_t>(ScreenSizeType::XL) + 1;
44 const inline std::map<ScreenSizeType, std::string> SCREEN_SIZE_VALUES = {
45 {ScreenSizeType::XS, "xs"},
46 {ScreenSizeType::SM, "sm"},
47 {ScreenSizeType::MD, "md"},
48 {ScreenSizeType::LG, "lg"},
49 {ScreenSizeType::XL, "xl"},
50 };
51
IsValid(ScreenSizeType val)52 inline bool IsValid(ScreenSizeType val)
53 {
54 return static_cast<size_t>(val) < SCREEN_SIZE_COUNT;
55 }
56
57 class ScreenSystemManager final {
58 public:
59 static ScreenSystemManager& GetInstance();
60
SetWindowInfo(double screenWidth,double density,double dipScale)61 void SetWindowInfo(double screenWidth, double density, double dipScale)
62 {
63 std::lock_guard<std::mutex> guard(lock);
64 screenWidth_ = screenWidth;
65 density_ = density;
66 dipScale_ = dipScale;
67 viewScale_ = density / dipScale;
68 }
69
SetWindowInfo(double density,double dipScale)70 void SetWindowInfo(double density, double dipScale)
71 {
72 std::lock_guard<std::mutex> guard(lock);
73 density_ = density;
74 dipScale_ = dipScale;
75 viewScale_ = density / dipScale;
76 }
77
78 void OnSurfaceChanged(double width);
79
80 double GetScreenWidth(const RefPtr<PipelineBase>& pipeline = nullptr) const;
81
GetDipScale()82 double GetDipScale() const
83 {
84 std::lock_guard<std::mutex> guard(lock);
85 return dipScale_;
86 }
87
88 ScreenSizeType GetSize(double width) const;
89
GetCurrentSize()90 ScreenSizeType GetCurrentSize() const
91 {
92 std::lock_guard<std::mutex> guard(lock);
93 return currentSize_;
94 }
95
GetDensity()96 double GetDensity() const
97 {
98 std::lock_guard<std::mutex> guard(lock);
99 return density_;
100 }
101
102 private:
103 double screenWidth_ = 0.0;
104 double density_ = 1.0;
105 double dipScale_ = 1.0;
106 double viewScale_ = 1.0;
107 ScreenSizeType currentSize_ = ScreenSizeType::UNDEFINED;
108
109 private:
110 ScreenSystemManager() = default;
111 ~ScreenSystemManager() = default;
112 static std::mutex lock;
113 ACE_DISALLOW_COPY_AND_MOVE(ScreenSystemManager);
114 };
115
GetInstance()116 inline ScreenSystemManager& ScreenSystemManager::GetInstance()
117 {
118 static ScreenSystemManager instance;
119 return instance;
120 }
121
122 template<typename T>
123 class ArrayByScreenType final {
124 public:
125 ArrayByScreenType() = default;
ArrayByScreenType(const T & defValue)126 ArrayByScreenType(const T& defValue)
127 {
128 std::fill(values_.begin(), values_.end(), defValue);
129 }
130
131 ~ArrayByScreenType() = default;
132
133 const T& operator [](ScreenSizeType idx) const
134 {
135 return values_[static_cast<size_t>(idx)];
136 }
137
138 T& operator [](ScreenSizeType idx)
139 {
140 return values_[static_cast<size_t>(idx)];
141 }
142
GetCurrentValue()143 const T& GetCurrentValue() const
144 {
145 auto idx = ScreenSystemManager::GetInstance().GetCurrentSize();
146 return values_[static_cast<size_t>(idx)];
147 }
148
GetCurrentValue()149 T& GetCurrentValue()
150 {
151 auto idx = ScreenSystemManager::GetInstance().GetCurrentSize();
152 return values_[static_cast<size_t>(idx)];
153 }
154 private:
155 std::array<T, SCREEN_SIZE_COUNT> values_;
156 };
157 } // namespace OHOS::Ace
158
159 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H
160