From 89e804b08065c9000c6b89318bf07ef7dd843df8 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Tue, 26 Nov 2024 12:32:32 -0600 Subject: [PATCH 01/17] button_base annotations in place Added annotations for the various functions. Corrected some documentation errors as well, regarding what the selected_label actually does. Additionally added terminalio.FONT as the default font. --- adafruit_button/button_base.py | 72 ++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8a7709c..9d76b11 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -22,9 +22,15 @@ """ from adafruit_display_text.bitmap_label import Label from displayio import Group +import terminalio +try: + from typing import Optional, Union, List, Tuple, Any + from fontio import FontProtocol +except ImportError: + pass -def _check_color(color): +def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> Optional[int]: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): r, g, b = color @@ -36,31 +42,31 @@ class ButtonBase(Group): # pylint: disable=too-many-instance-attributes """Superclass for creating UI buttons for ``displayio``. - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in tiles. - :param height: The height of the button in tiles. - :param name: A name, or miscellaneous string that is stored on the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: Text that appears when selected + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in tiles. + :param int height: The height of the button in tiles. + :param str name: A name, or miscellaneous string that is stored on the button. + :param str label: The text that appears inside the button. Defaults to not displaying the label. + :param FontProtocol label_font: The button label font. + :param int label_color: The color of the button label text. Defaults to 0x0. + :param selected_label: The color of button label text when the button is selected. """ def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - label_scale=None - ): + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[int] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = 1, + ) -> None: super().__init__(x=x, y=y) self.x = x self.y = y @@ -73,15 +79,15 @@ def __init__( self._label_color = label_color self._label_font = label_font self._selected_label = _check_color(selected_label) - self._label_scale = label_scale or 1 + self._label_scale = label_scale @property - def label(self): + def label(self) -> Optional[Tuple[str, str]]: """The text label of the button""" return getattr(self._label, "text", None) @label.setter - def label(self, newtext): + def label(self, newtext: str) -> None: if self._label and self and (self[-1] == self._label): self.pop() @@ -90,7 +96,7 @@ def label(self, newtext): return # nothing to do! if not self._label_font: - raise RuntimeError("Please provide label font") + self._label_font = terminalio.FONT self._label = Label(self._label_font, text=newtext, scale=self._label_scale) dims = list(self._label.bounding_box) dims[2] *= self._label.scale @@ -115,17 +121,17 @@ def label(self, newtext): if (self.selected_label is None) and (self._label_color is not None): self.selected_label = (~self._label_color) & 0xFFFFFF - def _subclass_selected_behavior(self, value): - # Subclasses should overide this! + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: + # Subclasses should override this! pass @property - def selected(self): + def selected(self) -> bool: """Selected inverts the colors.""" return self._selected @selected.setter - def selected(self, value): + def selected(self, value: bool) -> None: if value == self._selected: return # bail now, nothing more to do self._selected = value @@ -140,20 +146,20 @@ def selected(self, value): self._subclass_selected_behavior(value) @property - def selected_label(self): + def selected_label(self) -> int: """The font color of the button when selected""" return self._selected_label @selected_label.setter - def selected_label(self, new_color): + def selected_label(self, new_color: int) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self): + def label_color(self) -> int: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color): + def label_color(self, new_color: int) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From fdf13e9a170d8db5341cd79ad6019ca3680071f3 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:10:05 -0600 Subject: [PATCH 02/17] Add more annotations, improve documentation Added type annotations for button.py, as well as correcting/updating documentation. --- adafruit_button/button.py | 109 ++++++++++++++++++--------------- adafruit_button/button_base.py | 22 ++++--- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 45c87f7..5f31705 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -26,36 +26,49 @@ from adafruit_display_shapes.roundrect import RoundRect from adafruit_button.button_base import ButtonBase, _check_color +try: + from typing import Optional, Union, Tuple, Any, List + from fontio import FontProtocol +except ImportError: + pass + __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git" class Button(ButtonBase): # pylint: disable=too-many-instance-attributes, too-many-locals - """Helper class for creating UI buttons for ``displayio``. - - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in pixels. - :param height: The height of the button in pixels. - :param name: The name of the button. + """Helper class for creating UI buttons for ``displayio``. Provides the following + buttons: + RECT: A rectangular button. SHAWDOWRECT adds a drop shadow. + ROUNDRECT: A rectangular button with rounded corners. SHADOWROUNDRECT adds + a drop shadow. + + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in pixels. + :param int height: The height of the button in pixels. + :param str name: The name of the button. :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. - :param fill_color: The color to fill the button. Defaults to 0xFFFFFF. - :param outline_color: The color of the outline of the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_fill: Inverts the fill color. - :param selected_outline: Inverts the outline color. - :param selected_label: Inverts the label color. + :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. + :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. + :param str label: The text that appears inside the button. Defaults to not displaying the label. + :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. + Defaults to the inverse of the fill_color. + :param int|Tuple(int, int, int) selected_outline: The outline color when the button is selected. + Defaults to the inverse of outline_color. + :param selected_label: The label color when the button is selected. + Defaults to inverting the label_color. """ - def _empty_self_group(self): + def _empty_self_group(self) -> None: while len(self) > 0: self.pop() - def _create_body(self): + def _create_body(self) -> None: if (self.outline_color is not None) or (self.fill_color is not None): if self.style == Button.RECT: self.body = Rect( @@ -117,21 +130,21 @@ def _create_body(self): def __init__( self, *, - x, - y, - width, - height, - name=None, - style=RECT, - fill_color=0xFFFFFF, - outline_color=0x0, - label=None, - label_font=None, - label_color=0x0, - selected_fill=None, - selected_outline=None, - selected_label=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + style = RECT, + fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF, + outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[int] = 0x0, + selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, + selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_scale: Optional[int] = 1 ): super().__init__( x=x, @@ -167,7 +180,7 @@ def __init__( self.label = label - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: if self._selected: new_fill = self.selected_fill new_out = self.selected_outline @@ -180,7 +193,7 @@ def _subclass_selected_behavior(self, value): self.body.outline = new_out @property - def group(self): + def group(self) -> "Button": """Return self for compatibility with old API.""" print( "Warning: The group property is being deprecated. " @@ -189,7 +202,7 @@ def group(self): ) return self - def contains(self, point): + def contains(self, point: List[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -199,56 +212,56 @@ def contains(self, point): ) @property - def fill_color(self): + def fill_color(self) -> Optional[int]: """The fill color of the button body""" return self._fill_color @fill_color.setter - def fill_color(self, new_color): + def fill_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @property - def outline_color(self): + def outline_color(self) -> Optional[int]: """The outline color of the button body""" return self._outline_color @outline_color.setter - def outline_color(self, new_color): + def outline_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @property - def selected_fill(self): + def selected_fill(self) -> Optional[int]: """The fill color of the button body when selected""" return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color): + def selected_fill(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @property - def selected_outline(self): + def selected_outline(self) -> Optional[int]: """The outline color of the button body when selected""" return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color): + def selected_outline(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline @property - def width(self): + def width(self) -> int: """The width of the button""" return self._width @width.setter - def width(self, new_width): + def width(self, new_width: int) -> None: self._width = new_width self._empty_self_group() self._create_body() @@ -257,12 +270,12 @@ def width(self, new_width): self.label = self.label @property - def height(self): + def height(self) -> int: """The height of the button""" return self._height @height.setter - def height(self, new_height): + def height(self, new_height: int) -> None: self._height = new_height self._empty_self_group() self._create_body() @@ -270,7 +283,7 @@ def height(self, new_height): self.append(self.body) self.label = self.label - def resize(self, new_width, new_height): + def resize(self, new_width: int, new_height: int) -> None: """Resize the button to the new width and height given :param new_width int the desired width :param new_height int the desired height diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 9d76b11..d7bd329 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -24,7 +24,7 @@ from displayio import Group import terminalio try: - from typing import Optional, Union, List, Tuple, Any + from typing import Optional, Union, Tuple, Any from fontio import FontProtocol except ImportError: pass @@ -48,9 +48,10 @@ class ButtonBase(Group): :param int height: The height of the button in tiles. :param str name: A name, or miscellaneous string that is stored on the button. :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. - :param int label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: The color of button label text when the button is selected. + :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + :param int label_scale: The scale factor used for the label. Defaults to 1. """ def __init__( @@ -127,7 +128,7 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: @property def selected(self) -> bool: - """Selected inverts the colors.""" + """Returns whether the button is selected.""" return self._selected @selected.setter @@ -146,20 +147,21 @@ def selected(self, value: bool) -> None: self._subclass_selected_behavior(value) @property - def selected_label(self) -> int: - """The font color of the button when selected""" + def selected_label(self) -> Optional[Union[int, tuple[int, int, int]]]: + """The font color of the button when selected. + If no color is specified it defaults to the inverse of the label_color""" return self._selected_label @selected_label.setter - def selected_label(self, new_color: int) -> None: + def selected_label(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self) -> int: + def label_color(self) -> Optional[Union[int, tuple[int, int, int]]]: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color: int) -> None: + def label_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 94e60a7b018b5ae46f9846fef3742f91bc987d8b Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:25:21 -0600 Subject: [PATCH 03/17] Type annotations to SpriteButton, Further documentation changes. Completed type annotations for the sprite_button.py file as well as further improvements to documentation. --- adafruit_button/button.py | 4 +- adafruit_button/button_base.py | 13 ++++-- adafruit_button/sprite_button.py | 80 ++++++++++++++++++-------------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 5f31705..6940a10 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -53,8 +53,8 @@ class Button(ButtonBase): Defaults to RECT. :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. - :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT + :param str label: The text that appears inside the button. + :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. Defaults to the inverse of the fill_color. diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index d7bd329..9131229 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ UI Buttons for displayio -* Author(s): Limor Fried +* Author(s): Limor Fried, Channing Ramos Implementation Notes -------------------- @@ -47,10 +48,12 @@ class ButtonBase(Group): :param int width: The width of the button in tiles. :param int height: The height of the button in tiles. :param str name: A name, or miscellaneous string that is stored on the button. - :param str label: The text that appears inside the button. Defaults to not displaying the label. - :param FontProtocol label_font: The button label font. Defaults to terminalio.FONT - :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. + :param str label: The text that appears inside the button. + :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' + :param int|Tuple(int, int, int) label_color: The color of the button label text. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param int label_scale: The scale factor used for the label. Defaults to 1. """ @@ -83,7 +86,7 @@ def __init__( self._label_scale = label_scale @property - def label(self) -> Optional[Tuple[str, str]]: + def label(self) -> Optional[str]: """The text label of the button""" return getattr(self._label, "text", None) diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 67fd9ee..d9ab283 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ Bitmap 3x3 Spritesheet based UI Button for displayio -* Author(s): Tim Cocks +* Author(s): Tim Cocks, Channing Ramos Implementation Notes -------------------- @@ -24,40 +25,51 @@ from adafruit_imageload import load from adafruit_button.button_base import ButtonBase +try: + from typing import Optional, Union, Tuple, Any, List + from fontio import FontProtocol +except ImportError: + pass class SpriteButton(ButtonBase): - """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. - - :param x: The x position of the button. - :param y: The y position of the button. - :param width: The width of the button in tiles. - :param height: The height of the button in tiles. - :param name: A name, or miscellaneous string that is stored on the button. - :param label: The text that appears inside the button. Defaults to not displaying the label. - :param label_font: The button label font. - :param label_color: The color of the button label text. Defaults to 0x0. - :param selected_label: Text that appears when selected - :param string bmp_path: The path of the 3x3 spritesheet Bitmap file - :param string selected_bmp_path: The path of the 3x3 spritesheet Bitmap file to use when pressed - :param int or tuple transparent_index: Index(s) that will be made transparent on the Palette + """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format + supported by ''adafruit_imageload''. + + :param int x: The x position of the button. + :param int y: The y position of the button. + :param int width: The width of the button in tiles. + :param int height: The height of the button in tiles. + :param Optional[str] name: A name, or miscellaneous string that is stored on the button. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Accepts either + an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label text when the button + is selected. Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to the inverse of + label_color. + :param str bmp_path: The path of the 3x3 spritesheet mage file + :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when pressed + :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to transparent. PNG files have these index(s) + set automatically. Not compatible with JPG files. + :param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1. """ def __init__( self, *, - x, - y, - width, - height, - name=None, - label=None, - label_font=None, - label_color=0x0, - selected_label=None, - bmp_path=None, - selected_bmp_path=None, - transparent_index=None, - label_scale=None + x: int, + y: int, + width: int, + height: int, + name: Optional[str] = None, + label: Optional[str] = None, + label_font: Optional[FontProtocol] = None, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + bmp_path: str = None, + selected_bmp_path: Optional[str] = None, + transparent_index: Optional[Union[int, tuple]] = None, + label_scale: Optional[int] = 1 ): if bmp_path is None: raise ValueError("Please supply bmp_path. It cannot be None.") @@ -104,16 +116,16 @@ def __init__( self.label = label @property - def width(self): - """The width of the button""" + def width(self) -> int: + """The width of the button. Read-Only""" return self._width @property - def height(self): - """The height of the button""" + def height(self) -> int: + """The height of the button. Read-Only""" return self._height - def contains(self, point): + def contains(self, point: list[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. @@ -122,7 +134,7 @@ def contains(self, point): self.y <= point[1] <= self.y + self.height ) - def _subclass_selected_behavior(self, value): + def _subclass_selected_behavior(self, value: Optional[Any]) -> None: if self._selected: if self._selected_bmp is not None: self._btn_tilegrid.bitmap = self._selected_bmp From 86c6491aa69ab632b3b63e7ef0c0c6fa7dad91d7 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:32:09 -0600 Subject: [PATCH 04/17] Annotations correction Corrected some wrong annotations. --- adafruit_button/button_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 9131229..830dc13 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -150,21 +150,21 @@ def selected(self, value: bool) -> None: self._subclass_selected_behavior(value) @property - def selected_label(self) -> Optional[Union[int, tuple[int, int, int]]]: + def selected_label(self) -> int: """The font color of the button when selected. If no color is specified it defaults to the inverse of the label_color""" return self._selected_label @selected_label.setter - def selected_label(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_label(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_label = _check_color(new_color) @property - def label_color(self) -> Optional[Union[int, tuple[int, int, int]]]: + def label_color(self) -> int: """The font color of the button""" return self._label_color @label_color.setter - def label_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def label_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 11a570c3ab232f3f02bd6564c5b8cc3adcf783e9 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:49:31 -0600 Subject: [PATCH 05/17] Second Documentation Pass --- adafruit_button/button_base.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 830dc13..8854b67 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -31,7 +31,7 @@ pass -def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> Optional[int]: +def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): r, g, b = color @@ -47,14 +47,13 @@ class ButtonBase(Group): :param int y: The y position of the button. :param int width: The width of the button in tiles. :param int height: The height of the button in tiles. - :param str name: A name, or miscellaneous string that is stored on the button. - :param str label: The text that appears inside the button. - :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' - :param int|Tuple(int, int, int) label_color: The color of the button label text. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected. + :param Optional[str] name: A name, or miscellaneous string that is stored on the button. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[int] label_color: The color of the button label text. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. - :param int label_scale: The scale factor used for the label. Defaults to 1. + :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ def __init__( From ae6ced5c49781daa75d55614be6adf65cb33f774 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:56:30 -0600 Subject: [PATCH 06/17] Updated label_color for compatibility label_color now accepts a tuple as well as an int for color, bringing its behavior in line with selected_label --- adafruit_button/button_base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 8854b67..54ed1c6 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -50,7 +50,8 @@ class ButtonBase(Group): :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[int] label_color: The color of the button label text. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Defaults to 0x0. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. @@ -66,7 +67,7 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[int] = 0x0, + label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, label_scale: Optional[int] = 1, ) -> None: @@ -79,7 +80,7 @@ def __init__( self._selected = False self.name = name self._label = label - self._label_color = label_color + self._label_color = _check_color(label_color) self._label_font = label_font self._selected_label = _check_color(selected_label) self._label_scale = label_scale From 252cf7d4a3e0a645514f0ada2d17a6e897845978 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 13:59:42 -0600 Subject: [PATCH 07/17] Button.py second pass Second pass to finalize changes --- adafruit_button/button.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 6940a10..b15d683 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -48,20 +48,24 @@ class Button(ButtonBase): :param int y: The y position of the button. :param int width: The width of the button in pixels. :param int height: The height of the button in pixels. - :param str name: The name of the button. + :param Optional[str] name: The name of the button. :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. - :param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF. - :param int|Tuple(int, int, int) outline_color: The color of the outline of the button. - :param str label: The text that appears inside the button. - :param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT'' - :param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0. - :param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected. - Defaults to the inverse of the fill_color. - :param int|Tuple(int, int, int) selected_outline: The outline color when the button is selected. - Defaults to the inverse of outline_color. - :param selected_label: The label color when the button is selected. - Defaults to inverting the label_color. + :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the button. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[str] label: The text that appears inside the button. + :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of the fill_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the button is selected. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to inverting the label_color. + :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ def _empty_self_group(self) -> None: @@ -140,7 +144,7 @@ def __init__( outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[int] = 0x0, + label_color: Optional[Union[int, tuple[int, int , int]]] = 0x0, selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, selected_label: Optional[Union[int, tuple[int, int, int]]] = None, @@ -217,7 +221,7 @@ def fill_color(self) -> Optional[int]: return self._fill_color @fill_color.setter - def fill_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def fill_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @@ -228,7 +232,7 @@ def outline_color(self) -> Optional[int]: return self._outline_color @outline_color.setter - def outline_color(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def outline_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @@ -239,7 +243,7 @@ def selected_fill(self) -> Optional[int]: return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_fill(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @@ -250,7 +254,7 @@ def selected_outline(self) -> Optional[int]: return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color: Optional[Union[int, tuple[int, int, int]]]) -> None: + def selected_outline(self, new_color: Union[int, tuple[int, int, int]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline From acb718a1b58c90b8551d4174bb61f79a672c8b90 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:02:58 -0600 Subject: [PATCH 08/17] Fixed transparent_index The transparent index is now set to the index number given, rather than just using 0. --- adafruit_button/sprite_button.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index d9ab283..8dabb80 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -100,7 +100,7 @@ def __init__( for _index in transparent_index: self._selected_bmp_palette.make_transparent(_index) elif isinstance(transparent_index, int): - self._selected_bmp_palette.make_transparent(0) + self._selected_bmp_palette.make_transparent(transparent_index) self._btn_tilegrid = inflate_tilegrid( bmp_obj=self._bmp, From a11ed732f2d16678eaebfaa7d78a60869f40aa4d Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:22:09 -0600 Subject: [PATCH 09/17] Update Licensing Added my name to the licensing portion. --- adafruit_button/button.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index b15d683..d86a7b2 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -1,4 +1,5 @@ # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -9,7 +10,7 @@ UI Buttons for displayio -* Author(s): Limor Fried +* Author(s): Limor Fried, Channing Ramos Implementation Notes -------------------- From b2759a25e0897879e3a6f949d6005bced183dcd2 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:47:32 -0600 Subject: [PATCH 10/17] Formatting Correction - button.py Correct formatting errors for the button.py file to pass ruff checks. --- adafruit_button/button.py | 43 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index d86a7b2..333b2eb 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -54,18 +54,21 @@ class Button(ButtonBase): Defaults to RECT. :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. - :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the button. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the + button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of the fill_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to inverting the label_color. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label + text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of the fill_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of outline_color. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to inverting the label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ @@ -141,14 +144,14 @@ def __init__( height: int, name: Optional[str] = None, style = RECT, - fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF, - outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, + fill_color: Optional[Union[int, Tuple[int, int, int]]] = 0xFFFFFF, + outline_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int , int]]] = 0x0, - selected_fill: Optional[Union[int, tuple[int, int , int]]] = None, - selected_outline: Optional[Union[int, tuple[int, int, int]]] = None, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int , int]]] = 0x0, + selected_fill: Optional[Union[int, Tuple[int, int , int]]] = None, + selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1 ): super().__init__( @@ -222,7 +225,7 @@ def fill_color(self) -> Optional[int]: return self._fill_color @fill_color.setter - def fill_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def fill_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._fill_color = _check_color(new_color) if not self.selected: self.body.fill = self._fill_color @@ -233,7 +236,7 @@ def outline_color(self) -> Optional[int]: return self._outline_color @outline_color.setter - def outline_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def outline_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._outline_color = _check_color(new_color) if not self.selected: self.body.outline = self._outline_color @@ -244,7 +247,7 @@ def selected_fill(self) -> Optional[int]: return self._selected_fill @selected_fill.setter - def selected_fill(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_fill(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_fill = _check_color(new_color) if self.selected: self.body.fill = self._selected_fill @@ -255,7 +258,7 @@ def selected_outline(self) -> Optional[int]: return self._selected_outline @selected_outline.setter - def selected_outline(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_outline(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_outline = _check_color(new_color) if self.selected: self.body.outline = self._selected_outline From 2dd2d8ba1a6e4ab625d1761a4754503bf5c8a6c2 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:54:49 -0600 Subject: [PATCH 11/17] Formatting corrections for button_base.py Correct formatting to avoid ruff errors. --- adafruit_button/button_base.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 54ed1c6..907aaf7 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -49,11 +49,13 @@ class ButtonBase(Group): :param int height: The height of the button in tiles. :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. - :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Defaults to 0x0. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of button label text when the button is selected. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color. + :param Optional[FontProtocol] label_font: The button label font. + Defaults to ''terminalio.FONT'' + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the label text. + Defaults to 0x0. Accepts an int or a tuple of 3 integers representing RGB values. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the label text + when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to an inverse of label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ @@ -67,8 +69,8 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1, ) -> None: super().__init__(x=x, y=y) @@ -156,7 +158,7 @@ def selected_label(self) -> int: return self._selected_label @selected_label.setter - def selected_label(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def selected_label(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._selected_label = _check_color(new_color) @property @@ -165,6 +167,6 @@ def label_color(self) -> int: return self._label_color @label_color.setter - def label_color(self, new_color: Union[int, tuple[int, int, int]]) -> None: + def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) self._label.color = self._label_color From 3f1434dcdc8faa1bb5067010f352b18ced3e3308 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:03:00 -0600 Subject: [PATCH 12/17] Formatting for sprite_button Resolving ruff errors for sprite_button.py, some minor formatting elsewhere. --- adafruit_button/button.py | 19 ++++++++++--------- adafruit_button/sprite_button.py | 29 +++++++++++++++-------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 333b2eb..7eec017 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -53,22 +53,23 @@ class Button(ButtonBase): :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. Defaults to RECT. :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button. - Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. - :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of the - button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF. + :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of + the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[str] label: The text that appears inside the button. - :param Optional[FontProtocol] label_font: The button label font. Defaults to ''terminalio.FONT'' + :param Optional[FontProtocol] label_font: The button label font. Defaults to + ''terminalio.FONT'' :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label - text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. + text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0. :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the - button is selected. Accepts an int or a tuple of 3 integers representing RGB values. - Defaults to the inverse of the fill_color. + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to the inverse of the fill_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the button is selected. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to the inverse of outline_color. :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the - button is selected. Accepts an int or a tuple of 3 integers representing RGB values. - Defaults to inverting the label_color. + button is selected. Accepts an int or a tuple of 3 integers representing RGB values. + Defaults to inverting the label_color. :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1. """ diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 8dabb80..25119a4 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -32,8 +32,8 @@ pass class SpriteButton(ButtonBase): - """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format - supported by ''adafruit_imageload''. + """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. + Compatible with any format supported by ''adafruit_imageload''. :param int x: The x position of the button. :param int y: The y position of the button. @@ -42,15 +42,16 @@ class SpriteButton(ButtonBase): :param Optional[str] name: A name, or miscellaneous string that is stored on the button. :param Optional[str] label: The text that appears inside the button. :param Optional[FontProtocol] label_font: The button label font. - :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Accepts either - an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. - :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label text when the button - is selected. Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to the inverse of - label_color. + :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the label text. + Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0. + :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label + text when the button is selected. Accepts either an integer or a tuple of 3 integers + representing RGB values. Defaults to the inverse of label_color. :param str bmp_path: The path of the 3x3 spritesheet mage file - :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when pressed - :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to transparent. PNG files have these index(s) - set automatically. Not compatible with JPG files. + :param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when + pressed + :param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to + transparent. PNG files have these index(s) set automatically. Not compatible with JPG files. :param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1. """ @@ -64,11 +65,11 @@ def __init__( name: Optional[str] = None, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0, - selected_label: Optional[Union[int, tuple[int, int, int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, bmp_path: str = None, selected_bmp_path: Optional[str] = None, - transparent_index: Optional[Union[int, tuple]] = None, + transparent_index: Optional[Union[int, Tuple]] = None, label_scale: Optional[int] = 1 ): if bmp_path is None: @@ -125,7 +126,7 @@ def height(self) -> int: """The height of the button. Read-Only""" return self._height - def contains(self, point: list[int]) -> bool: + def contains(self, point: List[int]) -> bool: """Used to determine if a point is contained within a button. For example, ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for determining that a button has been touched. From a3ba72102d1caa7ccdccf48b9e3f16530ac79f73 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:13:31 -0600 Subject: [PATCH 13/17] CRLF to LF line ending conversion PyCharm has a mind of its own for settings. --- adafruit_button/button.py | 6 +++--- adafruit_button/button_base.py | 2 +- adafruit_button/sprite_button.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 7eec017..5bafa3e 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -294,8 +294,8 @@ def height(self, new_height: int) -> None: def resize(self, new_width: int, new_height: int) -> None: """Resize the button to the new width and height given - :param new_width int the desired width - :param new_height int the desired height + :param int new_width: The desired width in pixels. + :param int new_height: he desired height in pixels. """ self._width = new_width self._height = new_height @@ -303,4 +303,4 @@ def resize(self, new_width: int, new_height: int) -> None: self._create_body() if self.body: self.append(self.body) - self.label = self.label + self.label = self.label \ No newline at end of file diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 907aaf7..0c84237 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -169,4 +169,4 @@ def label_color(self) -> int: @label_color.setter def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) - self._label.color = self._label_color + self._label.color = self._label_color \ No newline at end of file diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 25119a4..389fe95 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -142,4 +142,4 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: self._btn_tilegrid.pixel_shader = self._selected_bmp_palette else: self._btn_tilegrid.bitmap = self._bmp - self._btn_tilegrid.pixel_shader = self._bmp_palette + self._btn_tilegrid.pixel_shader = self._bmp_palette \ No newline at end of file From db3d9e5e4abf834a345c530ca1c64226bd66097d Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:19:00 -0600 Subject: [PATCH 14/17] Black formatting Ran against local installation of black after correcting my own config. --- adafruit_button/button.py | 8 ++++---- adafruit_button/button_base.py | 3 ++- adafruit_button/sprite_button.py | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 5bafa3e..3a8bdd3 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -144,13 +144,13 @@ def __init__( width: int, height: int, name: Optional[str] = None, - style = RECT, + style=RECT, fill_color: Optional[Union[int, Tuple[int, int, int]]] = 0xFFFFFF, outline_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, label: Optional[str] = None, label_font: Optional[FontProtocol] = None, - label_color: Optional[Union[int, Tuple[int, int , int]]] = 0x0, - selected_fill: Optional[Union[int, Tuple[int, int , int]]] = None, + label_color: Optional[Union[int, Tuple[int, int, int]]] = 0x0, + selected_fill: Optional[Union[int, Tuple[int, int, int]]] = None, selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None, selected_label: Optional[Union[int, Tuple[int, int, int]]] = None, label_scale: Optional[int] = 1 @@ -303,4 +303,4 @@ def resize(self, new_width: int, new_height: int) -> None: self._create_body() if self.body: self.append(self.body) - self.label = self.label \ No newline at end of file + self.label = self.label diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 0c84237..58f429b 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -24,6 +24,7 @@ from adafruit_display_text.bitmap_label import Label from displayio import Group import terminalio + try: from typing import Optional, Union, Tuple, Any from fontio import FontProtocol @@ -169,4 +170,4 @@ def label_color(self) -> int: @label_color.setter def label_color(self, new_color: Union[int, Tuple[int, int, int]]) -> None: self._label_color = _check_color(new_color) - self._label.color = self._label_color \ No newline at end of file + self._label.color = self._label_color diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 389fe95..3b5feff 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -31,6 +31,7 @@ except ImportError: pass + class SpriteButton(ButtonBase): """Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format supported by ''adafruit_imageload''. @@ -142,4 +143,4 @@ def _subclass_selected_behavior(self, value: Optional[Any]) -> None: self._btn_tilegrid.pixel_shader = self._selected_bmp_palette else: self._btn_tilegrid.bitmap = self._bmp - self._btn_tilegrid.pixel_shader = self._bmp_palette \ No newline at end of file + self._btn_tilegrid.pixel_shader = self._bmp_palette From a00436d37cc1f832332a51893b4d08354a09a0bf Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 16:48:08 -0600 Subject: [PATCH 15/17] Allow setting label colors with Tuples Corrects issues with converting tuples of RGB values to ints, which Label objects require. --- adafruit_button/button.py | 4 ++-- adafruit_button/button_base.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 3a8bdd3..1ad1aa0 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -179,9 +179,9 @@ def __init__( self._selected_outline = _check_color(selected_outline) if self.selected_fill is None and fill_color is not None: - self.selected_fill = (~self._fill_color) & 0xFFFFFF + self.selected_fill = (~_check_color(self._fill_color)) & 0xFFFFFF if self.selected_outline is None and outline_color is not None: - self.selected_outline = (~self._outline_color) & 0xFFFFFF + self.selected_outline = (~_check_color(self._outline_color)) & 0xFFFFFF self._create_body() if self.body: diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index 58f429b..167a667 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -126,7 +126,7 @@ def label(self, newtext: str) -> None: self.append(self._label) if (self.selected_label is None) and (self._label_color is not None): - self.selected_label = (~self._label_color) & 0xFFFFFF + self.selected_label = (~_check_color(self._label_color)) & 0xFFFFFF def _subclass_selected_behavior(self, value: Optional[Any]) -> None: # Subclasses should override this! From fd29d0f496920039da9d68307b6ff0bd2ea4a210 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Sun, 1 Dec 2024 17:34:40 -0600 Subject: [PATCH 16/17] Update conf.py Point it to the new builtin libraries used, terminalio and fontio. --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 8e24264..56878d6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -27,7 +27,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -autodoc_mock_imports = ["displayio", "bitmaptools"] +autodoc_mock_imports = ["displayio", "bitmaptools", "terminalio", "fontio"] intersphinx_mapping = { From f6026b2b6abbaffc617a566d3cb92e3d03a59e14 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 4 Dec 2024 15:40:45 -0600 Subject: [PATCH 17/17] merge main, resolve conflicts --- adafruit_button/button.py | 2 +- adafruit_button/button_base.py | 1 - adafruit_button/sprite_button.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/adafruit_button/button.py b/adafruit_button/button.py index 6264822..1ab13f0 100644 --- a/adafruit_button/button.py +++ b/adafruit_button/button.py @@ -28,7 +28,7 @@ from adafruit_button.button_base import ButtonBase, _check_color try: - from typing import Optional, Union, Tuple, Any, List + from typing import Optional, Union, Tuple from fontio import FontProtocol from displayio import Group except ImportError: diff --git a/adafruit_button/button_base.py b/adafruit_button/button_base.py index e31e040..d118d04 100644 --- a/adafruit_button/button_base.py +++ b/adafruit_button/button_base.py @@ -32,7 +32,6 @@ pass - def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int: # if a tuple is supplied, convert it to a RGB number if isinstance(color, tuple): diff --git a/adafruit_button/sprite_button.py b/adafruit_button/sprite_button.py index 069b18e..337466a 100644 --- a/adafruit_button/sprite_button.py +++ b/adafruit_button/sprite_button.py @@ -26,7 +26,7 @@ from adafruit_button.button_base import ButtonBase try: - from typing import Optional, Union, Tuple, List + from typing import Optional, Union, Tuple from fontio import FontProtocol except ImportError: pass