feat: use transient instead of y-or-n-p

This commit is contained in:
SqrtMinusOne 2022-08-14 17:21:39 +05:00
parent 633540c7b5
commit 98e9904a98
2 changed files with 28 additions and 25 deletions

View file

@ -186,11 +186,10 @@ If the variable is nil, the function does nothing."
(alist-get 'context pomm--state) "") (alist-get 'context pomm--state) "")
nil pomm-third-time-csv-history-file 'append 1))) nil pomm-third-time-csv-history-file 'append 1)))
(defun pomm-third-time-reset () (transient-define-prefix pomm-third-time-reset ()
"Reset the Third Time timer." ["Are you sure you want to reset the Third Time timer?"
(interactive) ("y" "Yes" (lambda () (interactive) (pomm-third-time--do-reset)))
(when (y-or-n-p "Are you sure you want to reset the Third Time timer? ") ("n" "No" transient-quit-one)])
(pomm-third-time--do-reset)))
(defun pomm-third-time--dispatch-current-sound () (defun pomm-third-time--dispatch-current-sound ()
"Dispatch an appropriate sound for the current state of the timer." "Dispatch an appropriate sound for the current state of the timer."
@ -373,21 +372,24 @@ Take a look at the `pomm-third-time' function for more details."
(unless pomm-third-time--timer (unless pomm-third-time--timer
(setq pomm-third-time--timer (run-with-timer 0 1 'pomm-third-time--on-tick)))) (setq pomm-third-time--timer (run-with-timer 0 1 'pomm-third-time--on-tick))))
(defun pomm-third-time-stop () (defun pomm-third-time--running-p ()
"Stop the Third Time timer." "Check if the timer is running."
(interactive) (eq (alist-get 'status pomm-third-time--state) 'running))
(pcase (alist-get 'status pomm-third-time--state)
('stopped (message "The timer is already stopped!")) (transient-define-prefix pomm-third-time-stop ()
('running ["This will reset the accumulated break time. Continue?"
(when (y-or-n-p "This will reset the accumulated break time. Continue? ") ("y" "Yes" (lambda () (interactive)
(pomm-third-time--store-current-to-history) (unless (pomm-third-time--can-stop-p)
(setf (alist-get 'status pomm-third-time--state) 'stopped (user-error "The timer is not running!"))
(alist-get 'current pomm-third-time--state) nil (pomm-third-time--store-current-to-history)
(alist-get 'last-changed-time pomm-third-time--state) (setf (alist-get 'status pomm-third-time--state) 'stopped
(time-convert nil 'integer)) (alist-get 'current pomm-third-time--state) nil
(run-hooks 'pomm-third-time-on-status-changed-hook) (alist-get 'last-changed-time pomm-third-time--state)
(when pomm-reset-context-on-iteration-end (time-convert nil 'integer))
(setf (alist-get 'context pomm-third-time--state) nil)))))) (run-hooks 'pomm-third-time-on-status-changed-hook)
(when pomm-reset-context-on-iteration-end
(setf (alist-get 'context pomm-third-time--state) nil))))
("n" "No" transient-quit-one)])
(defun pomm-third-time-switch () (defun pomm-third-time-switch ()
"Toggle work/break in the Third Time timer." "Toggle work/break in the Third Time timer."
@ -616,6 +618,8 @@ The class doesn't actually have any value, but this is necessary for transient."
["Commands" ["Commands"
:class transient-row :class transient-row
("s" "Start the timer" pomm-third-time-start :transient t) ("s" "Start the timer" pomm-third-time-start :transient t)
;; XXX I tried to use the `:if' predicate here, but unfortunately
;; visibilty doesn't refresh with `:transient t'
("S" "Stop the timer" pomm-third-time-stop :transient t) ("S" "Stop the timer" pomm-third-time-stop :transient t)
("b" "Switch work/break" pomm-third-time-switch :transient t) ("b" "Switch work/break" pomm-third-time-switch :transient t)
("R" "Reset" pomm-third-time-reset :transient t) ("R" "Reset" pomm-third-time-reset :transient t)

View file

@ -312,11 +312,10 @@ variable doesn't exist, function does nothing."
(or (alist-get 'context pomm--state) "")) (or (alist-get 'context pomm--state) ""))
nil pomm-csv-history-file 'append 1))) nil pomm-csv-history-file 'append 1)))
(defun pomm-reset () (transient-define-prefix pomm-reset ()
"Reset the Pomodoro timer." ["Are you sure you want to reset the Pomodoro timer?"
(interactive) ("y" "Yes" (lambda () (interactive) (pomm--do-reset)))
(when (y-or-n-p "Are you sure you want to reset the Pomodoro timer? ") ("n" "No" transient-quit-one)])
(pomm--do-reset)))
(defun pomm--maybe-play-sound (kind) (defun pomm--maybe-play-sound (kind)
"Play a sound of KIND. "Play a sound of KIND.