Skip to content

Commit

Permalink
Set themes even if detection mechanism fails
Browse files Browse the repository at this point in the history
Previously, failing to “determine a viable theme detection mechanism” would
error, preventing the rest of `auto-dark-mode` setup from running. This is now a
warning, and you are effectively left in “manual” mode.

This is technically a fix for LionyxML#66, but doesn’t address all the related changes
there. Those will be addressed in LionyxML#62. It also partially addresses LionyxML#73 by adding
`auto-dark-toggle-appearance`.
  • Loading branch information
sellout committed Oct 17, 2024
1 parent 80d77a9 commit 5f7f233
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion auto-dark.el
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,19 @@ This will load themes if necessary."
(warn "Failed to enable theme(s): %s"
(mapconcat #'symbol-name failures ", "))))))

(defun auto-dark-toggle-appearance ()
"Switch between light and dark mode.
If `auto-dark-detection-method' is nil, this will persist until the next time
this is called. Otherwise, it could switch to the system appearance at any
time."
(interactive)
(auto-dark--set-theme (if (eq auto-dark--last-dark-mode-state 'dark)
'light
;; NB: This does _something_ even if we don’t know
;; what the previous state was, since the user
;; explicitly requested a change.
'dark)))

(defun auto-dark--set-theme (appearance)
"Set light/dark theme Argument APPEARANCE should be light or dark."
(setq auto-dark--last-dark-mode-state appearance)
Expand Down Expand Up @@ -346,7 +359,9 @@ Remove theme change callback registered with D-Bus."
((eq system-type 'windows-nt)
'winreg)
(t
(error "Could not determine a viable theme detection mechanism!"))))
(lwarn 'auto-dark :error "Could not determine a viable theme detection \
mechanism! You can use ‘auto-dark-toggle-appearance’ to manually switch between \
modes."))))

;;;###autoload
(define-minor-mode auto-dark-mode
Expand Down
10 changes: 10 additions & 0 deletions tests/basic.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
(expect auto-dark-themes :to-equal '((wombat) (leuven)))
(expect custom-enabled-themes :to-equal '(leuven))))

(describe "toggling"
(before-each
(auto-dark-toggle-appearance))
(it "should invert the current appearance"
(expect custom-enabled-themes :to-equal '(wombat)))
(it "should invert the current appearance again"
(expect custom-enabled-themes :to-equal '(leuven)))
(it "should invert the current appearance and again"
(expect custom-enabled-themes :to-equal '(wombat))))

;; Restore the original state
(unless auto-dark-tests--original-state
(auto-dark-mode -1))
Expand Down

0 comments on commit 5f7f233

Please sign in to comment.