🐛 fix(AppBar): layout creation and destruction order affected by async JS interop (#2382)

This commit is contained in:
capdiem
2025-04-11 17:17:28 +08:00
committed by GitHub
parent bf405a1c33
commit 65395002e9
6 changed files with 53 additions and 82 deletions

View File

@@ -13,14 +13,14 @@ public partial class MApp : MasaComponentBase, IDefaultsProvider
public IDictionary<string, IDictionary<string, object?>?>? Defaults => MasaBlazor.Defaults;
protected bool IsDark => MasaBlazor?.Theme is { Dark: true };
protected bool IsDark => MasaBlazor.Theme is { Dark: true };
private PopupService PopupService => (PopupService)InternalPopupService;
protected override void OnInitialized()
{
base.OnInitialized();
PopupService.StateChanged += OnStateChanged;
MasaBlazor.OnThemeChange += OnThemeChange;
MasaBlazor.RTLChanged += OnRTLChanged;

View File

@@ -464,26 +464,15 @@ public class MAppBar : MToolbar, IScrollable
}
if (!Bottom)
MasaBlazor!.Application.Top = 0;
MasaBlazor.Application.Top = 0;
else
MasaBlazor!.Application.Bottom = 0;
MasaBlazor.Application.Bottom = 0;
}
protected override async ValueTask DisposeAsyncCore()
{
try
{
await Js.RemoveHtmlElementEventListener(_scrollEventId);
}
catch (Exception)
{
// ignored
}
if (MasaBlazor == null) return;
RemoveApplication();
MasaBlazor.Application.PropertyChanged -= ApplicationPropertyChanged;
await Js.RemoveHtmlElementEventListener(_scrollEventId);
}
}

View File

@@ -44,6 +44,11 @@ public partial class MMain : MasaComponentBase
private void OnApplicationPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (IsDisposed)
{
return;
}
if (_applicationProperties.Contains(e.PropertyName))
{
_sized = true;

View File

@@ -660,8 +660,8 @@ public partial class MNavigationDrawer : MasaComponentBase, IOutsideClickJsCallb
}
if (Right)
MasaBlazor!.Application.Right = 0;
MasaBlazor.Application.Right = 0;
else
MasaBlazor!.Application.Left = 0;
MasaBlazor.Application.Left = 0;
}
}

View File

@@ -1,111 +1,90 @@
using System.Runtime.CompilerServices;
using System.ComponentModel;
[assembly:InternalsVisibleTo("Masa.Blazor.Test")]
[assembly: InternalsVisibleTo("Masa.Blazor.Test")]
namespace Masa.Blazor
{
public class Application : INotifyPropertyChanged
{
private double _bar;
private double _top;
private double _left;
private double _insetFooter;
private double _right;
private double _bottom;
private double _footer;
public double Bar
{
get => _bar;
get;
internal set
{
if (_bar != value)
{
_bar = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar)));
}
}
public double Top
{
get => _top;
get;
internal set
{
if (_top != value)
{
_top = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Top)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Top)));
}
}
public double Left
{
get => _left;
get;
internal set
{
if (_left != value)
{
_left = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Left)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Left)));
}
}
public double InsetFooter
{
get => _insetFooter;
get;
internal set
{
if (_insetFooter != value)
{
_insetFooter = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InsetFooter)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InsetFooter)));
}
}
public double Right
{
get => _right;
get;
internal set
{
if (_right != value)
{
_right = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Right)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Right)));
}
}
public double Bottom
{
get => _bottom;
get;
internal set
{
if (_bottom != value)
{
_bottom = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bottom)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bottom)));
}
}
public double Footer
{
get => _footer;
get;
internal set
{
if (_footer != value)
{
_footer = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Footer)));
}
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Footer)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;
}
}
}

View File

@@ -2,8 +2,6 @@
{
public class MasaBlazor
{
private bool _rtl;
public MasaBlazor(
bool rtl,
Breakpoint breakpoint,
@@ -38,13 +36,13 @@
public bool RTL
{
get => _rtl;
get;
set
{
if (_rtl != value)
if (field != value)
{
_rtl = value;
OnRTLChange?.Invoke(_rtl);
field = value;
OnRTLChange?.Invoke(field);
RTLChanged?.Invoke(this, EventArgs.Empty);
}
}
@@ -74,7 +72,7 @@
/// <summary>
/// An event that fires when the window size has changed.
/// </summary>
public event EventHandler<WindowSizeChangedEventArgs>? WindowSizeChanged;
public event EventHandler<WindowSizeChangedEventArgs>? WindowSizeChanged;
/// <summary>
/// An event that fires when the breakpoint has changed.
@@ -85,9 +83,9 @@
/// An event that fires when the value of Mobile property from <see cref="Breakpoint"/> has changed.
/// </summary>
public event EventHandler<MobileChangedEventArgs> MobileChanged;
public event EventHandler? DefaultsChanged;
public event EventHandler? DefaultsChanged;
public void ToggleTheme()
{
Theme.Dark = !Theme.Dark;
@@ -116,4 +114,4 @@
DefaultsChanged?.Invoke(this, EventArgs.Empty);
}
}
}
}