refactor: implement dedicated monochrome mode

This commit is contained in:
Eason Li 2026-01-04 16:20:48 +08:00
parent edaf7b727e
commit b6b93ca176
3 changed files with 31 additions and 43 deletions

View file

@ -3,7 +3,7 @@ import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useTheme } from '../themes/themeHandler' import { useTheme } from '../themes/themeHandler'
import type { DisplayMode } from '../themes/types' import type { DisplayMode } from '../themes/types'
const { mode, setMode, state, amoledEnabled, setAmoledEnabled, monochromeEnabled, setMonochromeEnabled } = useTheme() const { mode, setMode, state, amoledEnabled, setAmoledEnabled } = useTheme()
const isOpen = ref(false) const isOpen = ref(false)
const dropdownRef = ref<HTMLElement | null>(null) const dropdownRef = ref<HTMLElement | null>(null)
@ -13,25 +13,24 @@ interface ModeChoice {
label: string label: string
icon: string icon: string
isAmoled?: boolean isAmoled?: boolean
isMonochrome?: boolean
} }
const modeChoices: ModeChoice[] = [ const modeChoices: ModeChoice[] = [
{ mode: 'light', label: 'Light', icon: 'i-ph-sun-duotone' }, { mode: 'light', label: 'Light', icon: 'i-ph-sun-duotone' },
{ mode: 'dark', label: 'Dark', icon: 'i-ph-moon-duotone' }, { mode: 'dark', label: 'Dark', icon: 'i-ph-moon-duotone' },
{ mode: 'dark', label: 'AMOLED', icon: 'i-ph-moon-stars-duotone', isAmoled: true }, { mode: 'dark', label: 'AMOLED', icon: 'i-ph-moon-stars-duotone', isAmoled: true },
{ mode: 'dark', label: 'Monochrome', icon: 'i-ph-circle-half-tilt-duotone', isMonochrome: true } { mode: 'monochrome', label: 'Monochrome', icon: 'i-ph-circle-half-tilt-duotone' }
] ]
const currentChoice = computed(() => { const currentChoice = computed(() => {
const current = (mode && (mode as any).value) ? (mode as any).value : 'light' const current = (mode && (mode as any).value) ? (mode as any).value : 'light'
if (current === 'dark' && monochromeEnabled.value) { if (current === 'monochrome') {
return modeChoices[3] // Monochrome option return modeChoices[3] // Monochrome option
} }
if (current === 'dark' && amoledEnabled.value) { if (current === 'dark' && amoledEnabled.value) {
return modeChoices[2] // AMOLED option return modeChoices[2] // AMOLED option
} }
return modeChoices.find(choice => choice.mode === current && !choice.isAmoled && !choice.isMonochrome) || modeChoices[0] return modeChoices.find(choice => choice.mode === current && !choice.isAmoled) || modeChoices[0]
}) })
const toggleDropdown = () => { const toggleDropdown = () => {
@ -39,31 +38,32 @@ const toggleDropdown = () => {
} }
const selectMode = (choice: ModeChoice) => { const selectMode = (choice: ModeChoice) => {
if (choice.isMonochrome) { setMode(choice.mode)
setMode('dark')
setAmoledEnabled(false) if (choice.isAmoled) {
setMonochromeEnabled(true)
} else if (choice.isAmoled) {
setMode('dark')
setAmoledEnabled(true) setAmoledEnabled(true)
setMonochromeEnabled(false)
} else { } else {
setMode(choice.mode) // Only disable AMOLED if we are explicitly switching away from it
setAmoledEnabled(false) // But wait, if we switch to 'monochrome', 'amoled' flag might still be true?
setMonochromeEnabled(false) // It doesn't matter because amoled is only checked if mode is 'dark'.
// However, if we switch back to 'dark', should it be amoled or not?
// Standard behavior: clicking 'Dark' (non-amoled) disables amoled.
if (choice.mode === 'dark') {
setAmoledEnabled(false)
}
} }
isOpen.value = false isOpen.value = false
} }
const isActiveChoice = (choice: ModeChoice) => { const isActiveChoice = (choice: ModeChoice) => {
const current = (mode && (mode as any).value) ? (mode as any).value : 'light' const current = (mode && (mode as any).value) ? (mode as any).value : 'light'
if (choice.isMonochrome) { if (choice.mode === 'monochrome') {
return current === 'dark' && monochromeEnabled.value return current === 'monochrome'
} }
if (choice.isAmoled) { if (choice.isAmoled) {
return current === 'dark' && amoledEnabled.value return current === 'dark' && amoledEnabled.value
} }
return choice.mode === current && !choice.isAmoled && !choice.isMonochrome && !amoledEnabled.value && !monochromeEnabled.value return choice.mode === current && !choice.isAmoled && !amoledEnabled.value
} }
const handleClickOutside = (event: MouseEvent) => { const handleClickOutside = (event: MouseEvent) => {

View file

@ -21,7 +21,6 @@ import { themeRegistry } from './configs'
const STORAGE_KEY_THEME = 'vitepress-theme-name' const STORAGE_KEY_THEME = 'vitepress-theme-name'
const STORAGE_KEY_MODE = 'vitepress-display-mode' const STORAGE_KEY_MODE = 'vitepress-display-mode'
const STORAGE_KEY_AMOLED = 'vitepress-amoled-enabled' const STORAGE_KEY_AMOLED = 'vitepress-amoled-enabled'
const STORAGE_KEY_MONOCHROME = 'vitepress-monochrome-enabled'
export class ThemeHandler { export class ThemeHandler {
private state = ref<ThemeState>({ private state = ref<ThemeState>({
@ -30,7 +29,6 @@ export class ThemeHandler {
theme: null theme: null
}) })
private amoledEnabled = ref(false) private amoledEnabled = ref(false)
private monochromeEnabled = ref(false)
constructor() { constructor() {
this.initializeTheme() this.initializeTheme()
@ -43,7 +41,6 @@ export class ThemeHandler {
const savedTheme = localStorage.getItem(STORAGE_KEY_THEME) || 'color-swarm' const savedTheme = localStorage.getItem(STORAGE_KEY_THEME) || 'color-swarm'
const savedMode = localStorage.getItem(STORAGE_KEY_MODE) as DisplayMode | null const savedMode = localStorage.getItem(STORAGE_KEY_MODE) as DisplayMode | null
const savedAmoled = localStorage.getItem(STORAGE_KEY_AMOLED) === 'true' const savedAmoled = localStorage.getItem(STORAGE_KEY_AMOLED) === 'true'
const savedMonochrome = localStorage.getItem(STORAGE_KEY_MONOCHROME) === 'true'
if (themeRegistry[savedTheme]) { if (themeRegistry[savedTheme]) {
this.state.value.currentTheme = savedTheme this.state.value.currentTheme = savedTheme
@ -52,7 +49,6 @@ export class ThemeHandler {
// Set amoled preference // Set amoled preference
this.amoledEnabled.value = savedAmoled this.amoledEnabled.value = savedAmoled
this.monochromeEnabled.value = savedMonochrome
// Set mode // Set mode
if (savedMode) { if (savedMode) {
@ -84,8 +80,10 @@ export class ThemeHandler {
// Is this the WORST fix of all time??? // Is this the WORST fix of all time???
const root = document.documentElement const root = document.documentElement
const isMonochrome = currentMode === 'dark' && this.monochromeEnabled.value const isMonochrome = currentMode === 'monochrome'
// Monochrome overrides everything to pure black/white
// Standard Dark/Amoled logic applies otherwise
const bgColor = isMonochrome ? '#000000' : currentMode === 'dark' && this.amoledEnabled.value ? '#000000' : currentMode === 'dark' ? '#1A1A1A' : '#f8fafc' const bgColor = isMonochrome ? '#000000' : currentMode === 'dark' && this.amoledEnabled.value ? '#000000' : currentMode === 'dark' ? '#1A1A1A' : '#f8fafc'
root.style.setProperty('--vp-c-bg', bgColor) root.style.setProperty('--vp-c-bg', bgColor)
@ -119,9 +117,11 @@ export class ThemeHandler {
root.classList.add('amoled') root.classList.add('amoled')
} }
// Add monochrome class if enabled in dark mode // Add monochrome class if enabled
if (mode === 'dark' && this.monochromeEnabled.value) { if (mode === 'monochrome') {
root.classList.add('monochrome') root.classList.add('monochrome')
// Also add dark class because monochrome is effectively a high contrast dark mode
root.classList.add('dark')
} }
} }
@ -141,7 +141,7 @@ export class ThemeHandler {
let bgAltColor = colors.bgAlt let bgAltColor = colors.bgAlt
let bgElvColor = colors.bgElv let bgElvColor = colors.bgElv
const isMonochrome = this.state.value.currentMode === 'dark' && this.monochromeEnabled.value const isMonochrome = this.state.value.currentMode === 'monochrome'
if (this.state.value.currentMode === 'dark' && this.amoledEnabled.value) { if (this.state.value.currentMode === 'dark' && this.amoledEnabled.value) {
bgColor = '#000000' bgColor = '#000000'
@ -334,20 +334,6 @@ export class ThemeHandler {
this.setMode(newMode) this.setMode(newMode)
} }
public setMonochromeEnabled(enabled: boolean) {
this.monochromeEnabled.value = enabled
localStorage.setItem(STORAGE_KEY_MONOCHROME, enabled.toString())
this.applyTheme()
}
public getMonochromeEnabled() {
return this.monochromeEnabled.value
}
public isMonochromeMode() {
return this.state.value.currentMode === 'dark' && this.monochromeEnabled.value
}
public setAmoledEnabled(enabled: boolean) { public setAmoledEnabled(enabled: boolean) {
this.amoledEnabled.value = enabled this.amoledEnabled.value = enabled
localStorage.setItem(STORAGE_KEY_AMOLED, enabled.toString()) localStorage.setItem(STORAGE_KEY_AMOLED, enabled.toString())
@ -412,6 +398,10 @@ export class ThemeHandler {
public isAmoledMode() { public isAmoledMode() {
return this.state.value.currentMode === 'dark' && this.amoledEnabled.value return this.state.value.currentMode === 'dark' && this.amoledEnabled.value
} }
public isMonochromeMode() {
return this.state.value.currentMode === 'monochrome'
}
} }
// Global theme handler instance // Global theme handler instance
@ -447,8 +437,6 @@ export function useTheme() {
amoledEnabled: handler.getAmoledEnabledRef(), amoledEnabled: handler.getAmoledEnabledRef(),
setAmoledEnabled: (enabled: boolean) => handler.setAmoledEnabled(enabled), setAmoledEnabled: (enabled: boolean) => handler.setAmoledEnabled(enabled),
toggleAmoled: () => handler.toggleAmoled(), toggleAmoled: () => handler.toggleAmoled(),
monochromeEnabled: handler.getMonochromeEnabled(),
setMonochromeEnabled: (enabled: boolean) => handler.setMonochromeEnabled(enabled),
isMonochromeMode: () => handler.isMonochromeMode(), isMonochromeMode: () => handler.isMonochromeMode(),
state state
} }

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
export type DisplayMode = 'light' | 'dark' export type DisplayMode = 'light' | 'dark' | 'monochrome'
export interface ModeColors { export interface ModeColors {
// Brand colors (optional - if not specified, ColorPicker values are used) // Brand colors (optional - if not specified, ColorPicker values are used)