Source code for tessella.widgets.placeholder

import pygame
from ..constraints import WidgetConstraints
from . import Widget
from .style import Palette

[docs] class Placeholder(Widget): """ A widget meant to be used as a placeholder child, which has \ no practical function whatsoever. \ This widget optionally renders a simple box to show its position. Attributes: width (int): The width of the widget, in pixels. height (int): The height of the widget, in pixels. box_color (str): The color of the rendered box. box_thickness (int): The thickness of the lines of the rendered box. visible (bool): Whether to render the box or not """ def __init__( self, width: int | None = None, height: int | None = None, box_color: str = Palette.PLACEHOLDER, box_thickness: int = 2, visible: bool = True ): """ Initializes a new Placeholder object. Args: width (int): The width of the widget, in pixels. If left empty, \ the placeholder will expand to fill all the space available. \ Defaults to None. height (int): The height of the widget, in pixels. If left empty, \ the placeholder will expand to fill all the space available. \ Defaults to None. box_color (str): The color of the rendered box. \ Defaults to `Palette.PLACEHOLDER`. box_thickness (int): The thickness of the \ lines of the rendered box. Defaults to 2. visible (bool): Whether to render the box or not. Defaults to True. """ super().__init__() self.width: int | None = width self.height: int | None = height self.box_color: str = box_color self.box_thickness: int = box_thickness self.visible: bool = visible
[docs] def calculate_size(self, constraints: WidgetConstraints) -> tuple[int, int]: width: int | None = self.width height: int | None = self.height if width is None: width = constraints.max_width if height is None: height = constraints.max_height return (width, height)
def _draw(self) -> pygame.Surface | None: if not self.visible: # Nothing to draw return None surface = pygame.Surface(self.bounds.size, pygame.SRCALPHA) # Draws the box surface_rect = surface.get_rect() pygame.draw.rect(surface, self.box_color, surface_rect, self.box_thickness) pygame.draw.line(surface, self.box_color, surface_rect.topleft, surface_rect.bottomright, self.box_thickness) pygame.draw.line(surface, self.box_color, surface_rect.bottomleft, surface_rect.topright, self.box_thickness) return surface def _debug_draw(self) -> pygame.Surface | None: #! TODO: Implement return self._draw()
[docs] def depends_on_parent_size(self) -> bool: return self.width is None or self.height is None