46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using AcdiuTools.Services;
|
|
using Microsoft.AspNetCore.Razor.TagHelpers;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AcdiuTools.TagHelpers
|
|
{
|
|
/// <summary>
|
|
/// 主题切换按钮 Tag Helper
|
|
/// 用法: <theme-toggle />
|
|
/// </summary>
|
|
[HtmlTargetElement("theme-toggle")]
|
|
public class ThemeToggleTagHelper(IThemeService themeService) : TagHelper
|
|
{
|
|
private readonly IThemeService _themeService = themeService;
|
|
|
|
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
|
{
|
|
// 获取当前主题
|
|
var currentTheme = await _themeService.GetUserThemeAsync();
|
|
bool isDark = currentTheme == "dark";
|
|
|
|
// 设置标签名为 button
|
|
output.TagName = "button";
|
|
output.TagMode = TagMode.StartTagAndEndTag;
|
|
|
|
// 添加基础 CSS 类 (Bootstrap)
|
|
output.Attributes.Add("class", "btn btn-outline-secondary border-0");
|
|
output.Attributes.Add("id", "theme-toggle-btn");
|
|
output.Attributes.Add("type", "button");
|
|
output.Attributes.Add("aria-label", "Toggle Theme");
|
|
|
|
// 添加 Tooltip (Bootstrap data-bs-toggle)
|
|
output.Attributes.Add("data-bs-toggle", "tooltip");
|
|
output.Attributes.Add("data-bs-placement", "bottom");
|
|
output.Attributes.Add("title", isDark ? "切换到浅色模式" : "切换到深色模式");
|
|
|
|
// 根据当前主题设置图标内容
|
|
// 使用 Bootstrap Icons 或 FontAwesome
|
|
string iconHtml = isDark
|
|
? "<i class=\"bi bi-sun-fill\"></i>" // 深色模式下显示太阳图标(暗示切换到亮)
|
|
: "<i class=\"bi bi-moon-stars-fill\"></i>"; // 浅色模式下显示月亮图标
|
|
|
|
output.Content.SetHtmlContent(iconHtml);
|
|
}
|
|
}
|
|
} |