1 /*
2  * Copyright (c) 2020-2021 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 "scroll_layer.h"
17 #include "ace_log.h"
18 #include "component.h"
19 #include "component_utils.h"
20 #include "fatal_handler.h"
21 #include "root_view.h"
22 
23 namespace OHOS {
24 namespace ACELite {
ScrollLayer()25 ScrollLayer::ScrollLayer() : scroll_(nullptr), pageRootView_(nullptr) {}
26 
~ScrollLayer()27 ScrollLayer::~ScrollLayer()
28 {
29     if (scroll_ != nullptr) {
30         scroll_->RemoveAll();
31         delete (scroll_);
32         scroll_ = nullptr;
33     }
34     pageRootView_ = nullptr;
35     FatalHandler::GetInstance().SetCurrentPageRootView(nullptr);
36 }
37 
AddScrollLayer(UIView & view) const38 UIScrollView *ScrollLayer::AddScrollLayer(UIView &view) const
39 {
40     UIScrollView *scroll = new UIScrollView();
41     if (scroll == nullptr) {
42         HILOG_ERROR(HILOG_MODULE_ACE, "Scroll Layer: Create scroll view failed.");
43         return nullptr;
44     }
45     scroll->SetPosition(0, 0);
46     scroll->SetWidth(GetHorizontalResolution());
47     scroll->SetHeight(GetVerticalResolution());
48     scroll->SetXScrollBarVisible(false);
49     scroll->SetYScrollBarVisible(false);
50     scroll->SetThrowDrag(true);
51     scroll->Add(&view);
52     scroll->SetReboundSize(0);
53     return scroll;
54 }
55 
AppendScrollLayer(Component * rootComponent)56 void ScrollLayer::AppendScrollLayer(Component *rootComponent)
57 {
58     if (rootComponent == nullptr) {
59         HILOG_ERROR(HILOG_MODULE_ACE, "Scroll Layer: AppendScrollLayer function parameter rootComponent error.");
60         return;
61     }
62     rootComponent->EnableTransmitSwipe();
63     uint16_t horizontalResolution = GetHorizontalResolution();
64     uint16_t verticalResolution = GetVerticalResolution();
65     ConstrainedParameter rootViewParam(horizontalResolution, verticalResolution);
66     Component::BuildViewTree(rootComponent, nullptr, rootViewParam);
67     // root view will be attached soon, invoke the callback
68     rootComponent->OnViewAttached();
69 
70     UIView *view = rootComponent->GetComponentRootView();
71     if (view == nullptr) {
72         HILOG_ERROR(HILOG_MODULE_ACE, "Scroll Layer: Failed to get view from js object.");
73         return;
74     }
75 
76     scroll_ = AddScrollLayer(*view);
77     pageRootView_ = (scroll_ == nullptr) ? view : scroll_;
78     FatalHandler::GetInstance().SetCurrentPageRootView(pageRootView_);
79 }
80 
Hide() const81 void ScrollLayer::Hide() const
82 {
83     RootView *rootView = RootView::GetInstance();
84     if (rootView == nullptr) {
85         HILOG_ERROR(HILOG_MODULE_ACE, "get rootView is nullptr");
86         return;
87     }
88     DetachFromRootView();
89     rootView->Invalidate();
90 }
91 
DetachFromRootView() const92 void ScrollLayer::DetachFromRootView() const
93 {
94     if (pageRootView_ == nullptr) {
95         return;
96     }
97     RootView *rootView = RootView::GetInstance();
98     if (rootView == nullptr) {
99         return;
100     }
101     rootView->Remove(pageRootView_);
102 }
103 
Show() const104 void ScrollLayer::Show() const
105 {
106     RootView *rootView = RootView::GetInstance();
107     if (rootView == nullptr) {
108         HILOG_ERROR(HILOG_MODULE_ACE, "get rootView is nullptr");
109         return;
110     }
111     rootView->SetPosition(0, 0);
112     rootView->SetWidth(GetHorizontalResolution());
113     rootView->SetHeight(GetVerticalResolution());
114     rootView->Add(pageRootView_);
115     rootView->Invalidate();
116 }
117 } // namespace ACELite
118 } // namespace OHOS
119