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
1 change: 1 addition & 0 deletions changelog.d/+etb-search.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ETB can be searched: a field above the journal narrows it to the entries containing every word typed, in the Eintrag, Von, An or Erfasst von.
40 changes: 39 additions & 1 deletion src/LageBuch.App.Shared/Views/EtbView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
<Style Selector="TextBox#EditTextBox">
<Setter Property="Width" Value="420" />
</Style>
<!-- Wide enough for "Atemschutz Trupp 2" plus the count, and still one line on a phone. -->
<Style Selector="TextBox#EtbSearchBox">
<Setter Property="Width" Value="300" />
</Style>

<Style Selector="Panel#WorkspaceRoot.narrow StackPanel#DockFields">
<Setter Property="Orientation" Value="Vertical" />
Expand Down Expand Up @@ -140,10 +144,36 @@
by TabIndex in the order it appears on screen. -->
<DockPanel KeyboardNavigation.TabNavigation="Local">
<!-- No EINSATZTAGEBUCH heading: the ETB's tab names it. A WrapPanel rather than fixed
columns, so on a phone the four controls wrap onto more lines instead of overflowing. -->
columns, so on a phone the five controls wrap onto more lines instead of overflowing. -->
<WrapPanel DockPanel.Dock="Top" x:Name="EtbHeaderActions" Orientation="Horizontal" HorizontalAlignment="Right"
ItemSpacing="12" LineSpacing="6" Margin="0,0,0,14"
KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="0">
<!-- The search, built like the Beteiligte one (#831): every typed word has to appear in the
Eintrag, Von, An, Abschnitt or Erfasst von. A long Einsatz runs to hundreds of entries, and this is
how one of them is found again while the next is being taken down. -->
<TextBox x:Name="EtbSearchBox" VerticalAlignment="Center"
Text="{Binding FilterText}"
PlaceholderText="Eintrag, Von, An, Abschnitt, Erfasst von"
AutomationProperties.Name="ETB durchsuchen">
<TextBox.InnerLeftContent>
<PathIcon Width="14" Height="14" Margin="10,0,0,0" Foreground="{StaticResource TextSecondaryBrush}"
Data="M15.5 14h-.79l-.28-.27A6.47 6.47 0 0 0 16 9.5 6.5 6.5 0 1 0 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" />
</TextBox.InnerLeftContent>
<TextBox.InnerRightContent>
<StackPanel Orientation="Horizontal" Spacing="4" IsVisible="{Binding IsFiltered}">
<TextBlock x:Name="EtbMatchCountText" Classes="mono" FontSize="12" VerticalAlignment="Center"
Text="{Binding MatchCountText}" />
<Button x:Name="ClearEtbSearchButton" Classes="icon-btn" ToolTip.Tip="Suche zurücksetzen"
AutomationProperties.Name="Suche zurücksetzen"
Command="{Binding ClearFilterCommand}">
<PathIcon Data="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
</Button>
</StackPanel>
</TextBox.InnerRightContent>
<TextBox.KeyBindings>
<KeyBinding Gesture="Escape" Command="{Binding ClearFilterCommand}" />
</TextBox.KeyBindings>
</TextBox>
<!-- The Buchstabier-Helfer (#592), for a name that has to be spelled over the radio. Up here
rather than in the dock: it is wanted while reading the journal too, and the dock's row
is already as wide as a desktop window allows. -->
Expand Down Expand Up @@ -482,6 +512,14 @@
</ItemsControl>
</ScrollViewer>
</Border>

<!-- Over both layouts, below the grid's header row: an empty list mid-keystroke has to say it
is the search that found nothing, not that the ETB is empty. -->
<TextBlock x:Name="EtbNoMatchesText" Text="{Binding NoMatchesMessage}"
Margin="16,48,16,0" HorizontalAlignment="Center" VerticalAlignment="Top"
TextWrapping="Wrap" IsHitTestVisible="False"
Foreground="{StaticResource TextSecondaryBrush}"
IsVisible="{Binding ShowNoMatches}" />
</Panel>
</DockPanel>
</UserControl>
81 changes: 79 additions & 2 deletions src/LageBuch.AppLogic/ViewModels/EtbViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
Expand Down Expand Up @@ -152,6 +153,14 @@ public void Sync()
ReconcileVisible();
}

// During a search every change goes through the reconcile: an edit can move a row into the
// result or out of it, and an entry the search hides still grows the count's whole. One
// pass over the rows per change, and only while a search is typed.
if (IsFiltered)
{
ReconcileVisible();
}

SyncSections(refiled);
}

Expand Down Expand Up @@ -361,9 +370,57 @@ partial void OnSectionFilterChanged(Guid? value)

partial void OnShowOnlySituationReportsChanged(bool value) => ReconcileVisible();

/// <summary>
/// The ETB search: a row shows only when <em>every</em> whitespace-separated term appears in its
/// Eintrag, Von, An, Erfasst von or Abschnitt, ordinal and case-insensitive like the Beteiligte
/// search (#831). It narrows what the checkboxes and the Abschnitt filter already let through,
/// never widens it.
/// </summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsFiltered))]
[NotifyPropertyChangedFor(nameof(NoMatchesMessage))]
private string _filterText = string.Empty;

// FilterText split once per keystroke, so Sync() does not split it again for every new row.
private string[] _terms = [];

public bool IsFiltered => _terms.Length > 0;

/// <summary>Shown in place of the list when the search left nothing, so it does not look empty.</summary>
public bool ShowNoMatches => IsFiltered && Entries.Count == 0;

public string NoMatchesMessage => $"Keine Einträge passen zu „{FilterText.Trim()}“.";

/// <summary>
/// "2 von 35": how far the search narrowed the list. The whole is what the checkboxes and the
/// Abschnitt filter let through, so hidden Systemmeldungen and Messwerte do not count towards it.
/// </summary>
public string MatchCountText =>
string.Create(CultureInfo.InvariantCulture, $"{Entries.Count} von {_all.Count(PassesFilters)}");

partial void OnFilterTextChanged(string value)
{
_terms = value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
ReconcileVisible();
}

private void RaiseSearchResult()
{
OnPropertyChanged(nameof(MatchCountText));
OnPropertyChanged(nameof(ShowNoMatches));
}

[RelayCommand]
private void ClearFilter() => FilterText = string.Empty;

// In place, never Clear()+re-add: a Reset takes the grid's selected row and focus with it (#542).
private void ReconcileVisible() =>
// A checkbox can move the count's whole without moving a visible row, so the count is raised
// here too, not only when Entries changes.
private void ReconcileVisible()
{
RowReconciler.Reconcile(Entries, VisibleNewestFirst(), r => r.Id, r => r.Id, r => r, (_, _) => { });
RaiseSearchResult();
}

private List<EtbEntryRow> VisibleNewestFirst()
{
Expand All @@ -380,7 +437,10 @@ private List<EtbEntryRow> VisibleNewestFirst()
return visible;
}

private bool IsVisible(EtbEntryRow row) =>
private bool IsVisible(EtbEntryRow row) => PassesFilters(row) && MatchesSearch(row);

// The checkboxes and the Abschnitt filter: what the search then narrows further.
private bool PassesFilters(EtbEntryRow row) =>
!IsHiddenByKind(row)
&& (!ShowOnlySituationReports || row.SituationReportNumber is not null)
&& (SectionFilter is null || row.SectionId == SectionFilter);
Expand All @@ -389,6 +449,23 @@ private bool IsVisible(EtbEntryRow row) =>
private bool IsHiddenByKind(EtbEntryRow row) =>
(HideSystemEntries && row.IsSystem) || (HideMeasurementEntries && row.IsMeasurement);

private bool MatchesSearch(EtbEntryRow row)
{
foreach (var term in _terms)
{
if (!Contains(row.Text, term) && !Contains(row.From, term) && !Contains(row.To, term)
&& !Contains(row.EnteredBy, term) && !Contains(row.SectionName, term))
{
return false;
}
}

return true;

static bool Contains(string? field, string term) =>
field is not null && field.Contains(term, StringComparison.OrdinalIgnoreCase);
}

// The dock and the edit panel below the grid are two separate forms, so each keeps its own
// "the operator has asked" state: pressing one must not light up a field in the other (#412).
private bool _addErrorsShown;
Expand Down
3 changes: 2 additions & 1 deletion tests/LageBuch.Acceptance.Tests/AutomationPropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public void Etb_row_action_buttons_are_named_for_automation()
window.Show();
Dispatcher.UIThread.RunJobs();

var buttons = IconButtons(view);
// The grid only: the search's clear button is checked by the ETB search test.
var buttons = IconButtons(view.GetControl<DataGrid>("EtbGrid"));
Assert.Equal(3, buttons.Count); // Bearbeiten, Verlauf (bearbeitet), Aufgabe erstellen
foreach (var button in buttons)
{
Expand Down
7 changes: 6 additions & 1 deletion tests/LageBuch.Acceptance.Tests/ReadingOrderTabTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public void Tab_from_the_header_bars_enters_the_rail_and_then_the_module_s_first
window.Tab();
window.AssertFocused(Named<TabItem>(content, "JournalTab"));

// The header reads left to right: the search first, then BUCHSTABIEREN.
window.Tab();
window.AssertFocused(Named<TextBox>(content, "EtbSearchBox"));

window.Tab();
window.AssertFocused(Named<Button>(content, "OpenSpellingHelperButton"));
}
Expand All @@ -142,7 +146,8 @@ public void Shift_tab_from_the_module_s_first_control_goes_back_to_the_rail()

// Shift+Tab used to resolve the rail to the very control it started on, and went nowhere.
// The module's first control is now the ETB's own open tab (#592).
FocusByTab(Named<Button>(content, "OpenSpellingHelperButton"));
// The ETB's first control after its tabs is the search.
FocusByTab(Named<TextBox>(content, "EtbSearchBox"));
window.ShiftTab();
window.AssertFocused(Named<TabItem>(content, "JournalTab"));

Expand Down
57 changes: 57 additions & 0 deletions tests/LageBuch.Acceptance.Tests/WorkspaceAcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,63 @@ public void Etb_asks_for_no_direction()
f => string.Equals(f.Header as string, "RICHTUNG", StringComparison.Ordinal));
}

// Typing into the ETB search narrows the rendered grid, and a search that finds nothing says
// so instead of leaving an empty list. Doubles as the PR before/after screenshot capture.
[AvaloniaFact]
public void Typing_into_the_etb_search_narrows_the_grid_and_names_an_empty_result()
{
var vm = BuildWorkspace(out var session);
session.AddJournalEntry(EtbDirection.Incoming, "Atemschutz Trupp Nord vor", from: "Abschnitt Nord");
session.AddJournalEntry(EtbDirection.Incoming, "Wasserversorgung steht", from: "Wassertrupp");
session.AddJournalEntry(EtbDirection.Incoming, "Atemschutz Trupp Nord zurück", from: "Abschnitt Nord");
var view = new EtbView { DataContext = vm.Etb };
var window = new Window { Content = view, Width = 1280, Height = 700 };
window.Show();
Dispatcher.UIThread.RunJobs();

var dir = Path.Join(Path.GetTempPath(), "lagebuch-shots");
Directory.CreateDirectory(dir);
SaveFrame(window, Path.Join(dir, "etb-search-before.png"));

var search = view.GetControl<TextBox>("EtbSearchBox");
var grid = view.GetControl<DataGrid>("EtbGrid");
var noMatches = view.GetControl<TextBlock>("EtbNoMatchesText");
Assert.False(noMatches.IsVisible);

search.Focus();
window.Type("trupp nord");

Assert.Equal("trupp nord", vm.Etb.FilterText);
Assert.Equal(2, Rows(grid).Count());
Assert.True(view.GetControl<TextBlock>("EtbMatchCountText").IsEffectivelyVisible);
Assert.Equal("Suche zurücksetzen", Avalonia.Automation.AutomationProperties.GetName(view.GetControl<Button>("ClearEtbSearchButton")));
SaveFrame(window, Path.Join(dir, "etb-search-after.png"));

window.Type(" drehleiter");

Assert.Empty(Rows(grid));
Assert.True(noMatches.IsVisible);
SaveFrame(window, Path.Join(dir, "etb-search-none.png"));

window.Press(Avalonia.Input.PhysicalKey.Escape);

Assert.Equal(string.Empty, vm.Etb.FilterText);
Assert.Equal(3, Rows(grid).Count());

static void SaveFrame(Window window, string path)
{
var frame = window.CaptureRenderedFrame();
Assert.NotNull(frame);
using (frame)
{
frame.SavePng(path);
}
}

static IEnumerable<EtbEntryRow> Rows(DataGrid grid) =>
Assert.IsAssignableFrom<IEnumerable<EtbEntryRow>>(grid.ItemsSource);
}

// The Funktionen tab had no UI-level coverage at all before issue #17 widened it.
[AvaloniaFact]
public void Assigning_a_function_via_the_ui_fills_the_grid_and_stamps_von()
Expand Down
Loading