Skip to content

Commit

Permalink
[WebUI] Refactor changing theme
Browse files Browse the repository at this point in the history
Simplify searching for themes and ensure theme is ordered last.

Ideally themes would be set client-side but seems to be quite tricky to
accomplish with ExtJS.
  • Loading branch information
cas-- committed Feb 19, 2024
1 parent ee97864 commit 5dd7aa5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion deluge/ui/web/json_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,4 @@ def get_themes(self):
Returns:
list: of themes ``[theme1, theme2, ...]``
"""
return component.get('DelugeWeb').get_themes_list()
return component.get('DelugeWeb').get_themes()
34 changes: 21 additions & 13 deletions deluge/ui/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import mimetypes
import os
import tempfile
from pathlib import Path

from twisted.application import internet, service
from twisted.internet import defer, reactor
Expand Down Expand Up @@ -539,19 +540,27 @@ def __init__(self):
self.putChild(b'themes', Themes(rpath('themes')))
self.putChild(b'tracker', Tracker())

theme = component.get('DelugeWeb').config['theme']
if not os.path.isfile(rpath('themes', 'css', f'xtheme-{theme}.css')):
theme = CONFIG_DEFAULTS.get('theme')
self.__stylesheets.insert(1, f'themes/css/xtheme-{theme}.css')

@property
def stylesheets(self):
return self.__stylesheets

def change_theme(self, theme: str):
def get_themes(self):
themes_dir = Path(rpath('themes', 'css'))
themes = [
theme.stem.split('xtheme-')[1] for theme in themes_dir.glob('xtheme-*.css')
]
themes = [(theme, _(theme.capitalize())) for theme in themes]
return themes

def set_theme(self, theme: str):
if not os.path.isfile(rpath('themes', 'css', f'xtheme-{theme}.css')):
theme = CONFIG_DEFAULTS.get('theme')
self.__stylesheets[1] = f'themes/css/xtheme-{theme}.css'
self.__theme = f'themes/css/xtheme-{theme}.css'

# Only one xtheme CSS, ordered last to override other styles.
if 'xtheme-' in self.stylesheets[-1]:
self.__stylesheets.pop()
self.__stylesheets.append(self.__theme)

def add_script(self, script):
"""
Expand Down Expand Up @@ -688,6 +697,8 @@ def __init__(self, options=None, daemon=True):
elif options.no_ssl:
self.https = False

self.top_level.set_theme(self.config['theme'])

setup_translation()

# Remove twisted version number from 'server' http-header for security reasons
Expand Down Expand Up @@ -794,14 +805,11 @@ def _migrate_config_1_to_2(self, config):
config['language'] = CONFIG_DEFAULTS['language']
return config

def get_themes_list(self):
return [
(file[7:-4], _(file[7:-4].capitalize()))
for file in os.listdir(rpath('themes', 'css'))
]
def get_themes(self):
return self.top_level.get_themes()

def set_theme(self, theme: str):
self.top_level.change_theme(theme)
self.top_level.set_theme(theme)


if __name__ == '__builtin__':
Expand Down

0 comments on commit 5dd7aa5

Please sign in to comment.