2222"""
2323from adafruit_display_text .bitmap_label import Label
2424from displayio import Group
25+ import terminalio
26+ try :
27+ from typing import Optional , Union , List , Tuple , Any
28+ from fontio import FontProtocol
29+ except ImportError :
30+ pass
2531
2632
27- def _check_color (color ) :
33+ def _check_color (color : Optional [ Union [ int , tuple [ int , int , int ]]]) -> Optional [ int ] :
2834 # if a tuple is supplied, convert it to a RGB number
2935 if isinstance (color , tuple ):
3036 r , g , b = color
@@ -36,31 +42,31 @@ class ButtonBase(Group):
3642 # pylint: disable=too-many-instance-attributes
3743 """Superclass for creating UI buttons for ``displayio``.
3844
39- :param x: The x position of the button.
40- :param y: The y position of the button.
41- :param width: The width of the button in tiles.
42- :param height: The height of the button in tiles.
43- :param name: A name, or miscellaneous string that is stored on the button.
44- :param label: The text that appears inside the button. Defaults to not displaying the label.
45- :param label_font: The button label font.
46- :param label_color: The color of the button label text. Defaults to 0x0.
47- :param selected_label: Text that appears when selected
45+ :param int x: The x position of the button.
46+ :param int y: The y position of the button.
47+ :param int width: The width of the button in tiles.
48+ :param int height: The height of the button in tiles.
49+ :param str name: A name, or miscellaneous string that is stored on the button.
50+ :param str label: The text that appears inside the button. Defaults to not displaying the label.
51+ :param FontProtocol label_font: The button label font.
52+ :param int label_color: The color of the button label text. Defaults to 0x0.
53+ :param selected_label: The color of button label text when the button is selected.
4854 """
4955
5056 def __init__ (
5157 self ,
5258 * ,
53- x ,
54- y ,
55- width ,
56- height ,
57- name = None ,
58- label = None ,
59- label_font = None ,
60- label_color = 0x0 ,
61- selected_label = None ,
62- label_scale = None
63- ):
59+ x : int ,
60+ y : int ,
61+ width : int ,
62+ height : int ,
63+ name : Optional [ str ] = None ,
64+ label : Optional [ str ] = None ,
65+ label_font : Optional [ FontProtocol ] = None ,
66+ label_color : Optional [ int ] = 0x0 ,
67+ selected_label : Optional [ Union [ int , tuple [ int , int , int ]]] = None ,
68+ label_scale : Optional [ int ] = 1 ,
69+ ) -> None :
6470 super ().__init__ (x = x , y = y )
6571 self .x = x
6672 self .y = y
@@ -73,15 +79,15 @@ def __init__(
7379 self ._label_color = label_color
7480 self ._label_font = label_font
7581 self ._selected_label = _check_color (selected_label )
76- self ._label_scale = label_scale or 1
82+ self ._label_scale = label_scale
7783
7884 @property
79- def label (self ):
85+ def label (self ) -> Optional [ Tuple [ str , str ]] :
8086 """The text label of the button"""
8187 return getattr (self ._label , "text" , None )
8288
8389 @label .setter
84- def label (self , newtext ) :
90+ def label (self , newtext : str ) -> None :
8591 if self ._label and self and (self [- 1 ] == self ._label ):
8692 self .pop ()
8793
@@ -90,7 +96,7 @@ def label(self, newtext):
9096 return # nothing to do!
9197
9298 if not self ._label_font :
93- raise RuntimeError ( "Please provide label font" )
99+ self . _label_font = terminalio . FONT
94100 self ._label = Label (self ._label_font , text = newtext , scale = self ._label_scale )
95101 dims = list (self ._label .bounding_box )
96102 dims [2 ] *= self ._label .scale
@@ -115,17 +121,17 @@ def label(self, newtext):
115121 if (self .selected_label is None ) and (self ._label_color is not None ):
116122 self .selected_label = (~ self ._label_color ) & 0xFFFFFF
117123
118- def _subclass_selected_behavior (self , value ) :
119- # Subclasses should overide this!
124+ def _subclass_selected_behavior (self , value : Optional [ Any ]) -> None :
125+ # Subclasses should override this!
120126 pass
121127
122128 @property
123- def selected (self ):
129+ def selected (self ) -> bool :
124130 """Selected inverts the colors."""
125131 return self ._selected
126132
127133 @selected .setter
128- def selected (self , value ) :
134+ def selected (self , value : bool ) -> None :
129135 if value == self ._selected :
130136 return # bail now, nothing more to do
131137 self ._selected = value
@@ -140,20 +146,20 @@ def selected(self, value):
140146 self ._subclass_selected_behavior (value )
141147
142148 @property
143- def selected_label (self ):
149+ def selected_label (self ) -> int :
144150 """The font color of the button when selected"""
145151 return self ._selected_label
146152
147153 @selected_label .setter
148- def selected_label (self , new_color ) :
154+ def selected_label (self , new_color : int ) -> None :
149155 self ._selected_label = _check_color (new_color )
150156
151157 @property
152- def label_color (self ):
158+ def label_color (self ) -> int :
153159 """The font color of the button"""
154160 return self ._label_color
155161
156162 @label_color .setter
157- def label_color (self , new_color ) :
163+ def label_color (self , new_color : int ) -> None :
158164 self ._label_color = _check_color (new_color )
159165 self ._label .color = self ._label_color
0 commit comments