Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/EventLogExpert.Runtime/LogTable/EventFindMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// // Copyright (c) Microsoft Corporation.
// // Licensed under the MIT License.

using EventLogExpert.Eventing.Common.Events;

namespace EventLogExpert.Runtime.LogTable;

public static class EventFindMatcher
{
public static StringComparison ComparisonFor(bool caseSensitive) =>
caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

public static int IndexOfMatch(string text, string query, int startIndex, StringComparison comparison, bool wholeWord)
{
if (string.IsNullOrEmpty(query)) { return -1; }

if (!wholeWord) { return text.IndexOf(query, startIndex, comparison); }

int from = startIndex;

while (from <= text.Length)
{
int hit = text.IndexOf(query, from, comparison);

if (hit < 0) { return -1; }

if (IsWordBounded(text, hit, hit + query.Length)) { return hit; }

from = hit + 1;
}

return -1;
}

public static bool RowMatches(
ResolvedEvent @event,
IReadOnlyList<ColumnName> columns,
TimeZoneInfo timeZone,
string query,
bool caseSensitive,
bool wholeWord)
{
if (string.IsNullOrEmpty(query)) { return false; }

StringComparison comparison = ComparisonFor(caseSensitive);

for (int i = 0; i < columns.Count; i++)
{
if (IndexOfMatch(EventTableColumnFormatter.GetCellText(@event, columns[i], timeZone), query, 0, comparison, wholeWord) >= 0)
{
return true;
}
}

return IndexOfMatch(@event.Description ?? string.Empty, query, 0, comparison, wholeWord) >= 0;
}

// Word boundary = string edge, a non-word neighbor, or the match's own edge char is a separator (VS Code wordSeparators rule; word char = letter/digit/underscore).
private static bool IsWordBounded(string text, int start, int end) =>
(start == 0 || !IsWordChar(text[start - 1]) || !IsWordChar(text[start])) &&
(end == text.Length || !IsWordChar(text[end]) || !IsWordChar(text[end - 1]));

private static bool IsWordChar(char value) => char.IsLetterOrDigit(value) || value == '_';
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

namespace EventLogExpert.Runtime.LogTable;

internal sealed record SetAllGroupsCollapsedAction(bool Collapsed);
// Public so LogTablePane can subscribe via SubscribeToAction, like the other LogTable actions.
public sealed record SetAllGroupsCollapsedAction(bool Collapsed);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// // Licensed under the MIT License.

using EventLogExpert.UI.Keyboard;
using EventLogExpert.UI.LogTable.Find;
using EventLogExpert.UI.Menu;

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -22,6 +23,7 @@ public IServiceCollection AddEventLogUiServices()
ArgumentNullException.ThrowIfNull(services);

services.AddSingleton<IMenuHostRegistry, MenuHostRegistry>();
services.AddSingleton<IFindCoordinator, FindCoordinator>();
services.AddSingleton<KeyboardShortcutService>();

return services;
Expand Down
7 changes: 7 additions & 0 deletions src/EventLogExpert.UI/Keyboard/KeyboardShortcutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EventLogExpert.Runtime.Modal;
using EventLogExpert.Runtime.Settings;
using EventLogExpert.UI.Common.Interop;
using EventLogExpert.UI.LogTable.Find;
using Microsoft.JSInterop;

namespace EventLogExpert.UI.Keyboard;
Expand All @@ -17,9 +18,11 @@ namespace EventLogExpert.UI.Keyboard;
public sealed class KeyboardShortcutService(
IMenuActionService actions,
IModalCoordinator modalCoordinator,
IFindCoordinator findCoordinator,
ISettingsService settings) : IAsyncDisposable
{
private readonly IMenuActionService _actions = actions;
private readonly IFindCoordinator _findCoordinator = findCoordinator;
private readonly IModalCoordinator _modalCoordinator = modalCoordinator;
private readonly ISettingsService _settings = settings;

Expand Down Expand Up @@ -96,6 +99,10 @@ public async Task HandleShortcutAsync(string code, bool ctrl, bool alt, bool shi

switch (code)
{
case "KeyF":
_findCoordinator.RequestOpen();
return;

case "KeyO":
await _actions.OpenFileAsync(false);
return;
Expand Down
64 changes: 64 additions & 0 deletions src/EventLogExpert.UI/LogTable/Find/FindBar.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@namespace EventLogExpert.UI.LogTable.Find

<div class="find-bar" @onkeydown="HandleRootKeyDown" role="search">
<div class="find-main">
<div class="find-inputbox">
<input aria-label="Find in events"
class="find-input"
@oninput="OnInput"
@onkeydown="HandleInputKeyDown"
placeholder="Find in events"
@ref="_input"
spellcheck="false"
type="text"
value="@Query" />
<span class="find-inline">
<span aria-live="polite" class="find-count">@CountText</span>
<button aria-label="Previous match"
class="find-nav"
disabled="@NavDisabled"
@onclick="OnPrevious"
@onmousedown:preventDefault="true"
type="button">
<i aria-hidden="true" class="bi bi-chevron-up"></i>
</button>
<button aria-label="Next match"
class="find-nav"
disabled="@NavDisabled"
@onclick="OnNext"
@onmousedown:preventDefault="true"
type="button">
<i aria-hidden="true" class="bi bi-chevron-down"></i>
</button>
</span>
</div>
<span aria-live="polite" class="find-wrap">@WrapAnnouncement</span>
<button aria-controls="@(_optionsOpen ? "find-options" : null)"
aria-expanded="@(_optionsOpen ? "true" : "false")"
aria-label="Search options"
class="find-options-toggle @(OptionsActive ? "is-active" : null)"
@onclick="ToggleOptions"
type="button">
<i aria-hidden="true" class="bi bi-sliders"></i>
</button>
<button aria-label="Close find"
class="find-close"
@onclick="OnClose"
type="button">
<i aria-hidden="true" class="bi bi-x-lg"></i>
</button>
</div>
@if (_optionsOpen)
{
<div class="find-options" id="find-options">
<div class="flex-center-aligned-row">
<label for="find-opt-case">Match case</label>
<Toggle Id="find-opt-case" Value="CaseSensitive" ValueChanged="OnCaseChanged" />
</div>
<div class="flex-center-aligned-row">
<label for="find-opt-word">Match whole word</label>
<Toggle Id="find-opt-word" Value="WholeWord" ValueChanged="OnWholeWordChanged" />
</div>
</div>
}
</div>
145 changes: 145 additions & 0 deletions src/EventLogExpert.UI/LogTable/Find/FindBar.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// // Copyright (c) Microsoft Corporation.
// // Licensed under the MIT License.

using EventLogExpert.UI.Common.Interop;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

namespace EventLogExpert.UI.LogTable.Find;

public sealed partial class FindBar : IAsyncDisposable
{
private ElementReference _input;
private int _lastFocusSignal;
private IJSObjectReference? _module;
private bool _optionsOpen;

[Parameter]
public bool CaseSensitive { get; set; }

[Parameter]
public EventCallback<bool> CaseSensitiveChanged { get; set; }

/// <summary>The 1-based position of the current match, or 0 when there is none.</summary>
[Parameter]
public int CurrentOrdinal { get; set; }

[Parameter]
public int FocusSignal { get; set; }

[Parameter]
public bool IsScanning { get; set; }

[Parameter]
public int MatchCount { get; set; }

[Parameter]
public EventCallback OnClose { get; set; }

[Parameter]
public EventCallback OnNext { get; set; }

[Parameter]
public EventCallback OnPrevious { get; set; }

[Parameter]
public string Query { get; set; } = string.Empty;

[Parameter]
public EventCallback<string> QueryChanged { get; set; }

[Parameter]
public bool WholeWord { get; set; }

[Parameter]
public EventCallback<bool> WholeWordChanged { get; set; }

[Parameter]
public string WrapAnnouncement { get; set; } = string.Empty;

private string CountText =>
Query.Length == 0 ? string.Empty :
IsScanning ? "Searching\u2026" :
MatchCount == 0 ? "No results" :
$"{CurrentOrdinal}/{MatchCount}";

[Inject]
private IJSRuntime JSRuntime { get; init; } = null!;

private bool NavDisabled => IsScanning || MatchCount == 0;

private bool OptionsActive => CaseSensitive || WholeWord;

public async ValueTask DisposeAsync()
{
await JsModuleInterop.DisposeModuleSafelyAsync(
_module,
static module => module.InvokeVoidAsync("detachNativeFindSuppression"));

_module = null;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
try
{
if (firstRender)
{
_module = await JSRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/EventLogExpert.UI/LogTable/Find/FindBar.razor.js");
await _module.InvokeVoidAsync("attachNativeFindSuppression");
}

if (FocusSignal != _lastFocusSignal && _module is not null)
{
_lastFocusSignal = FocusSignal;
await _module.InvokeVoidAsync("focusAndSelect", _input);
}
}
catch (JSDisconnectedException) { }
catch (JSException) { }
}

// Enter is bound to the input only (a focused nav button's Enter stays a single native click); F3/Esc bind to the bar root so they fire whichever child holds focus.
private async Task HandleInputKeyDown(KeyboardEventArgs args)
{
switch (args.Key)
{
case "Enter" when args.ShiftKey:
await OnPrevious.InvokeAsync();
return;

case "Enter":
await OnNext.InvokeAsync();
return;
}
}

private async Task HandleRootKeyDown(KeyboardEventArgs args)
{
switch (args.Key)
{
case "F3" when args.ShiftKey:
await OnPrevious.InvokeAsync();
return;

case "F3":
await OnNext.InvokeAsync();
return;

case "Escape":
await OnClose.InvokeAsync();
return;
}
}

private async Task OnCaseChanged(bool value) => await CaseSensitiveChanged.InvokeAsync(value);

private async Task OnInput(ChangeEventArgs args) =>
await QueryChanged.InvokeAsync(args.Value as string ?? string.Empty);

private async Task OnWholeWordChanged(bool value) => await WholeWordChanged.InvokeAsync(value);

private void ToggleOptions() => _optionsOpen = !_optionsOpen;
}
Loading
Loading