]> git.mar77i.info Git - zenbook_gui/commitdiff
various cleanups, draw message window background as a separate child
authormar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 01:33:25 +0000 (02:33 +0100)
committermar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 01:42:03 +0000 (02:42 +0100)
ui/base.py
ui/message_box.py
ui/modal.py
ui/rect.py [new file with mode: 0644]
ui/scroll.py
ui/tab_bar.py

index 91d4b5f2ff43a6b2f084df884aaa0aece1767399..117837f0876e80d34ccb10db282d4f1b641bfc20 100644 (file)
@@ -70,12 +70,8 @@ class Parent(EventMethodDispatcher):
 class Child(EventMethodDispatcher):
     def __init__(self, parent):
         self._parent = parent
-        if parent is None:
-            return
-        for parent in (parent, getattr(parent, "parent", None)):
-            if isinstance(parent, Parent):
-                parent.children.append(self)
-                break
+        if parent is not None:
+            parent.children.append(self)
 
     @property
     def parent(self):
@@ -112,7 +108,7 @@ class Child(EventMethodDispatcher):
     def font(self):
         return self.root.font
 
-    @cached_property
+    @property
     def surf(self):
         parent = self.parent
         while not hasattr(parent, "surf"):
index 5b481b2f45f5a2cd80022cf7767dcdc69f2c453b..e514732c398ba78b3be0084f1a69bf9071ce0a32 100644 (file)
@@ -3,12 +3,14 @@ import pygame
 from .button import Button
 from .label import Label
 from .modal import Modal
+from .rect import Rect
 
 
 class MessageBox(Modal):
     def __init__(self, parent, rect, message):
         super().__init__(parent)
         self.rect = rect
+        Rect(self, self.rect, "black", "gray")
         self.label = Label(self, pygame.Rect(rect.center, (10, 10)), "")
         self.add_buttons()
         self.message = message
@@ -25,11 +27,6 @@ class MessageBox(Modal):
             self.deactivate,
         )
 
-    def draw(self):
-        pygame.draw.rect(self.surf, "black", self.rect)
-        pygame.draw.rect(self.surf, "gray", self.rect, 1)
-        super().draw()
-
     @property
     def message(self):
         return self.label.value
index f92e3c89f204207266446935a11a91f97eb609a2..e9115a5f634732366581b92fdfd0cebcc5e5c7ed 100644 (file)
@@ -7,7 +7,7 @@ from .focus import FocusableMixin
 class Modal(FocusableMixin, Parent, Child):
     def __init__(self, parent):
         super().__init__(parent)
-        self.draw = self._check_active(self._wrap_draw(self.draw))
+        self.draw = self._check_active(self.draw)
         self.update = self._check_active(self.update)
         self.handle_event = self._check_active(self.handle_event)
         self.activate = self._check_active(self.activate, False)
@@ -20,14 +20,6 @@ class Modal(FocusableMixin, Parent, Child):
             return None
         return inner
 
-    def _wrap_draw(self, method):
-        def inner():
-            tintsurf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA, 32)
-            tintsurf.fill(pygame.Color(0x80))
-            self.surf.blit(tintsurf, (0, 0))
-            return method()
-        return inner
-
     def activate(self):
         super().activate()
         self.root.stop_event = True
@@ -39,4 +31,7 @@ class Modal(FocusableMixin, Parent, Child):
         self.dirty = True
 
     def draw(self):
+        tintsurf = pygame.Surface(self.surf.get_size(), pygame.SRCALPHA, 32)
+        tintsurf.fill(pygame.Color(0x80))
+        self.surf.blit(tintsurf, (0, 0))
         super().draw()
diff --git a/ui/rect.py b/ui/rect.py
new file mode 100644 (file)
index 0000000..863eb00
--- /dev/null
@@ -0,0 +1,17 @@
+import pygame
+
+from .base import Child
+
+
+class Rect(Child):
+    def __init__(self, parent, rect, background_color, border_color):
+        super().__init__(parent)
+        self.rect = rect
+        self.background_color = background_color
+        self.border_color = border_color
+
+    def draw(self):
+        if self.background_color:
+            pygame.draw.rect(self.surf, self.background_color, self.rect)
+        if self.border_color:
+            pygame.draw.rect(self.surf, self.border_color, self.rect, 1)
index 292b689a5ac30337277a2a5f9a957652bfdc3d62..577021aa14196012598079f018b44e37536f1847 100644 (file)
@@ -7,13 +7,17 @@ class Scroll(Parent, Child):
     def __init__(self, parent, rect, surf_size):
         super().__init__(parent)
         self.rect = rect
-        self.__dict__["surf"] = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
+        self._surf = pygame.Surface(surf_size, pygame.SRCALPHA, 32)
         self.scroll_x = 0
         self.scroll_y = 0
 
+    @property
+    def surf(self):
+        return self._surf
+
     def get_subsurf(self):
-        size = self.surf.get_size()
-        return self.surf.subsurface(
+        size = self._surf.get_size()
+        return self._surf.subsurface(
             pygame.Rect(
                 (self.scroll_x, self.scroll_y),
                 (
index 7ff721d47dde4da230cb15cd1650959c183df7d8..5d44e2e5da1d34908c069bf29cf8a58a0a5ff209 100644 (file)
@@ -10,14 +10,9 @@ class TabBar(Parent, Child):
     def __init__(self, parent, rect, labels, groups, active):
         super().__init__(parent)
         self.labels = labels
-        # ...why do we have to reparent these items?
-        for group in groups:
-            for item in group:
-                item.parent = self
         self.groups = groups
         self.active = active
         num_labels = len(labels)
-        self.children.clear()
         self.buttons = [
             Button(
                 self,
@@ -31,18 +26,21 @@ class TabBar(Parent, Child):
             )
             for i in range(num_labels)
         ]
-        self.children.extend(self.groups[active])
+        self.update_children()
 
-    def update_children(self, i):
-        if self.active == i:
-            return
-        self.active = i
+    def update_children(self, i=None):
+        if i is not None:
+            if self.active == i:
+                return
+            self.active = i
         for i, (button, group) in enumerate(zip(self.buttons, self.groups)):
             is_group_active = i == self.active
             if button.is_active != is_group_active:
                 button.is_active = is_group_active
                 self.dirty = True
             for item in group:
+                if item.parent is not self:
+                    item.parent = self
                 is_child_active = item in self.children
                 if is_group_active == is_child_active:
                     continue