Skip to content

Commit 44aa89f

Browse files
authored
feat(DockView): support auto switch theme (#388)
* refactor: 代码重构 * refactor: 支持暗黑主题 * refactor: 增加 switchTheme 方法 * refactor: 增加主题响应式逻辑 * feat: 增加加载时检测主题逻辑 * chore: bump version 9.1.8
1 parent e1f8fff commit 44aa89f

4 files changed

Lines changed: 59 additions & 11 deletions

File tree

src/components/BootstrapBlazor.DockView/BootstrapBlazor.DockView.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Razor">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.1.7</Version>
4+
<Version>9.1.8</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
13+
<PackageReference Include="BootstrapBlazor" Version="9.4.12" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// DockViewV2 组件
1212
/// </summary>
13-
public partial class DockViewV2
13+
public partial class DockViewV2 : IDisposable
1414
{
1515
/// <summary>
1616
/// 获得/设置 DockView 名称 默认 null 用于本地存储识别
1717
/// </summary>
1818
[Parameter]
19-
#if NET6_0_OR_GREATER
2019
[EditorRequired]
21-
#endif
2220
[NotNull]
2321
public string? Name { get; set; }
2422

@@ -139,14 +137,18 @@ public partial class DockViewV2
139137
[NotNull]
140138
private IConfiguration? Configuration { get; set; }
141139

140+
[Inject]
141+
[NotNull]
142+
private IThemeProvider? ThemeProviderService { get; set; }
143+
142144
private string? ClassString => CssBuilder.Default("bb-dockview")
143145
.AddClassFromAttributes(AdditionalAttributes)
144146
.Build();
145147

146148
private readonly List<DockViewComponentBase> _components = [];
147149

148150
[NotNull]
149-
private DockViewOptions? _options = default!;
151+
private DockViewOptions? _options = null;
150152

151153
/// <summary>
152154
/// <inheritdoc/>
@@ -157,6 +159,8 @@ protected override void OnInitialized()
157159

158160
var section = Configuration.GetSection(nameof(DockViewOptions));
159161
_options = section.Exists() ? section.Get<DockViewOptions>() : new();
162+
163+
ThemeProviderService.ThemeChangedAsync += OnThemeChangedAsync;
160164
}
161165

162166
/// <summary>
@@ -224,6 +228,12 @@ public async Task Reset(string? layoutConfig = null)
224228
/// <returns></returns>
225229
public Task<string?> SaveLayout() => InvokeAsync<string?>("save", Id);
226230

231+
private Task OnThemeChangedAsync(string themeName)
232+
{
233+
Theme = themeName == "dark" ? DockViewTheme.Dark : DockViewTheme.Light;
234+
return Task.CompletedTask;
235+
}
236+
227237
/// <summary>
228238
/// 标签页关闭回调方法 由 JavaScript 调用
229239
/// </summary>
@@ -271,4 +281,21 @@ public async Task SplitterCallbackAsync()
271281
await OnSplitterCallbackAsync();
272282
}
273283
}
284+
285+
private void Dispose(bool disposing)
286+
{
287+
if (disposing)
288+
{
289+
ThemeProviderService.ThemeChangedAsync -= OnThemeChangedAsync;
290+
}
291+
}
292+
293+
/// <summary>
294+
/// <inheritdoc/>
295+
/// </summary>
296+
public void Dispose()
297+
{
298+
Dispose(true);
299+
GC.SuppressFinalize(this);
300+
}
274301
}

src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { addLink } from '../../BootstrapBlazor/modules/utility.js'
1+
import { addLink, getTheme } from '../../BootstrapBlazor/modules/utility.js'
22
import { cerateDockview } from '../js/dockview-utils.js'
33
import Data from '../../BootstrapBlazor/modules/data.js'
4+
import EventHandler from "../../BootstrapBlazor/modules/event-handler.js"
45

56
export async function init(id, invoke, options) {
67
await addLink("./_content/BootstrapBlazor.DockView/css/dockview-bb.css")
@@ -9,8 +10,15 @@ export async function init(id, invoke, options) {
910
return;
1011
}
1112

12-
const dockview = cerateDockview(el, options)
13-
Data.set(id, { el, dockview });
13+
if(options.theme === 'dockview-theme-light') {
14+
let theme = getTheme();
15+
if (theme === 'dark') {
16+
options.theme = `dockview-theme-dark`;
17+
}
18+
}
19+
const dockview = cerateDockview(el, options);
20+
const updateTheme = e => dockview.switchTheme(e.theme);
21+
Data.set(id, { el, dockview, updateTheme });
1422

1523
dockview.on('initialized', () => {
1624
invoke.invokeMethodAsync(options.initializedCallback);
@@ -24,6 +32,8 @@ export async function init(id, invoke, options) {
2432
dockview.on('groupSizeChanged', () => {
2533
invoke.invokeMethodAsync(options.splitterCallback);
2634
});
35+
36+
EventHandler.on(document, 'changed.bb.theme', updateTheme);
2737
}
2838

2939
export function update(id, options) {
@@ -57,6 +67,8 @@ export function dispose(id) {
5767
Data.remove(id);
5868

5969
if (dock) {
70+
EventHandler.off(document, 'changed.bb.theme', dock.updateTheme);
71+
6072
const { dockview } = dock;
6173
if (dockview) {
6274
dockview.dispose();

src/components/BootstrapBlazor.DockView/wwwroot/js/dockview-utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { getConfig, reloadFromConfig, loadPanelsFromLocalstorage, saveConfig } f
66
import './dockview-extensions.js'
77

88
const cerateDockview = (el, options) => {
9+
const theme = options.theme || "dockview-theme-light";
910
const template = el.querySelector('template');
1011
const dockview = new DockviewComponent(el, {
1112
parentElement: el,
1213
theme: {
1314
name: "bb-dockview",
14-
className: options.theme || "dockview-theme-light",
15+
className: theme,
1516
dndOverlayMounting: 'absolute',
1617
dndPanelOverlay: 'group'
1718
},
@@ -35,6 +36,14 @@ const initDockview = (dockview, options, template) => {
3536
window.dockview = dockview;
3637
}
3738

39+
dockview.switchTheme = theme => {
40+
const themeName = `dockview-theme-${theme}`;
41+
if (dockview.options.theme.className !== themeName) {
42+
dockview.options.theme.className = themeName;
43+
dockview.updateTheme();
44+
}
45+
}
46+
3847
dockview.update = options => {
3948
if (options.layoutConfig) {
4049
reloadFromConfig(dockview, options);

0 commit comments

Comments
 (0)