using System.Globalization;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using Chronos.App.Archiving;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.App.Server;
using Chronos.App.Shell;
using Chronos.App.Workspace;
using Chronos.Core.Advice;
using Chronos.Core.Analysis;
using Chronos.Core.Diagnostics;
using Chronos.Core.Export;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Video;
using Chronos.Ui.Controls;
using Chronos.Ui.Mvvm;
using Chronos.Ui.Visuals;
namespace Chronos.App.Editor;
///
/// The 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 steps of the Edit path from the analysis to the export (owner review 1, R7:
/// each with where it stands, Previous and Next, Alt+1…8, Ctrl+PgUp/PgDn) 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.
///
public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
{
public const int DefaultPreviewEdge = 1280;
private static readonly TimeSpan Debounce = TimeSpan.FromMilliseconds(150);
private readonly IReadOnlyList _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;
/// The frame on screen: it stays on screen when frames are inserted or removed before it.
private Guid _currentFrameId;
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;
/// The Camera step's Crop mask, on by default: the crop box is edited there (owner review 1, R5).
private bool _cameraCropMask = true;
private CropMaskMode _cropMaskMode = CropMaskMode.ShowLoss;
private PreviewZoomMode _zoomMode = PreviewZoomMode.Fit;
private Rect _cropRect = new(0, 0, 1, 1);
private Rect _editCrop = new(0, 0, 1, 1);
private double _cropLockRatio = 1;
private bool _isStraightening;
/// The orientation of the camera the picture on screen was framed with (the crop box maps onto that view frame).
private (int Rotate, bool Mirror, bool Flip, double Straighten) _shownOrientation;
private IReadOnlyList? _regionBoundary;
private IReadOnlyList? _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 _markers = [];
private IThumbnailSource _thumbnails;
private int _playLimit;
private double? _analysisSecondsPerFrame;
private ExportSyncGate _syncGate;
private RenderedSettings _rendered;
/// The step shown came from the section's memory of this sequence ().
private bool _stepRestored;
/// The user chose a step since the editor opened.
private bool _stepChosen;
private Workspace.ProjectMenuViewModel? _project;
private bool _disposed;
public EditorViewModel(ShellViewModel shell, EditorSession session, IReadOnlyList 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);
PreviousStepCommand = new RelayCommand(() => MoveStep(-1), () => StepIndex > 0);
NextStepCommand = new RelayCommand(NextStep, () => !SelectedTab.IsLast || Shell.Sequence.ChangeSequenceCommand.CanExecute(null));
GoToStepCommand = new RelayCommand(p =>
{
if (StepFor(p) is { } step) SelectedTab = step;
});
CommitCropCommand = new RelayCommand(CommitCrop, _ => CropEditable);
Analysis = new AnalysisTab(this);
Frames = new FramesTab(this);
Exposure = new ExposureTab(this);
Tabs = [Analysis, Frames, Exposure, new MotionTab(this), new CameraTab(this), new TimeTab(this), new StackTab(this), new ExportTab(this)];
for (var i = 0; i < Tabs.Count; i++)
{
Tabs[i].Number = i + 1;
Tabs[i].IsLast = i == Tabs.Count - 1;
}
_selectedTab = Step(EditPath.Start(null, session.Content));
_selectedTab.IsVisited = true;
_rendered = new RenderedSettings(session.Content.Renders);
_currentIndex = session.Project.HeroFrame is { } hero && (uint)hero < (uint)FrameCount ? hero : 0;
_currentFrameId = FrameId(_currentIndex);
// 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))];
_shownOrientation = OrientationOf(session.Project.Camera);
UpdateCropBox();
session.ProjectChanged += OnProjectChanged;
session.ContentChanged += OnContentChanged;
session.SaveFailed += OnSaveFailed;
session.ConflictFound += OnConflictFound;
session.EditSetAside += OnEditSetAside;
session.Source.JobChanged += OnJobChanged;
session.Source.ReadOnlyChanged += OnReadOnlyChanged;
shell.Services.Sync.Changed += OnSyncChanged;
shell.Services.SequenceReorganized += OnSequenceReorganized;
UpdateMarkers();
RefreshSteps();
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;
/// The title button's accessible name: its visible text (the title), then what it does ("Harbour sunset · Change sequence").
public string ChangeName => Title + Loc.Separator + Loc.L("Editor.Change");
/// The title button's tooltip: the whole title (it may be cut), then what the button does.
public string ChangeToolTip => Title + Loc.Separator + Loc.L("Tooltips.Editor.Change");
/// A server sequence edited with proxies (the header says where it lives).
public bool IsRemote => Session.IsRemote;
///
/// The project menu beside the title (owner review 2, R8) when the sequence is open as a workspace project: its state,
/// Save version, Sync, History and the rest; null otherwise (a server sequence, a capture's library sequence).
///
public Workspace.ProjectMenuViewModel? Project
{
get => _project;
internal set
{
_project?.Dispose();
Set(ref _project, value);
OnPropertyChanged(nameof(HasProject));
}
}
public bool HasProject => _project is not null;
/// The server a server sequence lives on ("Studio NAS"); null for a sequence of this PC.
public string? ServerName => IsRemote ? Shell.Services.Server.ServerName ?? Loc.L("Server.Label.Server") : null;
/// "Studio NAS" beside the title of a server sequence; "Studio NAS · read-only" when it is only shown.
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);
///
/// "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.
///
public string? ServerJobText => ServerJob is { } job ? EditorText.ServerJob(job) : null;
/// Frames, speed and time left of the server job.
public string? ServerJobToolTip => ServerJob is { } job ? EditorText.ServerJobDetail(job, ServerName ?? string.Empty) : null;
/// The glyph of the server job (DESIGN §4): ◐ an analysis running, ● a render running, ○ waiting, paused.
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,
};
/// The server job runs and reports how far it is (its progress bar shows).
public bool HasServerJobProgress => ServerJob is { State: JobState.Running, ProgressTotal: > 0 };
/// Fraction of the server job done (0–1).
public double ServerJobProgress => ServerJob is { State: JobState.Running, ProgressTotal: > 0 } job ? (double)job.ProgressDone / job.ProgressTotal : 0;
/// Cancels the server job (BRIEF §11.5: cancel is always available); the toast that follows offers Retry.
public ICommand CancelServerJobCommand => _cancelServerJob;
public string? CancelServerJobToolTip => IsRemote ? Loc.L("Tooltips.Editor.ServerJob.Cancel", ServerName) : null;
/// The Export "Sync" option: available, or why not.
public ExportSyncGate SyncGate { get => _syncGate; private set => Set(ref _syncGate, value); }
/// The server job the header shows (): never a workspace project's render on this PC or its send.
private Job? ServerJob => IsRemote ? _jobs.ShownAtHome : null;
///
/// "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).
///
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));
/// The first step: what the analyses found and where every other step stands.
public AnalysisTab Analysis { get; }
public FramesTab Frames { get; }
public ExposureTab Exposure { get; }
/// The steps of the Edit path, in order (owner review 1, R7).
public IReadOnlyList Tabs { get; }
/// The step on screen; any step can be chosen at any time, and choosing one marks it visited.
public EditorTab SelectedTab
{
get => _selectedTab;
set
{
if (value is null || !Tabs.Contains(value)) return;
_stepChosen = true;
Select(value);
}
}
/// The Export step swaps the preview tools for compare, crop mask and a 10-second preview (Export.html).
public bool IsExportTab => SelectedTab is ExportTab;
/// The Camera step: its crop mask shows the crop box itself, edited on the picture (owner review 1, R5).
public bool IsCameraTab => SelectedTab is CameraTab;
/// The measuring area takes more of the height than the preview (the Analysis findings, the Export preset cards).
public bool IsTallMeasure => SelectedTab.PrefersTallMeasure;
/// "3 / 8".
public string StepCounter => Loc.L("Editor.Step.Counter", SelectedTab.Number, Tabs.Count);
/// What screen readers hear when the step or its state changes: "Step 3 of 8: Exposure, Auto".
public string StepAnnouncement => Loc.L("Editor.Step.Announce", SelectedTab.Number, Tabs.Count, SelectedTab.Header, SelectedTab.StateText);
/// "Next", "Next sequence" on the last step.
public string NextText => Loc.L(SelectedTab.IsLast ? "Editor.Step.NextSequence" : "Editor.Step.Next");
public string NextToolTip => SelectedTab.IsLast ? Loc.L("Tooltips.Editor.Step.NextSequence") : Loc.L("Tooltips.Editor.Step.Next", Tabs[StepIndex + 1].Header);
public string PreviousToolTip => StepIndex == 0 ? Loc.L("Tooltips.Editor.Step.First") : Loc.L("Tooltips.Editor.Step.Previous", Tabs[StepIndex - 1].Header);
public ICommand PreviousStepCommand { get; }
/// The next step; on the last one, the choice of the next sequence.
public ICommand NextStepCommand { get; }
/// Opens a step, given as the step itself, its number (Alt+n) or its key.
public ICommand GoToStepCommand { get; }
private int StepIndex => SelectedTab.Number - 1;
// ---- Frames ----
public int FrameCount => Session.Content.Count;
/// The frame on screen; the filmstrip, the charts and the frame table seek it.
public int CurrentIndex
{
get => _currentIndex;
set
{
var clamped = FrameCount == 0 ? 0 : int.Clamp(value, 0, FrameCount - 1);
_currentFrameId = FrameId(clamped);
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); }
/// Exposure steps (as received), warnings and keyframes above the filmstrip.
public IReadOnlyList 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); }
/// Luma waveform of the corrected preview (Exposure tab), as an alpha mask drawn in the accent colour.
public ImageSource? WaveformImage { get => _waveformImage; private set => Set(ref _waveformImage, value); }
/// One sentence over the preview when the frame on screen cannot be shown; null otherwise.
public string? PreviewMessage { get => _previewMessage; private set => Set(ref _previewMessage, value); }
public bool IsCompare { get => _isCompare; set => Set(ref _isCompare, value); }
/// The Vectors tool (kept while the Export tab hides it, shown again on the other tabs).
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);
}
}
///
/// The Crop mask tool of the current tab: the Export tab has its own, on by default (Export.html), and so has the Camera
/// step, where the crop box is edited (owner review 1, R5).
///
public bool ShowCropMask
{
get => IsExportTab ? _exportCropMask : IsCameraTab ? _cameraCropMask : _showCropMask;
set
{
if (value == ShowCropMask) return;
if (IsExportTab) _exportCropMask = value;
else if (IsCameraTab) _cameraCropMask = value;
else _showCropMask = value;
OnPropertyChanged();
OnPropertyChanged(nameof(CropModeToolTip));
OnCropEditableChanged();
RequestPreview(immediate: true);
}
}
///
/// The crop box is moved and resized on the preview: the Camera step with its mask on, while the project can change
/// (not read-only, in a newer format, outdated or busy) and the picture on screen shows the project's view frame (after
/// a turn, a mirror, a flip or a straighten, the box waits for the picture it applies to: a drag before it would place
/// a box of the old frame in the new one).
///
public bool CropEditable => IsCameraTab && ShowCropMask && IsEditable && !Session.Project.IsNewerFormat && FrameCount > 0
&& OrientationOf(Session.Project.Camera) == _shownOrientation;
///
/// The crop box the Camera step shows and edits (), normalised in the view frame
/// the Source picture shows; the preview sets it to each finished edit before runs.
///
public Rect EditCrop
{
get => _editCrop;
set
{
if (!Set(ref _editCrop, value)) return;
if (IsCameraTab) OnPropertyChanged(nameof(CropRect));
}
}
/// The crop box's width over its height in normalised units (): the output proportions on the view frame.
public double CropLockRatio { get => _cropLockRatio; private set => Set(ref _cropLockRatio, value); }
///
/// Writes a finished edit of the crop box (, or a normalised ) as the
/// camera crop, locked and with the offsets at 0 (owner review 1, R5): one drag is one undo step, a run of key presses
/// merges into one ("Camera.Crop") that the key release ends.
///
public ICommand CommitCropCommand { get; }
/// The Straighten slider is dragged or has the keyboard: the preview shows a fine grid to level the horizon against.
public bool IsStraightening
{
get => _isStraightening;
set
{
if (Set(ref _isStraightening, value)) OnPropertyChanged(nameof(ShowStraightenGrid));
}
}
/// The straighten grid over the preview (Camera step, while straightening).
public bool ShowStraightenGrid => IsStraightening && IsCameraTab;
/// What a drag on the preview does: the crop box's moves and keys while it is edited there, else compare, pan and zoom.
public string PreviewToolTip => Loc.L(CropEditable ? "Tooltips.Editor.Preview.CropEdit" : "Tooltips.Editor.Preview.View");
/// Overlays the viewport draws: only tools that have a button on the current tab.
public bool ViewVectors => ShowVectors && !IsExportTab;
public bool ViewRegions => ShowRegions && !IsExportTab;
/// The crop mask shows "how it will look" (outside hidden) instead of "what I lose" (outside dimmed).
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;
/// What the crop-result tool switches to, or (disabled while the crop mask is off) why it waits.
public string CropModeToolTip => Loc.L(!ShowCropMask ? "Tooltips.Editor.Preview.CropModeOff"
: 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);
}
}
/// The 1:1 tool (raw pixels).
public bool IsActualSize
{
get => _zoomMode == PreviewZoomMode.Actual;
set => ZoomMode = value ? PreviewZoomMode.Actual : PreviewZoomMode.Fit;
}
///
/// The crop mask: on the Camera step the crop box (), elsewhere the footprint of the frame's output
/// (stabilisation and camera pose included) on the Source picture.
///
public Rect CropRect
{
get => IsCameraTab ? _editCrop : _cropRect;
private set
{
if (_cropRect == value) return;
_cropRect = value;
if (!IsCameraTab) OnPropertyChanged();
}
}
public IReadOnlyList? RegionBoundary { get => _regionBoundary; private set => Set(ref _regionBoundary, value); }
public IReadOnlyList? MotionVectors { get => _motionVectors; private set => Set(ref _motionVectors, value); }
/// Gain on vector lengths so a pixel of motion stays visible.
public double VectorScale => 4;
public string WipeLabel => Loc.L("Editor.Preview.Wipe");
/// Shot data (bottom-right pill): frame, time, exposure; on the Export tab output time and size.
public string? InfoText { get; private set; }
/// Long edge of the view in device pixels (the view reports its size); Fit renders at this size.
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();
}
}
/// Long edge the preview renders at: the view's in Fit, the source's own when zoomed (1:1 shows raw pixels).
public int RenderEdge => _zoomMode == PreviewZoomMode.Fit || SourceEdge <= 0 ? PreviewEdge : SourceEdge;
private int SourceEdge => int.Max(Session.Content.SourceWidth, Session.Content.SourceHeight);
// ---- Analysis, plan, jobs ----
/// The corrections of the current exposure settings, once prepared (target curves).
public RenderCorrections? Corrections { get => _correctionsValue; private set => Set(ref _correctionsValue, value); }
/// What the project's export will produce (size, time, encoder, name).
public ExportPlan? Plan { get => _plan; private set => Set(ref _plan, value); }
/// An analysis or render of this sequence is running: settings wait, viewing tools stay live.
public bool IsBusy => _jobs.IsBusy;
/// A deep analysis of this sequence is queued, running or paused (Analyse would queue it twice).
public bool IsAnalysing => _jobs.IsAnalysing;
///
/// The project is shown, not changed: a server sequence for an account without the Edit right, or one whose frames
/// changed on the server ().
///
public bool IsReadOnly => Session.IsReadOnly;
/// The frames on the server are no longer the proxies this PC shows: nothing is saved or rendered until they are fetched again.
public bool IsOutdated => Session.Source.IsOutdated;
/// Changes to the project wait (a job runs) or are not allowed (); viewing tools stay live.
public bool IsLocked => IsReadOnly || IsBusy;
public bool IsEditable => !IsLocked;
/// Why the controls that change the project are disabled; null while they are not.
public string? LockedToolTip => IsOutdated ? Loc.L("Tooltips.Editor.Outdated", ServerName)
: IsReadOnly ? Loc.L(Session.Source is WorkspaceSequenceSource ? "Errors.Workspace.ReadOnly" : "Tooltips.Editor.ReadOnly")
: IsBusy ? Loc.L("Tooltips.Editor.Busy")
: null;
/// What Analyse does and costs ("about 6 min" from the speed of the last analysis), or why it is disabled.
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");
/// "Preview 10 s" (Export tab): plays ten seconds of the video's frames, or pauses them.
public string ClipToolTip => Loc.L(IsPlaying ? "Tooltips.Editor.Pause" : "Tooltips.Editor.Preview.Clip");
// ---- Render ----
public IReadOnlyList RenderChoices { get; }
///
/// 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 (), only in this editor.
///
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();
}
}
///
/// 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).
///
public ExportSettings RenderExport => IsReadOnly && _renderPreset is { } id
? Session.ExportFor(id) with { Batch = Session.Project.Export.Batch }
: Session.Project.Export;
/// "Render · 4K archive".
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"));
/// The ▾ part of Render: another preset for the project, or only for this render when the project cannot change.
public string RenderChoiceToolTip => Loc.L(IsReadOnly ? "Tooltips.Editor.RenderChoiceReadOnly" : "Tooltips.Editor.RenderChoice");
public bool CanRender => RenderBlockedReason is null;
/// The settings format a render of needs ().
private int RenderFormat =>
Chronos.Data.Processing.ProcessingJobs.RequiredFormat(Session.Project, RenderExport, Session.Content.SourceWidth, Session.Content.SourceHeight);
/// Why the project's export cannot be rendered now, localised; null when it can.
public string? RenderBlockedReason => Plan switch
{
_ when IsRemote && !Shell.Services.Sync.CanRender => Loc.L("Tooltips.Editor.Render.NoRight", ServerName),
// Settings or proportions the server would render otherwise (owner review 1, phase 4: an older server, Send and render too).
// A workspace project rendered on this PC from its originals (phase 5c) renders with this PC's engine: the server's
// format does not decide (sending its version home is held by the workspace's own gate).
_ when IsRemote && Session.Source is not IProjectRenders { RendersLocally: true }
&& ServerJobs.NeedsNewerServer(RenderFormat, Shell.Services.Sync.SettingsFormat) is { } newer => newer,
_ 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; }
/// Exposure steps of the sequence: as received by the pixels when the deep analysis measured them, else from the metadata.
public static IReadOnlyList 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))))];
}
/// "Optimise section": the advisor's heavier decisions, reported in a toast (what changed, from what to what).
public void Optimise(AdvisorSection section) => Report(Session.Optimise(section));
/// The step steps away, stopping at the first and the last (Ctrl+PgUp, Ctrl+PgDn).
public void MoveStep(int delta)
{
var index = StepIndex + delta;
if ((uint)index < (uint)Tabs.Count) SelectedTab = Tabs[index];
}
/// The step on screen and the steps visited, which the section gives back when the sequence opens again in this run.
public StepMemory StepMemory() => new(SelectedTab.Key, [.. Tabs.Where(t => t.IsVisited).Select(t => t.Key)], EditPath.AnalysisStamp(Session.Content));
///
/// Shows the step kept, with its visited marks (before the editor is on screen); a deep
/// analysis that ended while the sequence was closed leaves only that step visited, as it would have with the editor open.
///
public void RestoreSteps(StepMemory memory)
{
ArgumentNullException.ThrowIfNull(memory);
var sameAnalysis = memory.Analysis == EditPath.AnalysisStamp(Session.Content);
foreach (var tab in Tabs) tab.IsVisited = sameAnalysis && memory.Visited.Contains(tab.Key);
if (Tabs.FirstOrDefault(t => t.Key == memory.Step) is not { } step) return;
_stepRestored = true;
Select(step);
}
///
/// Queues a render of the project's export (with its batch presets when ): on this PC's
/// engine, or one server job per preset for a server sequence.
///
public async Task RenderAsync(JobPriority priority, bool batch)
{
// A workspace project whose home is a server renders what the server holds: changes not sent are offered to be sent first (phase 5b).
if (Project is { } project && !await project.ReadyToRenderAsync()) return;
// The job embeds the project: its frame positions follow frames inserted since the editor read them first. A server
// sequence whose frames changed is outdated instead, and its toast says so and offers fresh proxies.
await Session.EnsureFramesAsync();
if (IsOutdated) return;
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 (Session.Source is IProjectRenders { RendersLocally: true })
Shell.Notify(Loc.L("Editor.Render.QueuedLocal"), requests[0].Label, Loc.L("Shell.Nav.Jobs"), Loc.L("Tooltips.Nav.Jobs"), () => Shell.Navigate(SectionKey.Jobs));
else 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));
}
/// Re-reads the enabled state of every command (the async ones keep their own event).
public void RefreshCommands()
{
_analyse.RaiseCanExecuteChanged();
_render.RaiseCanExecuteChanged();
_cancelServerJob.RaiseCanExecuteChanged();
foreach (var tab in Tabs) tab.RefreshCommands();
CommandManager.InvalidateRequerySuggested();
}
/// The Exposure tab switched to or from its waveform: the preview measures it.
public void OnWaveformNeeded() => RequestPreview(immediate: true);
///
/// 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.
///
/// False when the work was still going on after .
public async Task 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().Select(t => t.EstimatesIdle)];
}
///
/// 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.
///
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);
// A sequence opened while its analysis waits, runs or is paused shows that first (EditPath.Start), unless the user or
// the section's memory chose a step meanwhile; the step shown only until then is not counted as visited.
if (!_stepRestored && !_stepChosen && Step(EditPath.Start(null, Session.Content, _jobs.Active(JobKind.DeepAnalyse))) is var start
&& !ReferenceEquals(start, _selectedTab))
{
_selectedTab.IsVisited = false;
Select(start);
}
OnBusyChanged();
}
/// Reads again whether this sequence can send its renders to the signed-in server (session, link, rights).
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.EditSetAside -= OnEditSetAside;
Session.Source.JobChanged -= OnJobChanged;
Session.Source.ReadOnlyChanged -= OnReadOnlyChanged;
Shell.Services.Sync.Changed -= OnSyncChanged;
Shell.Services.SequenceReorganized -= OnSequenceReorganized;
foreach (var tab in Tabs) tab.Dispose();
_project?.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;
// The crop box of the picture now on screen: its view frame, its proportions.
var shown = OrientationOf(request.Project.Camera);
var reframed = shown != _shownOrientation;
_shownOrientation = shown;
CropLockRatio = r.LockRatio;
EditCrop = r.EditCrop;
if (reframed) OnCropEditableChanged();
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();
RefreshSteps();
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));
}
/// Next frame while playing; frames are skipped rather than queued when the preview is slower than the clock.
private void Advance()
{
if (_previewBusy) return;
if (CurrentIndex >= FrameCount - 1 || (_playLimit > 0 && --_playLimit == 0))
{
TogglePlay(0);
return;
}
CurrentIndex++;
}
/// Source frames that make ten seconds of video.
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)));
}
// ---- Steps ----
private void Select(EditorTab step)
{
step.IsVisited = true;
if (!Set(ref _selectedTab, step, nameof(SelectedTab))) return;
OnPropertyChanged(nameof(IsExportTab));
OnPropertyChanged(nameof(IsTallMeasure));
OnPropertyChanged(nameof(Summary));
OnStepNavigationChanged();
OnToolsChanged();
// 1:1 is not a tool of the Export step (Export.html): its picture fits the view.
if (IsExportTab) ZoomMode = PreviewZoomMode.Fit;
UpdateInfo();
RequestPreview(immediate: true);
CommandManager.InvalidateRequerySuggested();
}
private void OnStepNavigationChanged()
{
OnPropertyChanged(nameof(StepCounter));
OnPropertyChanged(nameof(StepAnnouncement));
OnPropertyChanged(nameof(NextText));
OnPropertyChanged(nameof(NextToolTip));
OnPropertyChanged(nameof(PreviousToolTip));
}
private EditorTab Step(string key) => Tabs.First(t => t.Key == key);
private EditorTab? StepFor(object? parameter) => parameter switch
{
EditorTab tab when Tabs.Contains(tab) => tab,
int number => (uint)(number - 1) < (uint)Tabs.Count ? Tabs[number - 1] : null,
string text when int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out var number) =>
(uint)(number - 1) < (uint)Tabs.Count ? Tabs[number - 1] : null,
string key => Tabs.FirstOrDefault(t => t.Key == key),
_ => null,
};
private void NextStep()
{
if (!SelectedTab.IsLast) MoveStep(1);
else if (Shell.Sequence.ChangeSequenceCommand.CanExecute(null)) Shell.Sequence.ChangeSequenceCommand.Execute(null);
}
/// Works out where every step stands: after any change of the project, the sequence, its jobs or the plan.
private void RefreshSteps()
{
if (_disposed) return;
var facts = new PathFacts(Session.Content, Session.Advice, Session.Project, _jobs.Active(JobKind.DeepAnalyse), _jobs.Active(JobKind.Render) ?? _jobs.Active(JobKind.WorkspaceRender),
_planned ? RenderBlockedReason : null, _rendered.Matches(Session.Project));
foreach (var tab in Tabs) tab.SetStep(EditPath.Of(tab.Key, facts));
Analysis.OnStepChanged();
OnPropertyChanged(nameof(StepAnnouncement));
}
/// A deep analysis the editor followed ended: the steps it changed are to be looked at again.
private void ClearVisited()
{
foreach (var tab in Tabs) tab.IsVisited = ReferenceEquals(tab, SelectedTab);
}
// ---- Changes ----
private void OnToolsChanged()
{
OnPropertyChanged(nameof(IsCameraTab));
OnPropertyChanged(nameof(ShowCropMask));
OnPropertyChanged(nameof(CropModeToolTip));
OnPropertyChanged(nameof(ViewVectors));
OnPropertyChanged(nameof(ViewRegions));
OnPropertyChanged(nameof(CropRect));
OnPropertyChanged(nameof(ShowStraightenGrid));
OnCropEditableChanged();
}
/// Whether the crop box is edited on the preview, and what a drag there does.
private void OnCropEditableChanged()
{
OnPropertyChanged(nameof(CropEditable));
OnPropertyChanged(nameof(PreviewToolTip));
}
/// The rendered orientation members of : the view frame the crop box is normalised in.
private static (int, bool, bool, double) OrientationOf(CameraSettings camera) =>
(FrameOrientation.TurnsOf(camera.Rotate), camera.Mirror, camera.Flip, FrameOrientation.StraightenOf(camera.Straighten));
///
/// The crop box and its proportions for the project now, at once, while the picture on screen shows the same view frame
/// (a crop row, an aspect or preset change: no frame to wait for); a turn, mirror, flip or straighten waits for the
/// picture it applies to.
///
private void UpdateCropBox()
{
var project = Session.Project;
if (OrientationOf(project.Camera) != _shownOrientation || Session.Content.SourceWidth <= 0 || Session.Content.SourceHeight <= 0) return;
try
{
var geometry = RenderGeometry.Create(project.Camera, ExportResolver.Resolve(project.Export), Session.Content.SourceWidth, Session.Content.SourceHeight);
CropLockRatio = geometry.LockRatio;
var box = geometry.EditableCrop;
EditCrop = new Rect(box.X, box.Y, box.Width, box.Height);
}
catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or RenderException)
{
Log.Warning("editor", "The crop box could not be worked out", ex);
}
}
private void CommitCrop(object? parameter)
{
var (crop, nudge) = parameter switch
{
CropEditEventArgs edit => (edit.Crop, edit.IsNudge),
Rect rect => (rect, false),
_ => (Rect.Empty, false),
};
if (crop.IsEmpty || !CropEditable) return;
// The box itself, locked to the proportions on screen, with the offsets at 0: the render's FitCrop returns it unchanged.
var box = CropFrame.Clamp(new RectD(crop.X, crop.Y, crop.Width, crop.Height), CropLockRatio);
EditCrop = new Rect(box.X, box.Y, box.Width, box.Height);
Session.Edit(p => p with { Camera = p.Camera with { Crop = CropFrame.ToCrop(box), OffsetX = 0, OffsetY = 0 } }, "Camera.Crop");
if (!nudge) Session.EndEditRun();
}
private void OnProjectChanged()
{
UpdateCropBox();
OnCropEditableChanged();
foreach (var tab in Tabs) tab.Refresh();
UpdateMarkers();
RefreshSteps();
RequestCorrections();
RequestPlan();
RequestPreview();
RefreshCommands();
}
private void OnContentChanged()
{
// The frame on screen stays on screen, wherever frames inserted or removed before it put it (the tabs that reload
// below may seek meanwhile).
var shown = IndexOfFrame(_currentFrameId) ?? int.Min(CurrentIndex, int.Max(0, FrameCount - 1));
_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;
UpdateCropBox();
_rendered = new RenderedSettings(Session.Content.Renders);
foreach (var tab in Tabs) tab.OnContentChanged();
OnPropertyChanged(nameof(Summary));
OnPropertyChanged(nameof(FrameCount));
CurrentIndex = shown;
UpdateMarkers();
RefreshSteps();
RequestCorrections();
RequestPlan();
RequestPreview(immediate: true);
}
/// The id of frame ; empty when there is none.
private Guid FrameId(int index) => (uint)index < (uint)FrameCount ? Session.Content.Frames[index].Id : Guid.Empty;
/// Where frame is now; null when the sequence no longer has it.
private int? IndexOfFrame(Guid id)
{
var frames = Session.Content.Frames;
for (var i = 0; i < frames.Count; i++)
{
if (frames[i].Id == id) return i;
}
return null;
}
///
/// The UI language changed: what the tabs keep in objects (chart legends and summaries, the Auto chips of the
/// settings), the timeline markers and the preview's info line are made again in the new language.
///
protected override void OnLanguageChanged()
{
if (!_disposed)
{
foreach (var tab in Tabs) tab.OnContentChanged();
UpdateMarkers();
UpdateInfo();
RefreshSteps();
}
base.OnLanguageChanged();
}
///
/// The first failure of a run of failed saves (they are retried until one succeeds); Retry tries at once. Why it
/// failed is one localized sentence (BRIEF §0.2 rule 6): the editor's own (frames changed, changed elsewhere, gone,
/// unreadable, not allowed), else the cause on the server or in the archive; the exception's own text is in the log.
///
private void OnSaveFailed(Exception ex) => Shell.ShowToast(new Toast
{
Title = Loc.L("Editor.Save.Failed"),
Message = ex is EditorException ? ex.Message : Session.IsRemote ? ErrorText.ForServer(ex) : ArchiveFailures.Cause(ex),
IsError = true,
ActionText = Loc.L("Editor.Save.Retry"),
ActionToolTip = Loc.L("Tooltips.Editor.Save.Retry"),
ActionCommand = new RelayCommand(() => _ = Session.FlushAsync()),
});
///
/// An edit not stored yet met a newer Chronos's settings a sync took (owner review 2, R8): it was kept aside in the
/// project's folder, never dropped, and the toast says so; the newer settings show read-only.
///
private void OnEditSetAside(string path) => _dispatcher.BeginInvoke(() =>
{
if (_disposed) return;
Shell.ShowToast(new Toast { Title = Loc.L("Editor.Workspace.EditSetAside"), Message = Loc.L("Editor.Workspace.EditSetAsideMessage") });
});
///
/// A save met settings someone changed to other values on the server (BRIEF §14.5 "explicit conflict prompt"), or, for
/// a workspace project, that a sync took from the library while the edit waited (owner review 2, R8): the dialog lists
/// them; Keep mine overwrites the other side's, Take server or the library's (the safe default, also on Esc) shows theirs
/// as one undoable step. Saving waits until then.
///
private void OnConflictFound(ProjectConflictException conflict) => _dispatcher.BeginInvoke(() =>
{
if (_disposed || Session.Conflict is null) return;
var request = Session.Source is WorkspaceSequenceSource workspace ? LibraryConflictRequest(conflict, Title, workspace.IsServerHome) : ConflictRequest(conflict, Title, ServerName ?? string.Empty);
var keepMine = Shell.Services.Dialogs.Confirm(request);
if (!_disposed) Session.ResolveConflict(keepMine);
});
///
/// The conflict prompt of a workspace project: what differs (the editor's value, then the home's) and what each choice
/// keeps, worded for a home (phase 5b) or the library.
///
internal static ConfirmRequest LibraryConflictRequest(ProjectConflictException conflict, string title, bool server = false) => new()
{
Heading = Loc.L("Editor.Workspace.Conflict.Title"),
Message = WorkspaceText.ForHome("Editor.Workspace.Edited.Message", server, title),
Detail = string.Join(Environment.NewLine, WorkspaceText.ConflictLines(conflict.Differences, 10, server)),
DetailToolTip = WorkspaceText.ForHome("Tooltips.Editor.Workspace.Differences", server),
SafeText = WorkspaceText.ForHome("Editor.Workspace.Conflict.TakeHome", server),
SafeToolTip = WorkspaceText.ForHome("Tooltips.Editor.Workspace.TakeHomeUndo", server),
ActionText = Loc.L("Server.Sync.Action.KeepMine"),
ActionToolTip = WorkspaceText.ForHome("Tooltips.Editor.Workspace.KeepMine", server),
IsDestructive = true,
};
/// The conflict prompt: what differs (the editor's value, then the server's) and what each choice replaces.
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(StepMarkers(content));
foreach (var finding in Session.Advice.Findings.Where(f => f.Severity >= FindingSeverity.Warning))
{
var text = Loc.Text(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(Loc.IslandSeparator, 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 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)
{
// Closed after the Jobs screen left this render's end to it (FollowsRender): said without a read.
if (job is { State: JobState.Done } && IsRender(job.Kind) && _jobs.IsActive(job.Id) && Shell.Jobs.ClaimRenderToast(job.Id))
AnnounceRender(job, read: false);
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 is { Kind: JobKind.DeepAnalyse, State: JobState.Done }) ClearVisited();
// A render this editor followed to its end is said here, with its output, unless the Jobs screen said it first.
if (job.State == JobState.Done) _ = ReloadAsync(IsRender(job.Kind) && Shell.Jobs.ClaimRenderToast(job.Id) ? job : null);
// A local render's send home (phase 5c): said once, here or on the Jobs screen.
if (job is { Kind: JobKind.WorkspaceSend, State: JobState.Done } && Shell.Jobs.ClaimRenderToast(job.Id)) Shell.Notify(Loc.L("Editor.Render.SentHome"), Title);
// An analysis may have written its Auto values into the library's settings (Main): the project takes them or says so.
if (job.State == JobState.Done && _project is { } project) _ = project.JobEndedAsync(job);
else if (IsRemote && job.Kind is not (JobKind.WorkspaceRender or JobKind.WorkspaceSend) && job.State is JobState.Failed or JobState.Cancelled)
ReportServerJobEnded(job);
});
}
/// A render of the sequence: at its home, or on this PC from a workspace project's originals (phase 5c).
private static bool IsRender(JobKind kind) => kind is JobKind.Render or JobKind.WorkspaceRender;
///
/// The frames of the server sequence changed while it is open, or a workspace project's sync took a newer Chronos's
/// settings (any thread): everything that changes the project is disabled and says why, and a toast says so (for a
/// server sequence, offering to fetch the proxies again).
///
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();
// A workspace project a sync turned read-only (a newer Chronos saved it): said once, the controls say why too.
if (IsReadOnly && Session.Source is WorkspaceSequenceSource)
{
Shell.ShowToast(new Toast { Title = Loc.L("Editor.Workspace.ReadOnly"), Message = Loc.L("Errors.Workspace.ReadOnly") });
return;
}
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)),
});
});
/// A server job of this sequence failed or was cancelled: what happened, and Retry (BRIEF §11.5).
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);
}
///
/// A Reorganize of this PC moved the folder of a sequence or renamed it or its files (any thread): the open sequence of
/// this PC is read again where it is now, so its frames decode and its project goes there.
///
private void OnSequenceReorganized(Guid sequenceId) => _dispatcher.BeginInvoke(() =>
{
if (!_disposed && !IsRemote && sequenceId == Session.Content.Sequence.Id) _ = ReloadAsync(null);
});
/// The session, the sync links or the rights changed (any thread): whether the Export "Sync" option applies.
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);
}
}
/// Seconds per frame of the latest finished deep analysis (any sequence): the cost Analyse announces.
private void LearnAnalysisSpeed(Job job)
{
if (job is { Kind: JobKind.DeepAnalyse, State: JobState.Done, ItemsPerSecond: > 0 and var speed }) _analysisSecondsPerFrame = 1 / speed;
}
///
/// Reads the sequence again after a job. A finished render whose toast this editor took ()
/// is said in any case, also when the editor closed meanwhile: with its output and follow-ups (BRIEF §11.5) once the
/// sequence is read again, without them when that failed.
///
private async Task ReloadAsync(Job? render)
{
var reloaded = false;
try
{
await Session.ReloadAsync();
reloaded = true;
}
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.ChronosApiException or OperationCanceledException
or Data.DatabaseException)
{
// A server that does not answer in time cancels the request (OperationCanceledException).
Shell.ReportError(ex);
}
finally
{
if (render is not null) AnnounceRender(render, reloaded);
}
}
///
/// This editor follows the render (UI thread): it saw it wait or run and has not seen it end yet, so it will take its
/// toast () when it does.
///
internal bool FollowsRender(Guid jobId) => !_disposed && _jobs.IsActive(jobId);
/// "Render finished" with the output file and Show in folder; : the sequence was read again after the render.
private void AnnounceRender(Job render, bool read)
{
// A server's render stays on the server: nothing to show in Explorer. Before a read the renders are the old ones.
var output = read && 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));
OnCropEditableChanged();
OnPropertyChanged(nameof(LockedToolTip));
OnPropertyChanged(nameof(AnalyseToolTip));
OnPropertyChanged(nameof(OptimiseAllToolTip));
OnServerJobChanged();
foreach (var tab in Tabs) tab.OnBusyChanged();
RefreshSteps();
RefreshCommands();
}
private void OnServerJobChanged()
{
if (!IsRemote) return;
OnPropertyChanged(nameof(ServerJobText));
OnPropertyChanged(nameof(ServerJobToolTip));
OnPropertyChanged(nameof(ServerJobKind));
OnPropertyChanged(nameof(HasServerJobProgress));
OnPropertyChanged(nameof(ServerJobProgress));
_cancelServerJob.RaiseCanExecuteChanged();
}
/// What Render renders and whether it can (the plan, the rights or the frames changed).
private void OnRenderStateChanged()
{
OnPropertyChanged(nameof(RenderText));
OnPropertyChanged(nameof(RenderToolTip));
OnPropertyChanged(nameof(RenderBlockedReason));
OnPropertyChanged(nameof(CanRender));
}
/// A rendered preview as frozen bitmaps and overlays in picture pixels (built off the UI thread).
private sealed record PreviewResult(ImageSource Corrected, ImageSource Original, Rect Crop, Rect EditCrop, double LockRatio, IReadOnlyList Horizon,
IReadOnlyList 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),
new Rect(frame.EditCrop.X, frame.EditCrop.Y, frame.EditCrop.Width, frame.EditCrop.Height),
frame.LockRatio,
[.. 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);
}
}