Skip to content

Commit

Permalink
feat: auto dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Cat committed Jun 21, 2024
1 parent 844095e commit 7882cc9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
23 changes: 21 additions & 2 deletions resources/views/tabler/admin/header.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
<!doctype html>
<html lang="{$user->locale}">
<html lang="{$user->locale}"
data-bs-theme="{$user->is_dark_mode === 1 ? 'dark' : ($user->is_dark_mode === 2 ? 'auto' : 'light')}">

<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"/>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/>
<meta name="format-detection" content="telephone=no"/>
<title>{$config['appName']}</title>
<!-- Auto dark mode -->
<script>
;(function () {
const htmlElement = document.querySelector("html")
const theme = htmlElement.getAttribute("data-bs-theme");
if(theme === 'dark-auto' || theme === 'auto') {
function updateTheme() {
htmlElement.setAttribute("data-bs-theme",
window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
updateTheme()
}
})()
</script>
<!-- CSS files -->
<link href="//{$config['jsdelivr_url']}/npm/@tabler/core@latest/dist/css/tabler.min.css" rel="stylesheet"/>
<link href="//{$config['jsdelivr_url']}/npm/@tabler/icons-webfont@latest/tabler-icons.min.css" rel="stylesheet"/>
Expand All @@ -26,8 +43,10 @@
</style>
</head>

{if $user->is_dark_mode}
{if $user->is_dark_mode === 1}
<body data-bs-theme="dark">
{elseif $user->is_dark_mode === 2}
<body data-bs-theme="auto">
{else}
<body>
{/if}
Expand Down
18 changes: 17 additions & 1 deletion resources/views/tabler/header.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="{$config['locale']}">
<html lang="{$config['locale']}" data-bs-theme="auto">

<head>
<meta charset="utf-8"/>
Expand All @@ -8,6 +8,22 @@
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>{$config['appName']}</title>
<!-- Auto dark mode -->
<script>
;(function () {
const htmlElement = document.querySelector("html")
const theme = htmlElement.getAttribute("data-bs-theme");
if(theme === 'dark-auto' || theme === 'auto') {
function updateTheme() {
htmlElement.setAttribute("data-bs-theme",
window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
updateTheme()
}
})()
</script>
<!-- CSS files -->
<link href="//{$config['jsdelivr_url']}/npm/@tabler/core@latest/dist/css/tabler.min.css" rel="stylesheet"/>
<link href="//{$config['jsdelivr_url']}/npm/@tabler/icons-webfont@latest/tabler-icons.min.css" rel="stylesheet"/>
Expand Down
7 changes: 5 additions & 2 deletions resources/views/tabler/user/edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,13 @@
<h3 class="card-title">修改主题模式</h3>
<div class="mb-3">
<select id="theme-mode" class="form-select">
<option value="0" {if ! $user->is_dark_mode}selected{/if}>
<option value="2" {if $user->is_dark_mode === 2}selected{/if}>
自动
</option>
<option value="0" {if $user->is_dark_mode === 0}selected{/if}>
浅色
</option>
<option value="1" {if $user->is_dark_mode}selected{/if}>
<option value="1" {if $user->is_dark_mode === 1}selected{/if}>
深色
</option>
</select>
Expand Down
23 changes: 21 additions & 2 deletions resources/views/tabler/user/header.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!doctype html>
<html lang="{$user->locale}">
<html lang="{$user->locale}"
data-bs-theme="{$user->is_dark_mode === 1 ? 'dark' : ($user->is_dark_mode === 2 ? 'auto' : 'light')}">

<head>
<meta charset="utf-8"/>
Expand All @@ -8,6 +9,22 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<meta name="referrer" content="never">
<title>{$config['appName']}</title>
<!-- Auto dark mode -->
<script>
;(function () {
const htmlElement = document.querySelector("html")
const theme = htmlElement.getAttribute("data-bs-theme");
if(theme === 'dark-auto' || theme === 'auto') {
function updateTheme() {
htmlElement.setAttribute("data-bs-theme",
window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
updateTheme()
}
})()
</script>
<!-- CSS files -->
<link href="//{$config['jsdelivr_url']}/npm/@tabler/core@latest/dist/css/tabler.min.css" rel="stylesheet"/>
<link href="//{$config['jsdelivr_url']}/npm/@tabler/icons-webfont@latest/tabler-icons.min.css" rel="stylesheet"/>
Expand Down Expand Up @@ -38,8 +55,10 @@
</style>
</head>

{if $user->is_dark_mode}
{if $user->is_dark_mode === 1}
<body data-bs-theme="dark">
{elseif $user->is_dark_mode === 2}
<body data-bs-theme="auto">
{else}
<body>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/tabler/user/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
速度限制
</div>
<div class="text-secondary">
{if $user->node_speedlimit !== 0.0}
{if $user->node_speedlimit !== 0}
<code>{$user->node_speedlimit}</code>
Mbps
{else}
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/User/InfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function updateThemeMode(ServerRequest $request, Response $response, arra
$theme_mode = (int) $this->antiXss->xss_clean($request->getParam('theme_mode'));
$user = $this->user;

$user->is_dark_mode = in_array($theme_mode, [0, 1]) ? $theme_mode : 0;
$user->is_dark_mode = in_array($theme_mode, [0, 1, 2]) ? $theme_mode : 0;

if (! $user->save()) {
return ResponseHelper::error($response, '切换失败');
Expand Down

0 comments on commit 7882cc9

Please sign in to comment.