diff options
author | Miguel Ángel Moreno <mail@migalmoreno.com> | 2023-02-20 14:40:00 +0100 |
---|---|---|
committer | Miguel Ángel Moreno <mail@migalmoreno.com> | 2023-02-20 14:40:00 +0100 |
commit | a7789f7a54180b44e737ee1ca13fec58380b1e8e (patch) | |
tree | deac597c6b72a22bd5a5df1fc332e1342a3b42af | |
parent | 2faf4690af0da8aa218dcdd2c1acfd1aa76b0c47 (diff) |
fix: Encapsulate automatic theme switching logic
-rw-r--r-- | tailor.lisp | 85 |
1 files changed, 43 insertions, 42 deletions
diff --git a/tailor.lisp b/tailor.lisp index 2782f7a..d2ef3d4 100644 --- a/tailor.lisp +++ b/tailor.lisp @@ -224,30 +224,17 @@ If DARK, find the first dark `user-theme'." (unless (or (not themes) (find (theme *browser*) themes :test #'equal) *current-theme*) - (or (load-automatic-theme mode) - (when main - (load-theme - (id - (case main - (:light light-theme) - (:dark dark-theme) - (t (find main themes :key #'id)))) - mode)) - (load-theme (id (car themes)) mode))) - (unless (or (not auto-p) (equal auto-p :gtk)) - (flet ((set-timer (timer theme threshold) - (unless timer - (sb-ext:schedule-timer - (setf timer (sb-ext:make-timer - (lambda () - (load-theme (id theme) mode)) - :thread t)) - (local-time:timestamp-to-universal threshold) - :absolute-p t - :repeat-interval 86400)))) - (hooks:once-on (nyxt:buffer-loaded-hook (buffer mode)) (_) - (set-timer *light-theme-timer* light-theme light-theme-threshold) - (set-timer *dark-theme-timer* dark-theme dark-theme-threshold))))))) + (if auto-p + (load-automatic-theme mode) + (if main + (load-theme + (id + (case main + (:light light-theme) + (:dark dark-theme) + (t (find main themes :key #'id)))) + mode) + (load-theme (id (car themes)) mode))))))) (defmethod nyxt:disable ((mode tailor-mode) &key) (hooks:remove-hook (nyxt:buffer-before-make-hook *browser*) 'style-web-buffer) @@ -292,24 +279,38 @@ If DARK, find the first dark `user-theme'." (defmethod load-automatic-theme ((mode tailor-mode)) "Automatically set the theme based on the specified criteria in MODE." - (alex:when-let ((auto (auto-p mode))) - (let* ((light-theme (or (when (consp (main mode)) - (car (main mode))) - (find-theme-variant mode))) - (dark-theme (or (when (consp (main mode)) - (cdr (main mode))) - (find-theme-variant mode :dark t))) - (light-theme-threshold (local-time:timestamp+ - (today) (light-theme-threshold mode) :sec)) - (dark-theme-threshold (local-time:timestamp+ - (today) (dark-theme-threshold mode) :sec))) - (case auto - (:gtk - (if (or (str:containsp ":light" (uiop:getenv "GTK_THEME")) - (null (uiop:getenv "GTK_THEME"))) - (load-theme (id light-theme) mode) - (load-theme (id dark-theme) mode))) - (t + (let* ((light-theme (or (when (consp (main mode)) + (car (main mode))) + (find-theme mode))) + (dark-theme (or (when (consp (main mode)) + (cdr (main mode))) + (find-theme mode :dark t))) + (light-theme-threshold (local-time:timestamp+ + (today) (light-theme-threshold mode) :sec)) + (dark-theme-threshold (local-time:timestamp+ + (today) (dark-theme-threshold mode) :sec))) + (case (auto-p mode) + (:gtk + (if (or (str:containsp ":light" (uiop:getenv "GTK_THEME")) + (null (uiop:getenv "GTK_THEME"))) + (load-theme (id light-theme) mode) + (load-theme (id dark-theme) mode))) + (t + (flet ((set-timer (timer theme threshold) + (unless timer + (sb-ext:schedule-timer + (setf timer (sb-ext:make-timer + (lambda () + (load-theme (id theme) mode)) + :thread t)) + (local-time:timestamp-to-universal threshold) + :absolute-p t + :repeat-interval 86400)))) + (nyxt:run-thread "tailor light-theme timer" + (set-timer *light-theme-timer* light-theme light-theme-threshold)) + (nyxt:run-thread "tailor dark-theme timer" + (sleep 0.1) + (set-timer *dark-theme-timer* dark-theme dark-theme-threshold)) (cond ((and (local-time:timestamp> (local-time:now) dark-theme-threshold) (not (local-time:timestamp< (local-time:now) light-theme-threshold))) |