Files
Chronos/src/Titano.App/Sections/ImportView.xaml.cs
T
Claude 0dada9bf47 fix(import): accessible, right-to-left safe Import screen after the visual review
- The folder path is validated only while Folder is the destination (no red
  box under Archive) and shows faint text when unused; typing one chooses
  Folder, as "…" does; a path change replans only for Folder.
- Session times are no longer cut: the Session column sizes to its content.
- Show plan lists file names under a two-line session heading (folder below),
  with full paths in tooltips; header and cells line up (scroll bar reserved).
- Failures say what happened in one localized sentence (source gone, disk
  full, access, server, other), wrap up to three lines, keep the raw text in
  the tooltip, and offer Retry only.
- The status sentence comes with one action (Folder…, Refresh devices,
  Library, Choose folder, Server, Retry).
- The form is locked once an import finished ("New import" plans again); the
  plan header drops the estimate and free space then.
- A card stays "new" until it has a nickname.
- Device entries show keyboard focus and a disabled state with its reason;
  row actions (Merge, Split here) are revealed on the row and named per row;
  composite buttons carry accessible names; tab order follows the layout;
  rows are 36 px.
- Live regions announce planning status, copy progress, the result and the
  failure; progress bars are labelled.
- Right-to-left: paths, templates, name previews and numbers stay left to
  right (numbers right-aligned), the session range uses its own arrow, and
  paths or serials inside sentences are embedded left to right.
- The 56 px number boxes fit four digits despite the kit's double padding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 17:13:20 +00:00

128 lines
5.7 KiB
C#

using System.ComponentModel;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
namespace Titano.App.Sections;
public partial class ImportView : UserControl
{
/// <summary>A polite text that keeps changing (copy progress) is announced at most this often.</summary>
private static readonly TimeSpan PoliteInterval = TimeSpan.FromSeconds(3);
private readonly Dictionary<UIElement, DateTime> _announced = [];
private readonly HashSet<UIElement> _pending = [];
private bool _nicknameOpen;
private bool _restoreFocus;
public ImportView()
{
InitializeComponent();
DataContextChanged += OnDataContextChanged;
AddHandler(Binding.TargetUpdatedEvent, new EventHandler<DataTransferEventArgs>(OnTargetUpdated));
}
private ImportViewModel? Model => DataContext as ImportViewModel;
/// <summary>"Edit ▾" and "{ }" open their menu under themselves.</summary>
private void OnOpenMenu(object sender, RoutedEventArgs e)
{
if (sender is not Button { ContextMenu: { } menu } button) return;
menu.DataContext = DataContext;
menu.PlacementTarget = button;
menu.Placement = PlacementMode.Bottom;
menu.IsOpen = true;
}
/// <summary>The "{ }" menu writes into the template field used last.</summary>
private void OnFolderTemplateFocus(object sender, KeyboardFocusChangedEventArgs e) => Model?.SetActiveTemplate(folder: true);
private void OnFileTemplateFocus(object sender, KeyboardFocusChangedEventArgs e) => Model?.SetActiveTemplate(folder: false);
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue is INotifyPropertyChanged old) old.PropertyChanged -= OnModelChanged;
if (e.NewValue is INotifyPropertyChanged model) model.PropertyChanged += OnModelChanged;
}
/// <summary>
/// Keyboard focus follows what opens: the nickname field when it appears (a new card, Rename), and "Show plan"
/// (focus returns to its button when it closes). A change of phase that hides the focused button (Import, Retry,
/// New import, the status action, "Split here" closing the plan) moves focus to what replaced it, once the card
/// is planned again.
/// </summary>
private void OnModelChanged(object? sender, PropertyChangedEventArgs e)
{
if (Model is not { } model) return;
if (e.PropertyName is nameof(ImportViewModel.IsNicknameOpen) or "")
{
var opened = model.IsNicknameOpen && !_nicknameOpen;
_nicknameOpen = model.IsNicknameOpen;
if (opened && IsVisible) FocusLater(NicknameBox);
}
if (e.PropertyName is nameof(ImportViewModel.IsDetailsOpen) && IsVisible)
{
if (model.IsDetailsOpen) FocusLater(ClosePlanButton);
else _restoreFocus = true;
}
if (e.PropertyName is nameof(ImportViewModel.IsDetailsOpen) or "" && IsVisible) Dispatcher.BeginInvoke(DispatcherPriority.Input, RestoreFocus);
}
private void RestoreFocus()
{
if (Model is not { } model || model.IsBusyPlanning) return;
var lost = Keyboard.FocusedElement is UIElement { IsVisible: false } focused && IsAncestorOf(focused);
if (!_restoreFocus && !lost) return;
_restoreFocus = false;
UIElement? target = model.IsRunning ? CancelButton
: model.IsDone ? NewImportButton
: model.IsFailed ? RetryButton
: model.HasPlan ? ShowPlanButton
: model.HasStatusAction ? StatusActionButton
: null;
target?.Focus();
}
/// <summary>After layout: an element that just became visible cannot take focus yet.</summary>
private void FocusLater(UIElement element) => Dispatcher.BeginInvoke(DispatcherPriority.Input, () => element.Focus());
/// <summary>
/// Live texts (AutomationProperties.LiveSetting in XAML, their binding with NotifyOnTargetUpdated) tell screen
/// readers that they changed: planning status, copy progress, the result and the failure. WPF raises nothing by itself.
/// </summary>
private void OnTargetUpdated(object? sender, DataTransferEventArgs e)
{
if (e.TargetObject is not UIElement element || AutomationProperties.GetLiveSetting(element) is var setting && setting == AutomationLiveSetting.Off) return;
if (AutomationPeer.ListenerExists(AutomationEvents.LiveRegionChanged)) Announce(element, setting);
}
/// <summary>Polite texts at most every <see cref="PoliteInterval"/>, ending with their latest value; assertive ones at once.</summary>
private void Announce(UIElement element, AutomationLiveSetting setting)
{
var wait = setting == AutomationLiveSetting.Polite && _announced.TryGetValue(element, out var last) ? last + PoliteInterval - DateTime.UtcNow : TimeSpan.Zero;
if (wait > TimeSpan.Zero)
{
if (_pending.Add(element)) _ = AnnounceLaterAsync(element, setting, wait);
return;
}
_announced[element] = DateTime.UtcNow;
// After the bindings of the same change: a text that is being hidden is not announced.
Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
{
if (element.IsVisible) UIElementAutomationPeer.CreatePeerForElement(element)?.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
});
}
private async Task AnnounceLaterAsync(UIElement element, AutomationLiveSetting setting, TimeSpan wait)
{
await Task.Delay(wait);
_pending.Remove(element);
Announce(element, setting);
}
}