feat: font setting

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-07-27 20:27:09 +08:00
parent 939a5f7f93
commit e4c9ca4428
8 changed files with 211 additions and 41 deletions
+3
View File
@@ -1,9 +1,12 @@
import { createRequire } from "node:module"
import { app, nativeTheme } from "electron"
import { setDockCount } from "../lib/dock"
import { createSettingWindow } from "../window"
import { t } from "./_instance"
const require = createRequire(import.meta.url)
export const settingRoute = {
getLoginItemSettings: t.procedure
.input<void>()
+3 -1
View File
@@ -20,8 +20,10 @@ const createDefaultSettings = (): UISettings => ({
modalOpaque: true,
reduceMotion: false,
// Font
uiFontFamily: "SN Pro",
readerFontFamily: "inherit",
// Content
readerFontFamily: "SN Pro",
readerRenderInlineStyle: false,
codeHighlightTheme: "github-dark",
})
@@ -133,9 +133,9 @@ function EntryContentRender({ entryId }: { entryId: string }) {
<div className="h-[calc(100%-3.5rem)] min-w-0 overflow-y-auto @container">
<m.div
style={{
style={readerFontFamily ? {
fontFamily: readerFontFamily,
}}
} : undefined}
className="p-5"
initial={{ opacity: 0.01, y: 100 }}
animate={{ opacity: 1, y: 0 }}
@@ -0,0 +1,176 @@
/* eslint-disable @eslint-react/hooks-extra/ensure-custom-hooks-using-other-hooks */
import {
setUISetting,
useUISettingSelector,
} from "@renderer/atoms/settings/ui"
import { StyledButton } from "@renderer/components/ui/button"
import { Input } from "@renderer/components/ui/input"
import { useModalStack } from "@renderer/components/ui/modal"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@renderer/components/ui/select"
import { tipcClient } from "@renderer/lib/client"
import { useQuery } from "@tanstack/react-query"
import { useCallback, useMemo, useRef } from "react"
const FALLBACK_FONT = "Default"
const DEFAULT_FONT = "SN Pro"
const CUSTOM_FONT = "Custom"
const useFontDataElectron = () => {
const { data } = useQuery({
queryFn: () => tipcClient?.getSystemFonts(),
queryKey: ["systemFonts"],
})
return [FALLBACK_FONT].concat(data || []).map((font) => ({
label: font,
value: font,
}))
}
const useFontDataWeb = () => [
{ label: FALLBACK_FONT, value: "inherit" },
{ label: "System UI", value: "system-ui" },
...["Arial", "PingFang SC", "Microsoft YaHei", "SF Pro"].map((font) => ({
label: font,
value: font,
})),
{
label: "Custom",
value: CUSTOM_FONT,
},
]
const useFontData = window.electron ? useFontDataElectron : useFontDataWeb
export const ContentFontSelector = () => {
const data = useFontData()
const readerFontFamily = useUISettingSelector(
(state) => state.readerFontFamily || DEFAULT_FONT,
)
const setCustom = usePresentCustomFontDialog("readerFontFamily")
const isCustomFont = useMemo(
() =>
readerFontFamily !== "inherit" &&
data.find((d) => d.value === readerFontFamily) === undefined,
[data, readerFontFamily],
)
return (
<div className="-mt-1 mb-3 flex items-center justify-between">
<span className="shrink-0 text-sm font-medium">Content Font</span>
<Select
defaultValue={FALLBACK_FONT}
value={readerFontFamily}
onValueChange={(value) => {
if (value === CUSTOM_FONT) {
setCustom()
return
}
setUISetting("readerFontFamily", value)
}}
>
<SelectTrigger size="sm" className="w-48">
<SelectValue />
</SelectTrigger>
<SelectContent className="h-64">
{isCustomFont && (
<SelectItem value={readerFontFamily}>{readerFontFamily}</SelectItem>
)}
{data.map(({ label, value }) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
export const UIFontSelector = () => {
// filter out the fallback font
const data = useFontData().slice(1)
const uiFont = useUISettingSelector((state) => state.uiFontFamily)
const setCustom = usePresentCustomFontDialog("uiFontFamily")
const isCustomFont = useMemo(
() =>
data.find((d) => d.value === uiFont) === undefined,
[data, uiFont],
)
return (
<div className="-mt-1 mb-3 flex items-center justify-between">
<span className="shrink-0 text-sm font-medium">UI Font</span>
<Select
defaultValue={FALLBACK_FONT}
value={uiFont}
onValueChange={(value) => {
if (value === CUSTOM_FONT) {
setCustom()
return
}
setUISetting("uiFontFamily", value)
}}
>
<SelectTrigger size="sm" className="w-48">
<SelectValue />
</SelectTrigger>
<SelectContent className="h-64">
{isCustomFont && (
<SelectItem value={uiFont}>{uiFont}</SelectItem>
)}
<SelectItem value={DEFAULT_FONT}>{DEFAULT_FONT}</SelectItem>
{data.map(({ label, value }) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
const usePresentCustomFontDialog = (
setKey: "uiFontFamily" | "readerFontFamily",
) => {
const { present } = useModalStack()
return useCallback(() => {
present({
title: "Custom Font",
content: function Content({ dismiss }) {
const inputRef = useRef<HTMLInputElement>(null)
return (
<div className="flex flex-col gap-2">
<Input ref={inputRef} />
<div className="flex justify-end">
<StyledButton
onClick={() => {
const value = inputRef.current?.value
if (value) {
setUISetting(setKey, value)
dismiss()
}
}}
>
Save
</StyledButton>
</div>
</div>
)
},
})
}, [present, setKey])
}
@@ -14,10 +14,10 @@ import {
import { useDark, useSetDarkInWebApp } from "@renderer/hooks/common"
import { tipcClient } from "@renderer/lib/client"
import { getOS } from "@renderer/lib/utils"
import { useQuery } from "@tanstack/react-query"
import { bundledThemes } from "shiki/themes"
import { SettingSwitch } from "../control"
import { ContentFontSelector, UIFontSelector } from "../modules/fonts"
import { createSettingBuilder } from "../setting-builder"
import { SettingsTitle } from "../title"
@@ -79,12 +79,16 @@ export const SettingAppearance = () => {
onChange: (value) =>
setUISetting("sidebarShowUnreadCount", value),
},
{
type: "title",
value: "Fonts",
},
UIFontSelector,
ContentFontSelector,
{
type: "title",
value: "Content",
},
!!window.electron && Fonts,
ShikiTheme,
{
label: "Render inline style",
@@ -140,40 +144,6 @@ const ShikiTheme = () => {
)
}
const Fonts = () => {
const { data } = useQuery({
queryFn: () => tipcClient?.getSystemFonts(),
queryKey: ["systemFonts"],
})
const readerFontFamily = useUISettingSelector(
(state) => state.readerFontFamily || "SN Pro",
)
return (
<div className="-mt-1 mb-3 flex items-center justify-between">
<span className="shrink-0 text-sm font-medium">Font family</span>
<Select
defaultValue="SN Pro"
value={readerFontFamily}
onValueChange={(value) => {
setUISetting("readerFontFamily", value)
}}
>
<SelectTrigger size="sm" className="w-48">
<SelectValue />
</SelectTrigger>
<SelectContent className="h-64">
<SelectItem value="SN Pro">SN Pro</SelectItem>
{data?.map((font) => (
<SelectItem key={font} value={font}>
{font}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)
}
const textSizeMap = {
smaller: 15,
default: 16,
@@ -12,6 +12,21 @@ const useUISettingSync = () => {
root.style.fontSize = `${setting.uiTextSize}px`
}, [setting.uiTextSize])
useInsertionEffect(() => {
const root = document.documentElement
const fontCss = `${setting.uiFontFamily},"SN Pro", system-ui, sans-serif`
Object.assign(root.style, {
fontFamily: fontCss,
})
root.style.cssText += `\n--fo-font-family: ${fontCss}`
Object.assign(document.body.style, {
fontFamily: fontCss,
})
}, [setting.uiFontFamily])
useEffect(() => {
if (setting.showDockBadge) {
return feedUnreadActions.subscribeUnreadCount(
+4 -1
View File
@@ -1,3 +1,7 @@
html {
--fo-font-family: "SN Pro", system-ui, sans-serif;
}
body,
#root {
@apply h-screen;
@@ -5,7 +9,6 @@ body,
@apply select-none;
/* @apply bg-white; */
--fo-font-family: "SN Pro", system-ui, sans-serif;
@apply font-theme;
overscroll-behavior: none;
+1
View File
@@ -19,6 +19,7 @@ export interface UISettings {
modalDraggable: boolean
modalOpaque: boolean
reduceMotion: boolean
uiFontFamily: string
readerFontFamily: string
readerRenderInlineStyle: boolean
codeHighlightTheme: string