support import local themes

This commit is contained in:
pompurin404 2024-09-20 14:34:27 +08:00
parent f156dd1311
commit 68aba2afc3
No known key found for this signature in database
4 changed files with 58 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import { readdir, readFile } from 'fs/promises' import { copyFile, readdir, readFile } from 'fs/promises'
import { themesDir } from '../utils/dirs' import { themesDir } from '../utils/dirs'
import path from 'path' import path from 'path'
import axios from 'axios' import axios from 'axios'
@ -40,6 +40,12 @@ export async function fetchThemes(): Promise<void> {
zip.extractAllTo(themesDir(), true) zip.extractAllTo(themesDir(), true)
} }
export async function importThemes(files: string[]): Promise<void> {
for (const file of files) {
if (existsSync(file)) await copyFile(file, path.join(themesDir(), path.basename(file)))
}
}
export async function applyTheme(theme: string): Promise<void> { export async function applyTheme(theme: string): Promise<void> {
if (theme === 'default.css') { if (theme === 'default.css') {
if (insertedCSSKey) { if (insertedCSSKey) {

View File

@ -65,7 +65,7 @@ import { getInterfaces } from '../sys/interface'
import { copyEnv } from '../resolve/tray' import { copyEnv } from '../resolve/tray'
import { registerShortcut } from '../resolve/shortcut' import { registerShortcut } from '../resolve/shortcut'
import { mainWindow } from '..' import { mainWindow } from '..'
import { applyTheme, fetchThemes, resolveThemes } from '../resolve/theme' import { applyTheme, fetchThemes, importThemes, resolveThemes } from '../resolve/theme'
import { subStoreCollections, subStoreSubs } from '../core/subStoreApi' import { subStoreCollections, subStoreSubs } from '../core/subStoreApi'
import { logDir } from './dirs' import { logDir } from './dirs'
import path from 'path' import path from 'path'
@ -204,6 +204,7 @@ export function registerIpcMainHandlers(): void {
}) })
ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)()) ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)())
ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)()) ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)())
ipcMain.handle('importThemes', (_e, file) => ipcErrorWrapper(importThemes)(file))
ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme)) ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme))
ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type)) ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type))
ipcMain.handle('alert', (_e, msg) => { ipcMain.handle('alert', (_e, msg) => {

View File

@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'
import SettingCard from '../base/base-setting-card' import SettingCard from '../base/base-setting-card'
import SettingItem from '../base/base-setting-item' import SettingItem from '../base/base-setting-item'
import { Button, Input, Select, SelectItem, Switch, Tab, Tabs, Tooltip } from '@nextui-org/react' import { Button, Input, Select, SelectItem, Switch, Tab, Tabs, Tooltip } from '@nextui-org/react'
import { BiCopy } from 'react-icons/bi' import { BiCopy, BiSolidFileImport } from 'react-icons/bi'
import useSWR from 'swr' import useSWR from 'swr'
import { import {
checkAutoRun, checkAutoRun,
@ -10,6 +10,8 @@ import {
disableAutoRun, disableAutoRun,
enableAutoRun, enableAutoRun,
fetchThemes, fetchThemes,
getFilePath,
importThemes,
relaunchApp, relaunchApp,
resolveThemes, resolveThemes,
restartCore restartCore
@ -223,6 +225,7 @@ const GeneralConfig: React.FC = () => {
<SettingItem <SettingItem
title="主题" title="主题"
actions={ actions={
<>
<Button <Button
size="sm" size="sm"
isLoading={fetching} isLoading={fetching}
@ -244,6 +247,26 @@ const GeneralConfig: React.FC = () => {
> >
<IoMdCloudDownload className="text-lg" /> <IoMdCloudDownload className="text-lg" />
</Button> </Button>
<Button
size="sm"
isIconOnly
title="导入主题"
variant="light"
className="ml-2"
onPress={async () => {
const files = await getFilePath(['css'])
if (!files) return
try {
await importThemes(files)
setCustomThemes(await resolveThemes())
} catch (e) {
alert(e)
}
}}
>
<BiSolidFileImport className="text-lg" />
</Button>
</>
} }
> >
<Select <Select

View File

@ -339,6 +339,10 @@ export async function fetchThemes(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('fetchThemes')) return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('fetchThemes'))
} }
export async function importThemes(files: string[]): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('importThemes', files))
}
export async function applyTheme(theme: string): Promise<void> { export async function applyTheme(theme: string): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme)) return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme))
} }