ui_label.cpp 10.8 KB
Newer Older
M
mamingshuai 已提交
1
/*
X
xucheng57@huawei.com 已提交
2
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
M
mamingshuai 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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_label.h"
#include "font/ui_font.h"
Z
zhangguyuan 已提交
18
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "themes/theme_manager.h"

namespace OHOS {
class LabelAnimator : public Animator, public AnimatorCallback {
public:
    LabelAnimator(uint16_t textX, uint16_t labelX, int16_t startPos, UIView* view)
        : Animator(this, view, 0, true),
          startPos_(startPos),
          textX_(textX),
          labelX_(labelX),
          offsetX_(startPos),
          waitCount_(ANIM_WAIT_COUNT),
          speed_(0),
          preRunTime_(0),
          decimal_(0)
    {
    }

P
pssea 已提交
37
    virtual ~LabelAnimator() {}
M
mamingshuai 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    int16_t GetStartPos() const
    {
        return startPos_;
    }

    void SetStartPos(int16_t pos)
    {
        startPos_ = pos;
    }

    void UpdateWidth(uint16_t textWidth, uint16_t labelWidth)
    {
        textX_ = textWidth;
        labelX_ = labelWidth;
        waitCount_ = ANIM_WAIT_COUNT;
        preRunTime_ = 0;
        decimal_ = 0;
        offsetX_ = startPos_;
        static_cast<UILabel*>(view_)->offsetX_ = offsetX_;
        view_->Invalidate();
    }

    void Callback(UIView* view) override
    {
        if (view == nullptr) {
            return;
        }

        uint32_t curTime = GetRunTime();
        if (waitCount_ > 0) {
            waitCount_--;
            preRunTime_ = curTime;
            return;
        }
73 74 75
        if (curTime == preRunTime_) {
            return;
        }
M
mamingshuai 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        uint32_t time = (curTime > preRunTime_) ? (curTime - preRunTime_) : (UINT32_MAX - preRunTime_ + curTime);
        // 1000: 1000 milliseconds is 1 second
        float floatStep = (static_cast<float>(time * speed_) / 1000) + decimal_;
        uint16_t integerStep = static_cast<uint16_t>(floatStep);
        decimal_ = floatStep - integerStep;
        preRunTime_ = curTime;

        if (integerStep != 0) {
            offsetX_ -= integerStep;
        } else {
            return;
        }
        offsetX_ = ((offsetX_ - labelX_) % (textX_ + labelX_)) + labelX_;
        static_cast<UILabel*>(view)->offsetX_ = offsetX_;
        view->Invalidate();
    }

    void SetAnimatorSpeed(uint16_t animSpeed)
    {
        speed_ = animSpeed;
        decimal_ = 0;
    }

S
shiqichang 已提交
99 100 101 102 103
    int16_t GetAnimatorSpeed() const
    {
        return speed_;
    }

M
mamingshuai 已提交
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
private:
    static constexpr uint8_t ANIM_WAIT_COUNT = 50;
    int16_t startPos_;
    uint16_t textX_;
    uint16_t labelX_;
    int16_t offsetX_;
    uint16_t waitCount_;
    uint16_t speed_;
    uint32_t preRunTime_;
    float decimal_;
};

UILabel::UILabel()
    : labelText_(nullptr),
      needRefresh_(false),
      useTextColor_(false),
      hasAnimator_(false),
      lineBreakMode_(LINE_BREAK_ELLIPSIS),
      ellipsisIndex_(Text::TEXT_ELLIPSIS_END_INV),
      offsetX_(0),
      textColor_(Color::White()),
      animator_{nullptr}
{
    Theme* theme = ThemeManager::GetInstance().GetCurrent();
    Style& style = (theme != nullptr) ? (theme->GetLabelStyle()) : (StyleDefault::GetLabelStyle());
    UIView::SetStyle(style);
    animator_.speed = DEFAULT_ANIMATOR_SPEED;
}

UILabel::~UILabel()
{
    if (hasAnimator_) {
        delete animator_.animator;
W
wangtiantian 已提交
137
        animator_.animator = nullptr;
M
mamingshuai 已提交
138 139 140 141 142 143 144 145
        hasAnimator_ = false;
    }
    if (labelText_ != nullptr) {
        delete labelText_;
        labelText_ = nullptr;
    }
}

146 147 148 149 150 151 152
void UILabel::InitLabelText()
{
    if (labelText_ == nullptr) {
        labelText_ = new Text();
    }
}

M
mamingshuai 已提交
153 154 155 156 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
int16_t UILabel::GetWidth()
{
    InitLabelText();
    if (needRefresh_ && labelText_->IsExpandWidth()) {
        ReMeasure();
    }
    return UIView::GetWidth();
}

int16_t UILabel::GetHeight()
{
    InitLabelText();
    if (needRefresh_ && labelText_->IsExpandHeight()) {
        ReMeasure();
    }
    return UIView::GetHeight();
}

void UILabel::SetStyle(uint8_t key, int64_t value)
{
    UIView::SetStyle(key, value);
    RefreshLabel();
}

void UILabel::SetText(const char* text)
{
    InitLabelText();
    labelText_->SetText(text);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

L
Lizhiqi 已提交
186
#if defined(ENABLE_SPANNABLE_STRING) && ENABLE_SPANNABLE_STRING
X
xucheng57@huawei.com 已提交
187 188 189 190 191 192 193 194 195 196
void UILabel::SetText(const SpannableString* text)
{
    InitLabelText();
    labelText_->SetSpannableString(text);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}
#endif

L
liuyuxiang-bear 已提交
197
void UILabel::SetAbsoluteSizeSpan(uint16_t start, uint16_t end, uint8_t size)
X
xucheng57@huawei.com 已提交
198 199 200 201 202 203 204 205 206 207
{
    if (labelText_ == nullptr) {
        return;
    }
    labelText_->SetAbsoluteSizeSpan(start, end, size);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

L
liuyuxiang-bear 已提交
208
void UILabel::SetRelativeSizeSpan(uint16_t start, uint16_t end, float size)
X
xucheng57@huawei.com 已提交
209 210 211 212 213 214 215 216 217 218
{
    if (labelText_ == nullptr) {
        return;
    }
    labelText_->SetRelativeSizeSpan(start, end, size);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

M
mamingshuai 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
void UILabel::SetLineBreakMode(const uint8_t lineBreakMode)
{
    InitLabelText();
    if ((lineBreakMode >= LINE_BREAK_MAX) || (lineBreakMode_ == lineBreakMode)) {
        return;
    }
    lineBreakMode_ = lineBreakMode;
    if ((lineBreakMode_ == LINE_BREAK_ADAPT) || (lineBreakMode_ == LINE_BREAK_STRETCH) ||
        (lineBreakMode_ == LINE_BREAK_MARQUEE)) {
        labelText_->SetExpandWidth(true);
    } else {
        labelText_->SetExpandWidth(false);
    }
    if ((lineBreakMode_ == LINE_BREAK_ADAPT) || (lineBreakMode_ == LINE_BREAK_WRAP)) {
        labelText_->SetExpandHeight(true);
    } else {
        labelText_->SetExpandHeight(false);
    }
W
wangtiantian 已提交
237 238 239 240 241 242
    if (lineBreakMode_ != LINE_BREAK_MARQUEE) {
        offsetX_ = 0;
        if (hasAnimator_) {
            animator_.animator->Stop();
        }
    }
M
mamingshuai 已提交
243 244 245 246 247 248 249 250 251 252 253 254
    RefreshLabel();
}

void UILabel::SetAlign(UITextLanguageAlignment horizontalAlign, UITextLanguageAlignment verticalAlign)
{
    InitLabelText();
    labelText_->SetAlign(horizontalAlign, verticalAlign);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

255
void UILabel::SetFontId(uint16_t fontId)
M
mamingshuai 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
    InitLabelText();
    labelText_->SetFontId(fontId);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

void UILabel::SetFont(const char* name, uint8_t size)
{
    InitLabelText();
    labelText_->SetFont(name, size);
    if (labelText_->IsNeedRefresh()) {
        RefreshLabel();
    }
}

uint16_t UILabel::GetTextWidth()
{
    InitLabelText();
    if (labelText_->IsNeedRefresh()) {
W
wanngtiantian 已提交
277
        ReMeasure();
M
mamingshuai 已提交
278 279 280 281 282 283 284 285
    }
    return labelText_->GetTextSize().x;
}

uint16_t UILabel::GetTextHeight()
{
    InitLabelText();
    if (labelText_->IsNeedRefresh()) {
W
wanngtiantian 已提交
286
        ReMeasure();
M
mamingshuai 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }
    return labelText_->GetTextSize().y;
}

void UILabel::SetWidth(int16_t width)
{
    if (GetWidth() != width) {
        UIView::SetWidth(width);
        RefreshLabel();
    }
}

void UILabel::SetHeight(int16_t height)
{
    if (GetHeight() != height) {
        UIView::SetHeight(height);
        RefreshLabel();
    }
}

void UILabel::RefreshLabel()
{
    Invalidate();
    ellipsisIndex_ = Text::TEXT_ELLIPSIS_END_INV;
    if (!needRefresh_) {
        needRefresh_ = true;
    }
}

void UILabel::ReMeasure()
{
    if (!needRefresh_) {
        return;
    }
    needRefresh_ = false;
    InitLabelText();
    Style style = GetStyleConst();
    style.textColor_ = GetTextColor();
325 326 327 328 329
    bool flag = false;
    if ((transMap_ != nullptr) && !transMap_->IsInvalid()) {
        transMap_->SetInvalid(true);
        flag = true;
    }
M
mamingshuai 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343
    labelText_->ReMeasureTextSize(GetContentRect(), style);
    Point textSize = labelText_->GetTextSize();
    switch (lineBreakMode_) {
        case LINE_BREAK_ADAPT:
            Resize(textSize.x, textSize.y);
            break;
        case LINE_BREAK_STRETCH:
            SetWidth(textSize.x);
            break;
        case LINE_BREAK_WRAP:
            SetHeight(textSize.y);
            break;
        case LINE_BREAK_ELLIPSIS:
            ellipsisIndex_ = labelText_->GetEllipsisIndex(GetContentRect(), style);
W
wanngtiantian 已提交
344
            labelText_->ReMeasureTextWidthInEllipsisMode(GetContentRect(), style, ellipsisIndex_);
M
mamingshuai 已提交
345 346 347 348 349 350 351
            break;
        case LINE_BREAK_MARQUEE:
            RemeasureForMarquee(textSize.x);
            break;
        default:
            break;
    }
352 353 354
    if ((transMap_ != nullptr) && flag) {
        transMap_->SetInvalid(false);
    }
M
mamingshuai 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

void UILabel::RemeasureForMarquee(int16_t textWidth)
{
    int16_t rectWidth = GetWidth();
    if (textWidth > rectWidth) {
        offsetX_ = GetRollStartPos();
        if (labelText_->GetDirect() == TEXT_DIRECT_RTL) {
            labelText_->SetAlign(TEXT_ALIGNMENT_RIGHT);
        } else {
            labelText_->SetAlign(TEXT_ALIGNMENT_LEFT);
        }
        if (hasAnimator_) {
            static_cast<LabelAnimator*>(animator_.animator)->UpdateWidth(textWidth, rectWidth);
        } else {
            LabelAnimator* animator = new LabelAnimator(textWidth, rectWidth, offsetX_, this);
            if (animator == nullptr) {
372
                GRAPHIC_LOGE("new LabelAnimator fail");
M
mamingshuai 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                return;
            }
            animator->SetAnimatorSpeed(animator_.speed);
            animator_.animator = animator;
            hasAnimator_ = true;
        }
        animator_.animator->Start();
    } else {
        offsetX_ = 0;
        if (hasAnimator_) {
            animator_.animator->Stop();
        }
    }
}

void UILabel::SetRollStartPos(int16_t pos)
{
    if (hasAnimator_) {
        static_cast<LabelAnimator*>(animator_.animator)->SetStartPos(pos);
    } else {
        animator_.pos = pos;
    }
}

int16_t UILabel::GetRollStartPos() const
{
    return hasAnimator_ ? static_cast<LabelAnimator*>(animator_.animator)->GetStartPos() : animator_.pos;
}

void UILabel::SetRollSpeed(uint16_t speed)
{
    if (hasAnimator_) {
        static_cast<LabelAnimator*>(animator_.animator)->SetAnimatorSpeed(speed);
    } else {
        animator_.speed = speed;
    }
}

S
shiqichang 已提交
411 412 413 414 415
uint16_t UILabel::GetRollSpeed() const
{
    return hasAnimator_ ? static_cast<LabelAnimator*>(animator_.animator)->GetAnimatorSpeed() : animator_.speed;
}

N
niulihua 已提交
416
void UILabel::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
M
mamingshuai 已提交
417 418
{
    InitLabelText();
419
    UIView::OnDraw(gfxDstBuffer, invalidatedArea);
M
mamingshuai 已提交
420 421 422
    Style style = GetStyleConst();
    style.textColor_ = GetTextColor();
    OpacityType opa = GetMixOpaScale();
423
    labelText_->OnDraw(gfxDstBuffer, invalidatedArea, GetOrigRect(),
N
niulihua 已提交
424
                       GetContentRect(), offsetX_, style, ellipsisIndex_, opa);
M
mamingshuai 已提交
425
}
426
} // namespace OHOS