Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Oct 10, 2023
1 parent 2c1a201 commit 5d1b227
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/magicgui/backends/_ipynb/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class _IPySupportsIcon(protocols.SupportsIcon):

_ipywidget: ipywdg.Button

def _mgui_set_icon(self, value: str, color: str) -> None:
def _mgui_set_icon(self, value: str | None, color: str | None) -> None:
"""Set icon."""
# only ipywdg.Button actually supports icons.
# but our button protocol allows it for all buttons subclasses
Expand All @@ -277,6 +277,7 @@ def _mgui_set_icon(self, value: str, color: str) -> None:
# which works for iconify icons served by qt, while still
# allowing for bare "icon-name" syntax which works for ipywidgets.
# note however... only fa4/5 icons will work for ipywidgets.
value = value or ""
self._ipywidget.icon = value.replace("fa-", "").split(":", 1)[-1]
self._ipywidget.style.text_color = color

Expand Down
5 changes: 5 additions & 0 deletions src/magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ def _update_icon(self) -> None:
pal = self._qwidget.palette()
color = pal.color(QPalette.ColorRole.WindowText).name()

if ":" not in value:
# for parity with the other backends, assume fontawesome
# if no prefix is given.
value = f"fa-regular:{value}"

try:
self._qwidget.setIcon(superqt.QIconifyIcon(value, color=color))
except (OSError, ValueError) as e:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,17 @@ def test_pushbutton_click_signal():
mock2.assert_called_once()


def test_pushbutton_icon(backend: str):
use_app(backend)
btn = widgets.PushButton(icon="mdi:folder")
btn.set_icon("smile", "red")
btn.set_icon(None)

if backend == "qt":
with pytest.warns(UserWarning, match="Could not set iconify icon"):
btn.set_icon("bad:key")


def test_list_edit():
"""Test ListEdit."""
from typing import List
Expand Down

0 comments on commit 5d1b227

Please sign in to comment.