• 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 #include "core/components/common/layout/screen_system_manager.h"
17 #include "core/pipeline/pipeline_base.h"
18 
19 namespace OHOS::Ace {
20 namespace {
21     constexpr Dimension MAX_SCREEN_WIDTH_SM = 320.0_vp;
22     constexpr Dimension MAX_SCREEN_WIDTH_MD = 600.0_vp;
23     constexpr Dimension MAX_SCREEN_WIDTH_LG = 840.0_vp;
24     constexpr Dimension MAX_SCREEN_WIDTH_XL = 1024.0_vp;
25 } // namespace
26 
27 std::mutex ScreenSystemManager::lock;
OnSurfaceChanged(double width)28 void ScreenSystemManager::OnSurfaceChanged(double width)
29 {
30     std::lock_guard<std::mutex> guard(lock);
31     screenWidth_ = width;
32     if (width < MAX_SCREEN_WIDTH_SM.Value() * density_) {
33         currentSize_ = ScreenSizeType::XS;
34     } else if (width < MAX_SCREEN_WIDTH_MD.Value() * density_) {
35         currentSize_ = ScreenSizeType::SM;
36     } else if (width < MAX_SCREEN_WIDTH_LG.Value() * density_) {
37         currentSize_ = ScreenSizeType::MD;
38     } else if (width < MAX_SCREEN_WIDTH_XL.Value() * density_) {
39         currentSize_ = ScreenSizeType::LG;
40     } else {
41         currentSize_ = ScreenSizeType::XL;
42     }
43 }
44 
GetSize(double width) const45 ScreenSizeType ScreenSystemManager::GetSize(double width) const
46 {
47     std::lock_guard<std::mutex> guard(lock);
48     ScreenSizeType size = ScreenSizeType::UNDEFINED;
49     auto context = PipelineBase::GetCurrentContext();
50     CHECK_NULL_RETURN(context, size);
51     double density = context->GetCurrentDensity();
52     auto finalBreakpoints = SystemProperties::GetWidthLayoutBreakpoints();
53     if (finalBreakpoints.widthVPXS_ < 0 || GreatNotEqual(finalBreakpoints.widthVPXS_ * density, width)) {
54         size = ScreenSizeType::XS;
55     } else if (finalBreakpoints.widthVPSM_ < 0 || GreatNotEqual(finalBreakpoints.widthVPSM_ * density, width)) {
56         size = ScreenSizeType::SM;
57     } else if (finalBreakpoints.widthVPMD_ < 0 || GreatNotEqual(finalBreakpoints.widthVPMD_ * density, width)) {
58         size = ScreenSizeType::MD;
59     } else if (finalBreakpoints.widthVPLG_ < 0 || GreatNotEqual(finalBreakpoints.widthVPLG_ * density, width)) {
60         size = ScreenSizeType::LG;
61     } else if (finalBreakpoints.widthVPXL_ < 0 || GreatNotEqual(finalBreakpoints.widthVPXL_ * density, width)) {
62         size = ScreenSizeType::XL;
63     } else {
64         size = ScreenSizeType::XL;
65     }
66     return size;
67 }
68 
GetScreenWidth(const RefPtr<PipelineBase> & pipeline) const69 double ScreenSystemManager::GetScreenWidth(const RefPtr<PipelineBase>& pipeline) const
70 {
71     std::lock_guard<std::mutex> guard(lock);
72     if (pipeline) {
73         return pipeline->GetRootWidth();
74     }
75     return screenWidth_;
76 }
77 } // namespace OHOS::Ace
78