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 "components/ui_box_scroll_bar.h"
17
18 #include "draw/draw_rect.h"
19 #include "engines/gfx/gfx_engine_manager.h"
20
21 namespace {
22 constexpr uint16_t SCROLL_BAR_MIN_LEN = 10;
23 } // namespace
24
25 namespace OHOS {
SetPosition(int16_t x,int16_t y,int16_t width,int16_t height)26 void UIBoxScrollBar::SetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
27 {
28 if ((width > 0) && (height > 0)) {
29 backgroundRect_.SetRect(x, y, x + width - 1, y + height - 1);
30 }
31 }
32
OnDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea,uint8_t backgroundOpa)33 void UIBoxScrollBar::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, uint8_t backgroundOpa)
34 {
35 Rect rect;
36 if (!rect.Intersect(invalidatedArea, backgroundRect_)) {
37 return;
38 }
39 /* Draw background */
40 BaseGfxEngine* baseGfxEngine = BaseGfxEngine::GetInstance();
41 baseGfxEngine->DrawRect(gfxDstBuffer, rect, rect, *backgroundStyle_, opacity_);
42
43 /* Draw foreground */
44 if (backgroundRect_.GetWidth() < backgroundRect_.GetHeight()) {
45 int16_t forgroundHeight = static_cast<int16_t>(foregroundProportion_ * backgroundRect_.GetHeight());
46 if (forgroundHeight < SCROLL_BAR_MIN_LEN) {
47 forgroundHeight = SCROLL_BAR_MIN_LEN;
48 }
49 int16_t forgroundTop = backgroundRect_.GetTop() +
50 static_cast<int16_t>(scrollProgress_ * (backgroundRect_.GetHeight() - forgroundHeight));
51 rect.SetRect(backgroundRect_.GetLeft(), forgroundTop, backgroundRect_.GetRight(),
52 forgroundTop + forgroundHeight - 1);
53 } else {
54 int16_t forgroundWidth = static_cast<int16_t>(foregroundProportion_ * backgroundRect_.GetWidth());
55 if (forgroundWidth < SCROLL_BAR_MIN_LEN) {
56 forgroundWidth = SCROLL_BAR_MIN_LEN;
57 }
58 int16_t forgroundLeft = backgroundRect_.GetLeft() +
59 static_cast<int16_t>(scrollProgress_ * (backgroundRect_.GetWidth() - forgroundWidth));
60 rect.SetRect(forgroundLeft, backgroundRect_.GetTop(), forgroundLeft + forgroundWidth - 1,
61 backgroundRect_.GetBottom());
62 }
63
64 rect.Intersect(invalidatedArea, rect);
65 // 8: Shift right 8 bits
66 backgroundOpa = (backgroundOpa == OPA_OPAQUE) ? opacity_ : (static_cast<uint16_t>(backgroundOpa) * opacity_) >> 8;
67 baseGfxEngine->DrawRect(gfxDstBuffer, rect, rect, *foregroundStyle_, backgroundOpa);
68 }
69 } // namespace OHOS
70