From 375262031b9dc970d81a5ece6ce12c85e4ec788b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 7 Nov 2016 14:05:27 +0100 Subject: [PATCH 1/2] [escher] add optional margins to modal view Change-Id: Ibeb77958f5fd6db9a110d1f49abb77b4ee01a72f --- escher/include/escher/app.h | 3 ++- escher/include/escher/modal_view_controller.h | 10 +++++-- escher/src/app.cpp | 5 ++-- escher/src/modal_view_controller.cpp | 26 +++++++++++++------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/escher/include/escher/app.h b/escher/include/escher/app.h index a8e75b7d0..a4f2c7c2b 100644 --- a/escher/include/escher/app.h +++ b/escher/include/escher/app.h @@ -24,7 +24,8 @@ public: void setFirstResponder(Responder * responder); Responder * firstResponder(); void processEvent(Ion::Events::Event event); - void displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment); + void displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin = 0, KDCoordinate leftMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate rightMargin = 0); void dismissModalViewController(); void displayWarning(const char * warningMessage); const char * name(); diff --git a/escher/include/escher/modal_view_controller.h b/escher/include/escher/modal_view_controller.h index 2c8dbd4d5..eb86a42f6 100644 --- a/escher/include/escher/modal_view_controller.h +++ b/escher/include/escher/modal_view_controller.h @@ -12,7 +12,8 @@ public: bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; - void displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment); + void displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin = 0, KDCoordinate leftMargin = 0, KDCoordinate bottomMargin = 0, KDCoordinate rightMargin = 0); void dismissModalViewController(); bool isDisplayingModal(); private: @@ -23,7 +24,8 @@ private: int numberOfSubviews() const override; View * subviewAtIndex(int index) override; void layoutSubviews() override; - void presentModalView(View * modalView, float verticalAlignment, float horizontalAlignment); + void presentModalView(View * modalView, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin); void dismissModalView(); bool isDisplayingModal() const; private: @@ -33,6 +35,10 @@ private: bool m_isDisplayingModal; float m_verticalAlignment; float m_horizontalAlignment; + KDCoordinate m_topMargin; + KDCoordinate m_leftMargin; + KDCoordinate m_bottomMargin; + KDCoordinate m_rightMargin; }; ContentView m_contentView; Responder * m_previousResponder; diff --git a/escher/src/app.cpp b/escher/src/app.cpp index 600c555fa..948c28a2a 100644 --- a/escher/src/app.cpp +++ b/escher/src/app.cpp @@ -62,8 +62,9 @@ const Image * App::icon() { return m_icon; } -void App::displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment) { - m_modalViewController.displayModalViewController(vc, verticalAlignment, horizontalAlignment); +void App::displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin) { + m_modalViewController.displayModalViewController(vc, verticalAlignment, horizontalAlignment, topMargin, leftMargin, bottomMargin, rightMargin); } void App::dismissModalViewController() { diff --git a/escher/src/modal_view_controller.cpp b/escher/src/modal_view_controller.cpp index bca74e4bd..6034a9927 100644 --- a/escher/src/modal_view_controller.cpp +++ b/escher/src/modal_view_controller.cpp @@ -8,7 +8,11 @@ ModalViewController::ContentView::ContentView() : m_currentModalView(nullptr), m_isDisplayingModal(false), m_verticalAlignment(0.0f), - m_horizontalAlignment(0.0f) + m_horizontalAlignment(0.0f), + m_topMargin(0), + m_leftMargin(0), + m_bottomMargin(0), + m_rightMargin(0) { } @@ -42,11 +46,11 @@ View * ModalViewController::ContentView::subviewAtIndex(int index) { KDRect ModalViewController::ContentView::frame() { KDCoordinate modalHeight = m_currentModalView->minimalSizeForOptimalDisplay().height(); - modalHeight = modalHeight == 0 ? bounds().height() : modalHeight; + modalHeight = modalHeight == 0 ? bounds().height()-m_topMargin-m_bottomMargin : modalHeight; KDCoordinate modalWidth = m_currentModalView->minimalSizeForOptimalDisplay().width(); - modalWidth = modalWidth == 0 ? bounds().width() : modalWidth; - KDRect modalViewFrame(m_horizontalAlignment*(bounds().width()-modalWidth), m_verticalAlignment*(bounds().height()-modalHeight), - modalWidth, modalHeight); + modalWidth = modalWidth == 0 ? bounds().width()-m_leftMargin-m_rightMargin : modalWidth; + KDRect modalViewFrame(m_leftMargin + m_horizontalAlignment*(bounds().width()-m_leftMargin-m_rightMargin-modalWidth), + m_topMargin+m_verticalAlignment*(bounds().height()-m_topMargin-m_bottomMargin-modalHeight), modalWidth, modalHeight); return modalViewFrame; } @@ -57,11 +61,16 @@ void ModalViewController::ContentView::layoutSubviews() { } } -void ModalViewController::ContentView::presentModalView(View * modalView, float verticalAlignment, float horizontalAlignment) { +void ModalViewController::ContentView::presentModalView(View * modalView, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin) { m_isDisplayingModal = true; m_currentModalView = modalView; m_horizontalAlignment = horizontalAlignment; m_verticalAlignment = verticalAlignment; + m_topMargin = topMargin; + m_leftMargin = leftMargin; + m_bottomMargin = bottomMargin; + m_rightMargin = rightMargin; markRectAsDirty(frame()); layoutSubviews(); } @@ -101,12 +110,13 @@ const char * ModalViewController::title() const { return "Modal View Controller"; } -void ModalViewController::displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment) { +void ModalViewController::displayModalViewController(ViewController * vc, float verticalAlignment, float horizontalAlignment, + KDCoordinate topMargin, KDCoordinate leftMargin, KDCoordinate bottomMargin, KDCoordinate rightMargin) { m_currentModalViewController = vc; vc->setParentResponder(this); m_previousResponder = app()->firstResponder(); app()->setFirstResponder(vc); - m_contentView.presentModalView(vc->view(), verticalAlignment, horizontalAlignment); + m_contentView.presentModalView(vc->view(), verticalAlignment, horizontalAlignment, topMargin, leftMargin, bottomMargin, rightMargin); } void ModalViewController::dismissModalViewController() { From 0743b57e5da1253248567a17075d420dd56f69ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 7 Nov 2016 14:06:25 +0100 Subject: [PATCH 2/2] [apps] create a tool box controller Change-Id: I2947f0e0656174f0218f0ef4147560c37c917069 --- apps/Makefile | 1 + apps/tool_box_controller.cpp | 95 ++++++++++++++++++++++++++++++++++++ apps/tool_box_controller.h | 33 +++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 apps/tool_box_controller.cpp create mode 100644 apps/tool_box_controller.h diff --git a/apps/Makefile b/apps/Makefile index 11eac9842..6f314656f 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -9,6 +9,7 @@ app_objs += $(addprefix apps/,\ constant.o\ expression_text_field_delegate.o\ main.o\ + tool_box_controller.o\ ) $(app_objs): $(inline_images:.png=.o) diff --git a/apps/tool_box_controller.cpp b/apps/tool_box_controller.cpp new file mode 100644 index 000000000..79ca60f41 --- /dev/null +++ b/apps/tool_box_controller.cpp @@ -0,0 +1,95 @@ +#include "tool_box_controller.h" +#include + +static const char * labels[] = { + "Abs()", + "Root()", + "Log()", + "Calcul", + "Nombres complexes", + "Probabilite", + "Arithmetiques", + "Matrices", + "Listes", + "Approximation", + "Trigonometrie hyperbolique" +}; + +ToolBoxController::ToolBoxController() : + StackViewController(nullptr, this, true), + m_selectableTableView(SelectableTableView(this, this)) +{ +} + +const char * ToolBoxController::title() const { + return "ToolBoxController"; +} + +bool ToolBoxController::handleEvent(Ion::Events::Event event) { + return false; +} + +void ToolBoxController::didBecomeFirstResponder() { + m_selectableTableView.selectCellAtLocation(0, 0); + app()->setFirstResponder(&m_selectableTableView); +} + +int ToolBoxController::numberOfRows() { + return k_numberOfCommandRows + k_numberOfMenuRows; +} + +TableViewCell * ToolBoxController::reusableCell(int index, int type) { + assert(type < 2); + assert(index >= 0); + if (type == 0) { + assert(index < k_numberOfCommandRows); + return &m_commandCells[index]; + } + assert(index < k_maxNumberOfDisplayedRows); + return &m_menuCells[index]; +} + +int ToolBoxController::reusableCellCount(int type) { + assert(type < 2); + if (type == 0) { + return k_numberOfCommandRows; + } + return k_maxNumberOfDisplayedRows; +} + +void ToolBoxController::willDisplayCellForIndex(TableViewCell * cell, int index) { + MenuListCell * myCell = (MenuListCell *)cell; + myCell->setText(labels[index]); +} + +KDCoordinate ToolBoxController::rowHeight(int j) { + if (typeAtLocation(0, j) == 0) { + return k_commandRowHeight; + } + return k_menuRowHeight; +} + +KDCoordinate ToolBoxController::cumulatedHeightFromIndex(int j) { + if (j < k_numberOfCommandRows) { + return j*k_commandRowHeight; + } + return k_commandRowHeight*k_numberOfCommandRows + k_menuRowHeight*(j-k_numberOfCommandRows); +} + +int ToolBoxController::indexFromCumulatedHeight(KDCoordinate offsetY) { + if (offsetY < k_commandRowHeight*k_numberOfCommandRows) { + return offsetY/k_commandRowHeight; + } + return k_numberOfCommandRows + (offsetY - k_commandRowHeight*k_numberOfCommandRows)/k_menuRowHeight; +} + +int ToolBoxController::typeAtLocation(int i, int j) { + if (j < k_numberOfCommandRows) { + return 0; + } + return 1; +} + +void ToolBoxController::setTextFieldCaller(TextField * textField) { + m_textFieldCaller = textField; +} diff --git a/apps/tool_box_controller.h b/apps/tool_box_controller.h new file mode 100644 index 000000000..4e534b692 --- /dev/null +++ b/apps/tool_box_controller.h @@ -0,0 +1,33 @@ +#ifndef APPS_TOOL_BOX_CONTROLLER_H +#define APPS_TOOL_BOX_CONTROLLER_H + +#include + +class ToolBoxController : public StackViewController, public ListViewDataSource { +public: + ToolBoxController(); + const char * title() const override; + bool handleEvent(Ion::Events::Event event) override; + void didBecomeFirstResponder() override; + int numberOfRows() override; + TableViewCell * reusableCell(int index, int type) override; + int reusableCellCount(int type) override; + void willDisplayCellForIndex(TableViewCell * cell, int index) override; + KDCoordinate rowHeight(int j) override; + KDCoordinate cumulatedHeightFromIndex(int j) override; + int indexFromCumulatedHeight(KDCoordinate offsetY) override; + int typeAtLocation(int i, int j) override; + void setTextFieldCaller(TextField * textField); +private: + constexpr static int k_numberOfCommandRows = 3; + constexpr static int k_numberOfMenuRows = 8; + constexpr static int k_maxNumberOfDisplayedRows = 6; + constexpr static KDCoordinate k_commandRowHeight = 50; + constexpr static KDCoordinate k_menuRowHeight = 40; + MenuListCell m_commandCells[k_numberOfCommandRows]; + MenuListCell m_menuCells[k_maxNumberOfDisplayedRows]; + SelectableTableView m_selectableTableView; + TextField * m_textFieldCaller; +}; + +#endif