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