[escher/scroll_view] Prepare ScrollViewIndicator for future ScrollViewArrow

This commit is contained in:
Ruben Dashyan
2019-01-23 17:19:47 +01:00
committed by EmilieNumworks
parent c02d44988b
commit 42d6ec66ab
4 changed files with 76 additions and 71 deletions

View File

@@ -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;
};

View File

@@ -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:

View File

@@ -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
));
}
}

View File

@@ -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";