-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathappearance.vue
More file actions
253 lines (212 loc) · 6.14 KB
/
Copy pathappearance.vue
File metadata and controls
253 lines (212 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<script setup lang="ts">
import type { ThemeType } from "~/types";
import type { SelectItem } from "@nuxt/ui";
import { useSettingManager } from "~/composables/useSettingManager";
interface FontItem {
id: string;
value: string;
label: string;
}
definePageMeta({
layout: "setting"
});
const { t } = useI18n();
const {
theme,
fontFamily,
primaryColorLight,
primaryColorDark,
hydrationPromise,
isHydrated,
setFontFamily,
setPrimaryColorLight,
setPrimaryColorDark
} = useSettingManager();
const { applyPrimaryColor } = useColor();
const { manualSetTheme, enableFollowSystem, followSystem, userTheme } = useThemeAdapter();
const selectedFont = ref<string>("system");
const selectedAppearance = computed<ThemeType>({
get: () => {
if (followSystem.value) return "withSystem";
const saved = (theme.value || "") as ThemeType;
if (saved === "dark" || saved === "light") return saved;
const current = (userTheme.value || "") as ThemeType;
if (current === "dark" || current === "light") return current;
return "light";
},
set: (id: ThemeType) => {
if (id === "withSystem") {
void enableFollowSystem().then(() => {
useTauriEventEmit("theme-changed", { mode: "withSystem" });
nextTick().then(() => applyCurrentThemeColor(true));
});
return;
}
manualSetTheme(id as any);
useTauriEventEmit("theme-changed", { mode: id });
nextTick().then(() => applyCurrentThemeColor(true));
}
});
const mainColor = computed<string>({
get: () => (userTheme.value === "dark" ? primaryColorDark.value || "#34d399" : primaryColorLight.value || "#1ab394"),
set: (color: string) => {
if (!color) return;
const hex = applyPrimaryColor(color);
if (userTheme.value === "dark") {
setPrimaryColorDark(hex);
} else {
setPrimaryColorLight(hex);
}
useTauriEventEmit("primary-color-changed", { hex, mode: userTheme.value });
}
});
const predefineColors = computed<string[]>(() => {
if (theme.value === "light") {
return [
"#1ab394",
"#3b82f6",
"#8b5cf6",
"#f59e0b",
"#ec4899",
"#06b6d4",
"#64748b",
"#f43f5e",
"#84cc16",
"#6366f1",
"#fbbf24",
"#14b8a6"
];
} else {
return [
"#34d399",
"#60a5fa",
"#a78bfa",
"#fbbf24",
"#f472b6",
"#67e8f9",
"#94a3b8",
"#fb7185",
"#a3e635",
"#818cf8",
"#fcd34d",
"#2dd4bf"
];
}
});
const appearanceItems = computed<SelectItem[]>(() => [
{ label: t("Common.WithSystem"), id: "withSystem" },
{ label: t("Common.Light"), id: "light" },
{ label: t("Common.Dark"), id: "dark" }
]);
const FALLBACK_FONTS = 'system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial';
const fontsItems = ref<FontItem[]>([
{
label: t("Common.SystemDefault"),
id: "system",
value: FALLBACK_FONTS
}
]);
const loadSystemFonts = async () => {
const fallback = FALLBACK_FONTS;
try {
const families = await useTauriCoreInvoke<string[]>("list_system_fonts");
const dynamicItems: FontItem[] = (families || []).map((name) => ({
id: name,
label: name,
value: `"${name}", ${fallback}`
}));
const systemDefault = fontsItems.value[0];
if (!systemDefault) return;
fontsItems.value = [systemDefault, ...dynamicItems];
} catch (e) {
fontsItems.value = [
fontsItems.value[0]!,
{ label: "System UI", id: "systemUI", value: fallback },
{
label: "Noto Sans SC",
id: "notoSansSC",
value: '"Noto Sans SC", "Noto Sans", ' + fallback
},
{ label: "Inter", id: "inter", value: '"Inter", ' + fallback }
];
} finally {
const savedRaw = fontFamily.value;
const normalizedSaved = !savedRaw || savedRaw === "System UI" ? FALLBACK_FONTS : savedRaw;
if (normalizedSaved !== savedRaw) {
setFontFamily(normalizedSaved);
}
const matched = fontsItems.value.find((i) => i.value === normalizedSaved);
selectedFont.value = matched?.id || "system";
applyFont(normalizedSaved);
}
};
const applyCurrentThemeColor = (broadcast = false) => {
const modeNow = (userTheme.value as string) || (selectedAppearance.value as string);
const hexNow = modeNow === "dark" ? primaryColorDark.value : primaryColorLight.value;
if (hexNow) {
applyPrimaryColor(hexNow);
if (broadcast) {
useTauriEventEmit("primary-color-changed", { hex: hexNow, mode: modeNow });
}
}
};
onMounted(async () => {
const promise = hydrationPromise.value;
if (promise) {
await promise;
} else if (!isHydrated.value) {
await nextTick();
}
applyCurrentThemeColor();
loadSystemFonts();
});
watch(
() => userTheme.value,
() => {
applyCurrentThemeColor();
}
);
watch(
() => selectedFont.value,
(id) => {
const item = fontsItems.value.find((i: FontItem) => i.id === id) || fontsItems.value[0];
const value = item?.value || fontsItems.value[0]?.value;
if (!value) return;
applyFont(value);
setFontFamily(value);
try {
useTauriEventEmit("font-changed", { value });
} catch {}
}
);
function applyFont(font: string) {
const root = document.documentElement;
root.style.setProperty("--font-sans", font);
root.style.setProperty("--font-heading", font);
}
</script>
<template>
<div class="space-y-4">
<div class="flex items-center justify-between">
<span class="text-sm font-medium">{{ t("Common.Appearance") }}</span>
<USelect v-model="selectedAppearance" value-key="id" :items="appearanceItems" class="w-40" />
</div>
<USeparator />
<div class="flex items-center justify-between">
<span class="text-sm font-medium">{{ t("Common.PrimaryColor") }}</span>
<ColorPicker v-model="mainColor" :colors="predefineColors" class="w-40" />
</div>
<USeparator />
<div class="flex items-center justify-between">
<span class="text-sm font-medium">{{ t("Common.Fonts") }}</span>
<USelectMenu
v-model="selectedFont"
:items="fontsItems"
:search-input="{ placeholder: t('Operation.Search') }"
value-key="id"
option-attribute="label"
class="w-56"
/>
</div>
</div>
</template>