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 path from 'path'
import axios from 'axios'
@ -40,6 +40,12 @@ export async function fetchThemes(): Promise<void> {
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> {
if (theme === 'default.css') {
if (insertedCSSKey) {

View File

@ -65,7 +65,7 @@ import { getInterfaces } from '../sys/interface'
import { copyEnv } from '../resolve/tray'
import { registerShortcut } from '../resolve/shortcut'
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 { logDir } from './dirs'
import path from 'path'
@ -204,6 +204,7 @@ export function registerIpcMainHandlers(): void {
})
ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)())
ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)())
ipcMain.handle('importThemes', (_e, file) => ipcErrorWrapper(importThemes)(file))
ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme))
ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type))
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 SettingItem from '../base/base-setting-item'
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 {
checkAutoRun,
@ -10,6 +10,8 @@ import {
disableAutoRun,
enableAutoRun,
fetchThemes,
getFilePath,
importThemes,
relaunchApp,
resolveThemes,
restartCore
@ -223,27 +225,48 @@ const GeneralConfig: React.FC = () => {
<SettingItem
title="主题"
actions={
<Button
size="sm"
isLoading={fetching}
isIconOnly
title="拉取主题"
variant="light"
className="ml-2"
onPress={async () => {
setFetching(true)
try {
await fetchThemes()
setCustomThemes(await resolveThemes())
} catch (e) {
alert(e)
} finally {
setFetching(false)
}
}}
>
<IoMdCloudDownload className="text-lg" />
</Button>
<>
<Button
size="sm"
isLoading={fetching}
isIconOnly
title="拉取主题"
variant="light"
className="ml-2"
onPress={async () => {
setFetching(true)
try {
await fetchThemes()
setCustomThemes(await resolveThemes())
} catch (e) {
alert(e)
} finally {
setFetching(false)
}
}}
>
<IoMdCloudDownload className="text-lg" />
</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

View File

@ -339,6 +339,10 @@ export async function fetchThemes(): Promise<void> {
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> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme))
}