diff --git a/windows/src/GoalMaker.App/Controls/KeyboardSpot.cs b/windows/src/GoalMaker.App/Controls/KeyboardSpot.cs new file mode 100644 index 00000000..bb1c5ff4 --- /dev/null +++ b/windows/src/GoalMaker.App/Controls/KeyboardSpot.cs @@ -0,0 +1,47 @@ +namespace GoalMaker.App.Controls; + +/// +/// Where the keyboard was in a list that is redrawn from new rows (the Focus Board, its add panel): the +/// row's id, its place and which part of the row. A redraw drops the element that had the keyboard, so +/// the page puts it back on the same row, or on the row now in its place when that one has gone. +/// Forgotten once the keyboard leaves the list, so a later redraw (a sync) does not pull it back. +/// +public sealed class KeyboardSpot +{ + private string? id; + private int index; + + /// Which part of the row had the keyboard, by its element name. + public string Part { get; private set; } = string.Empty; + + public bool IsSet => id is not null; + + public void Remember(string rowId, int rowIndex, string part) + { + id = rowId; + index = Math.Max(rowIndex, 0); + Part = part; + } + + public void Forget() + { + id = null; + Part = string.Empty; + } + + /// + /// The place in to put the keyboard back on: the same row if it is still there, + /// else the one now in its place (the last when it was the end). Null when nothing is remembered or the + /// list is empty. + /// + public int? Pick(IReadOnlyList ids) + { + if (id is null || ids.Count == 0) return null; + for (var i = 0; i < ids.Count; i++) + { + if (ids[i] == id) return i; + } + + return Math.Min(index, ids.Count - 1); + } +} diff --git a/windows/src/GoalMaker.App/Resources/Strings.xaml b/windows/src/GoalMaker.App/Resources/Strings.xaml index c19f79b1..60962596 100644 --- a/windows/src/GoalMaker.App/Resources/Strings.xaml +++ b/windows/src/GoalMaker.App/Resources/Strings.xaml @@ -1417,7 +1417,7 @@ Move up (Alt+Up) Move down (Alt+Down) Late - Add a task to the board, like "Hand in the form @School friday" + New task, like "Hand in the form @School friday" Search open tasks No open task matches. Nothing on the board matches the filter. diff --git a/windows/src/GoalMaker.App/ViewModels/FocusAddRowViewModel.cs b/windows/src/GoalMaker.App/ViewModels/FocusAddRowViewModel.cs index f1ffdef8..66b7a674 100644 --- a/windows/src/GoalMaker.App/ViewModels/FocusAddRowViewModel.cs +++ b/windows/src/GoalMaker.App/ViewModels/FocusAddRowViewModel.cs @@ -26,12 +26,16 @@ public sealed class FocusAddRowViewModel public bool HasSubtitle => Subtitle.Length > 0; - public FocusAddRowViewModel(TaskItem task, AreaItem? area, DateOnly today, Brush? areaBrush = null) + /// What Narrator reads after the title: "Late" first for a late task, then the date and area. + public string HelpText { get; } + + public FocusAddRowViewModel(TaskItem task, AreaItem? area, DateOnly today, string lateLabel, Brush? areaBrush = null) { Id = task.Id; Title = task.Title; (DateText, IsLate) = FocusDate.For(task, today); AreaName = area?.Name; AreaBrush = areaBrush; + HelpText = FocusDate.HelpText(Subtitle, IsLate, lateLabel); } } diff --git a/windows/src/GoalMaker.App/ViewModels/FocusDate.cs b/windows/src/GoalMaker.App/ViewModels/FocusDate.cs index 81713efb..08f88b70 100644 --- a/windows/src/GoalMaker.App/ViewModels/FocusDate.cs +++ b/windows/src/GoalMaker.App/ViewModels/FocusDate.cs @@ -10,4 +10,8 @@ public static (string Text, bool IsLate) For(TaskItem task, DateOnly today) => (task.Deadline ?? task.PlannedDate) is { } date ? (date.ToString("d MMM", CultureInfo.CurrentCulture), date < today) : (string.Empty, false); + + /// The help text a screen reader gets for a row: the late mark is otherwise only a color and an icon. + public static string HelpText(string subtitle, bool isLate, string lateLabel) => + isLate ? string.Join(" · ", new[] { lateLabel, subtitle }.Where(part => part.Length > 0)) : subtitle; } diff --git a/windows/src/GoalMaker.App/ViewModels/FocusRowViewModel.cs b/windows/src/GoalMaker.App/ViewModels/FocusRowViewModel.cs index 75d5ad9e..d2d87cc7 100644 --- a/windows/src/GoalMaker.App/ViewModels/FocusRowViewModel.cs +++ b/windows/src/GoalMaker.App/ViewModels/FocusRowViewModel.cs @@ -1,4 +1,5 @@ using System.Windows.Media; +using CommunityToolkit.Mvvm.ComponentModel; using GoalMaker.Core.Planning; namespace GoalMaker.App.ViewModels; @@ -7,7 +8,7 @@ namespace GoalMaker.App.ViewModels; /// One row on the Focus Board: its place as shown (1 at the top), the title, and a line with the date /// (the deadline, else the planned day; late ones in the danger color) and the area with its color. /// -public sealed class FocusRowViewModel +public sealed partial class FocusRowViewModel : ObservableObject { public string Id { get; } public string Title { get; } @@ -39,7 +40,17 @@ public sealed class FocusRowViewModel public bool HasSubtitle => Subtitle.Length > 0; - public FocusRowViewModel(TaskItem task, AreaItem? area, DateOnly today, int place = 1, int count = 1, Brush? areaBrush = null) + /// What Narrator reads after the title: "Late" first for a late task, then the date and area. + public string HelpText { get; } + + /// + /// The done box. Ticking it completes the task, which leaves the board; when the task stays open (a + /// repeating one moving on, a change that did nothing) the view model unticks it again. + /// + [ObservableProperty] + private bool isDone; + + public FocusRowViewModel(TaskItem task, AreaItem? area, DateOnly today, int place, int count, string lateLabel, Brush? areaBrush = null) { Id = task.Id; Title = task.Title; @@ -50,5 +61,6 @@ public FocusRowViewModel(TaskItem task, AreaItem? area, DateOnly today, int plac (DateText, IsLate) = FocusDate.For(task, today); AreaName = area?.Name; AreaBrush = areaBrush; + HelpText = FocusDate.HelpText(Subtitle, IsLate, lateLabel); } } diff --git a/windows/src/GoalMaker.App/ViewModels/FocusViewModel.cs b/windows/src/GoalMaker.App/ViewModels/FocusViewModel.cs index abf35455..6376e203 100644 --- a/windows/src/GoalMaker.App/ViewModels/FocusViewModel.cs +++ b/windows/src/GoalMaker.App/ViewModels/FocusViewModel.cs @@ -144,13 +144,13 @@ private void Refresh() private FocusRowViewModel Row(TaskItem task, Dictionary areaMap, DateOnly today, int place, int count) { var area = areaMap.GetValueOrDefault(task.AreaId ?? ""); - return new FocusRowViewModel(task, area, today, place, count, area is null ? null : areaBrush(area.ColorId)); + return new FocusRowViewModel(task, area, today, place, count, strings.Get("Focus.Late"), area is null ? null : areaBrush(area.ColorId)); } private FocusAddRowViewModel Candidate(TaskItem task, Dictionary areaMap, DateOnly today) { var area = areaMap.GetValueOrDefault(task.AreaId ?? ""); - return new FocusAddRowViewModel(task, area, today, area is null ? null : areaBrush(area.ColorId)); + return new FocusAddRowViewModel(task, area, today, strings.Get("Focus.Late"), area is null ? null : areaBrush(area.ColorId)); } private IReadOnlyList Narrow(IReadOnlyList found) => @@ -179,6 +179,9 @@ private void Complete(FocusRowViewModel? row) { if (row is null) return; tasks.SetDone(row.Id, true); + + // Not done after all (a repeating task moved on, or nothing changed): the box must not stay ticked. + if (!tasks.All().Any(task => task.Id == row.Id && task.State == TaskState.Done)) row.IsDone = false; } [RelayCommand] diff --git a/windows/src/GoalMaker.App/Views/FocusPage.xaml b/windows/src/GoalMaker.App/Views/FocusPage.xaml index e8269fb6..15fcda20 100644 --- a/windows/src/GoalMaker.App/Views/FocusPage.xaml +++ b/windows/src/GoalMaker.App/Views/FocusPage.xaml @@ -242,11 +242,12 @@ Foreground="{DynamicResource GM.AccentBrush}" /> + AutomationProperties.HelpText="{Binding HelpText}"> @@ -379,7 +380,7 @@ CommandParameter="{Binding}" ToolTip="{DynamicResource Focus.AddToBoard}" AutomationProperties.Name="{Binding Title}" - AutomationProperties.HelpText="{DynamicResource Focus.AddToBoard}"> + AutomationProperties.HelpText="{Binding HelpText}"> diff --git a/windows/src/GoalMaker.App/Views/FocusPage.xaml.cs b/windows/src/GoalMaker.App/Views/FocusPage.xaml.cs index 528e9a55..9adb59af 100644 --- a/windows/src/GoalMaker.App/Views/FocusPage.xaml.cs +++ b/windows/src/GoalMaker.App/Views/FocusPage.xaml.cs @@ -3,24 +3,24 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; +using GoalMaker.App.Controls; using GoalMaker.App.ViewModels; namespace GoalMaker.App.Views; /// /// The Focus Board. Behavior is in ; this only moves the keyboard: Ctrl+N to the -/// quick add field, Ctrl+F to the search box, Alt+Up and Alt+Down on a row, and back onto a row after the -/// board is redrawn (a move, a completion), since the redraw drops the element that had the keyboard. +/// quick add field, Ctrl+F to the search box, Alt+Up and Alt+Down on a row, Esc back to the board from the +/// add panel, and back onto a row after the board or the add panel is redrawn (a move, a completion, an +/// add), since the redraw drops the element that had the keyboard. /// public partial class FocusPage { private readonly FocusViewModel viewModel; - - // Where the keyboard was on the board: the row's task, its place and which part of it. - private string? keyboardRowId; - private int keyboardRowIndex; - private string keyboardPart = "Done"; - private WeakReference? keyboardElement; + private readonly KeyboardSpot boardSpot = new(); + private readonly KeyboardSpot candidateSpot = new(); + private WeakReference? boardElement; + private WeakReference? candidateElement; public FocusPage(FocusViewModel viewModel) { @@ -28,7 +28,11 @@ public FocusPage(FocusViewModel viewModel) this.viewModel = viewModel; DataContext = viewModel; PreviewKeyDown += OnPreviewKeyDown; + KeyDown += OnKeyDown; BoardRows.PreviewGotKeyboardFocus += OnBoardGotKeyboardFocus; + BoardRows.PreviewLostKeyboardFocus += (_, e) => ForgetWhenLeaving(BoardRows, boardSpot, e); + Candidates.PreviewGotKeyboardFocus += OnCandidateGotKeyboardFocus; + Candidates.PreviewLostKeyboardFocus += (_, e) => ForgetWhenLeaving(Candidates, candidateSpot, e); viewModel.PropertyChanged += OnViewModelChanged; Unloaded += (_, _) => viewModel.PropertyChanged -= OnViewModelChanged; Loaded += (_, _) => @@ -65,41 +69,95 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e) } } + // Esc anywhere on the page while the add panel is open goes back to the board. The search box's own Esc + // (clear what is typed first) and an open drop-down handle theirs before this sees it. + private void OnKeyDown(object sender, KeyEventArgs e) + { + if (e.Key != Key.Escape || Keyboard.Modifiers != ModifierKeys.None || !viewModel.IsAddOpen) return; + viewModel.CloseAddDialogCommand.Execute(null); + e.Handled = true; + } + private void OnBoardGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (e.NewFocus is not FrameworkElement element || element.DataContext is not FocusRowViewModel row) return; - keyboardRowId = row.Id; - keyboardRowIndex = viewModel.Items.IndexOf(row); - keyboardPart = element.Name is "Up" or "Down" or "Off" ? element.Name : "Done"; - keyboardElement = new WeakReference(element); + boardSpot.Remember(row.Id, viewModel.Items.IndexOf(row), element.Name is "Up" or "Down" or "Off" ? element.Name : "Done"); + boardElement = new WeakReference(element); + } + + private void OnCandidateGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) + { + if (e.NewFocus is not FrameworkElement element || element.DataContext is not FocusAddRowViewModel row) return; + candidateSpot.Remember(row.Id, viewModel.Candidates.IndexOf(row), "Add"); + candidateElement = new WeakReference(element); + } + + // The keyboard went to something else on show (not nowhere, as when its row is redrawn): forget the row, + // so a later redraw, such as a sync, does not pull the keyboard back onto it. + private void ForgetWhenLeaving(ItemsControl list, KeyboardSpot spot, KeyboardFocusChangedEventArgs e) + { + if (e.NewFocus is UIElement next and not Window && next != this && next.IsVisible && !list.IsAncestorOf(next)) + { + spot.Forget(); + } } private void OnViewModelChanged(object? sender, PropertyChangedEventArgs e) { - if (e.PropertyName != nameof(FocusViewModel.Items) || keyboardRowId is null) return; - Dispatcher.BeginInvoke(DispatcherPriority.Loaded, RestoreKeyboard); + if (e.PropertyName == nameof(FocusViewModel.Items) && boardSpot.IsSet) + { + Dispatcher.BeginInvoke(DispatcherPriority.Loaded, RestoreBoard); + } + else if (e.PropertyName == nameof(FocusViewModel.Candidates) && candidateSpot.IsSet) + { + Dispatcher.BeginInvoke(DispatcherPriority.Loaded, RestoreCandidate); + } } - // Once the board is redrawn: if the keyboard was on a row that is gone and has not moved on to anything - // else on show, put it on the same task (moved), or on the row now in its place (completed or taken off). - private void RestoreKeyboard() + // Whether the keyboard was dropped by a redraw: the element that had it is gone, and it has not moved on + // to anything else on show. + private bool WasDropped(WeakReference? had) { - if (keyboardElement is null || !keyboardElement.TryGetTarget(out var before) || before.IsVisible) return; + if (had is null || !had.TryGetTarget(out var before) || before.IsVisible) return false; var now = Keyboard.FocusedElement as UIElement; - if (now is not null and not Window && now.IsVisible && now != this) return; + return now is null or Window || !now.IsVisible || now == this; + } + + // Puts the keyboard on the same task (moved), or on the row now in its place (completed or taken off). + private void RestoreBoard() + { + if (!WasDropped(boardElement)) return; var items = viewModel.Items; - if (items.Count == 0) + if (boardSpot.Pick([.. items.Select(item => item.Id)]) is not { } index) { QuickAddBox.Focus(); return; } - var row = items.FirstOrDefault(item => item.Id == keyboardRowId) ?? items[Math.Clamp(keyboardRowIndex, 0, items.Count - 1)]; - if (BoardRows.ItemContainerGenerator.ContainerFromItem(row) is not ContentPresenter presenter) return; + if (BoardRows.ItemContainerGenerator.ContainerFromItem(items[index]) is not ContentPresenter presenter) return; presenter.ApplyTemplate(); var template = presenter.ContentTemplate; - var target = template?.FindName(keyboardPart, presenter) as UIElement; + var target = template?.FindName(boardSpot.Part, presenter) as UIElement; if (target is null || !target.IsEnabled || target.Visibility != Visibility.Visible) target = template?.FindName("Done", presenter) as UIElement; target?.Focus(); } + + // After an add from the panel: the next task offered in its place, or the search box when none is left. + private void RestoreCandidate() + { + if (!WasDropped(candidateElement)) return; + var rows = viewModel.Candidates; + if (candidateSpot.Pick([.. rows.Select(row => row.Id)]) is { } index + && Candidates.ItemContainerGenerator.ContainerFromItem(rows[index]) is ContentPresenter presenter + && System.Windows.Media.VisualTreeHelper.GetChildrenCount(presenter) > 0 + && System.Windows.Media.VisualTreeHelper.GetChild(presenter, 0) is UIElement button + && button.IsVisible) + { + button.Focus(); + return; + } + + candidateSpot.Forget(); + if (SearchBox.IsVisible) SearchBox.Focus(); + } } diff --git a/windows/tests/GoalMaker.App.Tests/FocusViewModelTests.cs b/windows/tests/GoalMaker.App.Tests/FocusViewModelTests.cs index 2d64744d..741f799e 100644 --- a/windows/tests/GoalMaker.App.Tests/FocusViewModelTests.cs +++ b/windows/tests/GoalMaker.App.Tests/FocusViewModelTests.cs @@ -210,4 +210,56 @@ public void WithNoOpenTasksTheAddPanelSaysSoInsteadOfAllOnTheBoard() Assert.False(page.HasNoOpenTasks); Assert.True(page.IsAllOnBoard); } + + [Fact] + public void ALateRowSaysLateToAScreenReaderNotOnlyInColor() + { + var late = planner.Tasks.Add("Hand in the form @School")!; + planner.Tasks.SetDeadline(late.Id, new DateOnly(2026, 9, 16)); + var other = planner.Tasks.Add("Renew the card")!; + planner.Tasks.SetDeadline(other.Id, new DateOnly(2026, 9, 10)); + var page = Page(); + page.AddToBoardCommand.Execute(page.Candidates.Single(row => row.Id == late.Id)); + + var row = page.Items.Single(); + Assert.StartsWith("Focus.Late · ", row.HelpText); + Assert.EndsWith(row.Subtitle, row.HelpText); + Assert.StartsWith("Focus.Late · ", page.Candidates.Single().HelpText); + } + + [Fact] + public void ARowThatIsNotLateReadsItsDateAndArea() + { + var page = Page(); + page.QuickAddTitle = "Hand in the form @School"; + page.QuickAddCommand.Execute(null); + Assert.Equal("School", page.Items.Single().HelpText); + } + + [Fact] + public void TheDoneBoxUnticksWhenTheTaskStaysOpen() + { + var page = Page(); + page.QuickAddTitle = "Hand in the form"; + page.QuickAddCommand.Execute(null); + // A row for a task the store does not know: completing it changes nothing, so the box must not stay ticked. + var row = new FocusRowViewModel(planner.Task("Hand in the form") with { Id = "not-in-the-store" }, null, new DateOnly(2026, 9, 18), 1, 1, "Late"); + row.IsDone = true; + page.CompleteCommand.Execute(row); + Assert.False(row.IsDone); + } + + [Fact] + public void TickingTheDoneBoxCompletesTheTaskAndTakesItOffTheBoard() + { + var page = Page(); + page.QuickAddTitle = "Hand in the form"; + page.QuickAddCommand.Execute(null); + var row = page.Items.Single(); + + row.IsDone = true; + page.CompleteCommand.Execute(row); + Assert.Empty(page.Items); + Assert.Equal(TaskState.Done, planner.Tasks.All().Single().State); + } } diff --git a/windows/tests/GoalMaker.App.Tests/KeyboardSpotTests.cs b/windows/tests/GoalMaker.App.Tests/KeyboardSpotTests.cs new file mode 100644 index 00000000..9055397e --- /dev/null +++ b/windows/tests/GoalMaker.App.Tests/KeyboardSpotTests.cs @@ -0,0 +1,49 @@ +using GoalMaker.App.Controls; + +namespace GoalMaker.App.Tests; + +/// +/// Where the Focus Board puts the keyboard back after a redraw: the same row, the one now in its place, +/// and nothing once the keyboard has left the list. +/// +public sealed class KeyboardSpotTests +{ + [Fact] + public void NothingIsPickedBeforeARowHadTheKeyboard() + { + var spot = new KeyboardSpot(); + Assert.False(spot.IsSet); + Assert.Null(spot.Pick(["a", "b"])); + } + + [Fact] + public void AMovedRowKeepsTheKeyboardAndItsPart() + { + var spot = new KeyboardSpot(); + spot.Remember("b", 1, "Up"); + Assert.Equal(0, spot.Pick(["b", "a", "c"])); + Assert.Equal("Up", spot.Part); + } + + [Fact] + public void AGoneRowHandsTheKeyboardToTheOneInItsPlace() + { + var spot = new KeyboardSpot(); + spot.Remember("b", 1, "Done"); + Assert.Equal(1, spot.Pick(["a", "c"])); + + spot.Remember("c", 2, "Done"); + Assert.Equal(1, spot.Pick(["a", "b"])); + Assert.Null(spot.Pick([])); + } + + [Fact] + public void AForgottenSpotPicksNothingSoASyncDoesNotPullTheKeyboardBack() + { + var spot = new KeyboardSpot(); + spot.Remember("a", 0, "Off"); + spot.Forget(); + Assert.False(spot.IsSet); + Assert.Null(spot.Pick(["a", "b"])); + } +} diff --git a/windows/tests/GoalMaker.App.Tests/PageSnapshots.cs b/windows/tests/GoalMaker.App.Tests/PageSnapshots.cs index f1498709..eb589e35 100644 --- a/windows/tests/GoalMaker.App.Tests/PageSnapshots.cs +++ b/windows/tests/GoalMaker.App.Tests/PageSnapshots.cs @@ -95,11 +95,14 @@ public void FocusBoard() => OnUiThread(folder => Save(new FocusPage(Board()), folder, $"focus-narrow-{name}", new Size(520, 760)); } - theme.Apply(planner.Settings.Appearance with { Mode = GoalMaker.Core.Settings.ThemeMode.Light }); - var searching = Board(); - searching.ShowAddDialogCommand.Execute(null); - searching.Query = "bi"; - Save(new FocusPage(searching), folder, "focus-narrow-add-panel", new Size(520, 760)); + foreach (var mode in new[] { GoalMaker.Core.Settings.ThemeMode.Light, GoalMaker.Core.Settings.ThemeMode.Dark }) + { + theme.Apply(planner.Settings.Appearance with { Mode = mode }); + var searching = Board(); + searching.ShowAddDialogCommand.Execute(null); + searching.Query = "bi"; + Save(new FocusPage(searching), folder, $"focus-narrow-add-panel-{mode}".ToLowerInvariant(), new Size(520, 760)); + } }); [Fact(Explicit = true)]