[escher] Makes input view controller inherits from modal view controller

Change-Id: I24e06c247401702fe53931bc1091a327e0c93171
This commit is contained in:
Émilie Feral
2016-11-03 15:23:03 +01:00
parent 2f42476604
commit 2114961b9c
2 changed files with 26 additions and 93 deletions

View File

@@ -2,81 +2,31 @@
#include <escher/app.h>
#include <assert.h>
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();
}