From 0a6d8220467553984e4e12e4630d3a00afbf39e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 14 Oct 2016 14:32:16 +0200 Subject: [PATCH] [escher] Create a class pointer text view inheriting from text view Change-Id: I10dd24ed9404e634e90f2bc7fecb6441f7a93550 --- apps/home/app_cell.h | 2 +- escher/Makefile | 1 + escher/include/escher.h | 1 + escher/include/escher/button.h | 4 ++-- escher/include/escher/pointer_text_view.h | 18 ++++++++++++++++++ escher/include/escher/table_view_cell.h | 6 +++--- escher/include/escher/text_view.h | 7 ++----- escher/src/button.cpp | 10 +++++----- escher/src/pointer_text_view.cpp | 23 +++++++++++++++++++++++ escher/src/table_view_cell.cpp | 12 ++++++------ escher/src/text_view.cpp | 18 ++++++------------ 11 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 escher/include/escher/pointer_text_view.h create mode 100644 escher/src/pointer_text_view.cpp diff --git a/apps/home/app_cell.h b/apps/home/app_cell.h index 4d27274f3..8800c7727 100644 --- a/apps/home/app_cell.h +++ b/apps/home/app_cell.h @@ -19,7 +19,7 @@ public: private: static constexpr KDCoordinate k_iconSize = 32; ImageView m_iconView; - TextView m_nameView; + PointerTextView m_nameView; bool m_visible; bool m_active; }; diff --git a/escher/Makefile b/escher/Makefile index edb9db91f..edae286bb 100644 --- a/escher/Makefile +++ b/escher/Makefile @@ -14,6 +14,7 @@ objs += $(addprefix escher/src/,\ list_view.o\ metric.o\ palette.o\ + pointer_text_view.o\ responder.o\ scroll_view.o\ scroll_view_indicator.o\ diff --git a/escher/include/escher.h b/escher/include/escher.h index 44478aceb..6491a2d7e 100644 --- a/escher/include/escher.h +++ b/escher/include/escher.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/escher/include/escher/button.h b/escher/include/escher/button.h index 7a5bcdd58..223dda744 100644 --- a/escher/include/escher/button.h +++ b/escher/include/escher/button.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include class Button : public View, public Responder { @@ -19,7 +19,7 @@ private: int numberOfSubviews() const override; View * subviewAtIndex(int index) override; void layoutSubviews() override; - TextView m_textView; + PointerTextView m_pointerTextView; Invocation m_invocation; KDColor m_backgroundColor; }; diff --git a/escher/include/escher/pointer_text_view.h b/escher/include/escher/pointer_text_view.h new file mode 100644 index 000000000..33e5421bf --- /dev/null +++ b/escher/include/escher/pointer_text_view.h @@ -0,0 +1,18 @@ +#ifndef ESCHER_POINTER_TEXT_VIEW_H +#define ESCHER_POINTER_TEXT_VIEW_H + +#include + +class PointerTextView : public TextView { +public: + PointerTextView(); + PointerTextView(const char * text, float horizontalAlignment, float verticalAlignment, + KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite); + void setText(const char * text); +protected: + const char * text() const override; +private: + const char * m_textPointer; +}; + +#endif \ No newline at end of file diff --git a/escher/include/escher/table_view_cell.h b/escher/include/escher/table_view_cell.h index f97bd8445..2bab9c388 100644 --- a/escher/include/escher/table_view_cell.h +++ b/escher/include/escher/table_view_cell.h @@ -2,7 +2,7 @@ #define ESCHER_TABLE_VIEW_CELL_H #include -#include +#include #include #include @@ -10,7 +10,7 @@ class TableViewCell : public View { public: TableViewCell(char * label = nullptr); - TextView * textView(); + PointerTextView * textView(); virtual View * contentView() const; bool isHighlighted() const; void setHighlighted(bool highlight); @@ -22,7 +22,7 @@ public: private: constexpr static KDCoordinate k_separatorThickness = 1; bool m_highlighted; - TextView m_textView; + PointerTextView m_pointerTextView; }; #endif diff --git a/escher/include/escher/text_view.h b/escher/include/escher/text_view.h index b3df3ea44..aefa67321 100644 --- a/escher/include/escher/text_view.h +++ b/escher/include/escher/text_view.h @@ -10,21 +10,18 @@ public: // alignment = 0 -> align left or top // alignment = 0.5 -> align center // alignment = 1.0 -> align right or bottom - TextView(const char * text, - float horizontalAlignment, float verticalAlignment, - KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite); + TextView(float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor); void drawRect(KDContext * ctx, KDRect rect) const override; - void setText(const char * text); void setBackgroundColor(KDColor backgroundColor); void setTextColor(KDColor textColor); void setAlignment(float horizontalAlignment, float verticalAlignment); KDSize minimalSizeForOptimalDisplay() override; protected: + virtual const char * text() const = 0; #if ESCHER_VIEW_LOGGING const char * className() const override; #endif private: - const char * m_text; float m_horizontalAlignment; float m_verticalAlignment; KDColor m_textColor; diff --git a/escher/src/button.cpp b/escher/src/button.cpp index f69e903f9..06b8bfa19 100644 --- a/escher/src/button.cpp +++ b/escher/src/button.cpp @@ -3,7 +3,7 @@ Button::Button(Responder * parentResponder, const char * textBody, Invocation invocation) : Responder(parentResponder), - m_textView(TextView(textBody, 0.5f, 0.5f)), + m_pointerTextView(PointerTextView(textBody, 0.5f, 0.5f)), m_invocation(invocation), m_backgroundColor(KDColorWhite) { @@ -19,11 +19,11 @@ int Button::numberOfSubviews() const { View * Button::subviewAtIndex(int index) { assert(index == 0); - return &m_textView; + return &m_pointerTextView; } void Button::layoutSubviews() { - m_textView.setFrame(bounds()); + m_pointerTextView.setFrame(bounds()); } bool Button::handleEvent(Ion::Events::Event event) { @@ -38,11 +38,11 @@ bool Button::handleEvent(Ion::Events::Event event) { void Button::setBackgroundColor(KDColor backgroundColor) { m_backgroundColor = backgroundColor; - m_textView.setBackgroundColor(backgroundColor); + m_pointerTextView.setBackgroundColor(backgroundColor); markRectAsDirty(bounds()); } KDSize Button::minimalSizeForOptimalDisplay() { - KDSize textSize = m_textView.minimalSizeForOptimalDisplay(); + KDSize textSize = m_pointerTextView.minimalSizeForOptimalDisplay(); return KDSize(textSize.width() + k_horizontalMargin, textSize.height() + k_verticalMargin); } diff --git a/escher/src/pointer_text_view.cpp b/escher/src/pointer_text_view.cpp new file mode 100644 index 000000000..92fc85e87 --- /dev/null +++ b/escher/src/pointer_text_view.cpp @@ -0,0 +1,23 @@ +#include + +PointerTextView::PointerTextView() : + TextView(), + m_textPointer(nullptr) +{ +} + +PointerTextView::PointerTextView(const char * text, float horizontalAlignment, float verticalAlignment, + KDColor textColor, KDColor backgroundColor) : + TextView(horizontalAlignment, verticalAlignment, textColor, backgroundColor), + m_textPointer(text) +{ +} + +const char * PointerTextView::text() const { + return m_textPointer; +} + +void PointerTextView::setText(const char * text) { + m_textPointer = text; + markRectAsDirty(bounds()); +} \ No newline at end of file diff --git a/escher/src/table_view_cell.cpp b/escher/src/table_view_cell.cpp index 8b397edbe..23953c0dd 100644 --- a/escher/src/table_view_cell.cpp +++ b/escher/src/table_view_cell.cpp @@ -6,7 +6,7 @@ constexpr KDCoordinate TableViewCell::k_separatorThickness; TableViewCell::TableViewCell(char * label) : View(), m_highlighted(false), - m_textView(TextView(label, 0, 0.5, KDColorBlack, Palette::CellBackgroundColor)) + m_pointerTextView(PointerTextView(label, 0, 0.5, KDColorBlack, Palette::CellBackgroundColor)) { } @@ -19,7 +19,7 @@ int TableViewCell::numberOfSubviews() const { View * TableViewCell::subviewAtIndex(int index) { if (index == 0) { - return &m_textView; + return &m_pointerTextView; } assert(numberOfSubviews() == 2 && index == 1); return contentView(); @@ -28,15 +28,15 @@ View * TableViewCell::subviewAtIndex(int index) { void TableViewCell::layoutSubviews() { KDCoordinate width = bounds().width(); KDCoordinate height = bounds().height(); - m_textView.setFrame(KDRect(k_separatorThickness, k_separatorThickness, 3*width/4 - 2*k_separatorThickness, height - 2*k_separatorThickness)); + m_pointerTextView.setFrame(KDRect(k_separatorThickness, k_separatorThickness, 3*width/4 - 2*k_separatorThickness, height - 2*k_separatorThickness)); View * content = contentView(); if (content) { content->setFrame(KDRect(k_separatorThickness + 3*width/4, k_separatorThickness, width/4-2*k_separatorThickness, height-2*k_separatorThickness)); } } -TextView * TableViewCell::textView() { - return &m_textView; +PointerTextView * TableViewCell::textView() { + return &m_pointerTextView; } View * TableViewCell::contentView() const { @@ -50,7 +50,7 @@ bool TableViewCell::isHighlighted() const { void TableViewCell::setHighlighted(bool highlight) { m_highlighted = highlight; KDColor backgroundColor = highlight? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor; - m_textView.setBackgroundColor(backgroundColor); + m_pointerTextView.setBackgroundColor(backgroundColor); markRectAsDirty(bounds()); } diff --git a/escher/src/text_view.cpp b/escher/src/text_view.cpp index 4ac1b8a1a..4434e9784 100644 --- a/escher/src/text_view.cpp +++ b/escher/src/text_view.cpp @@ -1,13 +1,12 @@ #include -TextView::TextView() : TextView(nullptr, 0.0f, 0.0f, KDColorBlack, KDColorWhite) +TextView::TextView() : TextView(0.0f, 0.0f, KDColorBlack, KDColorWhite) { } -TextView::TextView(const char * text, float horizontalAlignment, float verticalAlignment, +TextView::TextView(float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor) : ChildlessView(), - m_text(text), m_horizontalAlignment(horizontalAlignment), m_verticalAlignment(verticalAlignment), m_textColor(textColor), @@ -15,11 +14,6 @@ TextView::TextView(const char * text, float horizontalAlignment, float verticalA { } -void TextView::setText(const char * text) { - m_text = text; - markRectAsDirty(bounds()); -} - void TextView::setBackgroundColor(KDColor backgroundColor) { m_backgroundColor = backgroundColor; markRectAsDirty(bounds()); @@ -37,19 +31,19 @@ void TextView::setAlignment(float horizontalAlignment, float verticalAlignment) } KDSize TextView::minimalSizeForOptimalDisplay() { - return KDText::stringSize(m_text); + return KDText::stringSize(text()); } void TextView::drawRect(KDContext * ctx, KDRect rect) const { - if (m_text == nullptr) { + if (text() == nullptr) { return; } - KDSize textSize = KDText::stringSize(m_text); + KDSize textSize = KDText::stringSize(text()); KDPoint origin = { (KDCoordinate)(m_horizontalAlignment*(m_frame.width() - textSize.width())), (KDCoordinate)(m_verticalAlignment*(m_frame.height() - textSize.height())) }; - ctx->drawString(m_text, origin, m_textColor, m_backgroundColor); + ctx->drawString(text(), origin, m_textColor, m_backgroundColor); } #if ESCHER_VIEW_LOGGING