Skip to content

Commit 42a0e53

Browse files
committed
主题切换功能
1 parent 9bfbd34 commit 42a0e53

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/App.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<s-page theme="auto" id="soberpage">
2+
<s-page theme="auto" id="soberpage" ref="soberPage" :class="theme.value">
33
<s-drawer>
44
<Navbar @toggle-drawer="toogleDrawer" />
55
<s-drawer
@@ -27,9 +27,16 @@ import Navbar from "./ui/navbar.vue";
2727
import Graph from "./graph/index.vue";
2828
import Editor from "./editor/index.vue";
2929
30-
import { ref } from "vue";
31-
32-
30+
import { ref, watchEffect } from "vue";
31+
import { useTheme } from "@/consts";
32+
const theme = useTheme();
33+
const soberPage = ref<HTMLElementTagNameMap["s-page"]>();
34+
watchEffect(() =>
35+
soberPage.value?.toggle(
36+
theme.value,
37+
document.getElementById("themeTrigger") ?? undefined
38+
)
39+
);
3340
3441
// Drawer toogle
3542
const innerDrawer = ref<HTMLElementTagNameMap["s-drawer"]>();

src/graph/graph.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,18 @@ onMounted(async () => {
101101
margin: auto;
102102
color: black;
103103
user-select: none;
104+
transition: filter 1ms;
104105
}
106+
105107
@media (prefers-color-scheme: dark) {
106-
#graphRender {
108+
s-page.auto #graphRender {
107109
filter: invert(100%) hue-rotate(180deg) brightness(133%);
108110
}
109111
}
112+
s-page.dark #graphRender {
113+
filter: invert(100%) hue-rotate(180deg) brightness(133%);
114+
}
115+
110116
.onresize #graphRender {
111117
left: 50%;
112118
right: unset;

src/graph/index.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<script setup lang="ts">
1414
import Graph from "./graph.vue";
1515
import { computed, onMounted, ref } from "vue";
16-
import { debounce } from "lodash-es";
16+
import { throttle } from "lodash-es";
1717
import { useProfile } from "@/consts";
1818
1919
const graphWidth = ref(0),
@@ -24,10 +24,10 @@ const fullUpdateState = ref(false);
2424
const shellRef = ref<HTMLDivElement>();
2525
onMounted(() => {
2626
const observer = new ResizeObserver(
27-
debounce(() => {
27+
throttle(() => {
2828
graphWidth.value = shellRef.value!.clientWidth;
2929
graphHeight.value = shellRef.value!.clientHeight;
30-
}, 250)
30+
}, 180)
3131
);
3232
observer.observe(shellRef.value!);
3333
});
@@ -45,7 +45,7 @@ emitter.on("require-full-update", () => {
4545
fullUpdateState.value = true;
4646
graphKey.value++;
4747
});
48-
emitter.on('require-post-update', () => {
48+
emitter.on("require-post-update", () => {
4949
graphKey.value++;
5050
});
5151
</script>

src/i18n.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default {
1919
confirm: "确定",
2020
language: "语言",
2121
repo: "GitHub 仓库",
22+
toogleTheme: '切换主题',
23+
copy: "复制",
2224
},
2325
graphType: {
2426
interval: "默认",
@@ -54,6 +56,7 @@ export default {
5456
source: "数据源",
5557
importSuccess: "导入成功",
5658
importFail: "导入失败",
59+
copySuccess: "已复制",
5760
},
5861
},
5962
"en-US": {
@@ -70,6 +73,8 @@ export default {
7073
confirm: "Confirm",
7174
language: "Language",
7275
repo: "GitHub Repo",
76+
toogleTheme: 'Toggle Theme',
77+
copy: "Copy",
7378
},
7479
graphType: {
7580
interval: "Interval",
@@ -105,6 +110,7 @@ export default {
105110
source: "Data Source",
106111
importSuccess: "Import success",
107112
importFail: "Import failed",
113+
copySuccess: "Copied",
108114
},
109115
},
110116
},

src/ui/icons/autotheme.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<s-icon>
3+
<svg viewBox="0 -960 960 960">
4+
<path
5+
d="M440-200q52 0 99-21t81-60q-128-8-214-99.5T320-600q0-13 1-25.5t3-24.5q-57 32-90.5 88T200-440q0 100 70 170t170 70Zm0 80q-134 0-227-93t-93-227q0-134 93-227t227-93q5 0 10 .5t10 .5q-29 32-44.5 73T400-600q0 100 70 170t170 70q31 0 60.5-7.5T756-390q-18 118-108 194t-208 76Zm112-400 128-360h80l128 360h-76l-28-80H656l-28 80h-76Zm122-134h92l-46-146-46 146ZM407-381Z"
6+
></path>
7+
</svg>
8+
</s-icon>
9+
</template>

src/ui/navbar.vue

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,30 @@
99
</span>
1010
</div>
1111

12+
<s-tooltip slot="action" id="themeTrigger">
13+
<s-icon-button slot="trigger" @click="theme.toogle">
14+
<SIconAutoTheme v-if="theme.index === 0" />
15+
<s-icon
16+
v-else
17+
:name="theme.index - 1 ? 'light_mode' : 'dark_mode'"
18+
></s-icon>
19+
</s-icon-button>
20+
{{ t("buttons.toogleTheme") }}
21+
</s-tooltip>
22+
1223
<s-picker
1324
slot="action"
1425
v-model.lazy="locale"
1526
id="language"
1627
:label="t('inputs.language')"
1728
>
18-
<s-icon-button slot="trigger">
19-
<SIconLanguage />
20-
</s-icon-button>
29+
<s-tooltip slot="trigger">
30+
<s-icon-button slot="trigger">
31+
<SIconLanguage />
32+
</s-icon-button>
33+
{{ t("buttons.language") }}
34+
</s-tooltip>
35+
2136
<s-picker-item value="zh-CN" selected>简体中文</s-picker-item>
2237
<s-picker-item value="en-US">English</s-picker-item>
2338
</s-picker>
@@ -38,6 +53,7 @@ const { locale, t } = useI18n();
3853
3954
import SIconLanguage from "@/ui/icons/language.vue";
4055
import SIconGtihub from "@/ui/icons/github.vue";
56+
import SIconAutoTheme from "@/ui/icons/autotheme.vue";
4157
4258
const lang = ref("");
4359
@@ -48,6 +64,9 @@ function openGithub() {
4864
}
4965
5066
const emit = defineEmits(["toggleDrawer"]);
67+
68+
import { useTheme } from "@/consts";
69+
const theme = useTheme();
5170
</script>
5271

5372
<style>
@@ -56,6 +75,10 @@ const emit = defineEmits(["toggleDrawer"]);
5675
z-index: 9;
5776
}
5877
78+
#themeTrigger s-icon {
79+
z-index: -1;
80+
}
81+
5982
.nav-title {
6083
font-size: 20px;
6184
font-weight: bold;

0 commit comments

Comments
 (0)