prevent crash on theme change and improve theme changing

This commit is contained in:
Izalia Mae 2022-08-27 21:21:42 -04:00
parent 287c256794
commit 7a1b408e26
4 changed files with 46 additions and 24 deletions

View file

@ -108,6 +108,26 @@ function bitwarden_unlock() {
}
function toggle_theme(hash) {
const theme = document.getElementById(hash);
let activate = theme.getElementsByClassName('activate')[0];
const text = (activate.innerHTML == 'Enable' ? hash : 'default');
const new_text = (activate.innerHTML == 'Enable' ? 'Disable' : 'Enable')
request(`/preferences/theme/set/${text}`, (response, body) => {
if (response.status == 200) {
for (let elem of document.getElementsByClassName('theme')) {
if (elem.id != hash) {
elem.getElementsByClassName('activate')[0].innerHTML = 'Enable';
} else {
elem.getElementsByClassName('activate')[0].innerHTML = new_text;
}
}
}
})
}
function handle_reset_config(key) {
window.location = `/preferences/reset/${key}`;
}

View file

@ -28,15 +28,8 @@
%details.category.section open
%summary << System Themes
-for theme in themes.system.values()
%details.section.theme.system
%summary.grid-container
%span.grid-item -> =theme.name
-if not themes.current and themes.current_system == theme.hash
%a.grid-item.button href='{{var.local}}/preferences/theme/set/default' << Disable
-else
%a.grid-item.button href='{{var.local}}/preferences/theme/set/{{theme.hash}}' << Enable
%details.section.theme.system id='{{theme.hash}}' open
%summary -> =theme.name
.container
.author << Author: {{theme.author}}
@ -46,19 +39,18 @@
-if theme.url
%a.url href='{{theme.url}}' << Website
.buttons
-if not themes.current and themes.current_system == theme.hash
%a.grid-item.button.activate onclick='toggle_theme("{{theme.hash}}")' << Disable
-else
%a.grid-item.button.activate onclick='toggle_theme("{{theme.hash}}")' << Enable
%details.category.section open
%summary << User Themes
-for theme in themes.user.values()
%details.section.theme.user
%summary.grid-container
%span.grid-item -> =theme.name
%a.grid-item.button href='{{var.local}}/preferences/theme/delete/{{theme.hash}}' << Delete
-if themes.current == theme.hash
%a.grid-item.button href='{{var.local}}/preferences/theme/set/default' << Disable
-else
%a.grid-item.button href='{{var.local}}/preferences/theme/set/{{theme.hash}}' << Enable
%details.section.theme.user id='{{theme.hash}}' open
%summary -> =theme.name
.container
.author << Author: {{theme.author}}
@ -69,6 +61,15 @@
-if theme.url
%a.url href='{{theme.url}}' << Website
.buttons
-if themes.current == theme.hash
%a.grid-item.button.activate onclick='toggle_theme("{{theme.hash}}")' << Disable
-else
%a.grid-item.button.activate onclick='toggle_theme("{{theme.hash}}")' << Enable
%a.grid-item.button href='delete_item("/preferences/theme/delete", "{{theme.hash}}")' << Delete
%details.category.section {{'open' if pass_type == 'bitwarden' else ''}}
%summary << Password Storage: Bitwarden
.container

View file

@ -526,7 +526,7 @@ def preferences_theme_set(handler, request, hash):
except KeyError:
return request.error(f'Cannot find theme with hash: {hash}', 404)
return request.redirect('/preferences', message)
return request.ok_or_redirect(message)
### Search ###

View file

@ -13,7 +13,7 @@ from watchdog.observers import Observer
from . import var, __version__ as version, __software__ as swname
from .enums import StylePriority
from .functions import ComponentBase
from .functions import ComponentBase, run_in_gui_thread
class Themes(ComponentBase, ObjectBase):
@ -355,11 +355,12 @@ class MainWatchHandler(WatcherBase):
logging.verbose('Reloading main css')
try:
self.themes.load_main()
run_in_gui_thread(self.themes.load_main)
except:
traceback.print_exc()
class SystemWatchHandler(WatcherBase):
def on_modified(self, event):
path = Path(event.src_path)
@ -371,7 +372,7 @@ class SystemWatchHandler(WatcherBase):
traceback.print_exc()
return
self.handle_theme_modified(theme, path)
run_in_gui_thread(self.handle_theme_modified, theme, path)
class UserWatchHandler(WatcherBase):
@ -393,7 +394,7 @@ class UserWatchHandler(WatcherBase):
if path.name == 'manifest.ini':
if self.themes.current == theme.path:
self.themes.unset()
run_in_gui_thread(self.themes.unset)
del self.themes.user[theme.path]