diff --git a/src/components/BootstrapBlazor.DockView/BootstrapBlazor.DockView.csproj b/src/components/BootstrapBlazor.DockView/BootstrapBlazor.DockView.csproj index b6b7b0a2..05dd23b3 100644 --- a/src/components/BootstrapBlazor.DockView/BootstrapBlazor.DockView.csproj +++ b/src/components/BootstrapBlazor.DockView/BootstrapBlazor.DockView.csproj @@ -1,7 +1,7 @@ - + - 9.1.7 + 9.1.8 @@ -10,7 +10,7 @@ - + diff --git a/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.cs b/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.cs index f025e7f5..f1a3f025 100644 --- a/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.cs +++ b/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.cs @@ -10,15 +10,13 @@ namespace BootstrapBlazor.Components; /// /// DockViewV2 组件 /// -public partial class DockViewV2 +public partial class DockViewV2 : IDisposable { /// /// 获得/设置 DockView 名称 默认 null 用于本地存储识别 /// [Parameter] -#if NET6_0_OR_GREATER [EditorRequired] -#endif [NotNull] public string? Name { get; set; } @@ -139,6 +137,10 @@ public partial class DockViewV2 [NotNull] private IConfiguration? Configuration { get; set; } + [Inject] + [NotNull] + private IThemeProvider? ThemeProviderService { get; set; } + private string? ClassString => CssBuilder.Default("bb-dockview") .AddClassFromAttributes(AdditionalAttributes) .Build(); @@ -146,7 +148,7 @@ public partial class DockViewV2 private readonly List _components = []; [NotNull] - private DockViewOptions? _options = default!; + private DockViewOptions? _options = null; /// /// @@ -157,6 +159,8 @@ protected override void OnInitialized() var section = Configuration.GetSection(nameof(DockViewOptions)); _options = section.Exists() ? section.Get() : new(); + + ThemeProviderService.ThemeChangedAsync += OnThemeChangedAsync; } /// @@ -224,6 +228,12 @@ public async Task Reset(string? layoutConfig = null) /// public Task SaveLayout() => InvokeAsync("save", Id); + private Task OnThemeChangedAsync(string themeName) + { + Theme = themeName == "dark" ? DockViewTheme.Dark : DockViewTheme.Light; + return Task.CompletedTask; + } + /// /// 标签页关闭回调方法 由 JavaScript 调用 /// @@ -271,4 +281,21 @@ public async Task SplitterCallbackAsync() await OnSplitterCallbackAsync(); } } + + private void Dispose(bool disposing) + { + if (disposing) + { + ThemeProviderService.ThemeChangedAsync -= OnThemeChangedAsync; + } + } + + /// + /// + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } diff --git a/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.js b/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.js index 470f800b..06440b21 100644 --- a/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.js +++ b/src/components/BootstrapBlazor.DockView/Components/DockViewV2.razor.js @@ -1,6 +1,7 @@ -import { addLink } from '../../BootstrapBlazor/modules/utility.js' +import { addLink, getTheme } from '../../BootstrapBlazor/modules/utility.js' import { cerateDockview } from '../js/dockview-utils.js' import Data from '../../BootstrapBlazor/modules/data.js' +import EventHandler from "../../BootstrapBlazor/modules/event-handler.js" export async function init(id, invoke, options) { await addLink("./_content/BootstrapBlazor.DockView/css/dockview-bb.css") @@ -9,8 +10,15 @@ export async function init(id, invoke, options) { return; } - const dockview = cerateDockview(el, options) - Data.set(id, { el, dockview }); + if(options.theme === 'dockview-theme-light') { + let theme = getTheme(); + if (theme === 'dark') { + options.theme = `dockview-theme-dark`; + } + } + const dockview = cerateDockview(el, options); + const updateTheme = e => dockview.switchTheme(e.theme); + Data.set(id, { el, dockview, updateTheme }); dockview.on('initialized', () => { invoke.invokeMethodAsync(options.initializedCallback); @@ -24,6 +32,8 @@ export async function init(id, invoke, options) { dockview.on('groupSizeChanged', () => { invoke.invokeMethodAsync(options.splitterCallback); }); + + EventHandler.on(document, 'changed.bb.theme', updateTheme); } export function update(id, options) { @@ -57,6 +67,8 @@ export function dispose(id) { Data.remove(id); if (dock) { + EventHandler.off(document, 'changed.bb.theme', dock.updateTheme); + const { dockview } = dock; if (dockview) { dockview.dispose(); diff --git a/src/components/BootstrapBlazor.DockView/wwwroot/js/dockview-utils.js b/src/components/BootstrapBlazor.DockView/wwwroot/js/dockview-utils.js index beaf9e22..bfbe378a 100644 --- a/src/components/BootstrapBlazor.DockView/wwwroot/js/dockview-utils.js +++ b/src/components/BootstrapBlazor.DockView/wwwroot/js/dockview-utils.js @@ -6,12 +6,13 @@ import { getConfig, reloadFromConfig, loadPanelsFromLocalstorage, saveConfig } f import './dockview-extensions.js' const cerateDockview = (el, options) => { + const theme = options.theme || "dockview-theme-light"; const template = el.querySelector('template'); const dockview = new DockviewComponent(el, { parentElement: el, theme: { name: "bb-dockview", - className: options.theme || "dockview-theme-light", + className: theme, dndOverlayMounting: 'absolute', dndPanelOverlay: 'group' }, @@ -35,6 +36,14 @@ const initDockview = (dockview, options, template) => { window.dockview = dockview; } + dockview.switchTheme = theme => { + const themeName = `dockview-theme-${theme}`; + if (dockview.options.theme.className !== themeName) { + dockview.options.theme.className = themeName; + dockview.updateTheme(); + } + } + dockview.update = options => { if (options.layoutConfig) { reloadFromConfig(dockview, options);