Skip to content

Commit 0696acb

Browse files
committed
refactor: 支持暗黑主题
1 parent b3d2c4f commit 0696acb

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ 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 用于本地存储识别
@@ -137,14 +137,18 @@ public partial class DockViewV2
137137
[NotNull]
138138
private IConfiguration? Configuration { get; set; }
139139

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

144148
private readonly List<DockViewComponentBase> _components = [];
145149

146150
[NotNull]
147-
private DockViewOptions? _options = default!;
151+
private DockViewOptions? _options = null;
148152

149153
/// <summary>
150154
/// <inheritdoc/>
@@ -155,6 +159,8 @@ protected override void OnInitialized()
155159

156160
var section = Configuration.GetSection(nameof(DockViewOptions));
157161
_options = section.Exists() ? section.Get<DockViewOptions>() : new();
162+
163+
ThemeProviderService.ThemeChangedAsync += OnThemeChangedAsync;
158164
}
159165

160166
/// <summary>
@@ -222,6 +228,12 @@ public async Task Reset(string? layoutConfig = null)
222228
/// <returns></returns>
223229
public Task<string?> SaveLayout() => InvokeAsync<string?>("save", Id);
224230

231+
private Task OnThemeChangedAsync(string themeName)
232+
{
233+
Theme = themeName == "dark" ? DockViewTheme.Dark : DockViewTheme.Light;
234+
return Task.CompletedTask;
235+
}
236+
225237
/// <summary>
226238
/// 标签页关闭回调方法 由 JavaScript 调用
227239
/// </summary>
@@ -269,4 +281,21 @@ public async Task SplitterCallbackAsync()
269281
await OnSplitterCallbackAsync();
270282
}
271283
}
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+
}
272301
}

0 commit comments

Comments
 (0)