From 44b0c6f129fc1806c263d478df0cb4884c55dc7a Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <dos@dosowisko.net>
Date: Sat, 30 Sep 2023 05:53:34 +0200
Subject: [PATCH] python_payload: Clean up application's input handling during
 transitions

Apps don't have to check whether they're active anymore, unless they
explicitly ask to receive think calls after on_exit.

Make gr33nhouse ask for it, as it animates its background. Remove
is_active guards from other apps (and clean up some related codepaths
while at that too).
---
 python_payload/apps/demo_worms/__init__.py     |  4 +---
 python_payload/apps/gr33nhouse/__init__.py     | 14 ++++++--------
 python_payload/apps/gr33nhouse/applist.py      |  9 ++++++++-
 python_payload/apps/gr33nhouse/confirmation.py | 13 ++++---------
 python_payload/apps/gr33nhouse/manual.py       |  6 ++++--
 python_payload/apps/gr33nhouse/record.py       |  6 ------
 python_payload/apps/otamatone/__init__.py      |  3 ---
 python_payload/apps/w1f1/__init__.py           |  3 ---
 python_payload/apps/w1f1/k3yboard.py           |  2 +-
 9 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/python_payload/apps/demo_worms/__init__.py b/python_payload/apps/demo_worms/__init__.py
index d256ff708c..e4029bfee0 100644
--- a/python_payload/apps/demo_worms/__init__.py
+++ b/python_payload/apps/demo_worms/__init__.py
@@ -30,7 +30,6 @@ class AppWorms(Application):
     def on_enter(self, vm: Optional[ViewManager]) -> None:
         super().on_enter(vm)
         self.just_shown = False
-        self._bg = True
         self.worms = []  # reset worms
 
     def draw_background(self, ctx):
@@ -41,9 +40,8 @@ class AppWorms(Application):
         ctx.move_to(0, 0).rgb(*WHITE).text("touch me :)")
 
     def draw(self, ctx: Context) -> None:
-        if self._bg or (self.vm.transitioning and self.is_active()):
+        if self.vm.transitioning and self.is_active():
             self.draw_background(ctx)
-            self._bg = False
         else:
             for w in self.worms:
                 w.draw(ctx)
diff --git a/python_payload/apps/gr33nhouse/__init__.py b/python_payload/apps/gr33nhouse/__init__.py
index eec16b918f..ad5ac12761 100644
--- a/python_payload/apps/gr33nhouse/__init__.py
+++ b/python_payload/apps/gr33nhouse/__init__.py
@@ -33,11 +33,9 @@ class Gr33nhouseApp(Application):
         self.state = ViewState.CONTENT
         self.wifi_status = None
 
-    def on_enter(self, vm: ViewManager | None) -> None:
-        super().on_enter(vm)
-
-        if self.vm is None:
-            raise RuntimeError("vm is None")
+    def on_exit(self) -> bool:
+        # request thinks after on_exit
+        return True
 
     def draw(self, ctx: Context) -> None:
         if self.state == ViewState.NO_INTERNET:
@@ -106,9 +104,6 @@ class Gr33nhouseApp(Application):
         super().think(ins, delta_ms)
         self._sc.think(ins, delta_ms)
 
-        if self.vm is None:
-            raise RuntimeError("vm is None")
-
         if not network.WLAN(network.STA_IF).isconnected():
             self.state = ViewState.NO_INTERNET
             self.wifi_status = network.WLAN(network.STA_IF).status()
@@ -118,6 +113,9 @@ class Gr33nhouseApp(Application):
 
         self.background.think(ins, delta_ms)
 
+        if not self.is_active():
+            return
+
         if self.input.buttons.app.left.pressed or self.input.buttons.app.left.repeated:
             self._sc.scroll_left()
         elif (
diff --git a/python_payload/apps/gr33nhouse/applist.py b/python_payload/apps/gr33nhouse/applist.py
index 520f67e5cb..decbea03da 100644
--- a/python_payload/apps/gr33nhouse/applist.py
+++ b/python_payload/apps/gr33nhouse/applist.py
@@ -31,6 +31,10 @@ class AppList(BaseView):
         self.background = Flow3rView()
         self._sc = ScrollController()
 
+    def on_exit(self) -> bool:
+        # request thinks after on_exit
+        return True
+
     def draw(self, ctx: Context) -> None:
         ctx.move_to(0, 0)
 
@@ -117,10 +121,13 @@ class AppList(BaseView):
     def think(self, ins: InputState, delta_ms: int) -> None:
         super().think(ins, delta_ms)
         self._sc.think(ins, delta_ms)
-        if self.is_active() and self.vm.transitioning:
+
+        if self.is_active():
             return
 
         if self._state == ViewState.INITIAL:
+            if self.vm.transitioning:
+                return
             try:
                 self._state = ViewState.LOADING
                 print("Loading app list...")
diff --git a/python_payload/apps/gr33nhouse/confirmation.py b/python_payload/apps/gr33nhouse/confirmation.py
index fbf1845728..6832857653 100644
--- a/python_payload/apps/gr33nhouse/confirmation.py
+++ b/python_payload/apps/gr33nhouse/confirmation.py
@@ -21,11 +21,9 @@ class ConfirmationView(BaseView):
         self.name = name
         self.author = author
 
-    def on_enter(self, vm: ViewManager | None) -> None:
-        super().on_enter(vm)
-
-        if self.vm is None:
-            raise RuntimeError("vm is None")
+    def on_exit(self) -> bool:
+        # request thinks after on_exit
+        return True
 
     def draw(self, ctx: Context) -> None:
         ctx.move_to(0, 0)
@@ -72,10 +70,7 @@ class ConfirmationView(BaseView):
         super().think(ins, delta_ms)
         self.background.think(ins, delta_ms)
 
-        if self.vm is None:
-            raise RuntimeError("vm is None")
-
-        if self.input.buttons.app.middle.pressed:
+        if self.is_active() and self.input.buttons.app.middle.pressed:
             self.vm.replace(
                 DownloadView(
                     url=self.url,
diff --git a/python_payload/apps/gr33nhouse/manual.py b/python_payload/apps/gr33nhouse/manual.py
index 16960b7ab1..44e9c84f95 100644
--- a/python_payload/apps/gr33nhouse/manual.py
+++ b/python_payload/apps/gr33nhouse/manual.py
@@ -46,8 +46,10 @@ class ManualInputView(BaseView):
         super().on_enter(vm)
         self.flow3r_seed = ""
         self.state = ViewState.ENTER_SEED
-        if self.vm is None:
-            raise RuntimeError("vm is None")
+
+    def on_exit(self) -> bool:
+        # request thinks after on_exit
+        return True
 
     def draw(self, ctx: Context) -> None:
         self.background.draw(ctx)
diff --git a/python_payload/apps/gr33nhouse/record.py b/python_payload/apps/gr33nhouse/record.py
index 6fb6d6abcf..0fa59d39c1 100644
--- a/python_payload/apps/gr33nhouse/record.py
+++ b/python_payload/apps/gr33nhouse/record.py
@@ -10,12 +10,6 @@ class RecordView(BaseView):
         super().__init__()
         self.background = Flow3rView()
 
-    def on_enter(self, vm: ViewManager | None) -> None:
-        super().on_enter(vm)
-
-        if self.vm is None:
-            raise RuntimeError("vm is None")
-
     def draw(self, ctx: Context) -> None:
         ctx.move_to(0, 0)
         ctx.save()
diff --git a/python_payload/apps/otamatone/__init__.py b/python_payload/apps/otamatone/__init__.py
index 8f5dbe3b5f..610e8bfce8 100644
--- a/python_payload/apps/otamatone/__init__.py
+++ b/python_payload/apps/otamatone/__init__.py
@@ -170,9 +170,6 @@ class Otamatone(Application):
         self._ts += delta_ms
         self._blob.think(ins, delta_ms)
 
-        if not self.is_active():
-            return
-
         petal = self.input.captouch.petals[self.PETAL_NO]
         pos = ins.captouch.petals[self.PETAL_NO].position
         ctrl = pos[0] / 40000
diff --git a/python_payload/apps/w1f1/__init__.py b/python_payload/apps/w1f1/__init__.py
index b292b2e09e..252f3a1d3c 100644
--- a/python_payload/apps/w1f1/__init__.py
+++ b/python_payload/apps/w1f1/__init__.py
@@ -253,9 +253,6 @@ class WifiApp(Application):
         super().think(ins, delta_ms)
         self._scroll_pos += delta_ms / 1000
 
-        if not self.is_active():
-            return
-
         leds.set_all_rgb(0, 0, 0)
 
         if self.input.buttons.app.left.pressed and self._wlan_offset > 0:
diff --git a/python_payload/apps/w1f1/k3yboard.py b/python_payload/apps/w1f1/k3yboard.py
index 2fb478e757..c5333d1b78 100644
--- a/python_payload/apps/w1f1/k3yboard.py
+++ b/python_payload/apps/w1f1/k3yboard.py
@@ -540,7 +540,7 @@ class KeyboardDemoApp(Application):
     def think(self, ins: InputState, delta_ms: int) -> None:
         super().think(ins, delta_ms)  # Let Application do its thing
 
-        if self.is_active() and self.input.buttons.app.middle.pressed:
+        if self.input.buttons.app.middle.pressed:
             self.vm.push(KeyboardView(self._model))
 
 
-- 
GitLab