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