Skip to content

Commit be0e9ff

Browse files
committed
feat: add the option to disable side panel (#655)
1 parent 369b427 commit be0e9ff

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/api/chrome/sidePanel.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { IS_ANDROID, IS_MV3 } from '@util/constant/environment'
2+
import { handleError } from './common'
3+
4+
// NOT SUPPORTED in Firefox
5+
// Keep noticing at chrome.sidebarAction for Firefox
6+
export const SIDE_PANEL_STATE_SUPPORTED_CONTROL = !!chrome.sidePanel?.setOptions
7+
8+
export async function isSidePanelEnabled(): Promise<boolean> {
9+
if (IS_ANDROID || !SIDE_PANEL_STATE_SUPPORTED_CONTROL) return false
10+
11+
if (IS_MV3) {
12+
const result = await chrome.sidePanel.getOptions({})
13+
return result.enabled ?? true
14+
} else {
15+
return new Promise(resolve => chrome.sidePanel.getOptions({}, options => {
16+
handleError('isSidePanelEnabled')
17+
resolve(options.enabled ?? true)
18+
}))
19+
}
20+
}
21+
22+
export async function setSidePanelEnabled(enabled: boolean): Promise<void> {
23+
if (IS_ANDROID || !SIDE_PANEL_STATE_SUPPORTED_CONTROL) return
24+
25+
if (IS_MV3) {
26+
await chrome.sidePanel.setOptions({ enabled })
27+
} else {
28+
return new Promise(resolve => chrome.sidePanel.setOptions({ enabled }, () => {
29+
handleError('setSidePanelEnabled')
30+
resolve()
31+
}))
32+
}
33+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"timed": "定时开启"
3131
}
3232
},
33-
"animationDuration": "图表初始动画的时长 {input}"
33+
"animationDuration": "图表初始动画的时长 {input}",
34+
"sidePanel": "{input} 是否启用侧边栏视图"
3435
},
3536
"tracking": {
3637
"title": "统计",
@@ -285,7 +286,8 @@
285286
"timed": "Timed on"
286287
}
287288
},
288-
"animationDuration": "The duration of the chart's initial animation {input}"
289+
"animationDuration": "The duration of the chart's initial animation {input}",
290+
"sidePanel": "{input} Whether to enable the side panel"
289291
},
290292
"tracking": {
291293
"title": "Tracking",

src/i18n/message/app/option.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type OptionMessage = {
3636
options: Omit<Record<timer.option.DarkMode, string>, 'default'>
3737
}
3838
animationDuration: string
39+
sidePanel: string
3940
}
4041
tracking: {
4142
title: string

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8+
import { isSidePanelEnabled, setSidePanelEnabled, SIDE_PANEL_STATE_SUPPORTED_CONTROL } from '@api/chrome/sidePanel'
89
import { type I18nKey, t, tWith } from "@app/locale"
10+
import { useRequest } from '@hooks/useRequest'
911
import { ALL_LOCALES, localeSameAsBrowser } from "@i18n"
1012
import localeMessages from "@i18n/message/common/locale"
1113
import optionService from "@service/option-service"
@@ -39,16 +41,27 @@ function copy(target: timer.option.AppearanceOption, source: timer.option.Appear
3941
}
4042

4143
const DEFAULT_ANIMA_DURATION = defaultAppearance().chartAnimationDuration
44+
const DEFAULT_SIDE_PANEL_ENABLED = true
4245
const FOLLOW_BROWSER: I18nKey = msg => msg.option.followBrowser
4346

4447
const _default = defineComponent((_props, ctx) => {
4548
const { option } = useOption<timer.option.AppearanceOption>({
4649
defaultValue: defaultAppearance, copy,
4750
onChange: async val => optionService.isDarkMode(val).then(toggle)
4851
})
52+
const { data: sidePanelEnabled, refresh: refreshSidePanel } = useRequest(isSidePanelEnabled, {
53+
defaultValue: DEFAULT_SIDE_PANEL_ENABLED,
54+
})
55+
const handleSidePanelChange = async (val: boolean) => {
56+
await setSidePanelEnabled(val)
57+
refreshSidePanel()
58+
}
4959

5060
ctx.expose({
51-
reset: () => copy(option, defaultAppearance())
61+
reset() {
62+
handleSidePanelChange(DEFAULT_SIDE_PANEL_ENABLED)
63+
copy(option, defaultAppearance())
64+
}
5265
} satisfies OptionInstance)
5366

5467
const handleLocaleChange = (newVal: timer.option.LocaleOption) => {
@@ -152,6 +165,17 @@ const _default = defineComponent((_props, ctx) => {
152165
/>
153166
</OptionItem>
154167
</>}
168+
{SIDE_PANEL_STATE_SUPPORTED_CONTROL && (
169+
<OptionItem
170+
label={msg => msg.option.appearance.sidePanel}
171+
defaultValue={DEFAULT_SIDE_PANEL_ENABLED}
172+
>
173+
<ElSwitch
174+
modelValue={sidePanelEnabled.value}
175+
onChange={v => handleSidePanelChange(!!v)}
176+
/>
177+
</OptionItem>
178+
)}
155179
<OptionItem
156180
label={msg => msg.option.appearance.animationDuration}
157181
defaultValue={`${DEFAULT_ANIMA_DURATION}ms`}

0 commit comments

Comments
 (0)