• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
64     void OnSurfaceChanged(double width);
65 
66     double GetScreenWidth(const RefPtr<PipelineBase>& pipeline = nullptr) const
67     {
68         if (pipeline) {
69             return pipeline->GetRootWidth();
70         }
71         return screenWidth_;
72     }
73 
GetDipScale()74     double GetDipScale() const
75     {
76         return dipScale_;
77     }
78 
79     ScreenSizeType GetSize(double width) const;
80 
GetCurrentSize()81     ScreenSizeType GetCurrentSize() const
82     {
83         return currentSize_;
84     }
85 
GetDensity()86     double GetDensity() const
87     {
88         return density_;
89     }
90 
91 private:
92     double screenWidth_ = 0.0;
93     double density_ = 1.0;
94     double dipScale_ = 1.0;
95     double viewScale_ = 1.0;
96     ScreenSizeType currentSize_ = ScreenSizeType::UNDEFINED;
97 
98 private:
99     ScreenSystemManager() = default;
100     ~ScreenSystemManager() = default;
101 
102     ACE_DISALLOW_COPY_AND_MOVE(ScreenSystemManager);
103 };
104 
GetInstance()105 inline ScreenSystemManager& ScreenSystemManager::GetInstance()
106 {
107     static ScreenSystemManager instance;
108     return instance;
109 }
110 
111 template<typename T>
112 class ArrayByScreenType final {
113 public:
114     ArrayByScreenType() = default;
ArrayByScreenType(const T & defValue)115     ArrayByScreenType(const T& defValue)
116     {
117         std::fill(values_.begin(), values_.end(), defValue);
118     }
119 
120     ~ArrayByScreenType() = default;
121 
122     const T& operator [](ScreenSizeType idx) const
123     {
124         return values_[static_cast<size_t>(idx)];
125     }
126 
127     T& operator [](ScreenSizeType idx)
128     {
129         return values_[static_cast<size_t>(idx)];
130     }
131 
GetCurrentValue()132     const T& GetCurrentValue() const
133     {
134         auto idx = ScreenSystemManager::GetInstance().GetCurrentSize();
135         return values_[static_cast<size_t>(idx)];
136     }
137 
GetCurrentValue()138     T& GetCurrentValue()
139     {
140         auto idx = ScreenSystemManager::GetInstance().GetCurrentSize();
141         return values_[static_cast<size_t>(idx)];
142     }
143 private:
144     std::array<T, SCREEN_SIZE_COUNT> values_;
145 };
146 } // namespace OHOS::Ace
147 
148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H
149