Skip to content

Commit 4b17966

Browse files
authored
gui:iterate on dropdown style options (#2559)
1 parent 5aa322d commit 4b17966

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

arcade/gui/widgets/dropdown.py

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,50 @@ def on_change(event: UIOnChangeEvent):
6060
height: Height of each of the option.
6161
default: The default value shown.
6262
options: The options displayed when the layout is clicked.
63+
primary_style: The style of the primary button.
64+
dropdown_style: The style of the buttons in the dropdown.
65+
active_style: The style of the dropdown button, which represents the active option.
6366
"""
6467

6568
DIVIDER = None
6669

70+
DEFAULT_BUTTON_STYLE = {
71+
"normal": UIFlatButton.UIStyle(
72+
font_color=uicolor.GREEN_NEPHRITIS,
73+
),
74+
"hover": UIFlatButton.UIStyle(
75+
font_color=uicolor.WHITE,
76+
bg=uicolor.DARK_BLUE_WET_ASPHALT,
77+
border=uicolor.GRAY_CONCRETE,
78+
),
79+
"press": UIFlatButton.UIStyle(
80+
font_color=uicolor.DARK_BLUE_MIDNIGHT_BLUE,
81+
bg=uicolor.WHITE_CLOUDS,
82+
border=uicolor.GRAY_CONCRETE,
83+
),
84+
"disabled": UIFlatButton.UIStyle(
85+
font_color=uicolor.WHITE_SILVER,
86+
bg=uicolor.GRAY_ASBESTOS,
87+
),
88+
}
89+
DEFAULT_DROPDOWN_STYLE = {
90+
"normal": UIFlatButton.UIStyle(),
91+
"hover": UIFlatButton.UIStyle(
92+
font_color=uicolor.WHITE,
93+
bg=uicolor.DARK_BLUE_WET_ASPHALT,
94+
border=uicolor.GRAY_CONCRETE,
95+
),
96+
"press": UIFlatButton.UIStyle(
97+
font_color=uicolor.DARK_BLUE_MIDNIGHT_BLUE,
98+
bg=uicolor.WHITE_CLOUDS,
99+
border=uicolor.GRAY_CONCRETE,
100+
),
101+
"disabled": UIFlatButton.UIStyle(
102+
font_color=uicolor.WHITE_SILVER,
103+
bg=uicolor.GRAY_ASBESTOS,
104+
),
105+
}
106+
67107
def __init__(
68108
self,
69109
*,
@@ -73,8 +113,18 @@ def __init__(
73113
height: float = 30,
74114
default: Optional[str] = None,
75115
options: Optional[list[Union[str, None]]] = None,
116+
primary_style=None,
117+
dropdown_style=None,
118+
active_style=None,
76119
**kwargs,
77120
):
121+
if primary_style is None:
122+
primary_style = self.DEFAULT_BUTTON_STYLE
123+
if dropdown_style is None:
124+
dropdown_style = self.DEFAULT_DROPDOWN_STYLE
125+
if active_style is None:
126+
active_style = self.DEFAULT_BUTTON_STYLE
127+
78128
# TODO handle if default value not in options or options empty
79129
if options is None:
80130
options = []
@@ -83,13 +133,14 @@ def __init__(
83133

84134
super().__init__(x=x, y=y, width=width, height=height, **kwargs)
85135

136+
self._default_style = deepcopy(primary_style)
137+
self._dropdown_style = deepcopy(dropdown_style)
138+
self._active_style = deepcopy(active_style)
139+
86140
# Setup button showing value
87-
style = deepcopy(UIFlatButton.DEFAULT_STYLE)
88-
style["hover"].font_color = uicolor.GREEN_NEPHRITIS
89141
self._default_button = UIFlatButton(
90-
text=self._value or "", width=self.width, height=self.height, style=style
142+
text=self._value or "", width=self.width, height=self.height, style=self._default_style
91143
)
92-
93144
self._default_button.on_click = self._on_button_click # type: ignore
94145

95146
self._overlay = _UIDropdownOverlay()
@@ -100,8 +151,6 @@ def __init__(
100151

101152
self.register_event_type("on_change")
102153

103-
self.with_border(color=arcade.color.RED)
104-
105154
@property
106155
def value(self) -> Optional[str]:
107156
"""Current selected option."""
@@ -122,11 +171,6 @@ def _update_options(self):
122171
# generate options
123172
self._overlay.clear()
124173

125-
# is there another way then deepcopy, does it matter?
126-
# ("premature optimization is the root of all evil")
127-
active_style = deepcopy(UIFlatButton.DEFAULT_STYLE)
128-
active_style["normal"]["bg"] = uicolor.GREEN_NEPHRITIS
129-
130174
for option in self._options:
131175
if option is None: # None = UIDropdown.DIVIDER, required by pyright
132176
self._overlay.add(
@@ -139,7 +183,7 @@ def _update_options(self):
139183
text=option,
140184
width=self.width,
141185
height=self.height,
142-
style=active_style if self.value == option else UIFlatButton.DEFAULT_STYLE,
186+
style=self._active_style if self.value == option else self._dropdown_style,
143187
)
144188
)
145189
button.on_click = self._on_option_click

0 commit comments

Comments
 (0)