forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbadge-manager.ts
More file actions
168 lines (149 loc) · 5.2 KB
/
badge-manager.ts
File metadata and controls
168 lines (149 loc) · 5.2 KB
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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { setBadgeBgColor, setBadgeText } from "@api/chrome/action"
import { listTabs } from "@api/chrome/tab"
import { getFocusedNormalWindowId } from "@api/chrome/window"
import statDatabase from "@db/stat-database"
import optionHolder from "@service/components/option-holder"
import whitelistHolder from "@service/whitelist/holder"
import { IS_ANDROID } from "@util/constant/environment"
import { extractHostname, isBrowserUrl } from "@util/pattern"
import { MILL_PER_HOUR, MILL_PER_MINUTE, MILL_PER_SECOND } from "@util/time"
import MessageDispatcher from "./message-dispatcher"
export type BadgeLocation = {
/**
* The tab id of badge text show display with
*/
tabId: number
/**
* The url of tab
*/
url: string
focus?: number
}
function mill2Str(milliseconds: number) {
if (milliseconds < MILL_PER_MINUTE) {
// no more than 1 minutes
return `${Math.round(milliseconds / MILL_PER_SECOND)}s`
} else if (milliseconds < MILL_PER_HOUR) {
// no more than 1 hour
return `${Math.round(milliseconds / MILL_PER_MINUTE)}m`
} else {
return `${(milliseconds / MILL_PER_HOUR).toFixed(1)}h`
}
}
function setBadgeTextOfMills(milliseconds: number | undefined, tabId: number | undefined) {
const text = milliseconds === undefined ? '' : mill2Str(milliseconds)
setBadgeText(text, tabId)
}
async function findActiveTab(): Promise<BadgeLocation | undefined> {
const windowId = await getFocusedNormalWindowId()
if (!windowId) {
return undefined
}
const tabs = await listTabs({ active: true, windowId })
// Fix #131
// Edge will return two active tabs, including the new tab with url 'edge://newtab/', GG
for (const { id: tabId, url } of tabs) {
if (!tabId || !url || isBrowserUrl(url)) continue
return { tabId, url }
}
return undefined
}
async function clearAllBadge(): Promise<void> {
const tabs = await listTabs()
if (!tabs?.length) return
for (const tab of tabs) {
await setBadgeText('', tab?.id)
}
}
type BadgeState = 'HIDDEN' | 'NOT_SUPPORTED' | 'PAUSED' | 'TIME' | 'WHITELIST'
interface BadgeManager {
init(dispatcher: MessageDispatcher): void
updateFocus(location?: BadgeLocation): void
}
class DefaultBadgeManager {
pausedTabId: number | undefined
current: BadgeLocation | undefined
visible: boolean | undefined
state: BadgeState | undefined
async init(messageDispatcher: MessageDispatcher) {
const option = await optionHolder.get()
this.processOption(option)
optionHolder.addChangeListener(opt => this.processOption(opt))
whitelistHolder.addPostHandler(() => this.render())
messageDispatcher
.register('cs.idleChange', (isIdle, sender) => {
const tabId = sender?.tab?.id
isIdle ? this.pause(tabId) : this.resume(tabId)
})
this.updateFocus()
}
/**
* Hide the badge text
*/
private async pause(tabId?: number) {
this.pausedTabId = tabId
this.render()
}
/**
* Show the badge text
*/
private resume(tabId?: number) {
if (!this.pausedTabId || this.pausedTabId !== tabId) return
this.pausedTabId = undefined
this.render()
}
async updateFocus(target?: BadgeLocation) {
this.current = target || await findActiveTab()
await this.render()
}
private processOption(option: timer.option.AppearanceOption) {
const { displayBadgeText, badgeBgColor } = option || {}
const before = this.visible
this.visible = !!displayBadgeText
!this.visible && before && clearAllBadge()
setBadgeBgColor(badgeBgColor)
}
private async render(): Promise<void> {
this.state = await this.processState()
}
private async processState(): Promise<BadgeState> {
const { url, tabId, focus } = this.current || {}
if (!this.visible || !url) {
this.state !== 'HIDDEN' && setBadgeText('', tabId)
return 'HIDDEN'
}
if (isBrowserUrl(url)) {
this.state !== 'NOT_SUPPORTED' && setBadgeText('∅', tabId)
return 'NOT_SUPPORTED'
}
const host = extractHostname(url)?.host
if (whitelistHolder.contains(host, url)) {
this.state !== 'WHITELIST' && setBadgeText('W', tabId)
return 'WHITELIST'
}
if (this.pausedTabId === tabId) {
this.state !== 'PAUSED' && setBadgeText('P', tabId)
return 'PAUSED'
}
const milliseconds = focus || (host ? (await statDatabase.get(host, new Date())).focus : undefined)
setBadgeTextOfMills(milliseconds, tabId)
return 'TIME'
}
}
class SilentBadgeManager implements BadgeManager {
init(_dispatcher: MessageDispatcher): void {
// do nothing
}
updateFocus(_location?: BadgeLocation): void {
// do nothing
}
}
// Don't display badge on Android
const badgeManager: BadgeManager = IS_ANDROID ? new SilentBadgeManager() : new DefaultBadgeManager()
export default badgeManager