From 8f9d9d0b0ba943d9ca065db9e23750ce19ca7c6a Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Thu, 15 Sep 2016 15:56:54 +0200 Subject: [PATCH] [escher] focus -> firstResponder Change-Id: I2e84afe6f14abfc65e45814300e8db9703840337 --- apps/graph/graph/graph_controller.cpp | 8 ----- apps/graph/graph/graph_controller.h | 1 - apps/graph/list/function_cell.cpp | 9 +++-- apps/graph/list/function_cell.h | 3 +- apps/graph/list/list_controller.cpp | 2 +- apps/probability/law/law_cell.cpp | 33 ++++++++++++++++--- apps/probability/law/law_cell.h | 3 +- apps/probability/law/law_controller.cpp | 4 +-- escher/include/escher/app.h | 5 +-- escher/include/escher/responder.h | 4 ++- escher/include/escher/stack_view_controller.h | 1 + escher/src/app.cpp | 24 ++++++++------ escher/src/responder.cpp | 5 ++- escher/src/stack_view_controller.cpp | 7 +++- escher/src/tab_view_controller.cpp | 2 +- 15 files changed, 74 insertions(+), 37 deletions(-) diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 466d5b6d0..665443ddb 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -14,14 +14,6 @@ const char * GraphController::title() const { return "Graphique"; } -void GraphController::setFocused(bool focused) { - /* - if (focused) { - App::runningApp()->focus(&m_view); - } - */ -} - bool GraphController::handleEvent(Ion::Events::Event event) { switch (event) { case Ion::Events::Event::ENTER: diff --git a/apps/graph/graph/graph_controller.h b/apps/graph/graph/graph_controller.h index 662f6eb11..2ad59d4df 100644 --- a/apps/graph/graph/graph_controller.h +++ b/apps/graph/graph/graph_controller.h @@ -10,7 +10,6 @@ public: GraphController(Responder * parentResponder, Graph::FunctionStore * functionStore); View * view() override; const char * title() const override; - void setFocused(bool focused) override; bool handleEvent(Ion::Events::Event event) override; private: GraphView m_view; diff --git a/apps/graph/list/function_cell.cpp b/apps/graph/list/function_cell.cpp index 2ecb62590..e17d4f575 100644 --- a/apps/graph/list/function_cell.cpp +++ b/apps/graph/list/function_cell.cpp @@ -21,8 +21,13 @@ void FunctionCell::setFunction(Graph::Function * f) { m_function = f; } -void FunctionCell::setFocused(bool focused) { - m_focused = focused; +void FunctionCell::didBecomeFirstResponder() { + m_focused = true; + markRectAsDirty(bounds()); +} + +void FunctionCell::didResignFirstResponder() { + m_focused = false; markRectAsDirty(bounds()); } diff --git a/apps/graph/list/function_cell.h b/apps/graph/list/function_cell.h index 191d76571..1f331083b 100644 --- a/apps/graph/list/function_cell.h +++ b/apps/graph/list/function_cell.h @@ -11,7 +11,8 @@ public: void setEven(bool even); void drawRect(KDContext * ctx, KDRect rect) const override; - void setFocused(bool focused) override; + void didBecomeFirstResponder() override; + void didResignFirstResponder() override; private: Graph::Function * m_function; bool m_focused; diff --git a/apps/graph/list/list_controller.cpp b/apps/graph/list/list_controller.cpp index 0b03d1a7f..8b736e438 100644 --- a/apps/graph/list/list_controller.cpp +++ b/apps/graph/list/list_controller.cpp @@ -29,7 +29,7 @@ void ListController::setActiveCell(int index) { m_tableView.scrollToRow(index); FunctionCell * cell = (FunctionCell *)(m_tableView.cellAtIndex(index)); cell->setParentResponder(this); - app()->focus(cell); + app()->setFirstResponder(cell); } bool ListController::handleEvent(Ion::Events::Event event) { diff --git a/apps/probability/law/law_cell.cpp b/apps/probability/law/law_cell.cpp index af105a558..9028808a0 100644 --- a/apps/probability/law/law_cell.cpp +++ b/apps/probability/law/law_cell.cpp @@ -1,5 +1,11 @@ #include "law_cell.h" +constexpr KDColor separatorColor = KDColor(0xB4B7B9); +constexpr KDColor tableBackgroundColor = KDColor(0xF0F3F5); +constexpr KDCoordinate margin = 20; +constexpr KDColor focusedCellBackgroundColor = KDColor(0xBFD3EB); +constexpr KDColor cellBackgroundColor = KDColor(0xFCFCFC); + Probability::LawCell::LawCell() : ChildlessView(), Responder(nullptr), @@ -10,17 +16,34 @@ Probability::LawCell::LawCell() : } void Probability::LawCell::drawRect(KDContext * ctx, KDRect rect) const { - KDColor background = m_even ? KDColor(0xEEEEEE) : KDColor(0x777777); - ctx->fillRect(rect, background); - ctx->drawString(m_message, KDPointZero, m_focused); + KDCoordinate width = bounds().width(); + KDCoordinate height = bounds().height(); + KDColor backgroundColor = (m_focused ? focusedCellBackgroundColor : cellBackgroundColor); + KDColor textColor = (m_focused ? KDColorWhite : KDColorBlack); + + ctx->fillRect(KDRect(margin+1, 1, width-2*margin-1, height-1), backgroundColor); + ctx->fillRect(KDRect(0,0,margin,height), tableBackgroundColor); + ctx->fillRect(KDRect(margin,0,width-2*margin,1), separatorColor); + ctx->fillRect(KDRect(margin,0,1,height), separatorColor); + ctx->fillRect(KDRect(width-margin,0,1,height), separatorColor); + ctx->fillRect(KDRect(width-margin+1,0,margin, height), tableBackgroundColor); + + //KDColor background = m_even ? KDColor(0xEEEEEE) : KDColor(0x777777); + //ctx->fillRect(rect, background); + //ctx->drawString(m_message, KDPoint(margin+10, 5), textColor, backgroundColor); } void Probability::LawCell::setMessage(const char * message) { m_message = message; } -void Probability::LawCell::setFocused(bool focused) { - m_focused = focused; +void Probability::LawCell::didBecomeFirstResponder() { + m_focused = true; + markRectAsDirty(bounds()); +} + +void Probability::LawCell::didResignFirstResponder() { + m_focused = false; markRectAsDirty(bounds()); } diff --git a/apps/probability/law/law_cell.h b/apps/probability/law/law_cell.h index 03e98f48d..79d054889 100644 --- a/apps/probability/law/law_cell.h +++ b/apps/probability/law/law_cell.h @@ -12,7 +12,8 @@ public: void setEven(bool even); void drawRect(KDContext * ctx, KDRect rect) const override; - void setFocused(bool focused) override; + void didBecomeFirstResponder() override; + void didResignFirstResponder() override; private: const char * m_message; bool m_focused; diff --git a/apps/probability/law/law_controller.cpp b/apps/probability/law/law_controller.cpp index 6e4e9a1c9..f15bd6d6f 100644 --- a/apps/probability/law/law_controller.cpp +++ b/apps/probability/law/law_controller.cpp @@ -38,7 +38,7 @@ void Probability::LawController::setActiveCell(int index) { m_tableView.scrollToRow(index); LawCell * cell = (LawCell *)(m_tableView.cellAtIndex(index)); cell->setParentResponder(this); - app()->focus(cell); + app()->setFirstResponder(cell); } bool Probability::LawController::handleEvent(Ion::Events::Event event) { @@ -78,5 +78,5 @@ void Probability::LawController::willDisplayCellForIndex(View * cell, int index) } KDCoordinate Probability::LawController::cellHeight() { - return 40; + return 35; } diff --git a/escher/include/escher/app.h b/escher/include/escher/app.h index ee5a98576..e9c48c24b 100644 --- a/escher/include/escher/app.h +++ b/escher/include/escher/app.h @@ -18,13 +18,14 @@ public: constexpr static uint8_t Magic = 0xA8; App(); void setWindow(Window * window); - void focus(Responder * responder); + void setFirstResponder(Responder * responder); + Responder * firstResponder(); void processEvent(Ion::Events::Event event); uint8_t m_magic; // Poor man's RTTI protected: virtual ViewController * rootViewController() = 0; private: - Responder * m_focusedResponder; + Responder * m_firstResponder; }; #endif diff --git a/escher/include/escher/responder.h b/escher/include/escher/responder.h index 5f2350e0b..9abbe834c 100644 --- a/escher/include/escher/responder.h +++ b/escher/include/escher/responder.h @@ -9,7 +9,9 @@ class Responder { public: Responder(Responder * parentResponder); virtual bool handleEvent(Ion::Events::Event event); // Default implementation does nothing - virtual void setFocused(bool focused); // Default implementation does nothing. Used by subclasses to know when active or not + virtual void didBecomeFirstResponder(); + virtual void didResignFirstResponder(); + Responder * parentResponder() const; void setParentResponder(Responder * responder); App * app(); diff --git a/escher/include/escher/stack_view_controller.h b/escher/include/escher/stack_view_controller.h index 9288f772a..93dd91036 100644 --- a/escher/include/escher/stack_view_controller.h +++ b/escher/include/escher/stack_view_controller.h @@ -18,6 +18,7 @@ public: View * view() override; const char * title() const override; bool handleEvent(Ion::Events::Event event) override; + void didBecomeFirstResponder() override; private: class ControllerView : public View { public: diff --git a/escher/src/app.cpp b/escher/src/app.cpp index 86a062d20..226c5333c 100644 --- a/escher/src/app.cpp +++ b/escher/src/app.cpp @@ -7,15 +7,15 @@ extern "C" { App::App() : m_magic(Magic), Responder(nullptr), - m_focusedResponder(nullptr) + m_firstResponder(nullptr) { } void App::setWindow(Window * window) { ViewController * controller = rootViewController(); View * view = controller->view(); - if (m_focusedResponder == nullptr) { - focus(controller); + if (m_firstResponder == nullptr) { + setFirstResponder(controller); } assert(controller->app() == this); @@ -26,7 +26,7 @@ void App::setWindow(Window * window) { } void App::processEvent(Ion::Events::Event event) { - Responder * responder = m_focusedResponder; + Responder * responder = m_firstResponder; bool didHandleEvent = false; while (responder) { didHandleEvent = responder->handleEvent(event); @@ -37,12 +37,16 @@ void App::processEvent(Ion::Events::Event event) { } } -void App::focus(Responder * responder) { - if (m_focusedResponder) { - m_focusedResponder->setFocused(false); +Responder * App::firstResponder() { + return m_firstResponder; +} + +void App::setFirstResponder(Responder * responder) { + if (m_firstResponder) { + m_firstResponder->didResignFirstResponder(); } - m_focusedResponder = responder; - if (m_focusedResponder) { - m_focusedResponder->setFocused(true); + m_firstResponder = responder; + if (m_firstResponder) { + m_firstResponder->didBecomeFirstResponder(); } } diff --git a/escher/src/responder.cpp b/escher/src/responder.cpp index 5b867ba72..80b439a60 100644 --- a/escher/src/responder.cpp +++ b/escher/src/responder.cpp @@ -19,7 +19,10 @@ bool Responder::handleEvent(Ion::Events::Event event) { return false; } -void Responder::setFocused(bool focused) { +void Responder::didBecomeFirstResponder() { +} + +void Responder::didResignFirstResponder() { } /* We assume the app is the root parent. */ diff --git a/escher/src/stack_view_controller.cpp b/escher/src/stack_view_controller.cpp index 721c53a7a..cd10d0acc 100644 --- a/escher/src/stack_view_controller.cpp +++ b/escher/src/stack_view_controller.cpp @@ -97,7 +97,12 @@ void StackViewController::setupActiveViewController() { ViewController * vc = m_children[m_numberOfChildren-1]; vc->setParentResponder(this); m_view.setContentView(vc->view()); - app()->focus(vc); + app()->setFirstResponder(vc); +} + +void StackViewController::didBecomeFirstResponder() { + ViewController * vc = m_children[m_numberOfChildren-1]; + app()->setFirstResponder(vc); } bool StackViewController::handleEvent(Ion::Events::Event event) { diff --git a/escher/src/tab_view_controller.cpp b/escher/src/tab_view_controller.cpp index d82c24d73..24d5c3626 100644 --- a/escher/src/tab_view_controller.cpp +++ b/escher/src/tab_view_controller.cpp @@ -110,7 +110,7 @@ void TabViewController::setActiveTab(uint8_t i) { m_view.m_tabView.setActiveIndex(i); m_activeChildIndex = i; - app()->focus(activeVC); + app()->setFirstResponder(activeVC); } View * TabViewController::view() {