From 659da1dff8c78d422a6aeeb6bf4e07341949bddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 20 Mar 2020 13:38:27 +0100 Subject: [PATCH] [apps][python] ExecutionEnvironment handles hide sand display of sandbox and plot controller the same way --- apps/code/console_controller.cpp | 68 +++++++++----------- apps/code/console_controller.h | 6 +- apps/code/sandbox_controller.cpp | 16 +---- apps/code/sandbox_controller.h | 6 +- python/port/mod/matplotlib/modpyplot.cpp | 2 +- python/port/mod/matplotlib/plot_controller.h | 9 ++- python/port/port.cpp | 16 ++++- python/port/port.h | 29 +++++++-- 8 files changed, 81 insertions(+), 71 deletions(-) diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index 7ab437a64..3a0d62dbf 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -104,9 +104,7 @@ const char * ConsoleController::inputText(const char * prompt) { m_inputRunLoopActive = true; // Hide the sandbox if it is displayed - if (sandboxIsDisplayed()) { - hideSandbox(); - } + hideAnyDisplayedViewController(); const char * promptText = prompt; char * s = const_cast(prompt); @@ -180,10 +178,6 @@ const char * ConsoleController::inputText(const char * prompt) { return text; } -void ConsoleController::displayViewController(ViewController * controller) { - stackViewController()->push(controller); -} - void ConsoleController::viewWillAppear() { ViewController::viewWillAppear(); loadPythonEnvironment(); @@ -192,12 +186,12 @@ void ConsoleController::viewWillAppear() { autoImport(); } - Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME + //Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME m_selectableTableView.reloadData(); m_selectableTableView.selectCellAtLocation(0, m_consoleStore.numberOfLines()); m_editCell.setEditing(true); m_editCell.setText(""); - Container::activeApp()->setFirstResponder(firstResponder); // FIXME + //Container::activeApp()->setFirstResponder(firstResponder); // FIXME } void ConsoleController::didBecomeFirstResponder() { @@ -350,14 +344,15 @@ bool ConsoleController::textFieldDidFinishEditing(TextField * textField, const c } telemetryReportEvent("Console", text); runAndPrintForCommand(text); - Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME - if (!sandboxIsDisplayed()) { + //Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME + if (viewControllerIsDisplayed(nullptr)) { + // TODO factorize m_selectableTableView.reloadData(); m_editCell.setEditing(true); textField->setText(""); m_selectableTableView.selectCellAtLocation(0, m_consoleStore.numberOfLines()); } - Container::activeApp()->setFirstResponder(firstResponder); // FIXME + //Container::activeApp()->setFirstResponder(firstResponder); // FIXME return true; } @@ -383,29 +378,29 @@ bool ConsoleController::textFieldDidAbortEditing(TextField * textField) { return true; } -void ConsoleController::displaySandbox() { - if (sandboxIsDisplayed()) { - return; - } - stackViewController()->push(&m_sandboxController); -} - -void ConsoleController::hideSandbox() { - if (!sandboxIsDisplayed()) { - return; - } - m_sandboxController.hide(); -} - void ConsoleController::resetSandbox() { - if (!sandboxIsDisplayed()) { + if (!viewControllerIsDisplayed(sandbox())) { return; } m_sandboxController.reset(); } +void ConsoleController::displayViewController(ViewController * controller) { + if (m_displayedViewController == controller) { + return; + } + stackViewController()->push(controller); +} + +void ConsoleController::hideAnyDisplayedViewController() { + if (m_displayedViewController == nullptr) { + return; + } + stackViewController()->pop(); +} + void ConsoleController::refreshPrintOutput() { - if (sandboxIsDisplayed()) { + if (!viewControllerIsDisplayed(nullptr)) { // Displaying a controller return; } m_selectableTableView.reloadData(); @@ -461,12 +456,11 @@ void ConsoleController::printText(const char * text, size_t length) { } void ConsoleController::autoImportScript(Script script, bool force) { - if (sandboxIsDisplayed()) { - /* The sandbox might be displayed, for instance if we are auto-importing - * several scripts that draw at importation. In this case, we want to remove - * the sandbox. */ - hideSandbox(); - } + /* The sandbox might be displayed, for instance if we are auto-importing + * several scripts that draw at importation. In this case, we want to remove + * the sandbox. */ + hideAnyDisplayedViewController(); + if (script.importationStatus() || force) { // Step 1 - Create the command "from scriptName import *". @@ -492,13 +486,13 @@ void ConsoleController::autoImportScript(Script script, bool force) { // Step 2 - Run the command runAndPrintForCommand(command); } - if (!sandboxIsDisplayed() && force) { - Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME + if (viewControllerIsDisplayed(nullptr) && force) { + //Responder * firstResponder = Container::activeApp()->firstResponder(); // FIXME m_selectableTableView.reloadData(); m_selectableTableView.selectCellAtLocation(0, m_consoleStore.numberOfLines()); m_editCell.setEditing(true); m_editCell.setText(""); - Container::activeApp()->setFirstResponder(firstResponder); // FIXME + //Container::activeApp()->setFirstResponder(firstResponder); // FIXME } } diff --git a/apps/code/console_controller.h b/apps/code/console_controller.h index d3874a9ba..54caed4bb 100644 --- a/apps/code/console_controller.h +++ b/apps/code/console_controller.h @@ -60,13 +60,13 @@ public: bool textFieldDidAbortEditing(TextField * textField) override; // MicroPython::ExecutionEnvironment - void displaySandbox() override; - void hideSandbox() override; + ViewController * sandbox() override { return &m_sandboxController; } void resetSandbox() override; + void displayViewController(ViewController * controller) override; + void hideAnyDisplayedViewController() override; void refreshPrintOutput() override; void printText(const char * text, size_t length) override; const char * inputText(const char * prompt) override; - void displayViewController(ViewController * controller) override; #if EPSILON_GETOPT bool locked() const { diff --git a/apps/code/sandbox_controller.cpp b/apps/code/sandbox_controller.cpp index 5b056c53b..7ffc65f40 100644 --- a/apps/code/sandbox_controller.cpp +++ b/apps/code/sandbox_controller.cpp @@ -5,8 +5,8 @@ namespace Code { SandboxController::SandboxController(Responder * parentResponder, MicroPython::ExecutionEnvironment * executionEnvironment) : ViewController(parentResponder), - m_solidColorView(KDColorWhite), - m_executionEnvironment(executionEnvironment) + ExecutionViewControllerHelper(executionEnvironment), + m_solidColorView(KDColorWhite) { assert(executionEnvironment != nullptr); } @@ -20,21 +20,11 @@ void SandboxController::reset() { redrawWindow(); } -void SandboxController::hide() { - stackViewController()->pop(); -} - void SandboxController::viewWillAppear() { - assert(m_executionEnvironment != nullptr); - m_executionEnvironment->setSandboxIsDisplayed(true); + ExecutionViewControllerHelper::viewWillAppear(this); redrawWindow(); } -void SandboxController::viewDidDisappear() { - assert(m_executionEnvironment != nullptr); - m_executionEnvironment->setSandboxIsDisplayed(false); -} - bool SandboxController::handleEvent(Ion::Events::Event event) { // The sandbox handles or "absorbs" all keyboard events except Home and OnOff if (event == Ion::Events::Home || event == Ion::Events::OnOff) { diff --git a/apps/code/sandbox_controller.h b/apps/code/sandbox_controller.h index c50725789..96b82c695 100644 --- a/apps/code/sandbox_controller.h +++ b/apps/code/sandbox_controller.h @@ -9,24 +9,22 @@ namespace Code { -class SandboxController : public ViewController { +class SandboxController : public ViewController, public MicroPython::ExecutionViewControllerHelper { public: SandboxController(Responder * parentResponder, MicroPython::ExecutionEnvironment * executionEnvironment); StackViewController * stackViewController(); void reset(); - void hide(); // ViewController View * view() override { return &m_solidColorView; } void viewWillAppear() override; - void viewDidDisappear() override; + void viewDidDisappear() override { MicroPython::ExecutionViewControllerHelper::viewDidDisappear(this); } bool handleEvent(Ion::Events::Event event) override; ViewController::DisplayParameter displayParameter() override { return ViewController::DisplayParameter::WantsMaximumSpace; } private: void redrawWindow(); SolidColorView m_solidColorView; - MicroPython::ExecutionEnvironment * m_executionEnvironment; }; } diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 5c8f654ff..2310bbd58 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -28,7 +28,7 @@ static size_t extract_and_validate_plot_input(mp_obj_t x, mp_obj_t y, mp_obj_t * mp_obj_t modpyplot___init__() { static Matplotlib::PlotStore plotStore; - static Matplotlib::PlotController plotController(&plotStore); + static Matplotlib::PlotController plotController(&plotStore, MicroPython::ExecutionEnvironment::currentExecutionEnvironment()); sPlotStore = &plotStore; sPlotController = &plotController; sPlotStore->flush(); diff --git a/python/port/mod/matplotlib/plot_controller.h b/python/port/mod/matplotlib/plot_controller.h index 604da207f..52dcda31c 100644 --- a/python/port/mod/matplotlib/plot_controller.h +++ b/python/port/mod/matplotlib/plot_controller.h @@ -3,14 +3,18 @@ #include #include +#include #include "plot_view.h" #include "plot_store.h" namespace Matplotlib { -class PlotController : public Shared::SimpleInteractiveCurveViewController { +class PlotController : public Shared::SimpleInteractiveCurveViewController, public MicroPython::ExecutionViewControllerHelper { public: - PlotController(PlotStore * store) : Shared::SimpleInteractiveCurveViewController(nullptr, &m_cursor), m_store(store), m_view(m_store) {} + PlotController(PlotStore * store, MicroPython::ExecutionEnvironment * executiveEnvironment) : Shared::SimpleInteractiveCurveViewController(nullptr, &m_cursor), ExecutionViewControllerHelper(executiveEnvironment), m_store(store), m_view(m_store) {} + + void viewWillAppear() override { MicroPython::ExecutionViewControllerHelper::viewWillAppear(this); } + void viewDidDisappear() override { MicroPython::ExecutionViewControllerHelper::viewDidDisappear(this); } float cursorBottomMarginRatio() override { return 0.0f; @@ -33,5 +37,4 @@ private: } - #endif diff --git a/python/port/port.cpp b/python/port/port.cpp index d727332a4..1bbff1415 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -95,11 +95,21 @@ void MicroPython::ExecutionEnvironment::interrupt() { mp_keyboard_interrupt(); } -void MicroPython::ExecutionEnvironment::setSandboxIsDisplayed(bool display) { - if (m_sandboxIsDisplayed && !display) { +void MicroPython::ExecutionEnvironment::viewControllerDidDisappear(ViewController * vc) { + if (vc == sandbox()) { modturtle_view_did_disappear(); } - m_sandboxIsDisplayed = display; + m_displayedViewController = nullptr; +} + +void MicroPython::ExecutionViewControllerHelper::viewWillAppear(ViewController * vc) { + assert(m_executionEnvironment != nullptr); + m_executionEnvironment->viewControllerWillAppear(vc); +} + +void MicroPython::ExecutionViewControllerHelper::viewDidDisappear(ViewController * vc) { + assert(m_executionEnvironment != nullptr); + m_executionEnvironment->viewControllerDidDisappear(vc); } extern "C" { diff --git a/python/port/port.h b/python/port/port.h index 55441e2fb..70181e158 100644 --- a/python/port/port.h +++ b/python/port/port.h @@ -15,22 +15,37 @@ public: class ExecutionEnvironment { public: - ExecutionEnvironment() : m_sandboxIsDisplayed(false) {} + ExecutionEnvironment() : m_displayedViewController(nullptr) {} static ExecutionEnvironment * currentExecutionEnvironment(); void runCode(const char * ); virtual const char * inputText(const char * prompt) { return nullptr; } - virtual void displaySandbox() {} - virtual void hideSandbox() {} + + // Sandbox + void displaySandbox() { displayViewController(sandbox()); } + virtual ViewController * sandbox() { return nullptr; } virtual void resetSandbox() {} + + // Generic View Controller virtual void displayViewController(ViewController * controller) {} + virtual void hideAnyDisplayedViewController() {} + void viewControllerWillAppear(ViewController * vc) { m_displayedViewController = vc; } + void viewControllerDidDisappear(ViewController * vc); + virtual void printText(const char * text, size_t length) {} virtual void refreshPrintOutput() {} void interrupt(); - void setSandboxIsDisplayed(bool display); protected: - bool sandboxIsDisplayed() const { return m_sandboxIsDisplayed; } + bool viewControllerIsDisplayed(ViewController * vc) const { return m_displayedViewController == vc; } + ViewController * m_displayedViewController; +}; + +class ExecutionViewControllerHelper { +public: + ExecutionViewControllerHelper(ExecutionEnvironment * executionEnvironment) : m_executionEnvironment(executionEnvironment) {} + void viewWillAppear(ViewController * vc); + void viewDidDisappear(ViewController * vc); private: - bool m_sandboxIsDisplayed; + ExecutionEnvironment * m_executionEnvironment; }; void init(void * heapStart, void * heapEnd); @@ -38,6 +53,6 @@ void deinit(); void registerScriptProvider(ScriptProvider * s); void collectRootsAtAddress(char * address, int len); -}; +} #endif