From 8eefe8e49046d9f0af16f0e764b622866815e6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 24 Jan 2017 15:53:57 +0100 Subject: [PATCH] [escher] Improve scroll indicator render Change-Id: I22fa1035ba33d2cadfc68fa6727fbaecdadd4efe --- apps/home/controller.cpp | 2 +- escher/include/escher/scroll_view.h | 6 ++- escher/include/escher/scroll_view_indicator.h | 6 ++- escher/include/escher/selectable_table_view.h | 5 ++- escher/include/escher/table_view.h | 4 +- escher/src/scroll_view.cpp | 26 ++++++------- escher/src/scroll_view_indicator.cpp | 39 +++++++++++-------- escher/src/selectable_table_view.cpp | 7 ++-- escher/src/table_view.cpp | 5 ++- 9 files changed, 60 insertions(+), 40 deletions(-) diff --git a/apps/home/controller.cpp b/apps/home/controller.cpp index 52173b5e8..f908375fa 100644 --- a/apps/home/controller.cpp +++ b/apps/home/controller.cpp @@ -9,7 +9,7 @@ namespace Home { Controller::Controller(Responder * parentResponder, ::AppsContainer * container) : ViewController(parentResponder), m_container(container), - m_selectableTableView(SelectableTableView(this, this, 0, 4, 0, 4, this, true, true, KDColorWhite)) + m_selectableTableView(SelectableTableView(this, this, 0, 4, 0, 4, this, true, true, KDColorWhite, 28, Palette::YellowOne, Palette::GreyMiddle, 116)) { } diff --git a/escher/include/escher/scroll_view.h b/escher/include/escher/scroll_view.h index 9c424458d..a00a1f329 100644 --- a/escher/include/escher/scroll_view.h +++ b/escher/include/escher/scroll_view.h @@ -9,7 +9,9 @@ class ScrollView : public View { public: ScrollView(View * contentView, KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0, bool showIndicators = true, - bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen); + bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen, KDCoordinate indicatorThickness = 10, + KDColor indicatorColor = Palette::GreyMiddle, KDColor backgroundIndicatorColor = Palette::GreyWhite, + KDCoordinate indicatorMargin = 14); void setContentOffset(KDPoint offset); KDPoint contentOffset(); @@ -28,7 +30,6 @@ protected: #endif private: KDPoint m_offset; - constexpr static KDCoordinate k_indicatorThickness = 10; int numberOfSubviews() const override; View * subviewAtIndex(int index) override; @@ -40,6 +41,7 @@ private: KDCoordinate m_rightMargin; KDCoordinate m_bottomMargin; KDCoordinate m_leftMargin; + KDCoordinate m_indicatorThickness; bool m_showIndicators; bool m_colorBackground; KDColor m_backgroundColor; diff --git a/escher/include/escher/scroll_view_indicator.h b/escher/include/escher/scroll_view_indicator.h index b1e0756c9..b2f48ea83 100644 --- a/escher/include/escher/scroll_view_indicator.h +++ b/escher/include/escher/scroll_view_indicator.h @@ -9,7 +9,7 @@ public: Horizontal, Vertical }; - ScrollViewIndicator(Direction direction); + ScrollViewIndicator(Direction direction, KDColor indicatorColor, KDColor backgroundColor, KDCoordinate margin); void drawRect(KDContext * ctx, KDRect rect) const override; float start() const; @@ -22,9 +22,13 @@ protected: virtual void logAttributes(std::ostream &os) const override; #endif private: + constexpr static KDCoordinate k_indicatorThickness = 4; Direction m_direction; float m_start; float m_end; + KDColor m_indicatorColor; + KDColor m_backgroundColor; + KDCoordinate m_margin; }; #endif diff --git a/escher/include/escher/selectable_table_view.h b/escher/include/escher/selectable_table_view.h index dfdfbd938..6f9425ceb 100644 --- a/escher/include/escher/selectable_table_view.h +++ b/escher/include/escher/selectable_table_view.h @@ -19,7 +19,10 @@ public: SelectableTableView(Responder * parentResponder, TableViewDataSource * dataSource, KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0, SelectableTableViewDelegate * delegate = nullptr, - bool showIndicators = true, bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen); + bool showIndicators = true, bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen, + KDCoordinate indicatorThickness = 10, KDColor indicatorColor = Palette::GreyMiddle, + KDColor backgroundIndicatorColor = Palette::GreyWhite, KDCoordinate indicatorMargin = 14); + int selectedRow(); int selectedColumn(); virtual bool handleEvent(Ion::Events::Event event) override; diff --git a/escher/include/escher/table_view.h b/escher/include/escher/table_view.h index 8b5b55fdd..152a6428f 100644 --- a/escher/include/escher/table_view.h +++ b/escher/include/escher/table_view.h @@ -10,7 +10,9 @@ class TableView : public ScrollView { public: TableView(TableViewDataSource * dataSource, KDCoordinate topMargin = 0, KDCoordinate rightMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate leftMargin = 0, - bool showIndicators = true, bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen); + bool showIndicators = true, bool colorBackground = true, KDColor backgroundColor = Palette::WallScreen, + KDCoordinate indicatorThickness = 10, KDColor indicatorColor = Palette::GreyMiddle, + KDColor backgroundIndicatorColor = Palette::GreyWhite, KDCoordinate indicatorMargin = 14); virtual void scrollToCell(int i, int j); TableViewCell * cellAtLocation(int i, int j); diff --git a/escher/src/scroll_view.cpp b/escher/src/scroll_view.cpp index f58a3ede4..44b812916 100644 --- a/escher/src/scroll_view.cpp +++ b/escher/src/scroll_view.cpp @@ -4,20 +4,20 @@ extern "C" { #include } -constexpr KDCoordinate ScrollView::k_indicatorThickness; - ScrollView::ScrollView(View * contentView, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, bool showIndicators, bool colorBackground, - KDColor backgroundColor) : + KDColor backgroundColor, KDCoordinate indicatorThickness, KDColor indicatorColor, + KDColor backgroundIndicatorColor, KDCoordinate indicatorMargin) : View(), m_topMargin(topMargin), m_offset(KDPointZero), m_contentView(contentView), - m_verticalScrollIndicator(ScrollViewIndicator(ScrollViewIndicator::Direction::Vertical)), - m_horizontalScrollIndicator(ScrollViewIndicator(ScrollViewIndicator::Direction::Horizontal)), + m_verticalScrollIndicator(ScrollViewIndicator(ScrollViewIndicator::Direction::Vertical, indicatorColor, backgroundIndicatorColor, indicatorMargin)), + m_horizontalScrollIndicator(ScrollViewIndicator(ScrollViewIndicator::Direction::Horizontal, indicatorColor, backgroundIndicatorColor, indicatorMargin)), m_rightMargin(rightMargin), m_bottomMargin(bottomMargin), m_leftMargin(leftMargin), + m_indicatorThickness(indicatorThickness), m_showIndicators(showIndicators), m_colorBackground(colorBackground), m_backgroundColor(backgroundColor) @@ -81,27 +81,27 @@ void ScrollView::layoutSubviews() { * bottom corner. Otherwise, the only indicator uses all the height/width. */ if (hasHorizontalIndicator() && hasVerticalIndicator()) { KDRect verticalIndicatorFrame = KDRect( - m_frame.width() - k_indicatorThickness, 0, - k_indicatorThickness, m_frame.height() - k_indicatorThickness + m_frame.width() - m_indicatorThickness, 0, + m_indicatorThickness, m_frame.height() - m_indicatorThickness ); m_verticalScrollIndicator.setFrame(verticalIndicatorFrame); KDRect horizontalIndicatorFrame = KDRect( - 0, m_frame.height() - k_indicatorThickness, - m_frame.width() - k_indicatorThickness, k_indicatorThickness + 0, m_frame.height() - m_indicatorThickness, + m_frame.width() - m_indicatorThickness, m_indicatorThickness ); m_horizontalScrollIndicator.setFrame(horizontalIndicatorFrame); } else { if (hasVerticalIndicator()) { KDRect verticalIndicatorFrame = KDRect( - m_frame.width() - k_indicatorThickness, 0, - k_indicatorThickness, m_frame.height() + m_frame.width() - m_indicatorThickness, 0, + m_indicatorThickness, m_frame.height() ); m_verticalScrollIndicator.setFrame(verticalIndicatorFrame); } if (hasHorizontalIndicator()) { KDRect horizontalIndicatorFrame = KDRect( - 0, m_frame.height() - k_indicatorThickness, - m_frame.width(), k_indicatorThickness + 0, m_frame.height() - m_indicatorThickness, + m_frame.width(), m_indicatorThickness ); m_horizontalScrollIndicator.setFrame(horizontalIndicatorFrame); } diff --git a/escher/src/scroll_view_indicator.cpp b/escher/src/scroll_view_indicator.cpp index bd8605f72..833117f49 100644 --- a/escher/src/scroll_view_indicator.cpp +++ b/escher/src/scroll_view_indicator.cpp @@ -3,33 +3,40 @@ extern "C" { #include } -constexpr KDColor k_backgroundColor = KDColorBlack; -constexpr KDColor k_indicatorColor = KDColorRed; - -ScrollViewIndicator::ScrollViewIndicator(ScrollViewIndicator::Direction direction) : +ScrollViewIndicator::ScrollViewIndicator(ScrollViewIndicator::Direction direction, KDColor indicatorColor, KDColor backgroundColor, KDCoordinate margin) : View(), m_direction(direction), m_start(0), - m_end(0) + m_end(0), + m_indicatorColor(indicatorColor), + m_backgroundColor(backgroundColor), + m_margin(margin) { } void ScrollViewIndicator::drawRect(KDContext * ctx, KDRect rect) const { - ctx->fillRect(bounds(), k_backgroundColor); - KDRect indicatorFrame = KDRectZero; + KDRect frame = KDRectZero; if (m_direction == Direction::Horizontal) { - indicatorFrame = KDRect( - m_start*m_frame.width(), 0, - (m_end-m_start)*m_frame.width(), m_frame.height() - ); + frame = KDRect(m_margin, (m_frame.height() - k_indicatorThickness)/2, + m_frame.width() - 2*m_margin, k_indicatorThickness); } else { assert(m_direction == Direction::Vertical); - indicatorFrame = KDRect( - 0, m_start*m_frame.height(), - m_frame.width(), (m_end-m_start)*m_frame.height() - ); + frame = KDRect((m_frame.width() - k_indicatorThickness)/2, m_margin, + k_indicatorThickness, m_frame.height() - 2*m_margin); } - ctx->fillRect(indicatorFrame, k_indicatorColor); + ctx->fillRect(frame, m_backgroundColor); + KDRect indicatorFrame = KDRectZero; + if (m_direction == Direction::Horizontal) { + KDCoordinate indicatorWidth = m_frame.width() - 2*m_margin; + indicatorFrame = KDRect(m_margin+m_start*indicatorWidth, (m_frame.height() - k_indicatorThickness)/2, + (m_end-m_start)*indicatorWidth, k_indicatorThickness); + } else { + assert(m_direction == Direction::Vertical); + KDCoordinate indicatorHeight = m_frame.height() - 2*m_margin; + indicatorFrame = KDRect((m_frame.width() - k_indicatorThickness)/2, m_margin+m_start*indicatorHeight, + k_indicatorThickness, (m_end-m_start)*indicatorHeight); + } + ctx->fillRect(indicatorFrame, m_indicatorColor); } float ScrollViewIndicator::start() const { diff --git a/escher/src/selectable_table_view.cpp b/escher/src/selectable_table_view.cpp index 6887191ab..06210ee21 100644 --- a/escher/src/selectable_table_view.cpp +++ b/escher/src/selectable_table_view.cpp @@ -2,9 +2,10 @@ SelectableTableView::SelectableTableView(Responder * parentResponder, TableViewDataSource * dataSource, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, - SelectableTableViewDelegate * delegate, bool showIndicators, bool colorBackground, - KDColor backgroundColor) : - TableView(dataSource, topMargin, rightMargin, bottomMargin, leftMargin, showIndicators, colorBackground, backgroundColor), + SelectableTableViewDelegate * delegate, bool showIndicators, bool colorBackground, KDColor backgroundColor, + KDCoordinate indicatorThickness, KDColor indicatorColor, KDColor backgroundIndicatorColor, KDCoordinate indicatorMargin) : + TableView(dataSource, topMargin, rightMargin, bottomMargin, leftMargin, showIndicators, colorBackground, backgroundColor, + indicatorThickness, indicatorColor, backgroundIndicatorColor, indicatorMargin), Responder(parentResponder), m_delegate(delegate), m_selectedCellX(0), diff --git a/escher/src/table_view.cpp b/escher/src/table_view.cpp index 714e13f71..0a387b7ef 100644 --- a/escher/src/table_view.cpp +++ b/escher/src/table_view.cpp @@ -9,9 +9,10 @@ extern "C" { TableView::TableView(TableViewDataSource * dataSource, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, bool showIndicators, bool colorBackground, - KDColor backgroundColor) : + KDColor backgroundColor, KDCoordinate indicatorThickness, KDColor indicatorColor, + KDColor backgroundIndicatorColor, KDCoordinate indicatorMargin) : ScrollView(&m_contentView, topMargin, rightMargin, bottomMargin, leftMargin, showIndicators, colorBackground, - backgroundColor), + backgroundColor, indicatorThickness, indicatorColor, backgroundIndicatorColor, indicatorMargin), m_contentView(TableView::ContentView(this, dataSource)) { }