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