Skip to content

Commit

Permalink
NostrDebug - dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Jul 27, 2023
1 parent f62788b commit 0c273be
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
24 changes: 11 additions & 13 deletions apps/nostr-debug/NostrDebug.Web/App.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
@*<FluentDesignSystemProvider AccentBaseColor="@OfficeColor.Word.ToAttributeValue()" BaseLayerLuminance="@StandardLuminance.LightMode.GetLuminanceValue()" FillColor="#fbfbfb" class="full-height">*@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@*</FluentDesignSystemProvider>*@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
50 changes: 44 additions & 6 deletions apps/nostr-debug/NostrDebug.Web/Components/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
@inherits LayoutComponentBase
@using Microsoft.Fast.Components.FluentUI.DesignTokens
@inherits LayoutComponentBase
@inject RelayList Relays
@inject GlobalState GlobalState
@inject BaseLayerLuminance BaseLayerLuminance
@inject IJSRuntime JsRuntime

<div class="container">
<div class="container" @ref="_container">
<div class="siteheader">
<a class="logo" href="/">
<p class="logo-text">
<span>Nostr</span>Debug
</p>
</a>

<a class="m-left-auto m-r-2 clear-link cursor-hand menu-link" style="line-height: 44px" @onclick="OnDisplayRelayDialog">
<a class="m-left-auto clear-link cursor-hand menu-link" style="line-height: 44px" @onclick="OnDisplayRelayDialog">

@if (Relays.AreAllConnected && Relays.IsAnyConnected)
{
Expand All @@ -27,9 +31,17 @@
<span class="m-l">Relays</span>

</a>

<div class="menu-link" style="line-height: 44px">
<FluentSwitch @bind-Value="@_inDarkMode" @onchange="SwitchTheme" title="Switch theme">
<span class="label">Theme</span>
<span slot="unchecked-message">light</span>
<span slot="checked-message">dark</span>
</FluentSwitch>
</div>

<div class="git-repo menu-link" style="line-height: 44px">
<a class="clear-link" href="https://github.com/marfusios/nostr-client-websocket" target="_blank">Github Repository</a>
<a class="clear-link" href="https://github.com/marfusios/nostr-client-websocket" target="_blank">Github</a>
</div>
</div>
<div class="main">
Expand Down Expand Up @@ -62,9 +74,13 @@
</FluentDialog>

@code {
private ElementReference _container;
private FluentDialog? _relayDialog;

private ErrorBoundary? _errorBoundary;
private bool _inDarkMode;
private StandardLuminance _baseLayerLuminance = StandardLuminance.LightMode;
private IJSObjectReference? _jsModule;

protected override void OnParametersSet()
{
Expand All @@ -81,10 +97,21 @@
await base.OnInitializedAsync();
}

protected override void OnAfterRender(bool firstRender)
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_relayDialog!.Hide();

_jsModule = await JsRuntime.InvokeAsync<IJSObjectReference>("import",
"./Components/MainLayout.razor.js");

_inDarkMode = await _jsModule!.InvokeAsync<bool>("isDarkMode");

SwitchTheme();
StateHasChanged();
}

}

private void OnDisplayRelayDialog()
Expand All @@ -94,9 +121,20 @@

private void OnHideRelayDialog(DialogEventArgs? args)
{
if (args?.Reason != null && args.Reason == "dismiss")
if (args?.Reason is "dismiss")
{
_relayDialog!.Hide();
}
}

private async void SwitchTheme()
{
await Task.Delay(50);

_baseLayerLuminance = _inDarkMode ? StandardLuminance.DarkMode : StandardLuminance.LightMode;
await BaseLayerLuminance.SetValueFor(_container, _baseLayerLuminance.GetLuminanceValue());
GlobalState.SetLuminance(_baseLayerLuminance);

// await _jsModule!.InvokeVoidAsync("switchHighlightStyle", _baseLayerLuminance == StandardLuminance.DarkMode);
}
}
31 changes: 31 additions & 0 deletions apps/nostr-debug/NostrDebug.Web/Components/MainLayout.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function isDevice() {
return /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile/i.test(navigator.userAgent);
}

export function switchHighlightStyle(dark) {
if (dark) {
document
.querySelector(`link[title="dark"]`)
.removeAttribute("disabled");
document
.querySelector(`link[title="light"]`)
.setAttribute("disabled", "disabled");
}
else {
document
.querySelector(`link[title="light"]`)
.removeAttribute("disabled");
document
.querySelector(`link[title="dark"]`)
.setAttribute("disabled", "disabled");
}
}

export function isDarkMode() {
let matched = window.matchMedia("(prefers-color-scheme: dark)").matches;

if (matched)
return true;
else
return false;
}
1 change: 1 addition & 0 deletions apps/nostr-debug/NostrDebug.Web/Relay/RelayConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void Dispose()

public async Task<bool> Connect(string? relayUrl)
{
await Task.CompletedTask;
IsUsed = true;
if (_communicator.IsRunning)
{
Expand Down
5 changes: 4 additions & 1 deletion apps/nostr-debug/NostrDebug.Web/wwwroot/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ body {
font-family: var(--body-font);
font-size: var(--type-ramp-base-font-size);
line-height: var(--type-ramp-base-line-height);
background-color: transparent;
margin: 0;
height: 100%;
}
Expand All @@ -25,11 +26,12 @@ body {
"siteheader"
"main";
height: 100%;
background-color: var(--fill-color);
}

.siteheader {
grid-area: siteheader;
background-color: #f7f7f7;
background-color: var(--neutral-layer-card-container);
border-bottom: 1px solid #d6d5d5;
justify-content: flex-start;
height: 3.5rem;
Expand Down Expand Up @@ -78,6 +80,7 @@ body {
.menu-link {
padding-left: 10px;
padding-right: 10px;
color: var(--accent-foreground-rest);
}

.menu-link:hover {
Expand Down

0 comments on commit 0c273be

Please sign in to comment.