EditorText.Change picks Editor.Optimise.ChangeRtl ('{0}: {1} ← {2}',
invariant) in right-to-left languages, as Import.Session.TimeRtl and
Jobs.Target.RenderRtl do. A failed save and a failed server job raise error
toasts; a cancelled job stays a notice.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
1069 lines
47 KiB
C#
1069 lines
47 KiB
C#
using System.Windows;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Threading;
|
||
using Titano.App.Infrastructure;
|
||
using Titano.App.Jobs;
|
||
using Titano.App.Server;
|
||
using Titano.App.Shell;
|
||
using Titano.Core.Advice;
|
||
using Titano.Core.Analysis;
|
||
using Titano.Core.Diagnostics;
|
||
using Titano.Core.Export;
|
||
using Titano.Core.Localization;
|
||
using Titano.Core.Model;
|
||
using Titano.Core.Project;
|
||
using Titano.Core.Render;
|
||
using Titano.Core.Video;
|
||
using Titano.Ui.Controls;
|
||
using Titano.Ui.Mvvm;
|
||
using Titano.Ui.Visuals;
|
||
|
||
namespace Titano.App.Editor;
|
||
|
||
/// <summary>
|
||
/// The sequence editor of one open sequence (BRIEF §12.3, Editor.html): header actions, the preview with its tools
|
||
/// (wipe compare, zoom, overlays) rendered through the pipeline at preview size (at the source's own size for 1:1 and
|
||
/// wheel zoom), the filmstrip with markers, the section tabs and the keyboard (space, arrows, Ctrl+Z/Y, Ctrl+0/1,
|
||
/// Ctrl+R, Ctrl+Shift+A). Preview frames, corrections, the export plan and the card estimates are computed off the UI
|
||
/// thread; each request cancels the previous one. While an analysis or render of this sequence runs, settings are
|
||
/// disabled and viewing tools stay live.
|
||
/// </summary>
|
||
public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
|
||
{
|
||
public const int DefaultPreviewEdge = 1280;
|
||
private static readonly TimeSpan Debounce = TimeSpan.FromMilliseconds(150);
|
||
|
||
private readonly IReadOnlyList<IVideoEncoderProvider> _encoders;
|
||
private readonly EncoderHost _host;
|
||
private readonly Dispatcher _dispatcher;
|
||
private readonly LatestRunner _preview = new(Debounce);
|
||
private readonly LatestRunner _corrections = new(Debounce);
|
||
private readonly LatestRunner _plans = new(Debounce);
|
||
private readonly DispatcherTimer _player;
|
||
private readonly SequenceJobs _jobs;
|
||
private readonly AsyncRelayCommand _analyse;
|
||
private readonly AsyncRelayCommand _render;
|
||
private readonly AsyncRelayCommand _cancelServerJob;
|
||
private PreviewRenderer _renderer;
|
||
private int _currentIndex;
|
||
private EditorTab _selectedTab;
|
||
private ImageSource? _previewImage;
|
||
private ImageSource? _originalImage;
|
||
private ImageSource? _waveformImage;
|
||
private string? _previewMessage;
|
||
private bool _isCompare = true;
|
||
private bool _showVectors;
|
||
private bool _showRegions;
|
||
private bool _showCropMask;
|
||
private bool _exportCropMask = true;
|
||
private CropMaskMode _cropMaskMode = CropMaskMode.ShowLoss;
|
||
private PreviewZoomMode _zoomMode = PreviewZoomMode.Fit;
|
||
private Rect _cropRect = new(0, 0, 1, 1);
|
||
private IReadOnlyList<Point>? _regionBoundary;
|
||
private IReadOnlyList<MotionVector>? _motionVectors;
|
||
private int _previewEdge = DefaultPreviewEdge;
|
||
private int _previewVersion;
|
||
private bool _previewBusy;
|
||
private RenderCorrections? _correctionsValue;
|
||
private ExportPlan? _plan;
|
||
private int _planVersion;
|
||
private bool _planned;
|
||
private string? _renderPreset;
|
||
private IReadOnlyList<TimelineMarker> _markers = [];
|
||
private IThumbnailSource _thumbnails;
|
||
private int _playLimit;
|
||
private double? _analysisSecondsPerFrame;
|
||
private ExportSyncGate _syncGate;
|
||
private bool _disposed;
|
||
|
||
public EditorViewModel(ShellViewModel shell, EditorSession session, IReadOnlyList<IVideoEncoderProvider> encoders, EncoderHost host)
|
||
{
|
||
Shell = shell;
|
||
Session = session;
|
||
_encoders = encoders;
|
||
_host = host;
|
||
_dispatcher = Dispatcher.CurrentDispatcher;
|
||
_jobs = new SequenceJobs(session.Content.Sequence.Id);
|
||
_renderer = new PreviewRenderer(session.Content, session.Source);
|
||
Planner = new ExportPlanner(session.Content, encoders, host);
|
||
_thumbnails = new EditorThumbnails(session.Source, session.Content.Count);
|
||
_player = new DispatcherTimer(DispatcherPriority.Background, _dispatcher);
|
||
_player.Tick += (_, _) => Advance();
|
||
|
||
BackCommand = new RelayCommand(() => Shell.Navigate(SectionKey.Library));
|
||
_analyse = new AsyncRelayCommand(AnalyseAsync, () => !IsLocked && !IsAnalysing);
|
||
OptimiseAllCommand = new RelayCommand(OptimiseAll, () => !IsLocked);
|
||
_render = new AsyncRelayCommand(() => RenderAsync(JobPriority.User, batch: false), () => CanRender);
|
||
_cancelServerJob = new AsyncRelayCommand(CancelServerJobAsync, () => ServerJob is not null);
|
||
UndoCommand = new RelayCommand(() => Session.Undo(), () => !IsLocked && Session.History.CanUndo);
|
||
RedoCommand = new RelayCommand(() => Session.Redo(), () => !IsLocked && Session.History.CanRedo);
|
||
PlayCommand = new RelayCommand(() => TogglePlay(limit: 0));
|
||
PreviewClipCommand = new RelayCommand(() => TogglePlay(limit: ClipFrames()));
|
||
StepBackCommand = new RelayCommand(() => CurrentIndex--);
|
||
StepForwardCommand = new RelayCommand(() => CurrentIndex++);
|
||
FitCommand = new RelayCommand(() => ZoomMode = PreviewZoomMode.Fit);
|
||
ActualSizeCommand = new RelayCommand(() => ZoomMode = PreviewZoomMode.Actual, () => !IsExportTab);
|
||
|
||
Frames = new FramesTab(this);
|
||
Exposure = new ExposureTab(this);
|
||
Tabs = [Frames, Exposure, new MotionTab(this), new CameraTab(this), new TimeTab(this), new StackTab(this), new ExportTab(this)];
|
||
_selectedTab = Exposure;
|
||
_currentIndex = session.Project.HeroFrame is { } hero && (uint)hero < (uint)FrameCount ? hero : 0;
|
||
// Until the link is read (AttachJobsAsync), a sequence of this PC cannot send its renders.
|
||
_syncGate = session.IsRemote ? ExportSyncGate.Remote : ExportSyncGate.SignedOut;
|
||
RenderChoices = [.. ExportPresets.BuiltIn.Select(p => new Choice(p.Id, p.NameKey))];
|
||
|
||
session.ProjectChanged += OnProjectChanged;
|
||
session.ContentChanged += OnContentChanged;
|
||
session.SaveFailed += OnSaveFailed;
|
||
session.ConflictFound += OnConflictFound;
|
||
session.Source.JobChanged += OnJobChanged;
|
||
session.Source.ReadOnlyChanged += OnReadOnlyChanged;
|
||
shell.Services.Sync.Changed += OnSyncChanged;
|
||
UpdateMarkers();
|
||
RequestCorrections();
|
||
RequestPlan();
|
||
RequestPreview(immediate: true);
|
||
}
|
||
|
||
public ShellViewModel Shell { get; }
|
||
|
||
public EditorSession Session { get; }
|
||
|
||
public ExportPlanner Planner { get; private set; }
|
||
|
||
public string Title => Session.Content.Sequence.DisplayTitle;
|
||
|
||
/// <summary>A server sequence edited with proxies (the header says where it lives).</summary>
|
||
public bool IsRemote => Session.IsRemote;
|
||
|
||
/// <summary>The server a server sequence lives on ("Studio NAS"); null for a sequence of this PC.</summary>
|
||
public string? ServerName => IsRemote ? Shell.Services.Server.ServerName ?? Loc.L("Server.Label.Server") : null;
|
||
|
||
/// <summary>"Studio NAS" beside the title of a server sequence; "Studio NAS · read-only" when it is only shown.</summary>
|
||
public string? RemoteText => !IsRemote ? null
|
||
: IsOutdated ? Loc.L("Editor.Remote.Outdated", ServerName)
|
||
: IsReadOnly ? Loc.L("Editor.Remote.ReadOnly", ServerName)
|
||
: ServerName;
|
||
|
||
public string? RemoteToolTip => !IsRemote ? null
|
||
: IsOutdated ? Loc.L("Tooltips.Editor.Outdated", ServerName)
|
||
: IsReadOnly ? Loc.L("Tooltips.Editor.RemoteReadOnly", ServerName)
|
||
: Loc.L("Tooltips.Editor.Remote", ServerName);
|
||
|
||
/// <summary>
|
||
/// "Render · 46 %", "Render · queued": the analysis or render of a server sequence the server runs or holds (the job
|
||
/// bar shows this PC's jobs); null when there is none.
|
||
/// </summary>
|
||
public string? ServerJobText => ServerJob is { } job ? EditorText.ServerJob(job) : null;
|
||
|
||
/// <summary>Frames, speed and time left of the server job.</summary>
|
||
public string? ServerJobToolTip => ServerJob is { } job ? EditorText.ServerJobDetail(job, ServerName ?? string.Empty) : null;
|
||
|
||
/// <summary>The glyph of the server job (DESIGN §4): ◐ an analysis running, ● a render running, ○ waiting, paused.</summary>
|
||
public StatusKind ServerJobKind => ServerJob switch
|
||
{
|
||
{ State: JobState.Running, Kind: JobKind.DeepAnalyse or JobKind.LightAnalyse } => StatusKind.Analysing,
|
||
{ State: JobState.Running } => StatusKind.Running,
|
||
{ State: JobState.Paused } => StatusKind.Paused,
|
||
_ => StatusKind.Queued,
|
||
};
|
||
|
||
/// <summary>The server job runs and reports how far it is (its progress bar shows).</summary>
|
||
public bool HasServerJobProgress => ServerJob is { State: JobState.Running, ProgressTotal: > 0 };
|
||
|
||
/// <summary>Fraction of the server job done (0–1).</summary>
|
||
public double ServerJobProgress => ServerJob is { State: JobState.Running, ProgressTotal: > 0 } job ? (double)job.ProgressDone / job.ProgressTotal : 0;
|
||
|
||
/// <summary>Cancels the server job (BRIEF §11.5: cancel is always available); the toast that follows offers Retry.</summary>
|
||
public ICommand CancelServerJobCommand => _cancelServerJob;
|
||
|
||
public string? CancelServerJobToolTip => IsRemote ? Loc.L("Tooltips.Editor.ServerJob.Cancel", ServerName) : null;
|
||
|
||
/// <summary>The Export "Sync" option: available, or why not.</summary>
|
||
public ExportSyncGate SyncGate { get => _syncGate; private set => Set(ref _syncGate, value); }
|
||
|
||
/// <summary>The server job the header shows (<see cref="SequenceJobs.Shown"/>).</summary>
|
||
private Job? ServerJob => IsRemote ? _jobs.Shown : null;
|
||
|
||
/// <summary>
|
||
/// "3,600 frames · Canon EOS R5 · 2 s interval · 2 h 00 m · 3 warnings"; on the Export tab what the video will be:
|
||
/// "3,600 frames · 24 fps · 2 min 30 s · 3840×2160 · source 8192×5464" (Export.html).
|
||
/// </summary>
|
||
public string Summary => IsExportTab && Plan is { } plan
|
||
? EditorText.OutputSummary(Session.Content, plan)
|
||
: EditorText.Summary(Session.Content, Session.Advice.Findings.Count(f => f.Severity >= FindingSeverity.Warning));
|
||
|
||
public FramesTab Frames { get; }
|
||
|
||
public ExposureTab Exposure { get; }
|
||
|
||
public IReadOnlyList<EditorTab> Tabs { get; }
|
||
|
||
public EditorTab SelectedTab
|
||
{
|
||
get => _selectedTab;
|
||
set
|
||
{
|
||
if (value is null || !Set(ref _selectedTab, value)) return;
|
||
OnPropertyChanged(nameof(IsExportTab));
|
||
OnPropertyChanged(nameof(Summary));
|
||
OnToolsChanged();
|
||
// 1:1 is not a tool of the Export tab (Export.html): its picture fits the view.
|
||
if (IsExportTab) ZoomMode = PreviewZoomMode.Fit;
|
||
UpdateInfo();
|
||
RequestPreview(immediate: true);
|
||
CommandManager.InvalidateRequerySuggested();
|
||
}
|
||
}
|
||
|
||
/// <summary>The Export tab swaps the preview tools for compare, crop mask and a 10-second preview (Export.html).</summary>
|
||
public bool IsExportTab => SelectedTab is ExportTab;
|
||
|
||
// ---- Frames ----
|
||
|
||
public int FrameCount => Session.Content.Count;
|
||
|
||
/// <summary>The frame on screen; the filmstrip, the charts and the frame table seek it.</summary>
|
||
public int CurrentIndex
|
||
{
|
||
get => _currentIndex;
|
||
set
|
||
{
|
||
var clamped = FrameCount == 0 ? 0 : int.Clamp(value, 0, FrameCount - 1);
|
||
if (!Set(ref _currentIndex, clamped)) return;
|
||
foreach (var tab in Tabs) tab.OnCurrentFrameChanged();
|
||
UpdateInfo();
|
||
RequestPreview(immediate: _player.IsEnabled);
|
||
}
|
||
}
|
||
|
||
public IThumbnailSource Thumbnails { get => _thumbnails; private set => Set(ref _thumbnails, value); }
|
||
|
||
/// <summary>Exposure steps (as received), warnings and keyframes above the filmstrip.</summary>
|
||
public IReadOnlyList<TimelineMarker> Markers { get => _markers; private set => Set(ref _markers, value); }
|
||
|
||
// ---- Preview ----
|
||
|
||
public ImageSource? PreviewImage { get => _previewImage; private set => Set(ref _previewImage, value); }
|
||
|
||
public ImageSource? OriginalImage { get => _originalImage; private set => Set(ref _originalImage, value); }
|
||
|
||
/// <summary>Luma waveform of the corrected preview (Exposure tab), as an alpha mask drawn in the accent colour.</summary>
|
||
public ImageSource? WaveformImage { get => _waveformImage; private set => Set(ref _waveformImage, value); }
|
||
|
||
/// <summary>One sentence over the preview when the frame on screen cannot be shown; null otherwise.</summary>
|
||
public string? PreviewMessage { get => _previewMessage; private set => Set(ref _previewMessage, value); }
|
||
|
||
public bool IsCompare { get => _isCompare; set => Set(ref _isCompare, value); }
|
||
|
||
/// <summary>The Vectors tool (kept while the Export tab hides it, shown again on the other tabs).</summary>
|
||
public bool ShowVectors
|
||
{
|
||
get => _showVectors;
|
||
set
|
||
{
|
||
if (!Set(ref _showVectors, value)) return;
|
||
OnPropertyChanged(nameof(ViewVectors));
|
||
RequestPreview(immediate: true);
|
||
}
|
||
}
|
||
|
||
public bool ShowRegions
|
||
{
|
||
get => _showRegions;
|
||
set
|
||
{
|
||
if (!Set(ref _showRegions, value)) return;
|
||
OnPropertyChanged(nameof(ViewRegions));
|
||
RequestPreview(immediate: true);
|
||
}
|
||
}
|
||
|
||
/// <summary>The Crop mask tool of the current tab: the Export tab has its own, on by default (Export.html).</summary>
|
||
public bool ShowCropMask
|
||
{
|
||
get => IsExportTab ? _exportCropMask : _showCropMask;
|
||
set
|
||
{
|
||
if (value == ShowCropMask) return;
|
||
if (IsExportTab) _exportCropMask = value;
|
||
else _showCropMask = value;
|
||
OnPropertyChanged();
|
||
RequestPreview(immediate: true);
|
||
}
|
||
}
|
||
|
||
/// <summary>Overlays the viewport draws: only tools that have a button on the current tab.</summary>
|
||
public bool ViewVectors => ShowVectors && !IsExportTab;
|
||
|
||
public bool ViewRegions => ShowRegions && !IsExportTab;
|
||
|
||
/// <summary>The crop mask shows "how it will look" (outside hidden) instead of "what I lose" (outside dimmed).</summary>
|
||
public bool IsCropResult
|
||
{
|
||
get => _cropMaskMode == CropMaskMode.ShowResult;
|
||
set
|
||
{
|
||
if (!Set(ref _cropMaskMode, value ? CropMaskMode.ShowResult : CropMaskMode.ShowLoss, nameof(CropMaskMode))) return;
|
||
OnPropertyChanged();
|
||
OnPropertyChanged(nameof(CropModeToolTip));
|
||
}
|
||
}
|
||
|
||
public CropMaskMode CropMaskMode => _cropMaskMode;
|
||
|
||
public string CropModeToolTip => Loc.L(IsCropResult ? "Tooltips.Editor.Preview.CropLoss" : "Tooltips.Editor.Preview.CropResult");
|
||
|
||
public PreviewZoomMode ZoomMode
|
||
{
|
||
get => _zoomMode;
|
||
set
|
||
{
|
||
if (!Set(ref _zoomMode, value)) return;
|
||
OnPropertyChanged(nameof(IsActualSize));
|
||
// Fit renders at the view's size; 1:1 and wheel zoom need the source's own pixels.
|
||
RequestPreview(immediate: true);
|
||
}
|
||
}
|
||
|
||
/// <summary>The 1:1 tool (raw pixels).</summary>
|
||
public bool IsActualSize
|
||
{
|
||
get => _zoomMode == PreviewZoomMode.Actual;
|
||
set => ZoomMode = value ? PreviewZoomMode.Actual : PreviewZoomMode.Fit;
|
||
}
|
||
|
||
public Rect CropRect { get => _cropRect; private set => Set(ref _cropRect, value); }
|
||
|
||
public IReadOnlyList<Point>? RegionBoundary { get => _regionBoundary; private set => Set(ref _regionBoundary, value); }
|
||
|
||
public IReadOnlyList<MotionVector>? MotionVectors { get => _motionVectors; private set => Set(ref _motionVectors, value); }
|
||
|
||
/// <summary>Gain on vector lengths so a pixel of motion stays visible.</summary>
|
||
public double VectorScale => 4;
|
||
|
||
public string WipeLabel => Loc.L("Editor.Preview.Wipe");
|
||
|
||
/// <summary>Shot data (bottom-right pill): frame, time, exposure; on the Export tab output time and size.</summary>
|
||
public string? InfoText { get; private set; }
|
||
|
||
/// <summary>Long edge of the view in device pixels (the view reports its size); Fit renders at this size.</summary>
|
||
public int PreviewEdge
|
||
{
|
||
get => _previewEdge;
|
||
set
|
||
{
|
||
var rounded = int.Clamp((value + 63) / 64 * 64, 256, 4096);
|
||
if (Set(ref _previewEdge, rounded) && _zoomMode == PreviewZoomMode.Fit) RequestPreview();
|
||
}
|
||
}
|
||
|
||
/// <summary>Long edge the preview renders at: the view's in Fit, the source's own when zoomed (1:1 shows raw pixels).</summary>
|
||
public int RenderEdge => _zoomMode == PreviewZoomMode.Fit || SourceEdge <= 0 ? PreviewEdge : SourceEdge;
|
||
|
||
private int SourceEdge => int.Max(Session.Content.SourceWidth, Session.Content.SourceHeight);
|
||
|
||
// ---- Analysis, plan, jobs ----
|
||
|
||
/// <summary>The corrections of the current exposure settings, once prepared (target curves).</summary>
|
||
public RenderCorrections? Corrections { get => _correctionsValue; private set => Set(ref _correctionsValue, value); }
|
||
|
||
/// <summary>What the project's export will produce (size, time, encoder, name).</summary>
|
||
public ExportPlan? Plan { get => _plan; private set => Set(ref _plan, value); }
|
||
|
||
/// <summary>An analysis or render of this sequence is running: settings wait, viewing tools stay live.</summary>
|
||
public bool IsBusy => _jobs.IsBusy;
|
||
|
||
/// <summary>A deep analysis of this sequence is queued, running or paused (Analyse would queue it twice).</summary>
|
||
public bool IsAnalysing => _jobs.IsAnalysing;
|
||
|
||
/// <summary>
|
||
/// The project is shown, not changed: a server sequence for an account without the Edit right, or one whose frames
|
||
/// changed on the server (<see cref="IsOutdated"/>).
|
||
/// </summary>
|
||
public bool IsReadOnly => Session.IsReadOnly;
|
||
|
||
/// <summary>The frames on the server are no longer the proxies this PC shows: nothing is saved or rendered until they are fetched again.</summary>
|
||
public bool IsOutdated => Session.Source.IsOutdated;
|
||
|
||
/// <summary>Changes to the project wait (a job runs) or are not allowed (<see cref="IsReadOnly"/>); viewing tools stay live.</summary>
|
||
public bool IsLocked => IsReadOnly || IsBusy;
|
||
|
||
public bool IsEditable => !IsLocked;
|
||
|
||
/// <summary>Why the controls that change the project are disabled; null while they are not.</summary>
|
||
public string? LockedToolTip => IsOutdated ? Loc.L("Tooltips.Editor.Outdated", ServerName)
|
||
: IsReadOnly ? Loc.L("Tooltips.Editor.ReadOnly")
|
||
: IsBusy ? Loc.L("Tooltips.Editor.Busy")
|
||
: null;
|
||
|
||
/// <summary>What Analyse does and costs ("about 6 min" from the speed of the last analysis), or why it is disabled.</summary>
|
||
public string AnalyseToolTip => LockedToolTip
|
||
?? (IsAnalysing ? Loc.L("Tooltips.Editor.AnalyseQueued")
|
||
: _analysisSecondsPerFrame is { } perFrame ? Loc.L("Tooltips.Editor.AnalyseCost", Formatting.Duration(TimeSpan.FromSeconds(perFrame * FrameCount)))
|
||
: Loc.L("Tooltips.Editor.Analyse"));
|
||
|
||
public string OptimiseAllToolTip => LockedToolTip ?? Loc.L("Tooltips.Editor.OptimiseAll");
|
||
|
||
public bool IsPlaying => _player.IsEnabled;
|
||
|
||
public string PlayToolTip => Loc.L(IsPlaying ? "Tooltips.Editor.Pause" : "Tooltips.Editor.Play");
|
||
|
||
public string PlayName => Loc.L(IsPlaying ? "Editor.Preview.Pause" : "Editor.Preview.Play");
|
||
|
||
/// <summary>"Preview 10 s" (Export tab): plays ten seconds of the video's frames, or pauses them.</summary>
|
||
public string ClipToolTip => Loc.L(IsPlaying ? "Tooltips.Editor.Pause" : "Tooltips.Editor.Preview.Clip");
|
||
|
||
// ---- Render ----
|
||
|
||
public IReadOnlyList<Choice> RenderChoices { get; }
|
||
|
||
/// <summary>
|
||
/// The export preset the Render button renders: choosing another one sets it in the project, or, for an account that
|
||
/// may render but not edit (<see cref="IsReadOnly"/>), only in this editor.
|
||
/// </summary>
|
||
public Choice? SelectedRenderChoice
|
||
{
|
||
get
|
||
{
|
||
var id = (IsReadOnly ? _renderPreset : null) ?? Plan?.Export.PresetId ?? EditorSettings.ExportPreset.Effective(Session.Project, null);
|
||
return RenderChoices.FirstOrDefault(c => string.Equals((string)c.Value, id, StringComparison.OrdinalIgnoreCase));
|
||
}
|
||
set
|
||
{
|
||
if (value?.Value is not string id) return;
|
||
if (!IsReadOnly)
|
||
{
|
||
Session.Edit(p => p with { Export = p.Export with { Preset = p.Export.Preset.WithManual(id), Overrides = new ExportOverrides() } });
|
||
return;
|
||
}
|
||
if (string.Equals(id, _renderPreset, StringComparison.OrdinalIgnoreCase)) return;
|
||
_renderPreset = id;
|
||
OnPropertyChanged();
|
||
RequestPlan();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// The export Render renders: the project's, or with the preset chosen in the Render menu of a read-only session
|
||
/// (the project keeps its own; the batch stays the project's).
|
||
/// </summary>
|
||
public ExportSettings RenderExport => IsReadOnly && _renderPreset is { } id
|
||
? Session.ExportFor(id) with { Batch = Session.Project.Export.Batch }
|
||
: Session.Project.Export;
|
||
|
||
/// <summary>"Render · 4K archive".</summary>
|
||
public string RenderText => Loc.L("Editor.Render", Plan?.PresetName ?? string.Empty);
|
||
|
||
public string RenderToolTip => RenderBlockedReason ?? (IsRemote ? Loc.L("Tooltips.Editor.RenderServer", ServerName) : Loc.L("Tooltips.Editor.Render"));
|
||
|
||
/// <summary>The ▾ part of Render: another preset for the project, or only for this render when the project cannot change.</summary>
|
||
public string RenderChoiceToolTip => Loc.L(IsReadOnly ? "Tooltips.Editor.RenderChoiceReadOnly" : "Tooltips.Editor.RenderChoice");
|
||
|
||
public bool CanRender => RenderBlockedReason is null;
|
||
|
||
/// <summary>Why the project's export cannot be rendered now, localised; null when it can.</summary>
|
||
public string? RenderBlockedReason => Plan switch
|
||
{
|
||
_ when IsRemote && !Shell.Services.Sync.CanRender => Loc.L("Tooltips.Editor.Render.NoRight", ServerName),
|
||
_ when IsOutdated => Loc.L("Tooltips.Editor.Outdated", ServerName),
|
||
null when _planned => Loc.L("Tooltips.Editor.Render.NoSource"),
|
||
null => Loc.L("Tooltips.Editor.Render.Planning"),
|
||
{ NameError: { } error } => Loc.L("Tooltips.Editor.Render.NameError", error),
|
||
// The server picks its encoder (a render node, or its managed fallback): this PC's encoders do not decide.
|
||
{ Setup.Encoder.IsReady: false } plan when !IsRemote => plan.Setup.Encoder.Notices.LastOrDefault() is { } notice
|
||
? Loc.L(notice.Key, [.. notice.Args])
|
||
: Loc.L("Editor.Export.Unavailable"),
|
||
{ Setup.Plan.Frames.Count: 0 } => Loc.L("Tooltips.Editor.Render.NoFrames"),
|
||
_ => null,
|
||
};
|
||
|
||
public ICommand BackCommand { get; }
|
||
public ICommand AnalyseCommand => _analyse;
|
||
public ICommand OptimiseAllCommand { get; }
|
||
public ICommand RenderCommand => _render;
|
||
public ICommand UndoCommand { get; }
|
||
public ICommand RedoCommand { get; }
|
||
public ICommand PlayCommand { get; }
|
||
public ICommand PreviewClipCommand { get; }
|
||
public ICommand StepBackCommand { get; }
|
||
public ICommand StepForwardCommand { get; }
|
||
public ICommand FitCommand { get; }
|
||
public ICommand ActualSizeCommand { get; }
|
||
|
||
/// <summary>Exposure steps of the sequence: as received by the pixels when the deep analysis measured them, else from the metadata.</summary>
|
||
public static IReadOnlyList<TimelineMarker> StepMarkers(SequenceContent content)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(content);
|
||
if (content.Deep is { } deep)
|
||
{
|
||
return [.. deep.Exposure.Steps.Select(s => new TimelineMarker(s.Index, s.Received ? MarkerKind.ExposureStep : MarkerKind.Warning,
|
||
Loc.L(s.Received ? "Editor.Marker.Step" : "Editor.Marker.StepIgnored", EditorText.Ev(s.MeasuredEv ?? s.ExpectedEv))))];
|
||
}
|
||
return [.. content.Light.ExposureSteps.Select(s => new TimelineMarker(s.Index, MarkerKind.ExposureStep, Loc.L("Editor.Marker.Step", EditorText.Ev(s.DeltaEv))))];
|
||
}
|
||
|
||
/// <summary>"Optimise section": the advisor's heavier decisions, reported in a toast (what changed, from what to what).</summary>
|
||
public void Optimise(AdvisorSection section) => Report(Session.Optimise(section));
|
||
|
||
/// <summary>
|
||
/// Queues a render of the project's export (with its batch presets when <paramref name="batch"/>): on this PC's
|
||
/// engine, or one server job per preset for a server sequence.
|
||
/// </summary>
|
||
public async Task RenderAsync(JobPriority priority, bool batch)
|
||
{
|
||
var export = batch ? RenderExport : RenderExport with { Batch = [] };
|
||
var requests = Session.RenderRequests(export);
|
||
foreach (var request in requests) await Session.SubmitAsync(request with { Priority = priority });
|
||
if (IsRemote) Shell.Notify(Loc.L("Editor.Render.QueuedServer", ServerName), requests[0].Label);
|
||
else Shell.Notify(Loc.L("Editor.Render.Queued"), requests[0].Label, Loc.L("Shell.Nav.Jobs"), Loc.L("Tooltips.Nav.Jobs"), () => Shell.Navigate(SectionKey.Jobs));
|
||
}
|
||
|
||
/// <summary>Re-reads the enabled state of every command (the async ones keep their own event).</summary>
|
||
public void RefreshCommands()
|
||
{
|
||
_analyse.RaiseCanExecuteChanged();
|
||
_render.RaiseCanExecuteChanged();
|
||
_cancelServerJob.RaiseCanExecuteChanged();
|
||
foreach (var tab in Tabs) tab.RefreshCommands();
|
||
CommandManager.InvalidateRequerySuggested();
|
||
}
|
||
|
||
/// <summary>The Exposure tab switched to or from its waveform: the preview measures it.</summary>
|
||
public void OnWaveformNeeded() => RequestPreview(immediate: true);
|
||
|
||
/// <summary>
|
||
/// Completes once the preview, corrections, plan, estimates and filmstrip thumbnails in flight are done (capture and
|
||
/// tests); a finished step may start the next (corrections re-render the preview), so it waits until all are quiet.
|
||
/// </summary>
|
||
/// <returns>False when the work was still going on after <paramref name="timeout"/>.</returns>
|
||
public async Task<bool> WhenIdleAsync(TimeSpan timeout)
|
||
{
|
||
var deadline = DateTime.UtcNow + timeout;
|
||
while (deadline - DateTime.UtcNow is { Ticks: > 0 } left)
|
||
{
|
||
var work = Work();
|
||
try
|
||
{
|
||
await Task.WhenAll(work).WaitAsync(left);
|
||
}
|
||
catch (TimeoutException)
|
||
{
|
||
return false;
|
||
}
|
||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||
{
|
||
// A failed run was already logged by its owner; it counts as done.
|
||
}
|
||
await Task.Delay(50);
|
||
// Quiet means nothing new was scheduled meanwhile (the same tasks) and no thumbnail is loading.
|
||
if (work.SequenceEqual(Work()) && (Thumbnails as EditorThumbnails)?.Pending is null or 0) return true;
|
||
}
|
||
return false;
|
||
|
||
Task[] Work() => [_preview.Idle, _corrections.Idle, _plans.Idle, .. Tabs.OfType<ExportTab>().Select(t => t.EstimatesIdle)];
|
||
}
|
||
|
||
/// <summary>
|
||
/// Loads the jobs of this sequence already queued or running (busy state), the speed of the last analysis and
|
||
/// whether the Export "Sync" option applies.
|
||
/// </summary>
|
||
public async Task AttachJobsAsync()
|
||
{
|
||
await RefreshSyncGateAsync();
|
||
var jobs = await Session.Source.ListJobsAsync(CancellationToken.None);
|
||
foreach (var job in jobs) _jobs.Track(job);
|
||
foreach (var job in jobs.OrderBy(j => j.Finished)) LearnAnalysisSpeed(job);
|
||
OnBusyChanged();
|
||
}
|
||
|
||
/// <summary>Reads again whether this sequence can send its renders to the signed-in server (session, link, rights).</summary>
|
||
public async Task RefreshSyncGateAsync()
|
||
{
|
||
var sync = Shell.Services.Sync;
|
||
var server = sync.Server;
|
||
var linked = !IsRemote && server is not null && sync.Service is { } service
|
||
&& await service.LinkOfAsync(server, Session.Content.Sequence.Id) is not null;
|
||
if (_disposed) return;
|
||
SyncGate = ExportSync.Gate(IsRemote, server, linked, sync.CanUpload);
|
||
foreach (var tab in Tabs) tab.OnSyncGateChanged();
|
||
}
|
||
|
||
public async ValueTask DisposeAsync()
|
||
{
|
||
if (_disposed) return;
|
||
_disposed = true;
|
||
_player.Stop();
|
||
_preview.Dispose();
|
||
_corrections.Dispose();
|
||
_plans.Dispose();
|
||
Session.ProjectChanged -= OnProjectChanged;
|
||
Session.ContentChanged -= OnContentChanged;
|
||
Session.SaveFailed -= OnSaveFailed;
|
||
Session.ConflictFound -= OnConflictFound;
|
||
Session.Source.JobChanged -= OnJobChanged;
|
||
Session.Source.ReadOnlyChanged -= OnReadOnlyChanged;
|
||
Shell.Services.Sync.Changed -= OnSyncChanged;
|
||
foreach (var tab in Tabs) tab.Dispose();
|
||
base.Dispose();
|
||
await Session.DisposeAsync();
|
||
}
|
||
|
||
public override void Dispose() => _ = DisposeAsync().AsTask();
|
||
|
||
// ---- Preview pipeline ----
|
||
|
||
private void RequestPreview(bool immediate = false)
|
||
{
|
||
if (_disposed || FrameCount == 0) return;
|
||
var framing = ShowCropMask || ViewRegions ? PreviewFraming.Source : PreviewFraming.Output;
|
||
var waveform = SelectedTab is ExposureTab { ChartMode: ExposureChart.Waveform };
|
||
var request = new PreviewRequest(CurrentIndex, Session.Project, RenderEdge, framing, ViewVectors, waveform);
|
||
var version = ++_previewVersion;
|
||
_ = RunPreviewAsync(request, version, immediate);
|
||
}
|
||
|
||
private async Task RunPreviewAsync(PreviewRequest request, int version, bool immediate)
|
||
{
|
||
var renderer = _renderer;
|
||
PreviewResult? result = null;
|
||
var ran = false;
|
||
_previewBusy = true;
|
||
try
|
||
{
|
||
await _preview.Schedule(async token =>
|
||
{
|
||
using var frame = await renderer.RenderAsync(request, renderer.Cached(request.Project) ?? renderer.Latest, token);
|
||
if (frame is not null) result = PreviewResult.From(frame);
|
||
ran = true;
|
||
}, immediate);
|
||
}
|
||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||
{
|
||
Log.Warning("editor", $"Preview of frame {request.Index + 1} failed", ex);
|
||
ran = true;
|
||
}
|
||
finally
|
||
{
|
||
if (version == _previewVersion) _previewBusy = false;
|
||
}
|
||
if (version != _previewVersion || _disposed || !ran) return;
|
||
if (result is not { } r)
|
||
{
|
||
// The frame on screen cannot be shown: never leave the previous frame's picture under this frame's shot data.
|
||
PreviewImage = OriginalImage = null;
|
||
MotionVectors = null;
|
||
WaveformImage = null;
|
||
PreviewMessage = Loc.L("Editor.Preview.Unreadable");
|
||
return;
|
||
}
|
||
PreviewMessage = null;
|
||
PreviewImage = r.Corrected;
|
||
OriginalImage = r.Original;
|
||
CropRect = r.Crop;
|
||
RegionBoundary = ViewRegions ? r.Horizon : null;
|
||
MotionVectors = ViewVectors ? r.Vectors : null;
|
||
WaveformImage = r.Waveform;
|
||
}
|
||
|
||
private void RequestCorrections()
|
||
{
|
||
if (_disposed) return;
|
||
var renderer = _renderer;
|
||
var project = Session.Project;
|
||
if (renderer.Cached(project) is { } cached)
|
||
{
|
||
Corrections = cached;
|
||
NotifyCorrections();
|
||
return;
|
||
}
|
||
_ = RunCorrectionsAsync(renderer, project);
|
||
}
|
||
|
||
private async Task RunCorrectionsAsync(PreviewRenderer renderer, ProjectSettings project)
|
||
{
|
||
RenderCorrections? prepared = null;
|
||
try
|
||
{
|
||
await _corrections.Schedule(async token => prepared = await renderer.PrepareAsync(project, token));
|
||
}
|
||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||
{
|
||
Log.Warning("editor", "Preparing the preview corrections failed", ex);
|
||
}
|
||
if (prepared is null || _disposed || !ReferenceEquals(renderer, _renderer) || !ReferenceEquals(project, Session.Project)) return;
|
||
Corrections = prepared;
|
||
NotifyCorrections();
|
||
RequestPreview(immediate: true);
|
||
}
|
||
|
||
private void NotifyCorrections()
|
||
{
|
||
foreach (var tab in Tabs) tab.OnCorrectionsChanged();
|
||
}
|
||
|
||
private void RequestPlan() => _ = RunPlanAsync(Planner, Session.Project, RenderExport, ++_planVersion);
|
||
|
||
private async Task RunPlanAsync(ExportPlanner planner, ProjectSettings project, ExportSettings export, int version)
|
||
{
|
||
ExportPlan? plan = null;
|
||
// A superseded request ends quietly without planning; undo and redo bring back the very same project instance,
|
||
// so only a request that ran may publish (a failed plan publishes null: "nothing to render").
|
||
var ran = false;
|
||
try
|
||
{
|
||
await _plans.Schedule(_ =>
|
||
{
|
||
plan = planner.Plan(project, export);
|
||
ran = true;
|
||
return Task.CompletedTask;
|
||
});
|
||
}
|
||
catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or RenderException)
|
||
{
|
||
Log.Warning("editor", "Planning the export failed", ex);
|
||
ran = true;
|
||
}
|
||
// A later request (a new project, another render choice) owns the plan.
|
||
if (!ran || _disposed || !ReferenceEquals(planner, Planner) || version != _planVersion) return;
|
||
_planned = true;
|
||
Plan = plan;
|
||
OnRenderStateChanged();
|
||
OnPropertyChanged(nameof(SelectedRenderChoice));
|
||
OnPropertyChanged(nameof(Summary));
|
||
foreach (var tab in Tabs) tab.OnPlanChanged();
|
||
UpdateInfo();
|
||
RefreshCommands();
|
||
}
|
||
|
||
// ---- Playback ----
|
||
|
||
private void TogglePlay(int limit)
|
||
{
|
||
if (_player.IsEnabled)
|
||
{
|
||
_player.Stop();
|
||
}
|
||
else
|
||
{
|
||
if (CurrentIndex >= FrameCount - 1) CurrentIndex = 0;
|
||
_playLimit = limit;
|
||
var fps = double.Clamp(Plan?.Setup.Plan.FrameRate.Value ?? 24, 5, 30);
|
||
_player.Interval = TimeSpan.FromSeconds(1 / fps);
|
||
_player.Start();
|
||
}
|
||
OnPropertyChanged(nameof(IsPlaying));
|
||
OnPropertyChanged(nameof(PlayToolTip));
|
||
OnPropertyChanged(nameof(PlayName));
|
||
OnPropertyChanged(nameof(ClipToolTip));
|
||
}
|
||
|
||
/// <summary>Next frame while playing; frames are skipped rather than queued when the preview is slower than the clock.</summary>
|
||
private void Advance()
|
||
{
|
||
if (_previewBusy) return;
|
||
if (CurrentIndex >= FrameCount - 1 || (_playLimit > 0 && --_playLimit == 0))
|
||
{
|
||
TogglePlay(0);
|
||
return;
|
||
}
|
||
CurrentIndex++;
|
||
}
|
||
|
||
/// <summary>Source frames that make ten seconds of video.</summary>
|
||
private int ClipFrames()
|
||
{
|
||
if (Plan is not { } plan || plan.Setup.Plan.DurationSeconds <= 0) return 240;
|
||
return int.Max(1, (int)(plan.Setup.Plan.IncludedFrames.Count * double.Min(1, 10 / plan.Setup.Plan.DurationSeconds)));
|
||
}
|
||
|
||
// ---- Changes ----
|
||
|
||
private void OnToolsChanged()
|
||
{
|
||
OnPropertyChanged(nameof(ShowCropMask));
|
||
OnPropertyChanged(nameof(ViewVectors));
|
||
OnPropertyChanged(nameof(ViewRegions));
|
||
}
|
||
|
||
private void OnProjectChanged()
|
||
{
|
||
foreach (var tab in Tabs) tab.Refresh();
|
||
UpdateMarkers();
|
||
RequestCorrections();
|
||
RequestPlan();
|
||
RequestPreview();
|
||
RefreshCommands();
|
||
}
|
||
|
||
private void OnContentChanged()
|
||
{
|
||
_renderer = new PreviewRenderer(Session.Content, Session.Source);
|
||
Planner = new ExportPlanner(Session.Content, _encoders, _host);
|
||
if (Thumbnails.Count != FrameCount) Thumbnails = new EditorThumbnails(Session.Source, FrameCount);
|
||
Corrections = null;
|
||
foreach (var tab in Tabs) tab.OnContentChanged();
|
||
OnPropertyChanged(nameof(Summary));
|
||
OnPropertyChanged(nameof(FrameCount));
|
||
CurrentIndex = int.Min(CurrentIndex, int.Max(0, FrameCount - 1));
|
||
UpdateMarkers();
|
||
RequestCorrections();
|
||
RequestPlan();
|
||
RequestPreview(immediate: true);
|
||
}
|
||
|
||
/// <summary>The first failure of a run of failed saves (they are retried until one succeeds); Retry tries at once.</summary>
|
||
private void OnSaveFailed(Exception ex) => Shell.ShowToast(new Toast
|
||
{
|
||
Title = Loc.L("Editor.Save.Failed"),
|
||
Message = ex.Message,
|
||
IsError = true,
|
||
ActionText = Loc.L("Editor.Save.Retry"),
|
||
ActionToolTip = Loc.L("Tooltips.Editor.Save.Retry"),
|
||
ActionCommand = new RelayCommand(() => _ = Session.FlushAsync()),
|
||
});
|
||
|
||
/// <summary>
|
||
/// A save met settings someone changed to other values on the server (BRIEF §14.5 "explicit conflict prompt"): the
|
||
/// dialog lists them; Keep mine overwrites the server's, Take server (the safe default, also on Esc) shows theirs as
|
||
/// one undoable step. Saving waits until then.
|
||
/// </summary>
|
||
private void OnConflictFound(ProjectConflictException conflict) => _dispatcher.BeginInvoke(() =>
|
||
{
|
||
if (_disposed || Session.Conflict is null) return;
|
||
var keepMine = Shell.Services.Dialogs.Confirm(ConflictRequest(conflict, Title, ServerName ?? string.Empty));
|
||
if (!_disposed) Session.ResolveConflict(keepMine);
|
||
});
|
||
|
||
/// <summary>The conflict prompt: what differs (the editor's value, then the server's) and what each choice replaces.</summary>
|
||
internal static ConfirmRequest ConflictRequest(ProjectConflictException conflict, string title, string server) => new()
|
||
{
|
||
Heading = Loc.L("Editor.Conflict.Title", server),
|
||
Message = Loc.L("Editor.Conflict.Message", title, server),
|
||
Detail = string.Join(Environment.NewLine, SyncText.Differences(conflict.Differences, 10)),
|
||
DetailToolTip = Loc.L("Tooltips.Server.Conflict.Detail"),
|
||
SafeText = Loc.L("Server.Sync.Action.TakeServer"),
|
||
SafeToolTip = Loc.L("Tooltips.Editor.Conflict.TakeServer", server),
|
||
ActionText = Loc.L("Server.Sync.Action.KeepMine"),
|
||
ActionToolTip = Loc.L("Tooltips.Editor.Conflict.KeepMine", server),
|
||
IsDestructive = true,
|
||
};
|
||
|
||
private void UpdateMarkers()
|
||
{
|
||
var content = Session.Content;
|
||
var markers = new List<TimelineMarker>(StepMarkers(content));
|
||
foreach (var finding in Session.Advice.Findings.Where(f => f.Severity >= FindingSeverity.Warning))
|
||
{
|
||
var text = Loc.L(finding.Key, finding.Arguments);
|
||
markers.AddRange(finding.Frames.Take(64).Select(i => new TimelineMarker(i, finding.Severity == FindingSeverity.Error ? MarkerKind.Error : MarkerKind.Warning, text)));
|
||
}
|
||
markers.AddRange(Session.Project.Camera.Keyframes.Select(k => new TimelineMarker(k.Frame, MarkerKind.Keyframe, Loc.L("Editor.Marker.Keyframe"))));
|
||
if (!markers.SequenceEqual(Markers)) Markers = markers;
|
||
}
|
||
|
||
private void UpdateInfo()
|
||
{
|
||
if (FrameCount == 0)
|
||
{
|
||
InfoText = null;
|
||
}
|
||
else if (IsExportTab && Plan is { } plan)
|
||
{
|
||
var settings = plan.Setup.Encoder.Settings;
|
||
var position = plan.Setup.Plan.DurationSeconds * CurrentIndex / double.Max(1, FrameCount - 1);
|
||
InfoText = string.Join(" · ", EditorText.Position(CurrentIndex, FrameCount),
|
||
Loc.L("Editor.Preview.OutputTime", EditorText.Clock(position), EditorText.Clock(plan.Setup.Plan.DurationSeconds)),
|
||
Loc.L("Editor.Preview.OutputSize", EditorText.Size(settings.Width, settings.Height)));
|
||
}
|
||
else
|
||
{
|
||
InfoText = EditorText.Shot(Session.Content.Frames[CurrentIndex], CurrentIndex, FrameCount);
|
||
}
|
||
OnPropertyChanged(nameof(InfoText));
|
||
}
|
||
|
||
private void OptimiseAll() => Report(Session.OptimiseAll());
|
||
|
||
private void Report(IReadOnlyList<SettingChange> changes)
|
||
{
|
||
if (changes.Count == 0)
|
||
{
|
||
Shell.Notify(Loc.L("Editor.Optimise.Nothing"));
|
||
return;
|
||
}
|
||
Shell.Notify(Loc.Plural("Editor.Optimise.Done", changes.Count), string.Join(Environment.NewLine, changes.Select(EditorText.Change)));
|
||
}
|
||
|
||
private async Task AnalyseAsync()
|
||
{
|
||
await Session.SubmitAsync(Session.AnalysisRequest());
|
||
// The Jobs screen lists this PC's jobs; a server's analysis shows beside the title.
|
||
if (IsRemote) Shell.Notify(Loc.L("Editor.Analyse.QueuedServer", ServerName), Title);
|
||
else Shell.Notify(Loc.L("Editor.Analyse.Queued"), Title, Loc.L("Shell.Nav.Jobs"), Loc.L("Tooltips.Nav.Jobs"), () => Shell.Navigate(SectionKey.Jobs));
|
||
}
|
||
|
||
private void OnJobChanged(Job job)
|
||
{
|
||
if (!_jobs.Concerns(job)) return;
|
||
_dispatcher.BeginInvoke(() =>
|
||
{
|
||
if (_disposed) return;
|
||
var wasActive = _jobs.IsActive(job.Id);
|
||
// Progress alone (several times a second on a server) only moves the server job's text and bar.
|
||
if (_jobs.Track(job)) OnBusyChanged();
|
||
else OnServerJobChanged();
|
||
LearnAnalysisSpeed(job);
|
||
if (!wasActive) return;
|
||
if (job.State == JobState.Done) _ = ReloadAsync(job.Kind == JobKind.Render ? job : null);
|
||
else if (IsRemote && job.State is JobState.Failed or JobState.Cancelled) ReportServerJobEnded(job);
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// The frames of the server sequence changed while it is open (any thread): everything that changes the project is
|
||
/// disabled and says why, and a toast offers to fetch the proxies again.
|
||
/// </summary>
|
||
private void OnReadOnlyChanged() => _dispatcher.BeginInvoke(() =>
|
||
{
|
||
if (_disposed) return;
|
||
OnPropertyChanged(nameof(IsReadOnly));
|
||
OnPropertyChanged(nameof(IsOutdated));
|
||
OnPropertyChanged(nameof(RemoteText));
|
||
OnPropertyChanged(nameof(RemoteToolTip));
|
||
OnPropertyChanged(nameof(RenderChoiceToolTip));
|
||
OnRenderStateChanged();
|
||
OnBusyChanged();
|
||
if (!IsOutdated) return;
|
||
var (id, title) = (Session.Content.Sequence.Id, Title);
|
||
Shell.ShowToast(new Toast
|
||
{
|
||
Title = Loc.L("Editor.Outdated.Title", ServerName),
|
||
Message = title,
|
||
ActionText = Loc.L("Editor.Outdated.Fetch"),
|
||
ActionToolTip = Loc.L("Tooltips.Editor.Outdated.Fetch"),
|
||
ActionCommand = new AsyncRelayCommand(() => Shell.Sequence.FetchProxiesAsync(id, title)),
|
||
});
|
||
});
|
||
|
||
/// <summary>A server job of this sequence failed or was cancelled: what happened, and Retry (BRIEF §11.5).</summary>
|
||
private void ReportServerJobEnded(Job job)
|
||
{
|
||
var server = ServerName ?? string.Empty;
|
||
Shell.ShowToast(new Toast
|
||
{
|
||
Title = Loc.L("Editor.ServerJob.Ended", JobText.Kind(job.Kind), Loc.L("Jobs.State." + job.State), server),
|
||
Message = job.Error ?? job.TargetLabel ?? Title,
|
||
IsError = job.State == JobState.Failed,
|
||
ActionText = Loc.L("Jobs.Action.Retry"),
|
||
ActionToolTip = Loc.L("Tooltips.Editor.ServerJob.Retry", server),
|
||
ActionCommand = new AsyncRelayCommand(() => Session.Source.RetryJobAsync(job.Id, CancellationToken.None), () => !_disposed),
|
||
});
|
||
}
|
||
|
||
private async Task CancelServerJobAsync()
|
||
{
|
||
if (ServerJob is { } job) await Session.Source.CancelJobAsync(job.Id, CancellationToken.None);
|
||
}
|
||
|
||
/// <summary>The session, the sync links or the rights changed (any thread): whether the Export "Sync" option applies.</summary>
|
||
private void OnSyncChanged() => _dispatcher.BeginInvoke(() => _ = OnSyncChangedAsync());
|
||
|
||
private async Task OnSyncChangedAsync()
|
||
{
|
||
if (_disposed) return;
|
||
try
|
||
{
|
||
await RefreshSyncGateAsync();
|
||
}
|
||
catch (Data.DatabaseException ex)
|
||
{
|
||
Log.Warning("editor", "The sync state of the sequence could not be read", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>Seconds per frame of the latest finished deep analysis (any sequence): the cost Analyse announces.</summary>
|
||
private void LearnAnalysisSpeed(Job job)
|
||
{
|
||
if (job is { Kind: JobKind.DeepAnalyse, State: JobState.Done, ItemsPerSecond: > 0 and var speed }) _analysisSecondsPerFrame = 1 / speed;
|
||
}
|
||
|
||
/// <summary>Reads the sequence again after a job; a finished render offers its follow-ups (BRIEF §11.5).</summary>
|
||
private async Task ReloadAsync(Job? render)
|
||
{
|
||
try
|
||
{
|
||
await Session.ReloadAsync();
|
||
}
|
||
catch (Exception ex) when (ex is System.IO.IOException or System.IO.InvalidDataException or InvalidOperationException or UnauthorizedAccessException
|
||
or System.Net.Http.HttpRequestException or Protocol.Client.TitanoApiException or OperationCanceledException)
|
||
{
|
||
// A server that does not answer in time cancels the request (OperationCanceledException).
|
||
Shell.ReportError(ex);
|
||
return;
|
||
}
|
||
if (render is null || _disposed) return;
|
||
// A server's render stays on the server: nothing to show in Explorer.
|
||
var output = Session.Content.Renders.FirstOrDefault(r => render.Started is not { } started || r.Created >= started)?.OutputPath is { Length: > 0 } path
|
||
? path
|
||
: null;
|
||
Shell.ShowToast(new Toast
|
||
{
|
||
Title = Loc.L("Editor.Render.Done"),
|
||
Message = output is null ? render.TargetLabel : System.IO.Path.GetFileName(output),
|
||
ActionText = output is null ? null : Loc.L("Editor.Render.Reveal"),
|
||
ActionToolTip = output is null ? null : Loc.L("Tooltips.Editor.Render.Reveal"),
|
||
ActionCommand = output is null ? null : new RelayCommand(() => ShellActions.Reveal(output)),
|
||
});
|
||
}
|
||
|
||
private void OnBusyChanged()
|
||
{
|
||
OnPropertyChanged(nameof(IsBusy));
|
||
OnPropertyChanged(nameof(IsAnalysing));
|
||
OnPropertyChanged(nameof(IsLocked));
|
||
OnPropertyChanged(nameof(IsEditable));
|
||
OnPropertyChanged(nameof(LockedToolTip));
|
||
OnPropertyChanged(nameof(AnalyseToolTip));
|
||
OnPropertyChanged(nameof(OptimiseAllToolTip));
|
||
OnServerJobChanged();
|
||
foreach (var tab in Tabs) tab.OnBusyChanged();
|
||
RefreshCommands();
|
||
}
|
||
|
||
private void OnServerJobChanged()
|
||
{
|
||
if (!IsRemote) return;
|
||
OnPropertyChanged(nameof(ServerJobText));
|
||
OnPropertyChanged(nameof(ServerJobToolTip));
|
||
OnPropertyChanged(nameof(ServerJobKind));
|
||
OnPropertyChanged(nameof(HasServerJobProgress));
|
||
OnPropertyChanged(nameof(ServerJobProgress));
|
||
_cancelServerJob.RaiseCanExecuteChanged();
|
||
}
|
||
|
||
/// <summary>What Render renders and whether it can (the plan, the rights or the frames changed).</summary>
|
||
private void OnRenderStateChanged()
|
||
{
|
||
OnPropertyChanged(nameof(RenderText));
|
||
OnPropertyChanged(nameof(RenderToolTip));
|
||
OnPropertyChanged(nameof(RenderBlockedReason));
|
||
OnPropertyChanged(nameof(CanRender));
|
||
}
|
||
|
||
/// <summary>A rendered preview as frozen bitmaps and overlays in picture pixels (built off the UI thread).</summary>
|
||
private sealed record PreviewResult(ImageSource Corrected, ImageSource Original, Rect Crop, IReadOnlyList<Point> Horizon, IReadOnlyList<MotionVector> Vectors,
|
||
ImageSource? Waveform)
|
||
{
|
||
public static PreviewResult From(PreviewFrame frame) => new(
|
||
EditorImages.ToBitmap(frame.Corrected),
|
||
EditorImages.ToBitmap(frame.Original),
|
||
new Rect(frame.Crop.X, frame.Crop.Y, frame.Crop.Width, frame.Crop.Height),
|
||
[.. frame.Horizon.Select(p => new Point(p.X, p.Y))],
|
||
[.. frame.Vectors.Select(v => new MotionVector(v.X, v.Y, v.Dx, v.Dy))],
|
||
frame.Waveform is { } waveform ? EditorImages.ToMask(waveform) : null);
|
||
}
|
||
}
|