Skip to content

Commit 2668632

Browse files
committed
Support to change the backgound color of badge (#279)
1 parent 92f3b45 commit 2668632

File tree

9 files changed

+51
-12
lines changed

9 files changed

+51
-12
lines changed

src/api/chrome/action.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IS_MV3 } from "@util/constant/environment"
1+
import { IS_FIREFOX, IS_MV3 } from "@util/constant/environment"
22
import { handleError } from "./common"
33

44
const action = IS_MV3 ? chrome.action : chrome.browserAction
@@ -8,4 +8,19 @@ export function setBadgeText(text: string, tabId: number): Promise<void> {
88
handleError('setBadgeText')
99
resolve()
1010
}))
11+
}
12+
13+
export function setBadgeBgColor(color: string | chrome.action.ColorArray): Promise<void> {
14+
if (!color) {
15+
if (IS_FIREFOX) {
16+
// Use null to clear bg color for Firefox
17+
color = null
18+
} else {
19+
color = [0, 0, 0, 0]
20+
}
21+
}
22+
return new Promise(resolve => action?.setBadgeBackgroundColor({ color }, () => {
23+
handleError('setBadgeColor')
24+
resolve()
25+
}))
1126
}

src/app/components/Option/components/AppearanceOption/index.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import type { UnwrapRef } from "vue"
99

10-
import { ElMessageBox, ElOption, ElSelect, ElSwitch } from "element-plus"
10+
import { ElColorPicker, ElMessageBox, ElOption, ElSelect, ElSwitch } from "element-plus"
1111
import { defineComponent, reactive, unref, watch } from "vue"
1212
import optionService from "@service/option-service"
1313
import { defaultAppearance } from "@util/constant/option"
@@ -26,6 +26,7 @@ const allLocaleOptions: timer.option.LocaleOption[] = ["default", ...SORTED_LOCA
2626
function copy(target: timer.option.AppearanceOption, source: timer.option.AppearanceOption) {
2727
target.displayWhitelistMenu = source.displayWhitelistMenu
2828
target.displayBadgeText = source.displayBadgeText
29+
target.badgeBgColor = source.badgeBgColor
2930
target.locale = source.locale
3031
target.printInConsole = source.printInConsole
3132
target.darkMode = source.darkMode
@@ -119,6 +120,16 @@ const _default = defineComponent((_props, ctx) => {
119120
onChange={(val: boolean) => option.displayBadgeText = val}
120121
/>
121122
</OptionItem>
123+
<OptionItem
124+
v-show={option.displayBadgeText}
125+
label={msg => msg.option.appearance.badgeBgColor}
126+
>
127+
<ElColorPicker
128+
size="small"
129+
modelValue={option.badgeBgColor}
130+
onChange={val => option.badgeBgColor = val}
131+
/>
132+
</OptionItem>
122133
<OptionItem
123134
label={msg => msg.option.appearance.printInConsole.label}
124135
defaultValue={t(msg => msg.option.yes)}

src/app/components/Option/style/index.sass

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
margin-left: 5px
6262
.el-switch
6363
margin-right: 6px
64+
.el-color-picker
65+
margin-left: 6px
6466
.option-tag
6567
color: #F56C6C
6668
margin: 0 3px
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8-
import { setBadgeText } from "@api/chrome/action"
8+
import { setBadgeBgColor, setBadgeText } from "@api/chrome/action"
99
import { listTabs } from "@api/chrome/tab"
1010
import { getFocusedNormalWindow } from "@api/chrome/window"
1111
import StatDatabase from "@db/stat-database"
@@ -82,14 +82,14 @@ async function updateFocus(badgeLocation?: BadgeLocation, lastLocation?: BadgeLo
8282
return badgeLocation
8383
}
8484

85-
class BadgeTextManager {
85+
class BadgeManager {
8686
isPaused: boolean
8787
lastLocation: BadgeLocation
8888

8989
async init() {
90-
const option: Partial<timer.option.AllOption> = await optionService.getAllOption()
91-
this.pauseOrResumeAccordingToOption(!!option.displayBadgeText)
92-
optionService.addOptionChangeListener(({ displayBadgeText }) => this.pauseOrResumeAccordingToOption(displayBadgeText))
90+
const option: timer.option.AllOption = await optionService.getAllOption()
91+
this.processOption(option)
92+
optionService.addOptionChangeListener(opt => this.processOption(opt))
9393
whitelistHolder.addPostHandler(updateFocus)
9494
}
9595

@@ -116,9 +116,11 @@ class BadgeTextManager {
116116
this.lastLocation = await updateFocus(badgeLocation, this.lastLocation)
117117
}
118118

119-
private pauseOrResumeAccordingToOption(displayBadgeText: boolean) {
119+
private processOption(option: timer.option.AppearanceOption) {
120+
const { displayBadgeText, badgeBgColor } = option || {}
120121
displayBadgeText ? this.resume() : this.pause()
122+
setBadgeBgColor(badgeBgColor)
121123
}
122124
}
123125

124-
export default new BadgeTextManager()
126+
export default new BadgeManager()

src/background/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import BrowserActionMenuManager from "./browser-action-menu-manager"
1111
import IconAndAliasCollector from "./icon-and-alias-collector"
1212
import VersionManager from "./version-manager"
1313
import ActiveTabListener from "./active-tab-listener"
14-
import badgeTextManager from "./badge-text-manager"
14+
import badgeTextManager from "./badge-manager"
1515
import MessageDispatcher from "./message-dispatcher"
1616
import initLimitProcessor from "./limit-processor"
1717
import initCsHandler from "./content-script-handler"

src/background/timer/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import limitService from "@service/limit-service"
44
import periodService from "@service/period-service"
55
import statService from "@service/stat-service"
66
import { extractHostname } from "@util/pattern"
7-
import badgeTextManager from "../badge-text-manager"
7+
import badgeManager from "../badge-manager"
88
import MessageDispatcher from "../message-dispatcher"
99
import optionService from "@service/option-service"
1010

@@ -46,7 +46,7 @@ async function handleEvent(event: timer.stat.Event, sender: ChromeMessageSender)
4646
// Cause there is no way to determine whether this tab is selected in screen-split mode
4747
// So only show badge for first tab for screen-split mode
4848
// @see #246
49-
firstActiveTab?.id === tabId && badgeTextManager.forceUpdate({ tabId, url })
49+
firstActiveTab?.id === tabId && badgeManager.forceUpdate({ tabId, url })
5050
}
5151
}
5252

src/i18n/message/app/option-resource.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"whitelistItem": "白名单",
1818
"contextMenu": "浏览器的右键菜单",
1919
"displayBadgeText": "{input} 是否在 {icon} 上,显示 {timeInfo}",
20+
"badgeBgColor": "图标上文本的背景色 {input}",
2021
"icon": "扩展图标",
2122
"badgeTextContent": "当前网站的今日浏览时长",
2223
"locale": {
@@ -253,6 +254,7 @@
253254
"whitelistItem": "whitelist related shortcuts",
254255
"contextMenu": "the context menu",
255256
"displayBadgeText": "{input} Whether to display {timeInfo} in {icon}",
257+
"badgeBgColor": "The background color of the text on the icon {input}",
256258
"icon": "the icon of extension",
257259
"badgeTextContent": "the browsing time of current website",
258260
"locale": {

src/i18n/message/app/option.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type OptionMessage = {
2727
contextMenu: string
2828
// badge text
2929
displayBadgeText: string
30+
badgeBgColor: string
3031
icon: string
3132
badgeTextContent: string
3233
locale: {

types/timer/option.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ declare namespace timer.option {
7575
* @since 0.3.3
7676
*/
7777
displayBadgeText: boolean
78+
/**
79+
* The background color of badge text
80+
*
81+
* @since 2.3.0
82+
*/
83+
badgeBgColor?: string
7884
/**
7985
* The language of this extension
8086
*

0 commit comments

Comments
 (0)