Also include plugin icons when building resource files

And handle sub-directories more efficiently by flattening into a
single theme directory
This commit is contained in:
Kovid Goyal 2022-01-11 18:41:03 +05:30
parent 4bb2cb27b3
commit 3650033173
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 51 additions and 42 deletions

View File

@ -69,7 +69,6 @@ builtin_decorations = {
'wavy': {'text-decoration-style': 'wavy', 'text-decoration-color': 'red', 'text-decoration-line': 'underline'},
'strikeout': {'text-decoration-line': 'line-through', 'text-decoration-color': 'red'},
}
icons_subdirs = ('devices', 'plugins', 'mimetypes')
_osx_ver = None

View File

@ -23,9 +23,8 @@ from threading import Lock, RLock
import calibre.gui2.pyqt6_compat as pqc
from calibre import as_unicode, prints
from calibre.constants import (
DEBUG, __appname__ as APP_UID, __version__, config_dir, icons_subdirs,
is_running_from_develop, isbsd, isfrozen, islinux, ismacos, iswindows, isxp,
plugins_loc
DEBUG, __appname__ as APP_UID, __version__, config_dir, is_running_from_develop,
isbsd, isfrozen, islinux, ismacos, iswindows, isxp, plugins_loc
)
from calibre.ebooks.metadata import MetaInformation
from calibre.gui2.linux_file_dialogs import (
@ -110,21 +109,21 @@ class IconResourceManager:
self.override_icon_path = q
legacy_theme_metadata = os.path.join(q, 'icon-theme.json')
if os.path.exists(legacy_theme_metadata):
self.migrate_legacy_icon_theme()
self.migrate_legacy_icon_theme(legacy_theme_metadata)
self.register_user_resource_files()
def migrate_legacy_icon_theme(self, legacy_theme_metadata):
from calibre.utils.rcc import compile_icon_dir_as_themes
import shutil
from calibre.utils.rcc import compile_icon_dir_as_themes
images = os.path.dirname(legacy_theme_metadata)
os.replace(legacy_theme_metadata, os.path.join(images, 'metadata.json'))
compile_icon_dir_as_themes(
images, self.user_theme_resource_file('any'), theme_name='calibre-user-any', inherits='calibre-default')
for x in os.listdir(images):
q = os.path.join(images, x)
if os.path.isdir(q):
if x in icons_subdirs:
shutil.rmtree(q)
if os.path.isdir(q) and q != 'textures':
shutil.rmtree(q)
else:
os.remove(q)
@ -139,9 +138,7 @@ class IconResourceManager:
q = os.path.join(self.override_icon_path, name)
if os.path.exists(q):
return QIcon(q)
parts = name.split('/')
if len(parts) == 2 and parts[0] in icons_subdirs:
name = parts[1]
name = '__'.join(name.split('/'))
return QIcon.fromTheme(os.path.splitext(name)[0])
def set_theme(self, is_dark_theme):

View File

@ -8,7 +8,6 @@ import tempfile
from posixpath import normpath
from qt.core import QFile, QIODevice
from calibre.constants import icons_subdirs
from calibre_extensions import rcc_backend
@ -38,7 +37,7 @@ def index_theme(name, inherits=''):
if inherits:
lines.append(f'Inherits={inherits}')
lines.append('')
subdirs = ['images'] + [f'images/{x}' for x in icons_subdirs]
subdirs = ['images']
for sb in subdirs:
lines += [f'[{sb}]', f'Size={sz}', f'MinSize={min_sz}', f'MaxSize={max_sz}', '']
return '\n'.join(lines)
@ -53,6 +52,7 @@ def compile_icon_dir_as_themes(
print(f' <qresource prefix="{prefix}">', file=qrc)
def file(name):
name = name.replace('\\', '/')
print(f' <file>{normpath(name)}</file>', file=qrc)
specific_themes = []
@ -60,7 +60,7 @@ def compile_icon_dir_as_themes(
specific_themes = [theme_name + '-dark', theme_name + '-light']
for q in [theme_name] + specific_themes:
os.mkdir(os.path.join(tdir, q))
for sd in ['images'] + [f'images/{x}' for x in icons_subdirs]:
for sd in ['images']:
os.makedirs(os.path.join(tdir, q, sd))
with open(os.path.join(tdir, theme_name, 'index.theme'), 'w') as f:
f.write(index_theme(theme_name, inherits))
@ -70,33 +70,46 @@ def compile_icon_dir_as_themes(
f.write(index_theme(q, inherits=theme_name))
file(f'{q}/index.theme')
for sdir in ('.',) + icons_subdirs:
s = os.path.join(path_to_dir, sdir)
for x in os.listdir(s):
base, ext = os.path.splitext(x)
theme_dir = theme_name
dest_name = x
if ext.lower() not in ('.png',):
if sdir == '.' and x == 'metadata.json':
dest = theme_dir, dest_name
os.link(os.path.join(s, x), os.path.join(tdir, *dest))
file('/'.join(dest))
continue
if base.endswith('-for-dark-theme'):
if for_theme == 'any':
theme_dir += '-dark'
elif for_theme == 'light':
continue
dest_name = x.replace('-for-dark-theme', '')
elif base.endswith('-for-light-theme'):
if for_theme == 'any':
theme_dir += '-light'
elif for_theme == 'dark':
continue
dest_name = x.replace('-for-light-theme', '')
dest = theme_dir, 'images', sdir, dest_name
os.link(os.path.join(s, x), os.path.join(tdir, *dest))
file('/'.join(dest))
def handle_image(image_path):
image_name = os.path.basename(image_path)
rp = os.path.relpath(os.path.dirname(image_path), path_to_dir).replace('\\', '/').strip('/').replace('/', '__')
if rp == '.':
rp = ''
else:
rp += '__'
base, ext = os.path.splitext(image_name)
theme_dir = theme_name
dest_name = image_name
if ext.lower() not in ('.png',):
if image_name == 'metadata.json':
dest = theme_dir, dest_name
os.link(image_path, os.path.join(tdir, *dest))
file('/'.join(dest))
return
if base.endswith('-for-dark-theme'):
if for_theme == 'any':
theme_dir += '-dark'
elif for_theme == 'light':
return
dest_name = dest_name.replace('-for-dark-theme', '')
elif base.endswith('-for-light-theme'):
if for_theme == 'any':
theme_dir += '-light'
elif for_theme == 'dark':
return
dest_name = dest_name.replace('-for-light-theme', '')
dest = theme_dir, 'images', (rp + dest_name)
os.link(image_path, os.path.join(tdir, *dest))
file('/'.join(dest))
for dirpath, dirnames, filenames in os.walk(path_to_dir):
if 'textures' in dirnames:
dirnames.remove('textures')
if os.path.basename(tdir) in dirnames:
dirnames.remove(os.path.basename(tdir))
for f in filenames:
handle_image(os.path.join(dirpath, f))
print(' </qresource>', file=qrc)
print('</RCC>', file=qrc)
qrc.close()