Skip to content

Commit e91d6bc

Browse files
committed
增强性能
1 parent 340ca25 commit e91d6bc

File tree

6 files changed

+47
-43
lines changed

6 files changed

+47
-43
lines changed

index.html

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
background:#ffffff;
7272
z-index:99999;
7373
}
74+
#app-shell.dark {
75+
background:#121212;
76+
}
7477
#app-shell .shell-logo {
7578
width:72px;height:72px;
7679
border-radius:16px;
@@ -82,22 +85,30 @@
8285
border-radius:2px;
8386
overflow:hidden;
8487
}
88+
#app-shell.dark .shell-bar-track {
89+
background:#333333;
90+
}
8591
#app-shell .shell-bar-fill {
8692
width:40%;height:100%;
8793
background:#1976d2;
8894
border-radius:2px;
8995
animation:shell-slide 1.2s ease-in-out infinite;
9096
}
97+
#app-shell.dark .shell-bar-fill {
98+
background:#64b5f6;
99+
}
91100
@keyframes shell-slide {
92101
0%{transform:translateX(-100%)}
93102
100%{transform:translateX(350%)}
94103
}
95-
@media (prefers-color-scheme:dark) {
96-
#app-shell{background:#121212}
97-
#app-shell .shell-bar-track{background:#333333}
98-
#app-shell .shell-bar-fill{background:#64b5f6}
99-
}
100104
</style>
105+
<script>
106+
(function(){
107+
var t = localStorage.getItem('theme');
108+
var dark = t === 'dark' || (!t && window.matchMedia('(prefers-color-scheme:dark)').matches);
109+
if (dark) document.getElementById('app-shell').classList.add('dark');
110+
})();
111+
</script>
101112
<img class="shell-logo" src="/favicon.png" alt="ZeroCat" />
102113
<div class="shell-bar-track"><div class="shell-bar-fill"></div></div>
103114
</div>

src/components/AppHeader.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,13 @@ export default {
367367
368368
this.updateSubNavItems(this.$route);
369369
370-
// Initialize theme
370+
// Initialize theme — 无存储时跟随系统
371371
this.theme = useTheme();
372-
this.isDarkTheme = savedTheme === "dark";
372+
if (savedTheme) {
373+
this.isDarkTheme = savedTheme === "dark";
374+
} else {
375+
this.isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
376+
}
373377
this.applyTheme();
374378
},
375379
watch: {

src/layouts/DefaultLayout.vue

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,18 @@
2727
</template>
2828

2929
<script setup>
30-
import { ref, onMounted, watch } from "vue";
31-
import { useTheme } from "vuetify";
30+
import { ref } from "vue";
3231
import AppHeader from "@/components/AppHeader.vue";
3332
import UnifiedSidebar from "@/components/sidebar/UnifiedSidebar.vue";
3433
import Toast from "primevue/toast";
3534
import error404 from "@/components/error/404.vue";
3635
import { use404 } from "@/composables/use404";
3736
38-
const theme = useTheme();
39-
4037
// 抽屉状态 - 默认关闭,不缓存
4138
const drawer = ref(false);
4239
43-
// 主题管理
44-
const initTheme = () => {
45-
const savedTheme = localStorage.getItem("theme");
46-
if (savedTheme) {
47-
theme.global.name.value = savedTheme;
48-
} else {
49-
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
50-
theme.global.name.value = prefersDark ? "dark" : "light";
51-
localStorage.setItem("theme", theme.global.name.value);
52-
}
53-
};
54-
55-
watch(() => theme.global.name.value, (newTheme) => {
56-
localStorage.setItem("theme", newTheme);
57-
});
58-
59-
onMounted(() => {
60-
initTheme();
61-
});
40+
// 主题管理 — 初始化由 vuetify.js 的 getInitialTheme 完成,此处无需重复
41+
// watch 仅用于响应用户手动切换(来自 AppHeader/Sidebar 的 toggleTheme)
6242
</script>
6343

6444
<style>

src/layouts/SimpleLayout.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@
1313

1414
<script setup>
1515
import Toast from "primevue/toast";
16-
import {onMounted} from 'vue';
17-
import {useTheme} from 'vuetify';
18-
19-
const theme = useTheme();
20-
21-
// 从本地存储中获取主题设置
22-
onMounted(() => {
23-
const savedTheme = localStorage.getItem('theme');
24-
if (savedTheme) {
25-
theme.global.name.value = savedTheme;
26-
}
27-
});
16+
// 主题初始化由 vuetify.js 的 getInitialTheme 完成,无需在布局中重复
2817
</script>
2918

3019
<style>

src/plugins/vuetify.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ const vscodeTheme = {
5151
}
5252
}
5353

54+
// 根据本地存储或系统偏好决定初始主题
55+
function getInitialTheme() {
56+
const saved = localStorage.getItem('theme');
57+
if (saved === 'dark' || saved === 'vscodeTheme') return 'dark';
58+
if (saved === 'light') return 'light';
59+
// 无存储时跟随系统
60+
if (window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark';
61+
return 'light';
62+
}
63+
5464
export default createVuetify({
5565
theme: {
56-
defaultTheme: 'vscodeTheme',
66+
defaultTheme: getInitialTheme(),
5767
themes: {
5868
vscodeTheme,
5969
},

src/styles/settings.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
// $color-pack: false
1010
// );
1111

12+
// 覆盖 @mdi/font 的 @font-face,添加 font-display: swap 确保文本始终可见
13+
@font-face {
14+
font-family: "Material Design Icons";
15+
src: url("@mdi/font/fonts/materialdesignicons-webfont.woff2?v=7.4.47") format("woff2"),
16+
url("@mdi/font/fonts/materialdesignicons-webfont.woff?v=7.4.47") format("woff");
17+
font-weight: normal;
18+
font-style: normal;
19+
font-display: swap;
20+
}
21+
1222
// Material Design 3 页面过渡动画
1323
.md3-enter-active,
1424
.md3-leave-active {

0 commit comments

Comments
 (0)