Skip to content

Commit

Permalink
Update button display logic
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel committed Jun 15, 2024
1 parent b5761fa commit d860941
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions modules/app_components/layout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from events.input import BUTTON_TYPES
from events.input import BUTTON_TYPES, ButtonDownEvent
from . import tokens, utils

from system.eventbus import eventbus

class Layoutable:
height: int # Available after draw
Expand Down Expand Up @@ -44,10 +45,16 @@ def draw(self, ctx, focused=False):


class ButtonDisplay(Layoutable):
def __init__(self, text, font_size=None, rgb=None, button_handler=None):
def __init__(self, text, app, font_size=None, rgb=None, button_handler=None):
self.text = text
self.height = 60
self.button_handler = button_handler
self.app = app

eventbus.on(ButtonDownEvent, self._handle_buttondown, self.app)

def _cleanup(self):
eventbus.remove(ButtonDownEvent, self._handle_buttondown, self.app)

def draw(self, ctx, focused=False):
ctx.save()
Expand All @@ -72,11 +79,11 @@ def draw(self, ctx, focused=False):

ctx.restore()

async def button_event(self, event):
if self.button_handler:
return await self.button_handler(event)
return False

def _handle_buttondown(self, event: ButtonDownEvent):
self._cleanup()
if BUTTON_TYPES["CONFIRM"] in event.button:
if self.button_handler:
self.button_handler(event)

class DefinitionDisplay(Layoutable):
def __init__(self, label, value, button_handler=None):
Expand All @@ -91,7 +98,9 @@ async def button_event(self, event):
return False

def draw(self, ctx, focused=False):
print("draw")
ctx.save()
ctx.translate(-100,0)
self.height = 0

# Draw heading
Expand All @@ -113,7 +122,7 @@ def draw(self, ctx, focused=False):

# Draw value
ctx.font_size = tokens.ten_pt
value_lines = utils.wrap_text(ctx, self.value, 230, tokens.label_font_size)
value_lines = utils.wrap_text(ctx, self.value, tokens.label_font_size)
for line in value_lines:
ctx.move_to(10, self.height)
ctx.text(line)
Expand Down

0 comments on commit d860941

Please sign in to comment.