fix(reorganize): Apply on the first click, one offer per toast, no open dialog replaced
Review of owner review 2, phase 4a (R7): the Reorganize dialogs of the desktop and the web panel. Desktop - Clicking Apply first takes the focus from the template field, which committed it and started a dry run identical to the one on screen: Apply turned off under the click. A commit now plans only when the templates differ from the shown plan (or the last dry run failed). - The Settings toast's Reorganize… disposed whatever dialog the Archive screen had open, so a running Delete stopped without being asked. An open dialog now stays and its archive is not switched; Reorganize says it waits and opens once that dialog closes, with the archive's templates read again and every part the toasts offered meanwhile. - Each Settings toast takes its own archive's offer (one pending offer per archive), and a toast gone early (dismissed, pushed out) lets the next change of that archive raise its own toast: Toast.Closed now says when a toast leaves the screen. Web panel - The Library sends the selected ids up to a thousand. all:true is used only above that, when the selection is exactly what Select all fetched without a filter and the archive has not grown since, so a sequence unticked or added later is never reorganized; otherwise the toast asks to select again. - The preview's "before" of an untitled sequence is its old name, not the new title twice. - A dry run of the same request answering during the confirmation no longer drops the confirmed apply; a plan that really changed is shown and said (Admin.Reorganize.PlanChanged). - Enter in a template plans at once (the form never submitted on Enter). - The confirm tooltip and the module header no longer say the moves are verified: moves and renames are not hashed. Strings in en and it; the reworded key exists in no other culture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit bed718fb5af2ef69943fd348690c689259872a34)
This commit is contained in:
1 parent
9ae773aa5a
commit
59ee982e4d
14 files changed
+301
-49
No files matched your search
@@ -98,7 +98,7 @@ public sealed class ReorganizeDialogViewModel : ArchiveDialogViewModel
|
||||
ApplyCommand = new AsyncRelayCommand(ApplyAsync, () => ApplyBlocker is null);
|
||||
|
||||
ReorganizeTemplate Template(ReorganizeParts part, string text, string toolTip) =>
|
||||
new(part, text, enabled.HasFlag(part), toolTip, OnTyped, () => _ = PlanAsync(), () => _ = PlanAsync());
|
||||
new(part, text, enabled.HasFlag(part), toolTip, OnTyped, () => _ = PlanAsync(), OnCommitted);
|
||||
}
|
||||
|
||||
public ReorganizeTemplate Folders { get; }
|
||||
@@ -182,6 +182,23 @@ public sealed class ReorganizeDialogViewModel : ArchiveDialogViewModel
|
||||
_delay.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enter, or the focus leaving a field, plans at once, unless the dry run on screen or the one under way is already of
|
||||
/// these templates: a click on Apply takes the focus from the field first, and an identical dry run started then
|
||||
/// would turn Apply off under the click.
|
||||
/// </summary>
|
||||
private void OnCommitted()
|
||||
{
|
||||
if (IsPlanned()) _delay.Stop();
|
||||
else _ = PlanAsync();
|
||||
}
|
||||
|
||||
/// <summary>The templates on screen have their plan, or a dry run of them is under way; false after a failed one.</summary>
|
||||
private bool IsPlanned()
|
||||
{
|
||||
lock (_gate) return _run.For is { } templates && templates == Current() && (_run.Plan is not null || _planning is not null);
|
||||
}
|
||||
|
||||
private async Task DryRunAsync()
|
||||
{
|
||||
var templates = Current();
|
||||
|
||||
@@ -112,33 +112,45 @@ public static class ReorganizePlans
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What Settings › Archive offers to reorganize after its templates changed (owner review 2, R7): the archive and every
|
||||
/// template changed since the offer was last taken. A change while the toast is still up joins the offer on screen
|
||||
/// instead of raising another toast; a change in another archive raises its own.
|
||||
/// What Settings › Archive offers to reorganize after its templates changed (owner review 2, R7): per archive, every
|
||||
/// template changed since its offer was last taken. A change while that archive's toast is still up joins the offer on
|
||||
/// screen instead of raising another toast; a change in another archive raises its own, and each toast takes only its
|
||||
/// archive's offer.
|
||||
/// </summary>
|
||||
/// <param name="toastTime">How long a toast stays up.</param>
|
||||
public sealed class ReorganizeOffer(TimeSpan toastTime)
|
||||
{
|
||||
private (Core.Model.Archive Archive, ReorganizeParts Parts)? _pending;
|
||||
private DateTime _raised;
|
||||
private readonly Dictionary<Guid, Pending> _pending = [];
|
||||
|
||||
/// <summary>Records that <paramref name="part"/> of <paramref name="archive"/> changed at <paramref name="now"/>; true when a toast should offer it.</summary>
|
||||
/// <summary>
|
||||
/// Records that <paramref name="part"/> of <paramref name="archive"/> changed at <paramref name="now"/>; true when a
|
||||
/// toast should offer it (none of that archive is up), raised at <paramref name="now"/>.
|
||||
/// </summary>
|
||||
public bool Add(Core.Model.Archive archive, ReorganizeParts part, DateTime now)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(archive);
|
||||
var same = _pending is { } pending && pending.Archive.Id == archive.Id;
|
||||
_pending = same ? (archive, _pending!.Value.Parts | part) : (archive, part);
|
||||
if (same && now - _raised < toastTime) return false;
|
||||
_raised = now;
|
||||
return true;
|
||||
var known = _pending.TryGetValue(archive.Id, out var pending);
|
||||
var shown = known && pending!.Raised is { } raised && now - raised < toastTime;
|
||||
_pending[archive.Id] = new Pending(archive, (known ? pending!.Parts : ReorganizeParts.None) | part, shown ? pending!.Raised : now);
|
||||
return !shown;
|
||||
}
|
||||
|
||||
/// <summary>The offer when a toast's action runs: the archive as it was last saved and every template changed; null when taken already.</summary>
|
||||
public (Core.Model.Archive Archive, ReorganizeParts Parts)? Take()
|
||||
/// <summary>
|
||||
/// The toast of <paramref name="archiveId"/> raised at <paramref name="raised"/> left the screen (dismissed, timed out,
|
||||
/// pushed out by newer ones): the next change raises a new one. A toast older than the one up now changes nothing.
|
||||
/// </summary>
|
||||
public void Closed(Guid archiveId, DateTime raised)
|
||||
{
|
||||
var pending = _pending;
|
||||
_pending = null;
|
||||
_raised = default;
|
||||
return pending;
|
||||
if (_pending.TryGetValue(archiveId, out var pending) && pending.Raised == raised) _pending[archiveId] = pending with { Raised = null };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The offer of <paramref name="archiveId"/> when its toast's action runs: the archive as it was last saved and every
|
||||
/// template changed; null when taken already. Other archives' offers stay.
|
||||
/// </summary>
|
||||
public (Core.Model.Archive Archive, ReorganizeParts Parts)? Take(Guid archiveId) =>
|
||||
_pending.Remove(archiveId, out var pending) ? (pending.Archive, pending.Parts) : null;
|
||||
|
||||
/// <param name="Raised">When the toast on screen was raised; null once it left.</param>
|
||||
private sealed record Pending(Core.Model.Archive Archive, ReorganizeParts Parts, DateTime? Raised);
|
||||
}
|
||||
@@ -61,6 +61,10 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
private bool _jobEvents;
|
||||
private string _lastScanText = string.Empty;
|
||||
private ArchiveDialogViewModel? _dialog;
|
||||
|
||||
/// <summary>A Reorganize the Settings toast asked for while a dialog was open: it opens once that one closes.</summary>
|
||||
private (Core.Model.Archive Archive, ReorganizeParts Parts)? _waitingReorganize;
|
||||
|
||||
private IReadOnlyList<ArchiveTile> _tiles = [];
|
||||
private IReadOnlyList<ChartRow> _months = [];
|
||||
private IReadOnlyList<ChartItem> _storage = [];
|
||||
@@ -516,6 +520,7 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
_reloadDelay.Stop();
|
||||
_search?.Cancel();
|
||||
if (_jobEvents && Services.Jobs is { } jobs) jobs.JobChanged -= OnJobChanged;
|
||||
_waitingReorganize = null;
|
||||
_dialog?.Dispose();
|
||||
base.Dispose();
|
||||
}
|
||||
@@ -772,11 +777,19 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
|
||||
/// <summary>
|
||||
/// Opens Reorganize on every sequence of <paramref name="archive"/> (Settings › Archive offers it after its templates
|
||||
/// changed), with <paramref name="parts"/> switched on; the screen shows that archive.
|
||||
/// changed), with <paramref name="parts"/> switched on; the screen shows that archive. The toast is reached from
|
||||
/// another screen, so a dialog may be open here, a Delete moving sequences say: it is never replaced nor its archive
|
||||
/// switched under it; Reorganize waits, says so, and opens once that dialog closes.
|
||||
/// </summary>
|
||||
public async Task OpenReorganizeAllAsync(Core.Model.Archive archive, ReorganizeParts parts)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(archive);
|
||||
if (_dialog is not null)
|
||||
{
|
||||
_waitingReorganize = _waitingReorganize is { } waiting && waiting.Archive.Id == archive.Id ? (archive, waiting.Parts | parts) : (archive, parts);
|
||||
Shell.Notify(Loc.L("Archive.Reorganize.Waiting"));
|
||||
return;
|
||||
}
|
||||
if (Services.Archives is not { } archives || Services.Engine is not { } engine) return;
|
||||
if (_archive != archive) SelectedArchive = archive;
|
||||
var dialog = new ReorganizeDialogViewModel(archives, archive, null, parts, r => EnqueueAsync(engine, r), CloseDialog);
|
||||
@@ -784,6 +797,14 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
await dialog.PlanAsync();
|
||||
}
|
||||
|
||||
/// <summary>The Reorganize that waited for the dialog that just closed, with the archive's templates as they are now.</summary>
|
||||
private async Task OpenWaitingReorganizeAsync()
|
||||
{
|
||||
if (_waitingReorganize is not { } waiting) return;
|
||||
_waitingReorganize = null;
|
||||
await OpenReorganizeAllAsync(await FreshAsync(waiting.Archive), waiting.Parts);
|
||||
}
|
||||
|
||||
/// <summary>The archive's record as the index holds it now; <paramref name="archive"/> when it cannot be read.</summary>
|
||||
private async Task<Core.Model.Archive> FreshAsync(Core.Model.Archive archive)
|
||||
{
|
||||
@@ -853,12 +874,16 @@ public sealed class ArchiveViewModel : SectionViewModel
|
||||
await Shell.Library.RefreshAsync();
|
||||
}
|
||||
|
||||
/// <summary>Closes <paramref name="dialog"/> if it is still the open one (an apply that finishes late closes nothing else).</summary>
|
||||
/// <summary>
|
||||
/// Closes <paramref name="dialog"/> if it is still the open one (an apply that finishes late closes nothing else); a
|
||||
/// Reorganize asked for meanwhile opens then.
|
||||
/// </summary>
|
||||
private void CloseDialog(ArchiveDialogViewModel dialog)
|
||||
{
|
||||
if (!ReferenceEquals(_dialog, dialog)) return;
|
||||
dialog.Dispose();
|
||||
Dialog = null;
|
||||
_ = OpenWaitingReorganizeAsync();
|
||||
}
|
||||
|
||||
private async Task AdoptAsync()
|
||||
|
||||
@@ -1074,15 +1074,25 @@ public sealed class SettingsViewModel : SectionViewModel
|
||||
Log.Warning("settings", "The sequences of the archive were not counted", ex);
|
||||
return;
|
||||
}
|
||||
if (!_reorganize.Add(archive, part, DateTime.UtcNow)) return;
|
||||
Shell.Notify(Loc.L("Settings.Archive.TemplatesSaved"), Loc.L("Settings.Archive.ExistingKept"), Loc.L("Settings.Archive.Reorganize"),
|
||||
Loc.L("Tooltips.Settings.Reorganize"), () =>
|
||||
var raised = DateTime.UtcNow;
|
||||
if (!_reorganize.Add(archive, part, raised)) return;
|
||||
var toast = new Toast
|
||||
{
|
||||
Title = Loc.L("Settings.Archive.TemplatesSaved"),
|
||||
Message = Loc.L("Settings.Archive.ExistingKept"),
|
||||
ActionText = Loc.L("Settings.Archive.Reorganize"),
|
||||
ActionToolTip = Loc.L("Tooltips.Settings.Reorganize"),
|
||||
ActionCommand = new RelayCommand(() =>
|
||||
{
|
||||
// Every template changed since; what this toast offered when another one was used since.
|
||||
var (target, parts) = _reorganize.Take() ?? (archive, part);
|
||||
// Every template of this archive changed since; what this toast offered when another toast of it took them.
|
||||
var (target, parts) = _reorganize.Take(archive.Id) ?? (archive, part);
|
||||
Shell.Navigate(SectionKey.Archive);
|
||||
_ = Shell.Archive.OpenReorganizeAllAsync(target, parts);
|
||||
});
|
||||
}),
|
||||
};
|
||||
// Gone from the screen early (dismissed, pushed out), the next change of this archive raises a toast of its own.
|
||||
toast.Closed += (_, _) => _reorganize.Closed(archive.Id, raised);
|
||||
Shell.ShowToast(toast);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -425,17 +425,20 @@ async function renderList(root, { api, setMeta, actions, onEvent }, params) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorganize (owner review 2, R7) of the selected sequences: every sequence of the archive after Select all without a
|
||||
* filter, else their ids, at most a bulk request's thousand (one plan, one job).
|
||||
* Reorganize (owner review 2, R7) of the selected sequences: their ids, at most a bulk request's thousand (one plan,
|
||||
* one job). Beyond that, every sequence of the archive, but only while the selection is exactly what Select all
|
||||
* fetched without a filter and the archive has not grown since: a sequence unticked or added later is never included.
|
||||
*/
|
||||
async function reorganize() {
|
||||
const everything = matching !== null && !isFiltered() && selection.size === matching.length;
|
||||
if (!everything && selection.size > BULK_LIMIT) {
|
||||
const ids = [...selection.ids];
|
||||
const everything = ids.length > BULK_LIMIT && matching !== null && !isFiltered() && total === matching.length &&
|
||||
ids.length === matching.length && matching.every((s) => selection.has(s.id));
|
||||
if (!everything && ids.length > BULK_LIMIT) {
|
||||
toast(t("Admin.Reorganize.TooMany", formatNumber(BULK_LIMIT)), true);
|
||||
return;
|
||||
}
|
||||
const names = new Map(selectedRows().map((s) => [s.id, s.displayTitle]));
|
||||
await openReorganize({ api, target: everything ? { all: true } : { sequenceIds: [...selection.ids] }, names });
|
||||
await openReorganize({ api, target: everything ? { all: true } : { sequenceIds: ids }, names });
|
||||
}
|
||||
|
||||
async function retag() {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// to sequences already in the archive, the Library's selection or every sequence. Each change of a template asks the
|
||||
// server for a dry run (nothing is touched) and the table shows what every sequence becomes, in the order the changes
|
||||
// apply. Apply confirms that very plan by its fingerprint, and the server runs it as a job (kind "move") that moves and
|
||||
// renames the originals, verified, never overwriting or deleting one.
|
||||
// renames the originals, never overwriting or deleting one (only photos whose SHA-256 is known get new names).
|
||||
import { t, plural, applyStrings } from "./i18n.js";
|
||||
import { escapeHtml, attr, formatNumber, toast, errorText, confirmDialog, markInvalid, clearInvalid } from "./ui.js";
|
||||
|
||||
@@ -104,11 +104,14 @@ export function openReorganize({ api, target, names = new Map(), templates = nul
|
||||
schedule(PLAN_DELAY);
|
||||
});
|
||||
}
|
||||
// Enter in a field plans at once; only the Apply button changes anything.
|
||||
form.addEventListener("submit", (e) => {
|
||||
// Enter in a template plans at once (a form of several fields without a submit button never submits on Enter);
|
||||
// only the Apply button changes anything.
|
||||
form.addEventListener("keydown", (e) => {
|
||||
if (e.key !== "Enter" || e.isComposing || !(e.target instanceof HTMLInputElement) || e.target.type === "checkbox") return;
|
||||
e.preventDefault();
|
||||
if (loaded && !busy) schedule(0);
|
||||
});
|
||||
form.addEventListener("submit", (e) => e.preventDefault());
|
||||
closeButton.addEventListener("click", () => {
|
||||
if (!busy) dialog.close();
|
||||
});
|
||||
@@ -257,7 +260,10 @@ export function openReorganize({ api, target, names = new Map(), templates = nul
|
||||
const fromTitle = s.fromTitle ?? null;
|
||||
const toTitle = s.toTitle ?? null;
|
||||
const name = names.get(s.sequenceId) ?? toTitle ?? fromTitle ?? folderName(s.fromFolder);
|
||||
const sequence = fromTitle !== toTitle ? change(fromTitle ?? name, toTitle ?? name, "auto")
|
||||
// Before reads the old state only: an untitled sequence goes by the page's name for it or its folder, never by the
|
||||
// title it is about to get.
|
||||
const before = fromTitle ?? names.get(s.sequenceId) ?? folderName(s.fromFolder);
|
||||
const sequence = fromTitle !== toTitle ? change(before, toTitle ?? before, "auto")
|
||||
: `<span class="ellipsis" dir="auto" title="${attr(name)}">${escapeHtml(name)}</span>`;
|
||||
const folder = s.fromFolder !== s.toFolder ? change(s.fromFolder || "/", s.toFolder || "/", "ltr", "path")
|
||||
: `<span class="path muted" dir="ltr" title="${attr(t("Admin.Reorganize.SameFolder.Tip"))}">${escapeHtml(s.fromFolder || "/")}</span>`;
|
||||
@@ -285,7 +291,11 @@ export function openReorganize({ api, target, names = new Map(), templates = nul
|
||||
closeButton.disabled = busy;
|
||||
}
|
||||
|
||||
/** The confirmation names what moves; the plan sent back is the one the table shows (the fields cannot change meanwhile). */
|
||||
/**
|
||||
* The confirmation names what moves; the plan sent back is the one the table showed. The fields cannot change
|
||||
* meanwhile, but a dry run scheduled before may answer during it: the same plan again (same request and fingerprint)
|
||||
* is applied all the same; a different one is shown instead, and Apply waits for the user.
|
||||
*/
|
||||
async function apply() {
|
||||
if (applyButton.disabled) return;
|
||||
const chosen = plan;
|
||||
@@ -301,7 +311,12 @@ export function openReorganize({ api, target, names = new Map(), templates = nul
|
||||
cancel: t("Admin.Common.Keep"),
|
||||
cancelTip: t("Admin.Common.Keep.Tip"),
|
||||
});
|
||||
if (!confirmed || !dialog.open || plan !== chosen) return;
|
||||
if (!confirmed || !dialog.open) return;
|
||||
if (plan !== chosen && (plan?.key !== chosen.key || plan.fingerprint !== chosen.fingerprint)) {
|
||||
// A failed dry run already says why; a new plan is on screen to be checked.
|
||||
if (plan) showError(t("Admin.Reorganize.PlanChanged"));
|
||||
return;
|
||||
}
|
||||
busy = true;
|
||||
showError("");
|
||||
drawButtons();
|
||||
|
||||
@@ -29,6 +29,11 @@ public sealed class Toast : ObservableObject
|
||||
/// before they have read why.
|
||||
/// </summary>
|
||||
public bool RunsFromNotification { get; init; }
|
||||
|
||||
/// <summary>The toast left the screen: dismissed, timed out, pushed out by newer ones or closed by its action.</summary>
|
||||
public event EventHandler? Closed;
|
||||
|
||||
internal void OnClosed() => Closed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,8 +83,9 @@ public class ToastHost : ItemsControl
|
||||
{
|
||||
if (_timers.Remove(toast, out var timer)) timer.Stop();
|
||||
var hadFocus = IsKeyboardFocusWithin;
|
||||
Toasts.Remove(toast);
|
||||
var removed = Toasts.Remove(toast);
|
||||
if (hadFocus && !IsKeyboardFocusWithin && _returnFocus is UIElement { IsVisible: true } previous) previous.Focus();
|
||||
if (removed) toast.OnClosed();
|
||||
}
|
||||
|
||||
/// <summary>Moves the keyboard to the newest toast's first control; false when no toast is shown.</summary>
|
||||
|
||||
Reference in new issue
Block a user