aboutsummaryrefslogtreecommitdiff
path: root/tailor.lisp
blob: 758a75a6b27a205a109480442d72dadc219e4810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(in-package #:nx-tailor)
(nyxt:use-nyxt-package-nicknames)

(defvar *styles* '()
  "The list of `style' objects the user instantiates
by including `with-style' invocations in their configuration.")

(defvar *current-theme* nil
  "The currently-active `user-theme'.")

(defvar *dark-theme-timer* nil
  "`sb-ext:timer' used to apply the dark theme.")

(defvar *light-theme-timer* nil
  "`sb-ext:timer' used to apply the light theme.")

(defun today ()
  "Compute the correct timestamp for today according to
 the local timezone given by `local-time:*default-timezone*'."
  (local-time:adjust-timestamp (local-time:now)
    (set :hour 0)
    (set :minute 0)
    (set :sec 0)
    (set :nsec 0)))

(define-class theme-source (prompter:source)
  ((prompter:name "User themes")
   (prompter:constructor (themes (current-tailor-mode)))
   (prompter:filter-preprocessor #'prompter:filter-exact-matches)
   (prompter:active-attributes-keys '("Name"))))

(define-class style ()
  ((sym
    nil
    :type (or null symbol)
    :documentation "The symbol of the class the style belongs to.")
   (fn
    nil
    :type (or null function)
     :documentation "A function that takes a theme and returns a
new style based on its value."))
  (:export-class-name-p t)
  (:export-accessor-names-p t))

(define-class user-theme (theme:theme)
  ((name
    nil
    :type (or null symbol)))
  (:export-class-name-p t)
  (:export-accessor-names-p t))

(defun current-tailor-mode ()
  "Return `tailor-mode' if it's active in the current buffer."
  (alex:when-let ((mode (sym:resolve-symbol :tailor-mode :mode '(:nx-tailor))))
    (find-submode mode)))

(sera:export-always 'with-style)
(defmacro with-style (class-sym &body rules)
  "Add style RULES to CLASS-SYM and apply RULES.
This automatically adds the user-defined style to the list of
`*styles*' with the CLASS-SYM identifier and a function that takes
a single argument for the current theme to apply style RULES to."
  `(progn
     (pushnew
      (make-instance 'style
                     :sym ,class-sym
                     :fn (lambda (theme)
                           (theme:themed-css theme
                             ,@rules)))
      *styles* :key #'sym)
     (str:concat nyxt:%slot-default%
                 (theme:themed-css (theme *browser*)
                   ,@rules))))

(defun compute-current-style (style obj &optional style-slot)
  "Return the current STYLE of OBJ.
Optionally, retrieve the original style of OBJ via STYLE-SLOT."
  (str:concat (eval (getf (mopu:slot-properties
                           (find-class (class-name (class-of obj)))
                           (or style-slot 'nyxt:style))
                          :initform))
              (when style
                (funcall (fn style) *current-theme*))))

(defgeneric load-style (style interface)
  (:documentation "Load the STYLE of INTERFACE."))

(defmethod load-style (style (buffer nyxt:web-buffer))
  (flet ((style-web-buffer (buffer)
           (setf (nyxt:style buffer)
                 (compute-current-style style buffer))))
    (when (nyxt:internal-url-p (url buffer))
      (style-web-buffer buffer))
    (hooks:add-hook
     (nyxt:buffer-before-make-hook *browser*)
     (make-instance 'hooks:handler
                    :fn #'style-web-buffer
                    :name 'style-web-buffer))
    (loop for buffer in (nyxt::buffer-initial-suggestions)
          do (when (nyxt:internal-url-p (url buffer))
               (style-web-buffer buffer)
               (nyxt:buffer-load (url buffer) :buffer buffer)))))

(defmethod load-style (style (prompt-buffer nyxt:prompt-buffer))
  (flet ((style-prompt-buffer (prompt)
           (setf (nyxt:style prompt)
                 (compute-current-style style prompt-buffer))))
    (style-prompt-buffer prompt-buffer)
    (hooks:add-hook
     (nyxt:prompt-buffer-make-hook *browser*)
     (make-instance 'hooks:handler
                    :fn #'style-prompt-buffer
                    :name 'style-prompt-buffer))))

(defmethod load-style (style (window nyxt:window))
  (flet ((style-message-buffer (window)
           (setf (nyxt::message-buffer-style window)
                 (compute-current-style
                  style window
                  'nyxt:message-buffer-style))))
    (style-message-buffer window)
    (hooks:add-hook
     (nyxt:window-make-hook *browser*)
     (make-instance 'hooks:handler
                    :fn #'style-message-buffer
                    :name 'style-message-buffer))
    (nyxt:echo "")))

(defmethod load-style (style (status-buffer nyxt:status-buffer))
  (flet ((style-status-buffer (status-buffer)
           (setf (nyxt:style status-buffer)
                 (compute-current-style style status-buffer))))
    (style-status-buffer status-buffer)
    (hooks:add-hook
     (nyxt:window-make-hook *browser*)
     (make-instance 'hooks:handler
                    :fn #'style-status-buffer
                    :name 'style-status-buffer))
    (nyxt::print-status)))

(defmethod load-style (style (nyxt-mode nyxt:mode))
  (flet ((style-mode (mode)
           (setf (slot-value mode 'nyxt:style)
                 (compute-current-style style mode))))
    (hooks:once-on (nyxt:buffer-loaded-hook (buffer nyxt-mode)) (buffer)
      (style-mode nyxt-mode)
      (hooks:add-hook (nyxt:enable-mode-hook buffer)
                      (make-instance 'hooks:handler
                                     :fn #'style-mode
                                     :name 'style-mode)))))

(define-mode tailor-mode ()
  "Manage and apply user-defined browser themes on predefined criteria."
  ((themes
    '()
    :type list
    :documentation "`user-theme' objects among which to select the
 main interface theme.")
   (main
    nil
    :type (or symbol :dark :light cons null)
    :documentation "If a single theme name is specified, it will be chosen
at startup if `auto-p' is `nil'. If either `:dark' or `:light' is passed,
the corresponding `user-theme' will be selected at startup.
If a cons pair is specified and `auto-p' is non-`nil', the light and dark
theme variants will be selected from the pair in the form
(LIGHT-THEME . DARK-THEME) where each cell is the name of the theme.")
   (auto-p
    nil
    :type (or boolean :time :gtk)
    :documentation "Whether to automatically apply a `theme'. If `:time' or `t',
it will apply the corresponding theme automatically based on the time of the day.
If `:gtk' it will be applied based on the value of the `GTK_THEME' environment
variable.")
   (light-theme-threshold
    (* 6 60 60)
    :type number
    :documentation "Number of seconds after midnight when the light theme
should be activated.")
   (dark-theme-threshold
    (* 21 60 60)
    :type number
    :documentation "Number of seconds after midnight when the dark theme
should be activated.")
   (nyxt:glyph "⏾")))

(defmethod nyxt:customize-instance :after ((mode tailor-mode) &key)
  (with-slots (light-theme-threshold
               dark-theme-threshold
               main
               themes)
      mode
    (flet ((find-theme (name)
             (find name themes :key #'name)))
      (setf light-theme-threshold (round light-theme-threshold))
      (setf dark-theme-threshold (round dark-theme-threshold))
      (when (consp main)
        (setf main (cons (find-theme (car main)) (find-theme (cdr main))))))))

(defun find-theme (mode &key dark)
  "Find the first light `user-theme' in MODE.
If DARK, find the first dark `user-theme'."
  (if dark
      (find-if #'theme:dark-p (themes mode))
      (find-if-not #'theme:dark-p (themes mode))))

(define-command-global load-theme (&optional name (mode (current-tailor-mode)))
  "Load a custom `user-theme' with NAME from MODE, apply it, and return it."
  (flet ((find-style (sym)
           (find sym *styles* :key #'sym)))
    (let ((theme (or (and name (find name (themes mode) :key #'name))
                     (nyxt:prompt1
                      :prompt "Load theme"
                      :sources (make-instance 'theme-source))))
          (prompt-buffer (make-instance 'nyxt:prompt-buffer
                                        :window (current-window)))
          (modes-with-style
            (remove-if-not (lambda (mode)
                             (some (lambda (slot)
                                     (eq 'nyxt:style slot))
                                   (mopu:slot-names (class-of mode))))
                           (nyxt:modes (buffer mode)))))
      (setf *current-theme* theme)
      (setf (theme *browser*) theme)
      (load-style (find-style 'nyxt:window) (current-window))
      (load-style (find-style 'nyxt:web-buffer) (buffer mode))
      (load-style (find-style 'nyxt:status-buffer) (nyxt:status-buffer (current-window)))
      (load-style (find-style 'nyxt:prompt-buffer) prompt-buffer)
      (loop for mode-style in modes-with-style
            do (load-style (find-style (class-name (class-of mode-style))) mode-style))
      (nyxt::echo (format nil "Loaded theme ~a" (name theme)))
      theme)))

(defmethod nyxt:enable ((mode tailor-mode) &key)
  (with-slots (main themes auto-p) mode
    (let ((light-theme (or (when (consp main)
                             (car main))
                           (find-theme mode)))
          (dark-theme (or (when (consp main)
                            (cdr main))
                          (find-theme mode :dark t))))
      (unless (or (not themes)
                  (find (theme *browser*) themes :test #'equal)
                  *current-theme*)
        (if auto-p
            (load-automatic-theme mode)
            (if main
                (load-theme
                 (name
                  (case main
                    (:light light-theme)
                    (:dark dark-theme)
                    (t (find main themes :key #'name))))
                 mode)
                (load-theme (name (car themes)) mode)))))))

(defmethod nyxt:disable ((mode tailor-mode) &key)
  (hooks:remove-hook (nyxt:buffer-before-make-hook *browser*) 'style-web-buffer)
  (hooks:remove-hook (nyxt:prompt-buffer-make-hook *browser*) 'style-prompt-buffer)
  (hooks:remove-hook (nyxt:window-make-hook *browser*) 'style-message-buffer)
  (hooks:remove-hook (nyxt:window-make-hook *browser*) 'style-status-buffer)
  (setf (theme *browser*) *current-theme*)
  (setf *current-theme* nil)
  (when *light-theme-timer*
    (sb-ext:unschedule-timer *light-theme-timer*))
  (when *dark-theme-timer*
    (sb-ext:unschedule-timer *dark-theme-timer*)))

(defmethod load-automatic-theme ((mode tailor-mode))
  "Automatically set the theme based on the specified criteria in MODE."
  (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 (name light-theme) mode)
           (load-theme (name 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 (name 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)))
            (load-theme (name dark-theme) mode))
           ((local-time:timestamp< (local-time:now) light-theme-threshold)
            (load-theme (name dark-theme) mode))
           ((local-time:timestamp> (local-time:now) light-theme-threshold)
            (load-theme (name light-theme) mode))))))))