Created
June 30, 2024 14:56
-
-
Save cg505/8fbb74d14eedbe9f3e8c8d613bbcd995 to your computer and use it in GitHub Desktop.
Automatically switch between catppuccin flavors when system light/dark mode changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require 'dbus) | |
| (defun theme--color-scheme-to-catppuccin-flavor (scheme) | |
| (cond | |
| ((= 1 scheme) 'frappe ;; change to preferred dark theme | |
| ) | |
| ((or (= 0 scheme) (= 2 scheme)) | |
| ;; note: prefer-light has some issues (e.g. incorrect wallpaper) in gnome, | |
| ;; so we use default for light mode | |
| 'latte) | |
| (t nil))) | |
| (defun theme--set-initial-flavor () | |
| (let ((setting-values (dbus-call-method | |
| :session | |
| "org.freedesktop.portal.Desktop" | |
| "/org/freedesktop/portal/desktop" | |
| "org.freedesktop.portal.Settings" | |
| "ReadOne" | |
| "org.freedesktop.appearance" | |
| "color-scheme"))) | |
| (setf catppuccin-flavor | |
| (theme--color-scheme-to-catppuccin-flavor (car setting-values))))) | |
| (theme--set-initial-flavor) | |
| (load-theme 'catppuccin 'no-confirm) | |
| (defun theme--handle-dbus-event (namespace key values) | |
| "Handler for FreeDesktop theme changes." | |
| (when (and (string= namespace "org.freedesktop.appearance") | |
| (string= key "color-scheme")) | |
| (let* ((scheme (car values)) | |
| ;; 0=default, 1=prefer-dark, 2=prefer-light | |
| (flavor (theme--color-scheme-to-catppuccin-flavor scheme))) | |
| (cond | |
| ((eq flavor nil) | |
| (message "I don't know how to handle scheme: %s" scheme)) | |
| ((not (eq flavor catppuccin-flavor)) | |
| (setf catppuccin-flavor flavor) | |
| (catppuccin-reload)))))) | |
| (dbus-register-signal :session | |
| "org.freedesktop.portal.Desktop" | |
| "/org/freedesktop/portal/desktop" | |
| "org.freedesktop.portal.Settings" | |
| "SettingChanged" | |
| #'theme--handle-dbus-event) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://emacs.stackexchange.com/questions/69740/auto-detect-system-dark-mode-or-light-mode-and-select-themes-based-on-that
Alternative: https://github.com/LionyxML/auto-dark-emacs + https://www.rahuljuliato.com/posts/auto-dark-catppuccin