diff --git a/CaddyManager/Components/Layout/MainLayout.razor b/CaddyManager/Components/Layout/MainLayout.razor index 20c3b35..197a72c 100644 --- a/CaddyManager/Components/Layout/MainLayout.razor +++ b/CaddyManager/Components/Layout/MainLayout.razor @@ -10,22 +10,22 @@ @* Needed for snackbars *@ - - - - Caddy Manager - - - - - - - @Body - - - + -@code +@if (!_isInitialing) { - private NavigationDrawer _drawer = null!; -} + + + + Caddy Manager + + + + + + + @Body + + + +} \ No newline at end of file diff --git a/CaddyManager/Components/Layout/MainLayout.razor.cs b/CaddyManager/Components/Layout/MainLayout.razor.cs new file mode 100644 index 0000000..e87bac4 --- /dev/null +++ b/CaddyManager/Components/Layout/MainLayout.razor.cs @@ -0,0 +1,37 @@ +using MudBlazor; + +namespace CaddyManager.Components.Layout; + +public partial class MainLayout +{ + // To allow the menu button to control the drawer + private NavigationDrawer _drawer = null!; + + private bool _isDarkMode; + private bool _isInitialing = true; + private MudThemeProvider _mudThemeProvider = null!; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + // Get the system preference for dark mode + _isDarkMode = await _mudThemeProvider.GetSystemPreference(); + await _mudThemeProvider.WatchSystemPreference(OnSystemPreferenceChanged); + _isInitialing = false; + StateHasChanged(); + } + } + + /// + /// Method to handle the system preference change for dark mode + /// + /// + /// + private Task OnSystemPreferenceChanged(bool newValue) + { + _isDarkMode = newValue; + StateHasChanged(); + return Task.CompletedTask; + } +} \ No newline at end of file