-
Notifications
You must be signed in to change notification settings - Fork 480
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
Theme Detection (Dark Mode) #246
Merged
tlmii
merged 6 commits into
dotnet:main
from
tlmii:dev/system-theme-detection-and-dark-mode
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6d05c8
Theme Detection (Dark Mode)
tlmii 7f5b0f0
Add comment
tlmii 1391952
Merge branch 'main' into dev/system-theme-detection-and-dark-mode
tlmii a43e8c8
Update MainLayout.razor
vnbaaij 84aad88
Update MainLayout.razor.cs
vnbaaij a396e7b
Update MainLayout.razor.cs
vnbaaij File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Aspire.Dashboard/Components/Layout/MainLayout.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Fast.Components.FluentUI; | ||
using Microsoft.Fast.Components.FluentUI.DesignTokens; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Aspire.Dashboard.Components.Layout; | ||
|
||
public partial class MainLayout | ||
{ | ||
[Inject] | ||
private IJSRuntime JS { get; set; } = default!; | ||
|
||
[Inject] | ||
private BaseLayerLuminance BaseLayerLuminance { get; set; } = default!; | ||
|
||
[Inject] | ||
private PersistentComponentState ApplicationState { get; set; } = default!; | ||
|
||
[CascadingParameter] | ||
public HttpContext? HttpContext { get; set; } | ||
|
||
private StandardLuminance _baseLayerLuminance = StandardLuminance.LightMode; | ||
private PersistingComponentStateSubscription _persistingSubscription; | ||
|
||
private ElementReference _container = default!; | ||
|
||
protected override void OnParametersSet() | ||
{ | ||
if (HttpContext is not null) | ||
{ | ||
_persistingSubscription = ApplicationState.RegisterOnPersisting(PersistBaseLayerLuminance); | ||
|
||
// Look to see if we have a cookie saying what the last system theme was | ||
// and set the base layer luminance based on that | ||
var lastSystemTheme = HttpContext.Request.Cookies["lastSystemTheme"]; | ||
_baseLayerLuminance = lastSystemTheme switch | ||
{ | ||
"dark" => StandardLuminance.DarkMode, | ||
_ => StandardLuminance.LightMode | ||
}; | ||
} | ||
} | ||
|
||
protected override void OnInitialized() | ||
{ | ||
// See if we got a base layer luminance value from the cookie and set the value | ||
// This will avoid a flash of white if the last system theme and current system theme are both dark | ||
if (ApplicationState.TryTakeFromJson<StandardLuminance>("baseLayerLuminance", out var restoredBaseLayerLuminance)) | ||
{ | ||
_baseLayerLuminance = restoredBaseLayerLuminance; | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
// Use javascript to determine the current system theme and set the theme cookie | ||
// based on that value. Then use the result of that to update the base layer luminance. | ||
// If the system theme hasn't changed from last time, this will have no effect. | ||
// If it has, we might have a flash of (last system theme color) before the current | ||
// system theme color takes effect. | ||
var isSystemThemeDark = await JS.InvokeAsync<bool>("setThemeCookie"); | ||
_baseLayerLuminance = isSystemThemeDark ? StandardLuminance.DarkMode : StandardLuminance.LightMode; | ||
|
||
await BaseLayerLuminance.SetValueFor(_container, _baseLayerLuminance.GetLuminanceValue()); | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
private Task PersistBaseLayerLuminance() | ||
{ | ||
// Persist the base layer luminance value from pre-rendering (when we pull it out of the | ||
// cookie) to rendering (when setting it is important. | ||
ApplicationState.PersistAsJson("baseLayerLuminance", _baseLayerLuminance); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
// Uncomment the code below to overload the GetLuminanceValue extension method and use custom luminosity values. | ||
// Probably needs to be move to it's own file too. | ||
/* | ||
public static class StandardLuminanceExtensions | ||
{ | ||
private const float LightMode = 1.0f; | ||
private const float DarkMode = 0.15f; | ||
|
||
public static float GetLuminanceValue(this StandardLuminance value) | ||
{ | ||
return value == StandardLuminance.LightMode ? LightMode : DarkMode; | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 122 additions & 1 deletion
123
src/Aspire.Dashboard/Components/Pages/TraceDetail.razor.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this to avoid flash of white on page load? Add a comment.
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.
That's right. The custom properties (e.g. --neutral-fill-rest) aren't available at this point so I just took the raw color. Makes sure the page loads dark.