From f8f18fdfe800a406cb10fbecc08ac1d061313cc0 Mon Sep 17 00:00:00 2001 From: capdiem Date: Fri, 26 Jul 2024 10:49:18 +0800 Subject: [PATCH] refactor(Breakpoint): differentiate the window size, breakpoint and mobile change events (#2068) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻ refactor(Breakpoint): differentiate the window size, breakpoint and mobile change events * ♻ refactor: update the code that used deprecated methods --- .../features/breakpoints/Dialog.razor | 11 ++--- .../Examples/features/breakpoints/Name.razor | 4 +- .../pages/features/breakpoints/en-US.md | 3 -- .../pages/features/breakpoints/zh-CN.md | 3 -- .../Pages/AnnualService.razor | 4 +- docs/Masa.Docs.Shared/Pages/Home.razor.cs | 13 +----- .../Components/AppBar/MAppBarTitle.razor.cs | 17 ++++--- .../Components/DataTable/MDataTable.razor.cs | 46 ++++++++++++++----- .../Descriptions/MDescriptions.razor.cs | 10 ++-- .../MNavigationDrawer.razor.cs | 4 +- .../SlideGroup/MSlideGroup.razor.cs | 10 ++-- src/Masa.Blazor/Mixins/Menuable/MMenuable.cs | 6 +-- src/Masa.Blazor/Mixins/Mobile/IMobile.cs | 10 ---- .../Mixins/Mobile/MobileProvider.cs | 39 ---------------- .../Services/Breakpoint/Breakpoint.cs | 39 ++++++++-------- .../Breakpoint/BreakpointChangedEventArgs.cs | 12 ++--- .../Breakpoint/MobileChangedEventArgs.cs | 10 +++- .../Breakpoint/WindowSizeChangedEventArgs.cs | 6 +++ src/Masa.Blazor/Services/MasaBlazor.cs | 15 ++++-- 19 files changed, 115 insertions(+), 147 deletions(-) delete mode 100644 src/Masa.Blazor/Mixins/Mobile/IMobile.cs delete mode 100644 src/Masa.Blazor/Mixins/Mobile/MobileProvider.cs create mode 100644 src/Masa.Blazor/Services/Breakpoint/WindowSizeChangedEventArgs.cs diff --git a/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Dialog.razor b/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Dialog.razor index 9a68d80eb0..b0c1e7e3e9 100644 --- a/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Dialog.razor +++ b/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Dialog.razor @@ -25,22 +25,17 @@ { base.OnInitialized(); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + MasaBlazor.MobileChanged += MasaBlazorOnMobileChanged; } - private void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) + private void MasaBlazorOnMobileChanged(object? sender, MobileChangedEventArgs e) { - if (!e.MobileChanged) - { - return; - } - InvokeAsync(StateHasChanged); } public void Dispose() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.MobileChanged -= MasaBlazorOnMobileChanged; } } diff --git a/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Name.razor b/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Name.razor index b85d6fb0f0..77cec17c60 100644 --- a/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Name.razor +++ b/docs/Masa.Blazor.Docs/Examples/features/breakpoints/Name.razor @@ -11,7 +11,7 @@ { base.OnInitialized(); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged += BreakpointOnOnUpdate; } private void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) @@ -37,7 +37,7 @@ public void Dispose() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged -= BreakpointOnOnUpdate; } } diff --git a/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/en-US.md b/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/en-US.md index 09b448e0d7..51aeb10600 100644 --- a/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/en-US.md +++ b/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/en-US.md @@ -64,9 +64,6 @@ public class Breakpoint // Scrollbar public double ScrollBarWidth { get; set; } - - // Call if resize - public event Func OnUpdate; } ``` diff --git a/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/zh-CN.md b/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/zh-CN.md index dbdc4a1b6b..8a2ad12d72 100644 --- a/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/zh-CN.md +++ b/docs/Masa.Blazor.Docs/wwwroot/pages/features/breakpoints/zh-CN.md @@ -64,9 +64,6 @@ public class Breakpoint // 滚动条宽度 public double ScrollBarWidth { get; set; } - - // 大小发生变化时触发的事件 - public event Func OnUpdate; } ``` diff --git a/docs/Masa.Docs.Shared/Pages/AnnualService.razor b/docs/Masa.Docs.Shared/Pages/AnnualService.razor index c88bc6fcf5..85af875c72 100644 --- a/docs/Masa.Docs.Shared/Pages/AnnualService.razor +++ b/docs/Masa.Docs.Shared/Pages/AnnualService.razor @@ -83,7 +83,7 @@ { base.OnInitialized(); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged += BreakpointOnOnUpdate; } private void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) @@ -193,7 +193,7 @@ public void Dispose() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged -= BreakpointOnOnUpdate; } } diff --git a/docs/Masa.Docs.Shared/Pages/Home.razor.cs b/docs/Masa.Docs.Shared/Pages/Home.razor.cs index 31baa2a49e..ae93f2047e 100644 --- a/docs/Masa.Docs.Shared/Pages/Home.razor.cs +++ b/docs/Masa.Docs.Shared/Pages/Home.razor.cs @@ -12,8 +12,6 @@ public partial class Home : ComponentBase [SupplyParameterFromQuery(Name = "v")] [Parameter] public string? Version { get; set; } - private bool _prevXs; - protected override void OnInitialized() { base.OnInitialized(); @@ -21,19 +19,10 @@ protected override void OnInitialized() MasaBlazor.BreakpointChanged += MasaBlazorOnBreakpointChanged; } - protected override void OnAfterRender(bool firstRender) - { - if (firstRender) - { - _prevXs = MasaBlazor.Breakpoint.Xs; - } - } - private void MasaBlazorOnBreakpointChanged(object? sender, BreakpointChangedEventArgs e) { - if (_prevXs != MasaBlazor.Breakpoint.Xs) + if (e.Breakpoint == Breakpoints.Xs) { - _prevXs = MasaBlazor.Breakpoint.Xs; InvokeAsync(StateHasChanged); } } diff --git a/src/Masa.Blazor/Components/AppBar/MAppBarTitle.razor.cs b/src/Masa.Blazor/Components/AppBar/MAppBarTitle.razor.cs index 69a2368e44..ac43a4cb00 100644 --- a/src/Masa.Blazor/Components/AppBar/MAppBarTitle.razor.cs +++ b/src/Masa.Blazor/Components/AppBar/MAppBarTitle.razor.cs @@ -25,18 +25,17 @@ protected override void OnInitialized() { base.OnInitialized(); - MasaBlazor.BreakpointChanged += MasaBlazorOnBreakpointChanged; + MasaBlazor.WindowSizeChanged += MasaBlazorOnBreakpointChanged; } - private async void MasaBlazorOnBreakpointChanged(object? sender, BreakpointChangedEventArgs e) + private async void MasaBlazorOnBreakpointChanged(object? sender, WindowSizeChangedEventArgs e) { - if (Math.Abs(_windowsWidth - MasaBlazor.Breakpoint.Width) > 0.01) - { - _windowsWidth = MasaBlazor.Breakpoint.Width; + if (!(Math.Abs(_windowsWidth - MasaBlazor.Breakpoint.Width) > 0.01)) return; - await UpdateDimensions(); - await InvokeAsync(StateHasChanged); - } + _windowsWidth = MasaBlazor.Breakpoint.Width; + + await UpdateDimensions(); + await InvokeAsync(StateHasChanged); } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -89,7 +88,7 @@ private async Task UpdateDimensions() protected override ValueTask DisposeAsyncCore() { - MasaBlazor.BreakpointChanged -= MasaBlazorOnBreakpointChanged; + MasaBlazor.WindowSizeChanged -= MasaBlazorOnBreakpointChanged; return base.DisposeAsyncCore(); } } \ No newline at end of file diff --git a/src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs b/src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs index 836d458477..a7450d8332 100644 --- a/src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs +++ b/src/Masa.Blazor/Components/DataTable/MDataTable.razor.cs @@ -1,6 +1,6 @@ namespace Masa.Blazor; -public partial class MDataTable : MDataIterator, IMobile +public partial class MDataTable : MDataIterator { [Inject] public MasaBlazor MasaBlazor { get; set; } = null!; @@ -78,6 +78,7 @@ public partial class MDataTable : MDataIterator, IMobile public bool ShowSelect { get; set; } [Parameter] + [MasaApiParameter(ReleasedOn = "v1.4.0")] public bool FixedSelect { get; set; } [Parameter] @@ -128,8 +129,6 @@ public DataTableResizeMode ResizeMode set => SetValue(value); } - protected MobileProvider? MobileProvider { get; set; } - public IEnumerable> ComputedHeaders { get @@ -194,9 +193,34 @@ public IEnumerable> ComputedHeaders private bool HasEllipsis => Headers.Any(u => u.HasEllipsis); - public bool IsMobile => MobileProvider?.IsMobile ?? false; + private bool IsMobile { get; set; } + + private void CalculateIsMobile() + { + var (width, mobile, name, mobileBreakpoint) = MasaBlazor.Breakpoint; + + if (width == 0) + { + IsMobile = false; + return; + } - public Dictionary ColspanAttrs => new() + if (mobileBreakpoint.Equals(MobileBreakpoint)) + { + IsMobile = mobile; + return; + } + + if (MobileBreakpoint.IsT1) + { + IsMobile = width < MobileBreakpoint.AsT1; + return; + } + + IsMobile = name == MobileBreakpoint.AsT0; + } + + public Dictionary ColspanAttrs => new() { { "colspan", IsMobile ? null : (HeadersLength > 0 ? HeadersLength : ComputedHeaders.Count()) } }; @@ -223,8 +247,8 @@ protected override void OnInitialized() CustomFilter = CustomFilterWithColumns; ItemValues = Headers.Select(header => new ItemValue(header.Value)); - MobileProvider = new MobileProvider(this); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + CalculateIsMobile(); + MasaBlazor.WindowSizeChanged += MasaBlazorWindowSizeChanged; MasaBlazor.RTLChanged += MasaBlazorOnRTLChanged; } @@ -277,10 +301,10 @@ private string GetText(string value) return Headers.FirstOrDefault(h => h.Value == value)?.Text ?? value; } - private async void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) + private void MasaBlazorWindowSizeChanged(object? sender, WindowSizeChangedEventArgs e) { - MobileProvider = new MobileProvider(this); - await InvokeStateHasChangedAsync(); + CalculateIsMobile(); + InvokeAsync(StateHasChanged); } public Task HandleOnRowClickAsync(MouseEventArgs args, TItem item) @@ -362,7 +386,7 @@ public void RemoveGroup() protected override ValueTask DisposeAsyncCore() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.WindowSizeChanged -= MasaBlazorWindowSizeChanged; MasaBlazor.RTLChanged -= MasaBlazorOnRTLChanged; return base.DisposeAsyncCore(); diff --git a/src/Masa.Blazor/Components/Descriptions/MDescriptions.razor.cs b/src/Masa.Blazor/Components/Descriptions/MDescriptions.razor.cs index f6ef6661aa..f18c9d4035 100644 --- a/src/Masa.Blazor/Components/Descriptions/MDescriptions.razor.cs +++ b/src/Masa.Blazor/Components/Descriptions/MDescriptions.razor.cs @@ -189,7 +189,7 @@ protected override void OnInitialized() { base.OnInitialized(); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged += BreakpointOnOnUpdate; } @@ -197,7 +197,7 @@ protected override void OnAfterRender(bool firstRender) { base.OnAfterRender(firstRender); - // DescriptionItem may be update after render, so we need to render twice + // DescriptionItem may be updated after render, so we need to render twice if (_renderTwice) { _renderTwice = false; @@ -209,9 +209,9 @@ protected override void OnAfterRender(bool firstRender) } } - private async void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) + private void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) { - await InvokeStateHasChangedAsync(); + InvokeAsync(StateHasChanged); } private bool IndependentTheme => (IsDirtyParameter(nameof(Dark)) && Dark) || (IsDirtyParameter(nameof(Light)) && Light); @@ -277,7 +277,7 @@ internal async Task Unregister(IDescriptionsItem descriptionsItem) protected override ValueTask DisposeAsyncCore() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.BreakpointChanged -= BreakpointOnOnUpdate; return base.DisposeAsyncCore(); } diff --git a/src/Masa.Blazor/Components/NavigationDrawer/MNavigationDrawer.razor.cs b/src/Masa.Blazor/Components/NavigationDrawer/MNavigationDrawer.razor.cs index 5b88b5c185..8776f432f0 100644 --- a/src/Masa.Blazor/Components/NavigationDrawer/MNavigationDrawer.razor.cs +++ b/src/Masa.Blazor/Components/NavigationDrawer/MNavigationDrawer.razor.cs @@ -342,7 +342,7 @@ protected override void OnInitialized() _prevPermanent = Permanent; - MasaBlazor.Breakpoint.OnUpdate += OnBreakpointOnUpdate; + MasaBlazor.BreakpointChanged += OnBreakpointOnUpdate; if (Value == null && ValueChanged.HasDelegate) { @@ -592,7 +592,7 @@ public async Task HandleOnClickAsync(MouseEventArgs e) protected override async ValueTask DisposeAsyncCore() { RemoveApplication(); - MasaBlazor!.Breakpoint.OnUpdate -= OnBreakpointOnUpdate; + MasaBlazor!.BreakpointChanged -= OnBreakpointOnUpdate; MasaBlazor.Application.PropertyChanged -= ApplicationPropertyChanged; NavigationManager!.LocationChanged -= OnLocationChanged; await OutsideClickJsModule.UnbindAndDisposeAsync(); diff --git a/src/Masa.Blazor/Components/SlideGroup/MSlideGroup.razor.cs b/src/Masa.Blazor/Components/SlideGroup/MSlideGroup.razor.cs index 3c174a2d26..37e77ab7d2 100644 --- a/src/Masa.Blazor/Components/SlideGroup/MSlideGroup.razor.cs +++ b/src/Masa.Blazor/Components/SlideGroup/MSlideGroup.razor.cs @@ -56,15 +56,13 @@ protected override void OnInitialized() { base.OnInitialized(); - MasaBlazor.Breakpoint.OnUpdate += BreakpointOnOnUpdate; + MasaBlazor.MobileChanged += MasaBlazorOnMobileChanged; IsMobile = MasaBlazor.Breakpoint.Mobile; } - private async void BreakpointOnOnUpdate(object? sender, BreakpointChangedEventArgs e) + private async void MasaBlazorOnMobileChanged(object? sender, MobileChangedEventArgs e) { - if (!e.MobileChanged) return; - - IsMobile = MasaBlazor.Breakpoint.Mobile; + IsMobile = e.Mobile; await InvokeStateHasChangedAsync(); } @@ -385,7 +383,7 @@ protected async Task OnResize() protected override async ValueTask DisposeAsyncCore() { - MasaBlazor.Breakpoint.OnUpdate -= BreakpointOnOnUpdate; + MasaBlazor.MobileChanged -= MasaBlazorOnMobileChanged; await ResizeJSModule.UnobserveAsync(Ref); } } diff --git a/src/Masa.Blazor/Mixins/Menuable/MMenuable.cs b/src/Masa.Blazor/Mixins/Menuable/MMenuable.cs index 7890a88165..32e4ab44b6 100644 --- a/src/Masa.Blazor/Mixins/Menuable/MMenuable.cs +++ b/src/Masa.Blazor/Mixins/Menuable/MMenuable.cs @@ -304,10 +304,10 @@ protected override void OnInitialized() { base.OnInitialized(); - MasaBlazor.BreakpointChanged += MasaBlazorOnBreakpointChanged; + MasaBlazor.WindowSizeChanged += MasaBlazorOnWindowSizeChanged; } - private async void MasaBlazorOnBreakpointChanged(object? sender, BreakpointChangedEventArgs e) + private async void MasaBlazorOnWindowSizeChanged(object? sender, WindowSizeChangedEventArgs e) { if (!IsActive) { @@ -427,7 +427,7 @@ protected virtual Task DeactivateAsync() protected override async ValueTask DisposeAsyncCore() { - MasaBlazor.BreakpointChanged -= MasaBlazorOnBreakpointChanged; + MasaBlazor.WindowSizeChanged -= MasaBlazorOnWindowSizeChanged; if (ContentElement.Context is not null) { diff --git a/src/Masa.Blazor/Mixins/Mobile/IMobile.cs b/src/Masa.Blazor/Mixins/Mobile/IMobile.cs deleted file mode 100644 index 7449aa053b..0000000000 --- a/src/Masa.Blazor/Mixins/Mobile/IMobile.cs +++ /dev/null @@ -1,10 +0,0 @@ -using OneOf; - -namespace Masa.Blazor; - -public interface IMobile -{ - OneOf MobileBreakpoint { get; } - - MasaBlazor MasaBlazor { get; } -} diff --git a/src/Masa.Blazor/Mixins/Mobile/MobileProvider.cs b/src/Masa.Blazor/Mixins/Mobile/MobileProvider.cs deleted file mode 100644 index f3b3f61792..0000000000 --- a/src/Masa.Blazor/Mixins/Mobile/MobileProvider.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace Masa.Blazor; - -public class MobileProvider : IMobile -{ - public MobileProvider(IMobile instance) - { - MasaBlazor = instance.MasaBlazor; - MobileBreakpoint = instance.MobileBreakpoint; - } - - public MasaBlazor MasaBlazor { get; } - - public OneOf MobileBreakpoint { get; } - - public bool IsMobile - { - get - { - var (width, mobile, name, mobileBreakpoint) = MasaBlazor.Breakpoint; - - if (width == 0) - { - return false; - } - - if (mobileBreakpoint.Equals(this.MobileBreakpoint)) - { - return mobile; - } - - if (this.MobileBreakpoint.IsT1) - { - return width < this.MobileBreakpoint.AsT1; - } - - return name == this.MobileBreakpoint.AsT0; - } - } -} diff --git a/src/Masa.Blazor/Services/Breakpoint/Breakpoint.cs b/src/Masa.Blazor/Services/Breakpoint/Breakpoint.cs index a844a4ee84..b156a6b11e 100644 --- a/src/Masa.Blazor/Services/Breakpoint/Breakpoint.cs +++ b/src/Masa.Blazor/Services/Breakpoint/Breakpoint.cs @@ -13,12 +13,10 @@ public class BreakpointOptions public class Breakpoint { - private bool _prevMobile; - public Breakpoint() { } - + private IJSRuntime? JSRuntime { get; set; } /// @@ -81,7 +79,7 @@ public double GetMobileBreakpointValue() Breakpoints.Xs => Thresholds.Xs, Breakpoints.Sm => Thresholds.Sm, Breakpoints.Md => Thresholds.Md, - _ => Thresholds.Lg, + _ => Thresholds.Lg, }; } @@ -90,9 +88,9 @@ public double GetMobileBreakpointValue() public double ScrollBarWidth { get; internal set; } [Obsolete("Use MasaBlazor.BreakpointChanged instead")] - public event EventHandler OnUpdate; + public event EventHandler? OnUpdate; - internal Action OnChanged; + internal Action? OnWindowResize; public void Deconstruct( out double width, @@ -110,7 +108,7 @@ public async Task InitAsync(IJSRuntime jsRuntime) { JSRuntime = jsRuntime; - await UpdateAsync(); + await ResizeAsync(); _ = JSRuntime.AddHtmlElementEventListener("window", "resize", ResizeAsync, new EventListenerOptions { Passive = true }, @@ -118,11 +116,6 @@ public async Task InitAsync(IJSRuntime jsRuntime) } private async Task ResizeAsync() - { - await UpdateAsync(); - } - - public async Task UpdateAsync() { var height = await GetClientSizeAsync("Height"); var width = await GetClientSizeAsync("Width"); @@ -153,6 +146,9 @@ public async Task UpdateAsync() LgAndUp = !(xs || sm || md) && (lg || xl); XlOnly = xl; + var prevBreakpoint = Name; + var prevMobile = Mobile; + if (xs) { Name = Breakpoints.Xs; @@ -186,19 +182,20 @@ public async Task UpdateAsync() Mobile = current <= max; } - var eventArgs = new BreakpointChangedEventArgs(); - + // when breakpoint service is initialized at the first time, + // breakpointChanged and mobileChanged are always true + var breakpointChanged = Initialized == false || prevBreakpoint != Name; + var mobileChanged = Initialized == false || prevMobile != Mobile; Initialized = true; - if (_prevMobile != Mobile) + var eventArgs = new WindowSizeChangedEventArgs(Name, breakpointChanged, Mobile, mobileChanged); + + if (breakpointChanged) { - _prevMobile = Mobile; - eventArgs.MobileChanged = true; + OnUpdate?.Invoke(this, eventArgs); } - OnUpdate?.Invoke(this, eventArgs); - - OnChanged?.Invoke(eventArgs); + OnWindowResize?.Invoke(eventArgs); } private async Task GetClientSizeAsync(string sizeName) @@ -218,4 +215,4 @@ private async Task GetClientSizeAsync(string sizeName) var jsonElement = await JSRuntime.InvokeAsync(JsInteropConstants.GetProp, selector, name); return jsonElement.ValueKind == JsonValueKind.Number ? jsonElement.GetDouble() : null; } -} +} \ No newline at end of file diff --git a/src/Masa.Blazor/Services/Breakpoint/BreakpointChangedEventArgs.cs b/src/Masa.Blazor/Services/Breakpoint/BreakpointChangedEventArgs.cs index ade40ae842..508191beaf 100644 --- a/src/Masa.Blazor/Services/Breakpoint/BreakpointChangedEventArgs.cs +++ b/src/Masa.Blazor/Services/Breakpoint/BreakpointChangedEventArgs.cs @@ -1,9 +1,9 @@ namespace Masa.Blazor; -public class BreakpointChangedEventArgs : MobileChangedEventArgs +public class BreakpointChangedEventArgs(Breakpoints breakpoint, bool breakpointChanged, bool mobile, bool mobileChanged) + : MobileChangedEventArgs(mobile, mobileChanged) { - /// - /// Indicates whether the value of Mobile property has changed. First time is always false. - /// - public bool MobileChanged { get; set; } -} + public Breakpoints Breakpoint { get; } = breakpoint; + + internal bool BreakpointChanged { get; } = breakpointChanged; +} \ No newline at end of file diff --git a/src/Masa.Blazor/Services/Breakpoint/MobileChangedEventArgs.cs b/src/Masa.Blazor/Services/Breakpoint/MobileChangedEventArgs.cs index edbff893e7..1f73252242 100644 --- a/src/Masa.Blazor/Services/Breakpoint/MobileChangedEventArgs.cs +++ b/src/Masa.Blazor/Services/Breakpoint/MobileChangedEventArgs.cs @@ -1,5 +1,11 @@ namespace Masa.Blazor; -public class MobileChangedEventArgs : EventArgs +public class MobileChangedEventArgs(bool mobile, bool mobileChanged) : EventArgs { -} + public bool Mobile { get; } = mobile; + + /// + /// Indicates whether the value of Mobile property has changed. First time is always false. + /// + internal bool MobileChanged { get; } = mobileChanged; +} \ No newline at end of file diff --git a/src/Masa.Blazor/Services/Breakpoint/WindowSizeChangedEventArgs.cs b/src/Masa.Blazor/Services/Breakpoint/WindowSizeChangedEventArgs.cs new file mode 100644 index 0000000000..e44cc3e45f --- /dev/null +++ b/src/Masa.Blazor/Services/Breakpoint/WindowSizeChangedEventArgs.cs @@ -0,0 +1,6 @@ +namespace Masa.Blazor; + +public class WindowSizeChangedEventArgs(Breakpoints breakpoint, bool breakpointChanged, bool mobile, bool mobileChanged) + : BreakpointChangedEventArgs(breakpoint, breakpointChanged, mobile, mobileChanged) +{ +} \ No newline at end of file diff --git a/src/Masa.Blazor/Services/MasaBlazor.cs b/src/Masa.Blazor/Services/MasaBlazor.cs index 84bfc9f313..86679e4278 100644 --- a/src/Masa.Blazor/Services/MasaBlazor.cs +++ b/src/Masa.Blazor/Services/MasaBlazor.cs @@ -15,16 +15,20 @@ public MasaBlazor( { RTL = rtl; Breakpoint = breakpoint; - Breakpoint.OnChanged = e => + Breakpoint.OnWindowResize = e => { - BreakpointChanged?.Invoke(this, e); + WindowSizeChanged?.Invoke(this, e); + + if (e.BreakpointChanged) + { + BreakpointChanged?.Invoke(this, e); + } if (e.MobileChanged) { MobileChanged?.Invoke(this, e); } }; - Application = application; Theme = theme; Icons = icons; @@ -67,6 +71,11 @@ public bool RTL public event Action? OnThemeChange; + /// + /// An event that fires when the window size has changed. + /// + public event EventHandler? WindowSizeChanged; + /// /// An event that fires when the breakpoint has changed. ///