diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index 82baed48a..9a4455315 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -33,7 +33,7 @@ ConsoleController::ConsoleController(Responder * parentResponder, App * pythonDe m_selectableTableView(this, this, this, this), m_editCell(this, pythonDelegate, this), m_scriptStore(scriptStore), - m_sandboxController(this, this), + m_sandboxController(this), m_inputRunLoopActive(false) #if EPSILON_GETOPT , m_locked(lockOnConsole) @@ -190,7 +190,7 @@ void ConsoleController::viewWillAppear() { } void ConsoleController::didBecomeFirstResponder() { - if (viewControllerIsDisplayed(nullptr)) { + if (!isDisplayingViewController()) { Container::activeApp()->setFirstResponder(&m_editCell); } else { /* A view controller might be displayed: for example, when pushing the @@ -198,7 +198,7 @@ void ConsoleController::didBecomeFirstResponder() { * 'viewWillAppear' and then we set the console as first responder. The * sandbox or the matplotlib controller might have been pushed in the * auto-import. */ - Container::activeApp()->setFirstResponder(m_displayedViewController); + Container::activeApp()->setFirstResponder(stackViewController()->topViewController()); } } @@ -348,7 +348,7 @@ bool ConsoleController::textFieldDidFinishEditing(TextField * textField, const c } telemetryReportEvent("Console", text); runAndPrintForCommand(text); - if (viewControllerIsDisplayed(nullptr)) { + if (!isDisplayingViewController()) { reloadData(true); } return true; @@ -377,14 +377,14 @@ bool ConsoleController::textFieldDidAbortEditing(TextField * textField) { } void ConsoleController::resetSandbox() { - if (!viewControllerIsDisplayed(sandbox())) { + if (stackViewController()->topViewController() != sandbox()) { return; } m_sandboxController.reset(); } void ConsoleController::displayViewController(ViewController * controller) { - if (m_displayedViewController == controller) { + if (stackViewController()->topViewController() == controller) { return; } hideAnyDisplayedViewController(); @@ -392,14 +392,23 @@ void ConsoleController::displayViewController(ViewController * controller) { } void ConsoleController::hideAnyDisplayedViewController() { - if (m_displayedViewController == nullptr) { + if (!isDisplayingViewController()) { return; } stackViewController()->pop(); } +bool ConsoleController::isDisplayingViewController() { + /* The StackViewController model state is the best way to know wether the + * console is displaying a View Controller (Sandbox or Matplotlib). Indeed, + * keeping a boolean or a pointer raises the issue of when updating it - when + * 'viewWillAppear' or when 'didEnterResponderChain' - in both cases, the + * state would be wrong at some point... */ + return stackViewController()->depth() > 2; +} + void ConsoleController::refreshPrintOutput() { - if (viewControllerIsDisplayed(nullptr) && reloadData(false)) { + if (!isDisplayingViewController() && reloadData(false)) { AppsContainer::sharedAppsContainer()->redrawWindow(); } } @@ -493,7 +502,7 @@ void ConsoleController::autoImportScript(Script script, bool force) { // Step 2 - Run the command runAndPrintForCommand(command); } - if (viewControllerIsDisplayed(nullptr) && force) { + if (!isDisplayingViewController() && force) { reloadData(true); } } diff --git a/apps/code/console_controller.h b/apps/code/console_controller.h index 45a7e5fb6..09192ce39 100644 --- a/apps/code/console_controller.h +++ b/apps/code/console_controller.h @@ -82,6 +82,7 @@ private: static constexpr int k_numberOfLineCells = (Ion::Display::Height - Metric::TitleBarHeight) / 14 + 2; // 14 = KDFont::SmallFont->glyphSize().height() // k_numberOfLineCells = (240 - 18)/14 ~ 15.9. The 0.1 cell can be above and below the 15 other cells so we add +2 cells. static constexpr int k_outputAccumulationBufferSize = 100; + bool isDisplayingViewController(); bool reloadData(bool isEditing); void flushOutputAccumulationBufferToStore(); void appendTextToOutputAccumulationBuffer(const char * text, size_t length); diff --git a/apps/code/sandbox_controller.cpp b/apps/code/sandbox_controller.cpp index 7ffc65f40..7282f0138 100644 --- a/apps/code/sandbox_controller.cpp +++ b/apps/code/sandbox_controller.cpp @@ -1,14 +1,16 @@ #include "sandbox_controller.h" #include +extern "C" { +#include +} + namespace Code { -SandboxController::SandboxController(Responder * parentResponder, MicroPython::ExecutionEnvironment * executionEnvironment) : +SandboxController::SandboxController(Responder * parentResponder) : ViewController(parentResponder), - ExecutionViewControllerHelper(executionEnvironment), m_solidColorView(KDColorWhite) { - assert(executionEnvironment != nullptr); } StackViewController * SandboxController::stackViewController() { @@ -21,10 +23,13 @@ void SandboxController::reset() { } void SandboxController::viewWillAppear() { - ExecutionViewControllerHelper::viewWillAppear(this); redrawWindow(); } +void SandboxController::viewDidDisappear() { + modturtle_view_did_disappear(); +} + 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 96b82c695..b5f52bc0f 100644 --- a/apps/code/sandbox_controller.h +++ b/apps/code/sandbox_controller.h @@ -9,16 +9,16 @@ namespace Code { -class SandboxController : public ViewController, public MicroPython::ExecutionViewControllerHelper { +class SandboxController : public ViewController { public: - SandboxController(Responder * parentResponder, MicroPython::ExecutionEnvironment * executionEnvironment); + SandboxController(Responder * parentResponder); StackViewController * stackViewController(); void reset(); // ViewController View * view() override { return &m_solidColorView; } void viewWillAppear() override; - void viewDidDisappear() override { MicroPython::ExecutionViewControllerHelper::viewDidDisappear(this); } + void viewDidDisappear() override; bool handleEvent(Ion::Events::Event event) override; ViewController::DisplayParameter displayParameter() override { return ViewController::DisplayParameter::WantsMaximumSpace; } diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 3ab8682bd..32f156387 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -28,7 +28,7 @@ static size_t extractAndValidatePlotInput(mp_obj_t x, mp_obj_t y, mp_obj_t ** xI mp_obj_t modpyplot___init__() { static Matplotlib::PlotStore plotStore; - static Matplotlib::PlotController plotController(&plotStore, MicroPython::ExecutionEnvironment::currentExecutionEnvironment()); + static Matplotlib::PlotController plotController(&plotStore); sPlotStore = &plotStore; sPlotController = &plotController; sPlotStore->flush(); diff --git a/python/port/mod/matplotlib/plot_controller.cpp b/python/port/mod/matplotlib/plot_controller.cpp index 8a5e329b3..eb7aa0abf 100644 --- a/python/port/mod/matplotlib/plot_controller.cpp +++ b/python/port/mod/matplotlib/plot_controller.cpp @@ -3,12 +3,10 @@ namespace Matplotlib { void PlotController::viewWillAppear() { - MicroPython::ExecutionViewControllerHelper::viewWillAppear(this); curveView()->reload(); } void PlotController::viewDidDisappear() { - MicroPython::ExecutionViewControllerHelper::viewDidDisappear(this); m_store->flush(); } diff --git a/python/port/mod/matplotlib/plot_controller.h b/python/port/mod/matplotlib/plot_controller.h index 7b4390edc..ed35622dc 100644 --- a/python/port/mod/matplotlib/plot_controller.h +++ b/python/port/mod/matplotlib/plot_controller.h @@ -2,15 +2,14 @@ #define PYTHON_MATPLOTLIB_PLOT_CONTROLLER_H #include -#include #include "plot_view.h" #include "plot_store.h" namespace Matplotlib { -class PlotController : public Shared::ZoomAndPanCurveViewController, public MicroPython::ExecutionViewControllerHelper { +class PlotController : public Shared::ZoomAndPanCurveViewController { public: - PlotController(PlotStore * store, MicroPython::ExecutionEnvironment * executiveEnvironment) : Shared::ZoomAndPanCurveViewController(nullptr), ExecutionViewControllerHelper(executiveEnvironment), m_store(store), m_view(m_store) {} + PlotController(PlotStore * store) : Shared::ZoomAndPanCurveViewController(nullptr), m_store(store), m_view(m_store) {} void viewWillAppear() override; void viewDidDisappear() override; diff --git a/python/port/port.cpp b/python/port/port.cpp index 1bbff1415..6e3bc83e3 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -95,23 +95,6 @@ void MicroPython::ExecutionEnvironment::interrupt() { mp_keyboard_interrupt(); } -void MicroPython::ExecutionEnvironment::viewControllerDidDisappear(ViewController * vc) { - if (vc == sandbox()) { - modturtle_view_did_disappear(); - } - 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" { extern const void * _stack_start; extern const void * _stack_end; diff --git a/python/port/port.h b/python/port/port.h index 70181e158..67eecd3c5 100644 --- a/python/port/port.h +++ b/python/port/port.h @@ -15,7 +15,7 @@ public: class ExecutionEnvironment { public: - ExecutionEnvironment() : m_displayedViewController(nullptr) {} + ExecutionEnvironment() {} static ExecutionEnvironment * currentExecutionEnvironment(); void runCode(const char * ); virtual const char * inputText(const char * prompt) { return nullptr; } @@ -28,24 +28,10 @@ public: // 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(); -protected: - 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: - ExecutionEnvironment * m_executionEnvironment; }; void init(void * heapStart, void * heapEnd);