Files
frontend-dev/frontend/views/recipes/recipe_selection.py
Jannes Magnusson 90257a62a0 add edit name
2025-10-23 20:27:50 +02:00

99 lines
3.3 KiB
Python

from typing import Tuple
from ..base import View
from ..button_interface import ButtonInterface
from .recipe_manager import RecipeManager
from .recipe import V60, ESPRESSO
from PIL import ImageDraw, Image
class RecipeSelection(View, ButtonInterface):
@property
def recipes(self):
return self.recipe_manager.recipes
def __init__(self, parent, im_size, center,
recipe_manager: RecipeManager = None,
edit_recipe_command=None,
deactivate_command=None):
self.selected_index = 0
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.edit_recipe_command = edit_recipe_command
super().__init__(parent, im_size, center)
def _get_visual_recipes(self):
recipes = self.recipes + ['+', 'BACK']
if len(recipes) < 5:
return recipes, 0
start = max(0, self.selected_index - 2)
end = min(len(recipes), start + 5)
recipes = recipes[start:end]
return recipes, start
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
recipes, start = self._get_visual_recipes()
for idx, recipe in enumerate(recipes):
if idx + start == self.selected_index:
r = 10
offset = 15
for i in range(0, 90, r // 2):
draw.circle((40 + i, offset + idx * 20), r, fill='black')
if str(recipe) != 'BACK':
draw.text((40, 10 + idx * 20), str(recipe), fill='white')
else:
draw.regular_polygon((40 + 5, 15 + idx * 20, 5), 3, fill='white', rotation=90)
elif str(recipe) == 'BACK':
draw.regular_polygon((40 + 5, 15 + idx * 20, 5), 3, fill='black', rotation=90)
else:
draw.text((40, 10 + idx * 20), str(recipe), fill='black')
return im
def left_press(self):
self.selected_index = (self.selected_index - 1) % (len(self.recipes) + 2)
def left_long_press(self):
if self.selected_index < len(self.recipes):
self.edit_recipe_command(self.selected_index)
def right_press(self):
self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 2)
def right_long_press(self):
if self.selected_index < len(self.recipes):
# activate selected recipe
print(f"Activating recipe: {self.recipes[self.selected_index]}")
elif self.selected_index == len(self.recipes):
# add new recipe
self.edit_recipe_command()
else:
# back
self.selected_index = 0
self.deactivate_command()
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, True
def render_left_press(self, draw, x, y):
draw.regular_polygon((x, y+2, 5), 3, fill='black')
def render_left_long_press(self, draw, x, y):
draw.text((x, y-5), 'Edit', fill='black')
def render_right_press(self, draw, x, y):
draw.regular_polygon((x, y+4, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
draw.regular_polygon((x+2, y, 5), 3, fill='black', rotation=270)