1 /*
2  * Copyright (c) 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 "core/common/text_field_manager.h"
17 
18 namespace OHOS::Ace {
19 
SetClickPosition(const Offset & position)20 void TextFieldManager::SetClickPosition(const Offset& position)
21 {
22     position_ = position;
23 }
24 
SetScrollElement(int32_t pageId,const WeakPtr<ScrollElement> & scrollElement)25 void TextFieldManager::SetScrollElement(int32_t pageId, const WeakPtr<ScrollElement>& scrollElement)
26 {
27     auto iter = scrollMap_.find(pageId);
28     if (iter == scrollMap_.end()) {
29         scrollMap_.try_emplace(pageId, scrollElement);
30     }
31 }
32 
MovePage(int32_t pageId,const Offset & rootRect,double offsetHeight)33 void TextFieldManager::MovePage(int32_t pageId, const Offset& rootRect, double offsetHeight)
34 {
35     auto iter = scrollMap_.find(pageId);
36     if (iter == scrollMap_.end()) {
37         return;
38     }
39     auto scrollElement = iter->second.Upgrade();
40     CHECK_NULL_VOID(scrollElement);
41     const auto& scroll = AceType::DynamicCast<RenderScroll>(scrollElement->GetRenderNode());
42     CHECK_NULL_VOID(scroll);
43     if (GreatNotEqual(position_.GetY(), rootRect.GetY())) {
44         hasMove_ = true;
45         scroll->SetNeedMove(true);
46     }
47 
48     if (LessNotEqual(offsetHeight, 0) && hasMove_) {
49         scroll->SetNeedMove(false);
50         hasMove_ = false;
51     }
52 }
53 
SetHeight(float height)54 void TextFieldManager::SetHeight(float height)
55 {
56     height_ = height;
57 }
58 
GetHeight() const59 float TextFieldManager::GetHeight() const
60 {
61     return height_;
62 }
63 
RemovePageId(int32_t pageId)64 void TextFieldManager::RemovePageId(int32_t pageId)
65 {
66     scrollMap_.erase(pageId);
67 }
68 
GetClickPosition()69 const Offset& TextFieldManager::GetClickPosition()
70 {
71     return position_;
72 }
73 
UpdatePanelForVirtualKeyboard(double offsetY,double fullHeight)74 bool TextFieldManager::UpdatePanelForVirtualKeyboard(double offsetY, double fullHeight)
75 {
76     auto onFocusTextField = onFocusTextField_.Upgrade();
77     CHECK_NULL_RETURN(onFocusTextField, false);
78 #ifndef NG_BUILD
79     auto slidingPanelParent = onFocusTextField->GetSlidingPanelAncest();
80     CHECK_NULL_RETURN(slidingPanelParent, false);
81     if (GreatNotEqual(onFocusTextField->GetPaintRect().Height() +
82         onFocusTextField->GetGlobalOffset().GetY(), fullHeight)) {
83         LOGI("Raising panel with offset %{public}f",
84             onFocusTextField->GetPaintRect().Height() +
85             onFocusTextField->GetGlobalOffset().GetY() - fullHeight);
86         offsetY -= onFocusTextField->GetPaintRect().Height() +
87             onFocusTextField->GetGlobalOffset().GetY() - fullHeight;
88     }
89     slidingPanelParent->LiftPanelForVirtualKeyboard(offsetY);
90 #endif
91     return true;
92 }
93 
ResetSlidingPanelParentHeight()94 bool TextFieldManager::ResetSlidingPanelParentHeight()
95 {
96     auto onFocusTextField = onFocusTextField_.Upgrade();
97     CHECK_NULL_RETURN(onFocusTextField, false);
98 #ifndef NG_BUILD
99     auto slidingPanelParent = onFocusTextField->GetSlidingPanelAncest();
100     CHECK_NULL_RETURN(slidingPanelParent, false);
101     slidingPanelParent->UpdatePanelHeightByCurrentMode();
102 #endif
103     return true;
104 }
105 
ClearOnFocusTextField()106 void TextFieldManager::ClearOnFocusTextField()
107 {
108     onFocusTextField_ = nullptr;
109 }
110 
111 }; // namespace OHOS::Ace
112