Skip to content

Commit

Permalink
Fix styling the library foreground color
Browse files Browse the repository at this point in the history
The option was not passed through to the library delegate and thus never
applied.

fixes #840
  • Loading branch information
karlch committed Jul 14, 2024
1 parent 41e59b1 commit 86df2d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion vimiv/config/_style_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"library.even.bg": "{base01}",
"library.odd.bg": "{base01}",
"library.selected.bg": "{base0d}",
"library.selected.fg": "{base07}",
"library.selected.fg": "{library.fg}",
"library.search.highlighted.fg": "{base01}",
"library.search.highlighted.bg": "{base04}",
"library.scrollbar.width": "{image.scrollbar.width}",
Expand Down
27 changes: 17 additions & 10 deletions vimiv/gui/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ def __init__(self):
self.fg = styles.get("library.fg")
self.dir_fg = styles.get("library.directory.fg")
self.search_fg = styles.get("library.search.highlighted.fg")
self.selected_fg = styles.get("library.selected.fg")

# QColor options for background drawing
self.selection_bg = QColor(styles.get("library.selected.bg"))
Expand All @@ -534,10 +535,11 @@ def paint(self, painter, option, index):
option: The QStyleOptionViewItem.
index: The QModelIndex.
"""
self._draw_background(painter, option, index)
self._draw_text(painter, option, index)
is_selected = option.state & QStyle.StateFlag.State_Selected
self._draw_background(painter, option, index, is_selected)
self._draw_text(painter, option, index, is_selected)

def _draw_text(self, painter, option, index):
def _draw_text(self, painter, option, index, is_selected: bool):
"""Draw text for the library.
Sets the font and the foreground color using html. The foreground color
Expand All @@ -548,10 +550,11 @@ def _draw_text(self, painter, option, index):
painter: The QPainter.
option: The QStyleOptionViewItem.
index: The QModelIndex.
is_selected: True if the text is for a selected item.
"""
text = index.model().data(index)
painter.save()
color = self._get_foreground_color(index, text)
color = self._get_foreground_color(index, text, is_selected)
text = self.elided(text, painter.fontMetrics(), option.rect.width() - 1)
text = wrap_style_span(f"color: {color}; font: {self.font}", text)
self.doc.setHtml(text)
Expand All @@ -560,7 +563,7 @@ def _draw_text(self, painter, option, index):
self.doc.drawContents(painter)
painter.restore()

def _draw_background(self, painter, option, index):
def _draw_background(self, painter, option, index, is_selected: bool):
"""Draw the background rectangle of the text.
The color depends on whether the item is selected, in an even row or in
Expand All @@ -570,15 +573,16 @@ def _draw_background(self, painter, option, index):
painter: The QPainter.
option: The QStyleOptionViewItem.
index: The QModelIndex.
is_selected: True if the background is for a selected item.
"""
color = self._get_background_color(index, option.state)
color = self._get_background_color(index, is_selected)
painter.save()
painter.setBrush(color)
painter.setPen(Qt.PenStyle.NoPen)
painter.drawRect(option.rect)
painter.restore()

def _get_foreground_color(self, index, text):
def _get_foreground_color(self, index, text, is_selected: bool):
"""Return the foreground color of an item.
The color depends on highlighted as search result and whether it is a
Expand All @@ -587,22 +591,25 @@ def _get_foreground_color(self, index, text):
Args:
index: Index of the element indicating even/odd/highlighted.
text: Text indicating directory or not.
is_selected: True if the foreground is for a selected item.
"""
if is_selected:
return self.selected_fg
if index.model().is_highlighted(index):
return self.search_fg
return self.dir_fg if text.endswith("/") else self.fg

def _get_background_color(self, index, state):
def _get_background_color(self, index, is_selected: bool):
"""Return the background color of an item.
The color depends on selected, highlighted as search result and
even/odd.
Args:
index: Index of the element indicating even/odd/highlighted.
state: State of the index indicating selected.
is_selected: True if the background is for a selected item.
"""
if state & QStyle.StateFlag.State_Selected:
if is_selected:
if api.modes.current() == api.modes.LIBRARY:
return self.selection_bg
return self.selection_bg_unfocus
Expand Down

0 comments on commit 86df2d7

Please sign in to comment.