Commit 6b567990 authored by Ricardo J. Mendez's avatar Ricardo J. Mendez

Setting up alarm for periodically checking the focused window

Chrome will not always send us a focus event after switching away from
a Chrome window to another application and then back. Whenever we
get a message that no windows have focus, I'm setting up an alarm to
periodically check for if we have gotten focus again (since tests
show that get-last-focused will return window with :focused true even
if we did not get a notification).

https://app.asana.com/0/17212341117686/59269883974921
parent 76637969
......@@ -15,7 +15,7 @@
"128": "icon128.png"
},
"permissions": [
"tabs", "storage", "idle"
"tabs", "storage", "idle", "alarms"
],
"background": {
"scripts": [
......
......@@ -4,6 +4,7 @@
[booklet.utils :refer [on-channel from-transit to-transit]]
[cognitect.transit :as transit]
[khroma.log :as console]
[khroma.alarms :as alarms]
[khroma.runtime :as runtime]
[khroma.windows :as windows]
[khroma.storage :as storage]
......@@ -19,9 +20,11 @@
;;;; Functions
;;;;-------------------------------------
(def window-alarm "window-alarm")
(defn now [] (.now js/Date))
(def relevant-tab-keys [:windowId :id :active :url :selected :start-time :title :favIconUrl])
(def select-tab-keys #(select-keys % relevant-tab-keys))
......@@ -29,6 +32,18 @@
(def url-time-path [:data :url-times])
(defn check-window-status
"Checks if we have any focused window, and compares it against the
window id for the active tab. If they do not match, it dispatches
a ::window-focus message"
[tab]
(go
(let [last-focused (<! (windows/get-last-focused {:populate false}))]
(when (and (:focused last-focused)
(not= (:id last-focused) (:windowId tab)))
(dispatch [::window-focus {:windowId (:id last-focused)}])))))
(defn process-tab
"Filters a tab down to the relevant keys, and adds a start time which is
now if the tab is active, or 0 otherwise.
......@@ -167,6 +182,15 @@
)))
(register-handler
::on-alarm
(fn [app-state [_ {:keys [alarm]}]]
(when (= window-alarm (:name alarm))
(check-window-status (:active-tab app-state)))
app-state
))
(register-handler
:suspend
;; The message itself is not relevant, we only care that we are being suspended
......@@ -245,12 +269,15 @@
(fn [app-state [_ {:keys [windowId]}]]
(console/trace "Current window" windowId)
(let [active-tab (:active-tab app-state)
replacing? (not= windowId (:windowId active-tab))]
replacing? (not= windowId (:windowId active-tab))
is-none? (= windowId windows/none)]
(when is-none?
(alarms/create window-alarm {:periodInMinutes 5}))
(if replacing?
(do
(dispatch [:handle-deactivation active-tab])
;; TODO: Add alarm when none
(when (not= windowId windows/none)
(when (not is-none?)
(alarms/clear window-alarm)
(go (dispatch [:handle-activation
(first (<! (tabs/query {:active true :windowId windowId})))])))
(assoc app-state :active-tab nil))
......@@ -276,6 +303,7 @@
(on-channel tabs/on-activated dispatch ::tab-activated)
(on-channel tabs/on-updated dispatch ::tab-updated)
(on-channel windows/on-focus-changed dispatch ::window-focus)
(on-channel alarms/on-alarm dispatch-sync ::on-alarm)
(idle/set-detection-interval 60)
(on-channel idle/on-state-changed dispatch :idle-state-change))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment