Skip to content

Commit eeb3e32

Browse files
committed
Widget: Use appropriate colours when Spyder is in dark mode
1 parent 36da67b commit eeb3e32

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

spyder_unittest/unittestplugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from qtpy.QtWidgets import QVBoxLayout
1313
from spyder.api.plugins import SpyderPluginWidget
1414
from spyder.config.base import get_translation
15+
from spyder.config.gui import is_dark_interface
1516
from spyder.py3compat import getcwd
1617
from spyder.utils import icon_manager as ima
1718
from spyder.utils.qthelpers import create_action
@@ -175,6 +176,7 @@ def register_plugin(self):
175176
# Get information from Spyder proper into plugin
176177
self.update_pythonpath()
177178
self.update_default_wdir()
179+
self.unittestwidget.use_dark_interface(is_dark_interface())
178180

179181
# Connect to relevant signals
180182
self.main.sig_pythonpath_changed.connect(self.update_pythonpath)

spyder_unittest/widgets/datatree.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
Category.PENDING: QBrush(QColor("#C5C5C5"))
3535
}
3636

37+
COLORS_DARK = {
38+
Category.OK: QBrush(QColor("#008000")),
39+
Category.FAIL: QBrush(QColor("#C6001E")),
40+
Category.SKIP: QBrush(QColor("#505050")),
41+
Category.PENDING: QBrush(QColor("#505050"))
42+
}
43+
3744
STATUS_COLUMN = 0
3845
NAME_COLUMN = 1
3946
MESSAGE_COLUMN = 2
@@ -188,6 +195,11 @@ class TestDataModel(QAbstractItemModel):
188195
a tuple (row, column, id). The id is TOPLEVEL_ID for top-level items.
189196
For level-2 items, the id is the index of the test in `self.testresults`.
190197
198+
Attributes
199+
----------
200+
is_dark_interface : bool
201+
Whether to use colours appropriate for a dark user interface.
202+
191203
Signals
192204
-------
193205
sig_summary(str)
@@ -200,6 +212,7 @@ def __init__(self, parent=None):
200212
"""Constructor."""
201213
QAbstractItemModel.__init__(self, parent)
202214
self.abbreviator = Abbreviator()
215+
self.is_dark_interface = False
203216
self.testresults = []
204217
try:
205218
self.monospace_font = parent.window().editor.get_plugin_font()
@@ -319,7 +332,10 @@ def data(self, index, role):
319332
elif role == Qt.BackgroundRole:
320333
if id == TOPLEVEL_ID:
321334
testresult = self.testresults[row]
322-
return COLORS[testresult.category]
335+
if self.is_dark_interface:
336+
return COLORS_DARK[testresult.category]
337+
else:
338+
return COLORS[testresult.category]
323339
elif role == Qt.TextAlignmentRole:
324340
if id == TOPLEVEL_ID and column == TIME_COLUMN:
325341
return Qt.AlignRight

spyder_unittest/widgets/tests/test_datatree.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
# Local imports
1414
from spyder_unittest.backend.runnerbase import Category, TestResult
15-
from spyder_unittest.widgets.datatree import (COLORS, TestDataModel,
16-
TestDataView)
15+
from spyder_unittest.widgets.datatree import (COLORS, COLORS_DARK,
16+
TestDataModel, TestDataView)
1717

1818
try:
1919
from unittest.mock import Mock
@@ -156,15 +156,19 @@ def test_testdatamodel_shows_time_when_blank(qtmodeltester):
156156
model.testresults = [res]
157157
assert model.data(model.index(0, 3), Qt.DisplayRole) == ''
158158

159-
def test_testdatamodel_data_background():
159+
@pytest.mark.parametrize('dark', [False, True])
160+
def test_testdatamodel_data_background(dark):
160161
model = TestDataModel()
162+
if dark:
163+
model.is_dark_interface = True
161164
res = [TestResult(Category.OK, 'status', 'foo.bar'),
162165
TestResult(Category.FAIL, 'error', 'foo.bar', 'kadoom')]
163166
model.testresults = res
164167
index = model.index(0, 0)
165-
assert model.data(index, Qt.BackgroundRole) == COLORS[Category.OK]
168+
colors = COLORS_DARK if dark else COLORS
169+
assert model.data(index, Qt.BackgroundRole) == colors[Category.OK]
166170
index = model.index(1, 2)
167-
assert model.data(index, Qt.BackgroundRole) == COLORS[Category.FAIL]
171+
assert model.data(index, Qt.BackgroundRole) == colors[Category.FAIL]
168172

169173
def test_testdatamodel_data_userrole():
170174
model = TestDataModel()

spyder_unittest/widgets/unittestgui.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def __init__(self, parent, options_button=None, options_menu=None):
8888
self.default_wdir = None
8989
self.pre_test_hook = None
9090
self.testrunner = None
91+
9192
self.output = None
9293
self.testdataview = TestDataView(self)
9394
self.testdatamodel = TestDataModel(self)
@@ -146,6 +147,10 @@ def set_config_without_emit(self, new_config):
146147
"""Set test configuration but do not emit any signal."""
147148
self._config = new_config
148149

150+
def use_dark_interface(self, flag):
151+
"""Set whether widget should use colours appropriate for dark UI."""
152+
self.testdatamodel.is_dark_interface = flag
153+
149154
def create_actions(self):
150155
"""Create the actions for the unittest widget."""
151156
self.config_action = create_action(

0 commit comments

Comments
 (0)