Skip to content

Commit

Permalink
For icon rules and emblems, use the standard theme names for the icon…
Browse files Browse the repository at this point in the history
…s. Generalize the naming so 1 or both of the icons can have themed names.
  • Loading branch information
cbhaley committed Sep 25, 2024
1 parent 1f5e9d0 commit 5941de8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/calibre/gui2/library/alternate_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from calibre.gui2.dnd import path_from_qurl
from calibre.gui2.gestures import GestureManager
from calibre.gui2.library.caches import CoverCache, ThumbnailCache
from calibre.gui2.library.models import themed_icon_name
from calibre.gui2.pin_columns import PinContainer
from calibre.utils import join_with_timeout
from calibre.utils.config import prefs, tweaks
Expand Down Expand Up @@ -555,11 +556,9 @@ def cached_emblem(self, cache, name, raw_icon=None):
ans = QIcon.ic('ok.png').pixmap(sz, sz)
elif name:
pmap = None
if is_dark_theme():
n,ext = os.path.splitext(name)
d = os.path.join(config_dir, 'cc_icons', n + '-dark' + ext)
if os.path.exists(d):
pmap = QIcon(d).pixmap(sz, sz)
d = themed_icon_name(os.path.join(config_dir, 'cc_icons'), name)
if d is not None:
pmap = QIcon(d).pixmap(sz, sz)
if pmap is None:
pmap = QIcon(os.path.join(config_dir, 'cc_icons', name)).pixmap(sz, sz)
if not pmap.isNull():
Expand Down
24 changes: 16 additions & 8 deletions src/calibre/gui2/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ def __call__(self, id_, key, fmt, db, color_cache, template_cache):
# }}}


def themed_icon_name(icon_dir, icon_name):
root,ext = os.path.splitext(icon_name)
# Remove any theme from the icon name
root = root.removesuffix('-for-dark-theme').removesuffix('-for-light-theme')
# Check if the correct themed icon exists.
theme_suffix = '-for-dark-theme' if is_dark_theme() else '-for-light-theme'
d = os.path.join(icon_dir, root + theme_suffix + ext)
if os.path.exists(d):
return d
# No themed icon exists. Try the original name
d = os.path.join(icon_dir, icon_name)
return d if os.path.exists(d) else None


class ColumnIcon: # {{{

def __init__(self, formatter, model):
Expand Down Expand Up @@ -134,14 +148,8 @@ def __call__(self, id_, fmts, cache_index, db, icon_cache, icon_bitmap_cache,
dim = int(self.dpr * rh)
icon_dir = os.path.join(config_dir, 'cc_icons')
for icon in icons:
d = None
if is_dark_theme():
root,ext = os.path.splitext(icon)
d = os.path.join(icon_dir, root + '-dark' + ext)
d = d if os.path.exists(d) else None
if d is None:
d = os.path.join(icon_dir, icon)
if (os.path.exists(d)):
d = themed_icon_name(icon_dir, icon)
if d is not None:
bm = QPixmap(d)
scaled, nw, nh = fit_image(bm.width(), bm.height(), bm.width(), dim)
bm = bm.scaled(int(nw), int(nh), aspectRatioMode=Qt.AspectRatioMode.IgnoreAspectRatio,
Expand Down
23 changes: 15 additions & 8 deletions src/calibre/gui2/preferences/coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,22 @@ def create_filename_box():
def show_theme_help(self):
msg = '<p>'+ _(
'You can use different icons in light and dark themes. To do this, '
'add two icons to the icon list. The light theme icon will have '
'the "normal" name, for example "ok.png". The dark theme icon must '
'have the same name with "-dark" appended. For example, if the light '
'theme icon is named "ok.png" then the dark theme icon must be named '
'"ok-dark.png".'
'add two icons to the icon list. One of the icons must have either the '
'"plain" name, for example "ok.png", or the themed name, for example '
'"ok-for-light-theme.png". The other icon must have a themed name with '
'the same prefix, for example "ok-for-dark-theme.png". '
'</p><p>'
'When defining a rule, always use the "normal" name. The -dark icon will be '
'automatically substituted for the normal icon when a dark theme is '
'being used.'
'Example: if the light theme icon is named "ok.png" then the dark '
'theme icon must be named "ok-for-dark-theme.png". If the light '
'theme icon is named "ok-for-light-theme.png" then the dark theme '
'icon must be named either ok.png or "ok-for-dark-theme.png".'
'</p><p>'
'When defining a rule, use either of the icon names. The correct '
'icon for the theme will automatically be used, if it exists.'
'</p><p>'
'You are not required to change existing rules to use theming. Decide '
'the theme where the existing icon should be used then add the '
'other icon with the correct themed name. '
'</p><p>'
'Remember to add both the light and dark theme icons to the list of icons.'
) + '</p>'
Expand Down

0 comments on commit 5941de8

Please sign in to comment.