From 42d6ec66ab307e37bf29611c4e8e20228ef28d33 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Wed, 23 Jan 2019 17:19:47 +0100 Subject: [PATCH] [escher/scroll_view] Prepare ScrollViewIndicator for future ScrollViewArrow --- escher/include/escher/scroll_view.h | 8 +- escher/include/escher/scroll_view_indicator.h | 30 +++---- escher/src/scroll_view.cpp | 30 +++---- escher/src/scroll_view_indicator.cpp | 79 ++++++++++--------- 4 files changed, 76 insertions(+), 71 deletions(-) diff --git a/escher/include/escher/scroll_view.h b/escher/include/escher/scroll_view.h index 86a505e8a..dcd2096e8 100644 --- a/escher/include/escher/scroll_view.h +++ b/escher/include/escher/scroll_view.h @@ -31,12 +31,12 @@ public: int numberOfIndicators(); View * indicatorAtIndex(int index); void layoutIndicators(KDSize content, KDPoint offset, KDSize frame); - ScrollViewVerticalIndicator * verticalBar() { return &m_verticalScrollIndicator; } - ScrollViewHorizontalIndicator * horizontalBar() { return &m_horizontalScrollIndicator; } + ScrollViewVerticalBar * verticalBar() { return &m_verticalBar; } + ScrollViewHorizontalBar * horizontalBar() { return &m_horizontalBar; } void setIndicatorThickness(KDCoordinate t) { m_indicatorThickness = t; } private: - ScrollViewVerticalIndicator m_verticalScrollIndicator; - ScrollViewHorizontalIndicator m_horizontalScrollIndicator; + ScrollViewVerticalBar m_verticalBar; + ScrollViewHorizontalBar m_horizontalBar; KDCoordinate m_indicatorThickness; }; diff --git a/escher/include/escher/scroll_view_indicator.h b/escher/include/escher/scroll_view_indicator.h index 7b8c9062c..e75ec52f2 100644 --- a/escher/include/escher/scroll_view_indicator.h +++ b/escher/include/escher/scroll_view_indicator.h @@ -6,37 +6,37 @@ class ScrollViewIndicator : public View { public: ScrollViewIndicator(); - - void setIndicatorColor(KDColor c) { m_indicatorColor = c; } - KDColor indicatorColor() const { return m_indicatorColor; } - void setBackgroundColor(KDColor c) { m_backgroundColor = c; } - KDColor backgroundColor() const { return m_backgroundColor; } void setMargin(KDCoordinate m) { m_margin = m; } KDCoordinate margin() const { return m_margin; } - - void update(KDCoordinate totalContentLength, KDCoordinate contentOffset, KDCoordinate visibleContentLength); - bool visible() const { return 0 < m_offset || m_visibleLength < 1; } protected: #if ESCHER_VIEW_LOGGING virtual const char * className() const override; virtual void logAttributes(std::ostream &os) const override; #endif - constexpr static KDCoordinate k_indicatorThickness = 4; - float m_offset; - float m_visibleLength; - KDColor m_indicatorColor; - KDColor m_backgroundColor; + KDColor m_color; KDCoordinate m_margin; }; -class ScrollViewHorizontalIndicator : public ScrollViewIndicator { +class ScrollViewBar : public ScrollViewIndicator { +public: + ScrollViewBar(); + void update(KDCoordinate totalContentLength, KDCoordinate contentOffset, KDCoordinate visibleContentLength); + bool visible() const { return 0 < m_offset || m_visibleLength < 1; } +protected: + constexpr static KDCoordinate k_indicatorThickness = 4; + float m_offset; + float m_visibleLength; + KDColor m_trackColor; +}; + +class ScrollViewHorizontalBar : public ScrollViewBar { public: void drawRect(KDContext * ctx, KDRect rect) const override; private: KDCoordinate totalLength() const { return m_frame.width() - 2*m_margin; } }; -class ScrollViewVerticalIndicator : public ScrollViewIndicator { +class ScrollViewVerticalBar : public ScrollViewBar { public: void drawRect(KDContext * ctx, KDRect rect) const override; private: diff --git a/escher/src/scroll_view.cpp b/escher/src/scroll_view.cpp index 0421c2135..5a70f135d 100644 --- a/escher/src/scroll_view.cpp +++ b/escher/src/scroll_view.cpp @@ -146,53 +146,53 @@ KDCoordinate ScrollView::maxContentHeightDisplayableWithoutScrolling() { } ScrollView::Decorator::Decorator() : - m_verticalScrollIndicator(), - m_horizontalScrollIndicator(), + m_verticalBar(), + m_horizontalBar(), m_indicatorThickness(20) { } int ScrollView::Decorator::numberOfIndicators() { - return m_verticalScrollIndicator.visible() + m_horizontalScrollIndicator.visible(); + return m_verticalBar.visible() + m_horizontalBar.visible(); } View * ScrollView::Decorator::indicatorAtIndex(int index) { switch (index) { case 1: - if (m_horizontalScrollIndicator.visible()) { - return &m_horizontalScrollIndicator; + if (m_horizontalBar.visible()) { + return &m_horizontalBar; } else { - return &m_verticalScrollIndicator; + return &m_verticalBar; } case 2: - return &m_verticalScrollIndicator; + return &m_verticalBar; } return nullptr; } void ScrollView::Decorator::layoutIndicators(KDSize content, KDPoint offset, KDSize frame) { - m_horizontalScrollIndicator.update( + m_horizontalBar.update( content.width(), offset.x(), frame.width() ); - m_verticalScrollIndicator.update( + m_verticalBar.update( content.height(), offset.y(), frame.height() ); /* If the two indicators are visible, we leave an empty rectangle in the right * bottom corner. Otherwise, the only indicator uses all the height/width. */ - if (m_verticalScrollIndicator.visible()) { - m_verticalScrollIndicator.setFrame(KDRect( + if (m_verticalBar.visible()) { + m_verticalBar.setFrame(KDRect( frame.width() - m_indicatorThickness, 0, - m_indicatorThickness, frame.height() - m_horizontalScrollIndicator.visible() * m_indicatorThickness + m_indicatorThickness, frame.height() - m_horizontalBar.visible() * m_indicatorThickness )); } - if (m_horizontalScrollIndicator.visible()) { - m_horizontalScrollIndicator.setFrame(KDRect( + if (m_horizontalBar.visible()) { + m_horizontalBar.setFrame(KDRect( 0, frame.height() - m_indicatorThickness, - frame.width() - m_verticalScrollIndicator.visible() * m_indicatorThickness, m_indicatorThickness + frame.width() - m_verticalBar.visible() * m_indicatorThickness, m_indicatorThickness )); } } diff --git a/escher/src/scroll_view_indicator.cpp b/escher/src/scroll_view_indicator.cpp index 808f40719..c8c46acb7 100644 --- a/escher/src/scroll_view_indicator.cpp +++ b/escher/src/scroll_view_indicator.cpp @@ -6,49 +6,20 @@ extern "C" { ScrollViewIndicator::ScrollViewIndicator() : View(), - m_offset(0), - m_visibleLength(0), - m_indicatorColor(Palette::GreyDark), - m_backgroundColor(Palette::GreyMiddle), + m_color(Palette::GreyDark), m_margin(14) { } -void ScrollViewHorizontalIndicator::drawRect(KDContext * ctx, KDRect rect) const { - ctx->fillRect( - KDRect( - m_margin, (m_frame.height() - k_indicatorThickness)/2, - totalLength(), k_indicatorThickness - ), - m_backgroundColor - ); - ctx->fillRect( - KDRect( - m_margin+m_offset*totalLength(), (m_frame.height() - k_indicatorThickness)/2, - m_visibleLength*totalLength(), k_indicatorThickness - ), - m_indicatorColor - ); +ScrollViewBar::ScrollViewBar() : + ScrollViewIndicator(), + m_offset(0), + m_visibleLength(0), + m_trackColor(Palette::GreyMiddle) +{ } -void ScrollViewVerticalIndicator::drawRect(KDContext * ctx, KDRect rect) const { - ctx->fillRect( - KDRect( - (m_frame.width() - k_indicatorThickness)/2, m_margin, - k_indicatorThickness, totalLength() - ), - m_backgroundColor - ); - ctx->fillRect( - KDRect( - (m_frame.width() - k_indicatorThickness)/2, m_margin+m_offset*totalLength(), - k_indicatorThickness, m_visibleLength*totalLength() - ), - m_indicatorColor - ); -} - -void ScrollViewIndicator::update(KDCoordinate totalContentLength, KDCoordinate contentOffset, KDCoordinate visibleContentLength) { +void ScrollViewBar::update(KDCoordinate totalContentLength, KDCoordinate contentOffset, KDCoordinate visibleContentLength) { float offset = contentOffset; float visibleLength = visibleContentLength; offset = offset / totalContentLength; @@ -60,6 +31,40 @@ void ScrollViewIndicator::update(KDCoordinate totalContentLength, KDCoordinate c } } +void ScrollViewHorizontalBar::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect( + KDRect( + m_margin, (m_frame.height() - k_indicatorThickness)/2, + totalLength(), k_indicatorThickness + ), + m_trackColor + ); + ctx->fillRect( + KDRect( + m_margin+m_offset*totalLength(), (m_frame.height() - k_indicatorThickness)/2, + m_visibleLength*totalLength(), k_indicatorThickness + ), + m_color + ); +} + +void ScrollViewVerticalBar::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect( + KDRect( + (m_frame.width() - k_indicatorThickness)/2, m_margin, + k_indicatorThickness, totalLength() + ), + m_trackColor + ); + ctx->fillRect( + KDRect( + (m_frame.width() - k_indicatorThickness)/2, m_margin+m_offset*totalLength(), + k_indicatorThickness, m_visibleLength*totalLength() + ), + m_color + ); +} + #if ESCHER_VIEW_LOGGING const char * ScrollViewIndicator::className() const { return "ScrollViewIndicator";