[escher/scroll_view] Put the decorators inside a union

This commit is contained in:
Ruben Dashyan
2019-02-12 16:18:10 +01:00
committed by EmilieNumworks
parent 76537cd432
commit 1662b06f36
2 changed files with 51 additions and 21 deletions

View File

@@ -8,6 +8,7 @@
class ScrollView : public View {
public:
ScrollView(View * contentView, ScrollViewDataSource * dataSource);
ScrollView(ScrollView&& other);
KDSize minimalSizeForOptimalDisplay() const override;
void setTopMargin(KDCoordinate m) { m_topMargin = m; }
@@ -73,8 +74,11 @@ public:
ScrollViewArrow m_leftArrow;
};
Decorator * decorator();
void setDecoratorType(Decorator::Type t) { m_decoratorType = t; }
Decorator * decorator() { return m_decorators.activeDecorator(); }
void setDecoratorType(Decorator::Type t) {
m_decoratorType = t;
m_decorators.setActiveDecorator(t);
}
virtual void setBackgroundColor(KDColor c) {
m_backgroundColor = c;
decorator()->setBackgroundColor(m_backgroundColor);
@@ -126,9 +130,19 @@ private:
InnerView m_innerView;
Decorator::Type m_decoratorType;
Decorator m_decorator;
BarDecorator m_barDecorator;
ArrowDecorator m_arrowDecorator;
union Decorators {
public:
/* Enforce trivial constructor and destructor that just leaves the memory unmodified. */
Decorators() {}
~Decorators() {}
Decorator * activeDecorator() { return &m_none; }
void setActiveDecorator(Decorator::Type t);
private:
Decorator m_none;
BarDecorator m_bars;
ArrowDecorator m_arrows;
};
Decorators m_decorators;
KDColor m_backgroundColor;
};

View File

@@ -2,6 +2,8 @@
#include <escher/palette.h>
#include <escher/metric.h>
#include <new>
extern "C" {
#include <assert.h>
}
@@ -15,13 +17,24 @@ ScrollView::ScrollView(View * contentView, ScrollViewDataSource * dataSource) :
m_bottomMargin(0),
m_leftMargin(0),
m_innerView(this),
m_decoratorType(Decorator::Type::Bars),
m_decorator(),
m_barDecorator(),
m_arrowDecorator(),
m_decorators(),
m_backgroundColor(Palette::WallScreen)
{
assert(m_dataSource != nullptr);
setDecoratorType(Decorator::Type::Bars);
}
ScrollView::ScrollView(ScrollView&& other) :
m_contentView(other.m_contentView),
m_dataSource(other.m_dataSource),
m_topMargin(other.m_topMargin),
m_rightMargin(other.m_rightMargin),
m_bottomMargin(other.m_bottomMargin),
m_leftMargin(other.m_leftMargin),
m_innerView(this),
m_backgroundColor(other.m_backgroundColor)
{
setDecoratorType(other.m_decoratorType);
}
KDSize ScrollView::minimalSizeForOptimalDisplay() const {
@@ -39,18 +52,6 @@ void ScrollView::setCommonMargins() {
setLeftMargin(Metric::CommonLeftMargin);
}
ScrollView::Decorator * ScrollView::decorator() {
switch (m_decoratorType) {
case Decorator::Type::Bars:
return &m_barDecorator;
case Decorator::Type::Arrows:
return &m_arrowDecorator;
default:
assert(m_decoratorType == Decorator::Type::None);
return &m_decorator;
}
}
void ScrollView::scrollToContentPoint(KDPoint p, bool allowOverscroll) {
if (!allowOverscroll && !m_contentView->bounds().contains(p)) {
return;
@@ -197,6 +198,21 @@ void ScrollView::ArrowDecorator::setBackgroundColor(KDColor c) {
}
}
void ScrollView::Decorators::setActiveDecorator(Decorator::Type t) {
/* We do NOT need to destroy the previous decorator because they don't have a destructor */
switch (t) {
case Decorator::Type::Bars:
new (&m_bars) BarDecorator();
break;
case Decorator::Type::Arrows:
new (&m_arrows) ArrowDecorator();
break;
default:
assert(t == Decorator::Type::None);
new (&m_none) Decorator();
}
}
#if ESCHER_VIEW_LOGGING
const char * ScrollView::className() const {
return "ScrollView";