Browse Source

affects_buttons now works when the stats have a different 'cmd' entry

Noah Vogt 2 months ago
parent
commit
e96add125d
2 changed files with 20 additions and 22 deletions
  1. 10 20
      VulcanBoard.py
  2. 10 2
      ui/button.py

+ 10 - 20
VulcanBoard.py

@@ -19,7 +19,6 @@
 import threading
 import sys
 import asyncio
-from functools import partial
 import colorama
 
 from kivy.app import App
@@ -80,8 +79,7 @@ class VulcanBoardApp(App):
                     defined_button = self.button_config_map.get((row, col))
                     if defined_button:
                         states = defined_button.get("states", [])
-                        state_id = [DEFAULT_STATE_ID]
-                        state = get_state_from_id(states, state_id[0])
+                        state = get_state_from_id(states, DEFAULT_STATE_ID)
 
                         btn = AutoResizeButton(
                             text=state.get("txt", ""),
@@ -94,21 +92,18 @@ class VulcanBoardApp(App):
                             halign="center",
                             valign="middle",
                             background_normal="",
+                            state_id=DEFAULT_STATE_ID,
                         )
 
                         if defined_button.get("autostart", False):
                             self.async_task(
-                                self.execute_command_async(
-                                    defined_button, state_id, btn
-                                )
+                                self.execute_command_async(defined_button, btn)
                             )
 
                         # pylint: disable=no-member
                         btn.bind(  # pyright: ignore
-                            on_release=lambda btn_instance, button=defined_button, state_id=state_id: self.async_task(
-                                self.execute_command_async(
-                                    button, state_id, btn_instance
-                                )
+                            on_release=lambda btn_instance, button=defined_button: self.async_task(
+                                self.execute_command_async(button, btn_instance)
                             )
                         )
 
@@ -143,14 +138,11 @@ class VulcanBoardApp(App):
         popup.dismiss()
         sys.exit(1)
 
-    async def execute_command_async(
-        self, button: dict, state_id: list[int], btn: AutoResizeButton
-    ):
+    async def execute_command_async(self, button: dict, btn: AutoResizeButton):
         follow_up_state_loop = True
         states = button["states"]
         while follow_up_state_loop:
-            new_state_id = get_state_id_from_exit_code(states, state_id[0])
-            state = get_state_from_id(states, new_state_id)
+            state = get_state_from_id(states, btn.state_id)
             follow_up_state_loop = False
 
             try:
@@ -169,7 +161,6 @@ class VulcanBoardApp(App):
                 ):
                     follow_up_state_loop = True
                     exit_code = follow_up_state
-                state_id[0] = exit_code  # pyright: ignore
 
                 Clock.schedule_once(
                     lambda _: self.update_button_feedback(
@@ -181,7 +172,6 @@ class VulcanBoardApp(App):
                     for affected_btn_dims in affects_buttons:
                         btn_pos = (affected_btn_dims[0], affected_btn_dims[1])
                         affected_button = self.button_grid[btn_pos]
-                        # TODO: check if also works if cmd is not the same for each state
                         Clock.schedule_once(
                             lambda _, btn_pos=btn_pos, affected_button=affected_button: self.update_button_feedback(
                                 self.button_config_map[btn_pos]["states"],
@@ -193,11 +183,11 @@ class VulcanBoardApp(App):
     def update_button_feedback(
         self, states: list, btn: AutoResizeButton, exit_code: int
     ):
-        state = get_state_from_id(
-            states, get_state_id_from_exit_code(states, exit_code)
-        )
+        state_id = get_state_id_from_exit_code(states, exit_code)
+        state = get_state_from_id(states, state_id)
 
         btn.text = state.get("txt", "")
+        btn.state_id = state_id
         btn.background_color = get_color_from_hex(
             state.get("bg_color", DEFAULT_BUTTON_BG_COLOR)
         )

+ 10 - 2
ui/button.py

@@ -13,15 +13,22 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from typing import override
+
 from kivy.uix.button import Button
-from kivy.properties import ListProperty  # pylint: disable=no-name-in-module
+from kivy.properties import (  # pylint: disable=no-name-in-module
+    ListProperty,
+    NumericProperty,
+)
 
 
 class AutoResizeButton(Button):
     """A button that adjusts its label font size dynamically."""
 
     original_pos = ListProperty([0, 0])
+    state_id = NumericProperty(0)
 
+    @override
     def __init__(self, **kwargs):
         super().__init__(**kwargs)
         self.dragged = False
@@ -30,7 +37,8 @@ class AutoResizeButton(Button):
             size=self.adjust_font_size, text=self.adjust_font_size
         )
 
-    def adjust_font_size(self, *args):
+    @override
+    def adjust_font_size(self, *_):
         """Dynamically adjusts font size to fit the button's bounds."""
         if not self.text:
             return