-
-
Notifications
You must be signed in to change notification settings - Fork 364
fix(DockView): theme parameter not work #5705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ca9547
07b8dc3
0219f00
35bff67
7c5c1b5
d567baa
1a3b1f6
3ed83cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using Microsoft.JSInterop; | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Components; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -13,6 +15,10 @@ public partial class ThemeMode | |
| [Inject, NotNull] | ||
| private IIconTheme? IconTheme { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IThemeProvider? ThemeProvider { get; set; } | ||
|
|
||
| private string? GetLightIconClassString => CssBuilder.Default("icon-light") | ||
| .AddClass(_lightModeIcon) | ||
| .Build(); | ||
|
|
@@ -35,4 +41,17 @@ protected override void OnInitialized() | |
| _darkModeIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderDarkModeIcon); | ||
| _lightModeIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderLightModeIcon); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(OnThemeChanged)); | ||
|
|
||
| /// <summary> | ||
| /// The callback when theme changed | ||
| /// </summary> | ||
| /// <param name="themeName"></param> | ||
| /// <returns></returns> | ||
| [JSInvokable] | ||
| public ValueTask OnThemeChanged(string themeName) => ThemeProvider.SetThemeAsync(themeName); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,10 @@ public abstract class BaseDockView : ComponentBase | |
| [NotNull] | ||
| private MockDataTableDynamicService? DataTableDynamicService { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private IThemeProvider? ThemeProviderService { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 数据集合 | ||
| /// </summary> | ||
|
|
@@ -39,6 +43,11 @@ public abstract class BaseDockView : ComponentBase | |
| /// </summary> | ||
| protected DataTableDynamicContext? DataTableDynamicContext { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the theme | ||
| /// </summary> | ||
| protected DockViewTheme Theme { get; set; } = DockViewTheme.Light; | ||
|
|
||
| /// <summary> | ||
| /// 获得 <see cref="DynamicObjectContext"/> 实例方法 | ||
| /// </summary> | ||
|
|
@@ -51,6 +60,8 @@ public abstract class BaseDockView : ComponentBase | |
| /// </summary> | ||
| protected override void OnInitialized() | ||
| { | ||
| base.OnInitialized(); | ||
|
|
||
| Items = Foo.GenerateFoo(LocalizerFoo, 50); | ||
|
|
||
| // 模拟数据从数据库中获得 | ||
|
|
@@ -63,6 +74,22 @@ protected override void OnInitialized() | |
| TreeItems.AddRange(TreeFoo.GenerateFoos(LocalizerFoo, 3, 101, 1010)); | ||
|
|
||
| DataTableDynamicContext = DataTableDynamicService.CreateContext(); | ||
|
|
||
| ThemeProviderService.ThemeChangedAsync += OnThemeChanged; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Unsubscribe from ThemeChangedAsync to prevent potential memory leaks. Since the component registers for theme change notifications during OnInitialized, consider unsubscribing in Dispose to ensure that disposed instances do not remain referenced through the delegate. Suggested implementation: base.OnInitialized();
ThemeProviderService.ThemeChangedAsync += OnThemeChanged; // Unsubscribe from the ThemeChangedAsync event to avoid memory leaks
public void Dispose()
{
if (ThemeProviderService != null)
{
ThemeProviderService.ThemeChangedAsync -= OnThemeChanged;
}
GC.SuppressFinalize(this);
}If your class already overrides a Dispose(bool disposing) method, then incorporate the unsubscription logic there instead of creating a new Dispose() method. Additionally, ensure that the class implements IDisposable if it isn’t already. |
||
| } | ||
|
|
||
| private Task OnThemeChanged(string themeName) | ||
| { | ||
| if (themeName == "dark") | ||
| { | ||
| Theme = DockViewTheme.Dark; | ||
| } | ||
| else | ||
| { | ||
| Theme = DockViewTheme.Light; | ||
| } | ||
| StateHasChanged(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| // See the LICENSE file in the project root for more information. | ||
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -14,20 +16,24 @@ public enum ThemeValue | |
| /// <summary> | ||
| /// 自动 | ||
| /// </summary> | ||
| [Description("auto")] | ||
| Auto, | ||
|
|
||
| /// <summary> | ||
| /// 明亮主题 | ||
| /// </summary> | ||
| [Description("light")] | ||
| Light, | ||
|
|
||
| /// <summary> | ||
| /// 暗黑主题 | ||
| /// </summary> | ||
| [Description("dark")] | ||
| Dark, | ||
|
|
||
| /// <summary> | ||
| /// 使用本地保存选项 | ||
| /// </summary> | ||
| [Description("useLocalStorage")] | ||
| UseLocalStorage, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Manage event subscription lifecycle in Pre component.
Just as in other components, ensure that the event handler registered to ThemeProviderService.ThemeChangedAsync is removed when the component is disposed. This helps avoid unintended behavior and memory retention.
Suggested implementation:
Ensure that the method OnThemeChanged exists in the component. If not, implement it accordingly.