diff --git a/escher/include/escher/input_view_controller.h b/escher/include/escher/input_view_controller.h index 27ccbf3bd..b624ef3c0 100644 --- a/escher/include/escher/input_view_controller.h +++ b/escher/include/escher/input_view_controller.h @@ -1,43 +1,32 @@ #ifndef ESCHER_INPUT_VIEW_CONTROLLER_H #define ESCHER_INPUT_VIEW_CONTROLLER_H -#include +#include #include #include #include -class InputViewController : public ViewController { +class InputViewController : public ModalViewController { public: InputViewController(Responder * parentResponder, ViewController * child, TextFieldDelegate * textFieldDelegate = nullptr); - View * view() override; const char * title() const override; - bool handleEvent(Ion::Events::Event event) override; void edit(Responder * caller, const char * initialContent, void * context, Invocation::Action successAction, Invocation::Action failureAction); const char * textBody(); private: - class ContentView : public View { + class TextFieldController : public ViewController { public: - ContentView(TextFieldDelegate * textFieldDelegate); - void setMainView(View * subview); - int numberOfSubviews() const override; - View * subviewAtIndex(int index) override; - void layoutSubviews() override; - void setVisibleInput(bool showInput); - bool visibleInput(); + TextFieldController(Responder * parentResponder, TextFieldDelegate * textFieldDelegate); + void didBecomeFirstResponder() override; + View * view() override; TextField * textField(); private: - static constexpr KDCoordinate k_textFieldHeight = 20; - View * m_mainView; TextField m_textField; - bool m_visibleInput; char m_textBody[255]; }; - void showInput(bool show); + void showInput(); void setTextBody(const char * text); - ContentView m_contentView; - Responder * m_previousResponder; - ViewController * m_regularViewController; + TextFieldController m_textFieldController; Invocation m_successAction; Invocation m_failureAction; }; diff --git a/escher/src/input_view_controller.cpp b/escher/src/input_view_controller.cpp index 5e817f657..f312ba17f 100644 --- a/escher/src/input_view_controller.cpp +++ b/escher/src/input_view_controller.cpp @@ -2,81 +2,31 @@ #include #include -InputViewController::ContentView::ContentView(TextFieldDelegate * textFieldDelegate) : - View(), - m_mainView(nullptr), - m_textField(nullptr, m_textBody, 255, textFieldDelegate), - m_visibleInput(false) +InputViewController::TextFieldController::TextFieldController(Responder * parentResponder, TextFieldDelegate * textFieldDelegate) : + ViewController(parentResponder), + m_textField(parentResponder, m_textBody, 255, textFieldDelegate) { m_textBody[0] = 0; } -void InputViewController::ContentView::setMainView(View * subview) { - m_mainView = subview; +View * InputViewController::TextFieldController::view() { + return &m_textField; } -int InputViewController::ContentView::numberOfSubviews() const { - if (m_visibleInput) { - return 2; - } - return 1; +void InputViewController::TextFieldController::didBecomeFirstResponder() { + app()->setFirstResponder(&m_textField); } -View * InputViewController::ContentView::subviewAtIndex(int index) { - switch (index) { - case 0: - return m_mainView; - case 1: - if (numberOfSubviews() == 2) { - return &m_textField; - } else { - assert(false); - return nullptr; - } - default: - assert(false); - return nullptr; - } -} - -void InputViewController::ContentView::layoutSubviews() { - m_mainView->setFrame(bounds()); - if (numberOfSubviews() == 2) { - KDRect inputView(0, bounds().height() - k_textFieldHeight, bounds().width(), k_textFieldHeight); - m_textField.setFrame(inputView); - } -} - -void InputViewController::ContentView::setVisibleInput(bool showInput) { - m_visibleInput = showInput; - markRectAsDirty(KDRect(0, bounds().height() - k_textFieldHeight, bounds().width(), k_textFieldHeight)); - layoutSubviews(); -} - -bool InputViewController::ContentView::visibleInput() { - return m_visibleInput; -} - -TextField * InputViewController::ContentView::textField() { +TextField * InputViewController::TextFieldController::textField() { return &m_textField; } InputViewController::InputViewController(Responder * parentResponder, ViewController * child, TextFieldDelegate * textFieldDelegate) : - ViewController(parentResponder), - m_contentView(textFieldDelegate), - m_previousResponder(nullptr), - m_regularViewController(child), + ModalViewController(parentResponder, child), + m_textFieldController(TextFieldController(this, textFieldDelegate)), m_successAction(Invocation(nullptr, nullptr)), m_failureAction(Invocation(nullptr, nullptr)) { - m_contentView.textField()->setParentResponder(this); -} - -View * InputViewController::view() { - if (m_contentView.subviewAtIndex(0) == nullptr) { - m_contentView.setMainView(m_regularViewController->view()); - } - return &m_contentView; } const char * InputViewController::title() const { @@ -84,35 +34,29 @@ const char * InputViewController::title() const { } const char * InputViewController::textBody() { - return m_contentView.textField()->textBuffer(); + return m_textFieldController.textField()->textBuffer(); } -void InputViewController::showInput(bool show) { - m_contentView.setVisibleInput(show); - if (show) { - m_previousResponder = app()->firstResponder(); - app()->setFirstResponder(m_contentView.textField()); - } else { - app()->setFirstResponder(m_previousResponder); - } +void InputViewController::showInput() { + displayModalViewController(&m_textFieldController, 1.0f, 1.0f); } void InputViewController::setTextBody(const char * text) { - m_contentView.textField()->setTextBuffer(text); + m_textFieldController.textField()->setTextBuffer(text); } bool InputViewController::handleEvent(Ion::Events::Event event) { - if (!m_contentView.visibleInput()) { + if (!isDisplayingModal()) { return false; } switch (event) { case Ion::Events::Event::ENTER: m_successAction.perform(this); - showInput(false); + dismissModalViewController(); return true; case Ion::Events::Event::ESC: m_failureAction.perform(this); - showInput(false); + dismissModalViewController(); return true; default: return false; @@ -123,5 +67,5 @@ void InputViewController::edit(Responder * caller, const char * initialContent, m_successAction = Invocation(successAction, context); m_failureAction = Invocation(failureAction, context); setTextBody(initialContent); - showInput(true); + showInput(); }