Files
Khaemhat/src/Khaemhat.Web/Components/Shell/KhCommandPalette.razor
T
Alby96andClaude Opus 5.5 cf6f849cc1 feat: application shell, localized texts and user preferences (M3, part 1)
The pages built so far now live inside the shell of the mockups instead of the
M0 placeholder, every interface text comes from Resources/it-IT, and each user
keeps their own look and start page.

- Shell: top bar, collapsible side menu with fly-out sub-menus, status bar and
  tabs that keep each page alive (page type and route values per tab, so a tab
  never shows another page); at most 12 tabs, least-used clean tab evicted.
- One shortcut table with Alt variants for the combinations browsers keep
  (Ctrl+W, Ctrl+Tab, Ctrl+N); command palette for pages and commands.
- ITexts over embedded JSON; tests forbid literal texts in .razor components
  and check that every key used exists.
- User settings in sys_user_settings / user_settings; theme written on the
  server; profile page with live preview, start page, sessions and shortcuts.
- Home, coming-soon pages for later milestones, localized sign-in and error
  pages; ADR 0020; wiki, CHANGELOG, STATUS and data model updated.

Refs #25 #26 #31 #33

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:44:59 +01:00

198 lines
7.1 KiB
Plaintext

@inject ShellSession Session
@inject NavigationManager Navigation
@inject ThemeService Theme
@* The global search of SPEC §10.2 (Ctrl+K, mockup 009). Today it finds pages of the menu and commands (prefix >);
records (parties, items, documents with #, people with @) join as their modules arrive, through
GlobalSearchService. Arrows move, Enter opens, Escape closes. *@
@if (Open)
{
<div class="overlay" @onclick="CloseAsync">
<div class="dialog palette" role="dialog" aria-modal="true" aria-label="@T["Shell.Search.Title"]" @onclick:stopPropagation="true">
<div class="pin">
<KhIcon Name="search" Class="lg" />
<input @ref="_input" class="palette-input" type="search" placeholder="@T["Shell.Search.Placeholder"]" aria-label="@T["Shell.Search.Title"]"
aria-controls="kh-palette-hits" aria-activedescendant="@(Hits.Count > 0 ? "kh-hit-" + _selected : null)"
value="@_query" @oninput="OnInput" @onkeydown="OnKeyDownAsync" autocomplete="off" />
<span class="kbdhint"><kbd>@T["Shell.Keys.Escape"]</kbd></span>
</div>
<div id="kh-palette-hits" role="listbox" class="hits">
@if (Hits.Count == 0)
{
<div class="grp">@T["Shell.Search.NoResults"]</div>
}
@{
string? group = null;
var index = 0;
}
@foreach (var hit in Hits)
{
if (hit.Group != group)
{
group = hit.Group;
<div class="grp">@T[group]</div>
}
var i = index++;
<button type="button" id="@("kh-hit-" + i)" role="option" aria-selected="@(i == _selected)" class="hit @(i == _selected ? "on" : null)"
@onclick="() => RunAsync(hit)" @onmouseover="() => _selected = i">
<KhIcon Name="@hit.Icon" />
<span>@hit.Title</span>
@if (hit.Hint is not null)
{
<span class="k">@hit.Hint</span>
}
</button>
}
</div>
<div class="df palette-foot">
<span class="kbdhint"><kbd>↑</kbd><kbd>↓</kbd> @T["Shell.Search.Move"]</span>
<span class="kbdhint"><kbd>↵</kbd> @T["Shell.Search.Open"]</span>
<span class="kbdhint"><kbd>&gt;</kbd> @T["Shell.Search.Commands"]</span>
</div>
</div>
</div>
}
@code {
private sealed record Hit(string Group, string Icon, string Title, string? Hint, Func<Task> Run);
private ElementReference _input;
private string _query = string.Empty;
private int _selected;
private bool _focus;
/// <summary>Whether the search is open.</summary>
[Parameter]
public bool Open { get; set; }
/// <summary>Opens or closes it.</summary>
[Parameter]
public EventCallback<bool> OpenChanged { get; set; }
/// <summary>Collapses or expands the menu (command).</summary>
[Parameter]
public EventCallback OnToggleMenu { get; set; }
private List<Hit> Hits { get; set; } = [];
/// <inheritdoc />
protected override void OnParametersSet()
{
if (Open && !_focus && Hits.Count == 0)
{
_query = string.Empty;
_selected = 0;
_focus = true;
Hits = Search(_query);
}
else if (!Open)
{
Hits = [];
}
}
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (_focus && Open)
{
_focus = false;
await _input.FocusAsync();
}
}
private void OnInput(ChangeEventArgs e)
{
_query = e.Value?.ToString() ?? string.Empty;
_selected = 0;
Hits = Search(_query);
}
private async Task OnKeyDownAsync(KeyboardEventArgs e)
{
switch (e.Key)
{
case "ArrowDown":
_selected = Hits.Count == 0 ? 0 : (_selected + 1) % Hits.Count;
break;
case "ArrowUp":
_selected = Hits.Count == 0 ? 0 : (_selected - 1 + Hits.Count) % Hits.Count;
break;
case "Enter" when Hits.Count > 0:
await RunAsync(Hits[Math.Min(_selected, Hits.Count - 1)]);
break;
case "Escape":
await CloseAsync();
break;
}
}
private async Task RunAsync(Hit hit)
{
await CloseAsync();
await hit.Run();
}
private Task CloseAsync()
{
Hits = [];
return OpenChanged.InvokeAsync(false);
}
private List<Hit> Search(string query)
{
var text = query.Trim();
var commandsOnly = text.StartsWith('>');
if (commandsOnly)
{
text = text[1..].Trim();
}
var hits = new List<Hit>();
if (!commandsOnly)
{
foreach (var item in MenuCatalog.All.Where(Session.CanSee))
{
var title = T[item.Key];
var area = MenuCatalog.FindArea(item.Href);
var label = area is not null && area != item ? T[area.Key] + " · " + title : title;
if (Matches(label, text))
{
hits.Add(new Hit("Shell.Search.Pages", item.Icon, label, null, () => Go(item.Href)));
}
}
}
foreach (var command in Commands())
{
if (Matches(command.Title, text))
{
hits.Add(command);
}
}
return [.. hits.Take(30)];
}
private IEnumerable<Hit> Commands()
{
yield return new Hit("Shell.Search.CommandsGroup", "moon", T["Shell.Commands.ToggleTheme"], null, () => Theme.ToggleDarkAsync());
yield return new Hit("Shell.Search.CommandsGroup", "sidebar", T["Shell.Commands.ToggleMenu"], KeyboardShortcuts.KeysOf(KeyboardShortcuts.Commands.ToggleMenu),
() => OnToggleMenu.InvokeAsync());
yield return new Hit("Shell.Search.CommandsGroup", "palette", T["Shell.Commands.Appearance"], null, () => Go("/profilo?scheda=aspetto"));
yield return new Hit("Shell.Search.CommandsGroup", "user", T["Shell.Commands.Profile"], null, () => Go("/profilo"));
yield return new Hit("Shell.Search.CommandsGroup", "monitor", T["Shell.User.Sessions"], null, () => Go("/profilo?scheda=sessioni"));
yield return new Hit("Shell.Search.CommandsGroup", "keyboard", T["Shell.Commands.Shortcuts"], null, () => Go("/profilo?scheda=scorciatoie"));
}
private Task Go(string href)
{
Navigation.NavigateTo(href.TrimStart('/'));
return Task.CompletedTask;
}
private static bool Matches(string title, string text) =>
text.Length == 0 || text.Split(' ', StringSplitOptions.RemoveEmptyEntries).All(word => title.Contains(word, StringComparison.CurrentCultureIgnoreCase));
}