ui_view_group.cpp 7.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "components/ui_view_group.h"

#include <cstring>
P
pssea 已提交
19

M
mamingshuai 已提交
20
#include "components/root_view.h"
P
pssea 已提交
21
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
22 23 24

namespace OHOS {
UIViewGroup::UIViewGroup()
P
pssea 已提交
25 26 27 28 29 30
    : childrenHead_(nullptr),
      childrenTail_(nullptr),
      childrenNum_(0),
      isDragging_(false),
      disallowIntercept_(false),
      isAutoSize_(false)
M
mamingshuai 已提交
31 32
{
    isViewGroup_ = true;
S
suyue 已提交
33 34 35
#if ENABLE_FOCUS_MANAGER
    isInterceptFocus_ = false;
#endif
M
mamingshuai 已提交
36 37
}

38
UIViewGroup::~UIViewGroup() {}
M
mamingshuai 已提交
39 40 41 42

void UIViewGroup::Add(UIView* view)
{
    if ((view == this) || (view == nullptr)) {
43
        GRAPHIC_LOGE("view can not be nullptr and added to self");
P
pssea 已提交
44 45 46
        return;
    }
    if (view->GetParent() != nullptr) {
47
        GRAPHIC_LOGE("can not add view multi times");
M
mamingshuai 已提交
48 49
        return;
    }
P
pssea 已提交
50

M
mamingshuai 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    if (childrenHead_ == nullptr) {
        childrenHead_ = view;
    } else {
        if (childrenTail_ == nullptr) {
            return;
        }
        childrenTail_->SetNextSibling(view);
    }
    view->SetParent(this);
    view->SetNextSibling(nullptr);
    childrenTail_ = view;
    childrenNum_++;
    if (isAutoSize_) {
        AutoResize();
    }
    OnChildChanged();
}

void UIViewGroup::Insert(UIView* prevView, UIView* insertView)
{
P
pssea 已提交
71
    if ((insertView == nullptr) || (insertView == this)) {
72
        GRAPHIC_LOGE("insertView can not be nullptr and insert to self");
M
mamingshuai 已提交
73 74
        return;
    }
P
pssea 已提交
75 76

    if (insertView->GetParent() != nullptr) {
77
        GRAPHIC_LOGE("can not insert view multi times");
P
pssea 已提交
78 79 80
        return;
    }

M
mamingshuai 已提交
81 82 83 84
    if (childrenHead_ == nullptr) {
        Add(insertView);
        return;
    }
P
pssea 已提交
85

M
mamingshuai 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    if (prevView == nullptr) {
        insertView->SetNextSibling(childrenHead_);
        insertView->SetParent(this);
        childrenHead_ = insertView;
    } else {
        UIView* nextView = prevView->GetNextSibling();
        prevView->SetNextSibling(insertView);
        insertView->SetNextSibling(nextView);
        insertView->SetParent(this);
    }
    if (childrenTail_ == prevView) {
        childrenTail_ = insertView;
    }
    childrenNum_++;
    if (isAutoSize_) {
        AutoResize();
    }
    OnChildChanged();
}

void UIViewGroup::Remove(UIView* view)
{
    if ((childrenHead_ == nullptr) || (view == nullptr)) {
        return;
    }

#if LOCAL_RENDER
    RootView::GetInstance()->RemoveViewFromInvalidMap(view);
    InvalidateRect(view->GetRect());
#endif
    if (childrenHead_ == view) {
        childrenHead_ = childrenHead_->GetNextSibling();
        view->SetParent(nullptr);
        view->SetNextSibling(nullptr);
        if (childrenTail_ == view) {
            childrenTail_ = nullptr;
        }
        childrenNum_--;
        OnChildChanged();
        return;
    }
    UIView* node = childrenHead_;
    while (node->GetNextSibling() != nullptr) {
        if (node->GetNextSibling() == view) {
            node->SetNextSibling(view->GetNextSibling());
            view->SetParent(nullptr);
            view->SetNextSibling(nullptr);
            if (childrenTail_ == view) {
                childrenTail_ = node;
            }
            childrenNum_--;
            OnChildChanged();
            return;
        }
        node = node->GetNextSibling();
    }
}

void UIViewGroup::RemoveAll()
{
146 147 148 149 150 151 152 153 154 155 156
    UIView* node = childrenHead_;
    childrenHead_ = nullptr;
    childrenTail_ = nullptr;
    childrenNum_ = 0;
    UIView* tmp = nullptr;
    while (node != nullptr) {
        tmp = node;
        node = node->GetNextSibling();
        tmp->SetParent(nullptr);
        tmp->SetNextSibling(nullptr);
    }
M
mamingshuai 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    OnChildChanged();
}

void UIViewGroup::GetTargetView(const Point& point, UIView** last)
{
    if (last == nullptr) {
        return;
    }

    Rect rect = GetRect();
    if (disallowIntercept_) {
        *last = nullptr;
        return;
    }
    if (!rect.IsContains(point)) {
        return;
    }
    if (!visible_) {
        return;
    }
    if (touchable_) {
        *last = this;
    }
    if (isDragging_) {
        return;
    }
    UIView* view = childrenHead_;
    while (view != nullptr) {
        if (!view->IsViewGroup()) {
            rect = view->GetRect();
            if (rect.IsContains(point)) {
                view->GetTargetView(point, last);
            }
        } else {
            UIViewGroup* viewGroup = static_cast<UIViewGroup*>(view);
            viewGroup->GetTargetView(point, last);
        }
        view = view->GetNextSibling();
    }
}

void UIViewGroup::GetTargetView(const Point& point, UIView** current, UIView** target)
{
    if ((current == nullptr) || (target == nullptr)) {
        return;
    }

    Rect rect = GetRect();
    if (disallowIntercept_) {
        *current = nullptr;
        *target = nullptr;
        return;
    }
    if (!rect.IsContains(point)) {
        return;
    }
    if (!visible_) {
        return;
    }
    *target = this;
    if (touchable_) {
        *current = this;
    }
    if (isDragging_) {
        return;
    }
    UIView* view = childrenHead_;
    while (view != nullptr) {
        if (!view->IsViewGroup()) {
            rect = view->GetRect();
            if (rect.IsContains(point)) {
                view->GetTargetView(point, current, target);
            }
        } else {
            UIViewGroup* viewGroup = static_cast<UIViewGroup*>(view);
            viewGroup->GetTargetView(point, current, target);
        }
        view = view->GetNextSibling();
    }
}

Rect UIViewGroup::GetAllChildRelativeRect() const
{
    Rect rect;
    UIView* view = childrenHead_;
    if (view != nullptr) {
        rect = view->GetRelativeRect();
        view = view->GetNextSibling();
    }
    while (view != nullptr) {
        Rect rectChild = view->GetRelativeRect();
        rect.Join(rect, rectChild);
        view = view->GetNextSibling();
    }
    return rect;
}

UIView* UIViewGroup::GetChildById(const char* id) const
{
    if (id == nullptr || childrenHead_ == nullptr) {
        return nullptr;
    }
    UIView* child = childrenHead_;
    while (child != nullptr) {
        if ((child->GetViewId() != nullptr) && !strcmp(child->GetViewId(), id)) {
            return child;
        } else if (child->IsViewGroup() && static_cast<UIViewGroup*>(child)->GetChildrenHead() != nullptr) {
            child = static_cast<UIViewGroup*>(child)->GetChildrenHead();
            continue;
        } else if (child->GetNextSibling() != nullptr) {
            child = child->GetNextSibling();
            continue;
        }
        while (child->GetParent() != this && child->GetParent()->GetNextSibling() == nullptr) {
            child = child->GetParent();
        }
        if (child->GetParent() != this) {
            child = child->GetParent()->GetNextSibling();
            continue;
        }
        break;
    }
    return nullptr;
}

void UIViewGroup::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
{
    UIView* view = childrenHead_;
    int16_t x;
    int16_t y;
    while (view != nullptr) {
        x = view->GetX() + xOffset;
        y = view->GetY() + yOffset;
        view->SetPosition(x, y);
        view = view->GetNextSibling();
    }
}

void UIViewGroup::AutoResize()
{
    Rect rect = GetAllChildRelativeRect();
    SetWidth(rect.GetWidth() + rect.GetLeft());
    SetHeight(rect.GetHeight() + rect.GetTop());
}
} // namespace OHOS