merge: sequence editor with Export tab (preview through the render pipeline, section tabs, autosave with undo/redo)
Keeps the sync captures and sample server next to the editor's sample sunset; the editor's capture runs last since it leaves an analysis running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
53 files changed
+8622
-18
No files matched your search
@@ -2,10 +2,172 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:ui="clr-namespace:Titano.Ui.Controls;assembly=Titano.Ui"
|
||||
xmlns:l="clr-namespace:Titano.Ui.Localization;assembly=Titano.Ui">
|
||||
<Grid Margin="20">
|
||||
<ui:EmptyState Message="{l:Loc Editor.Empty}">
|
||||
xmlns:v="clr-namespace:Titano.Ui.Visuals;assembly=Titano.Ui"
|
||||
xmlns:l="clr-namespace:Titano.Ui.Localization;assembly=Titano.Ui"
|
||||
KeyDown="OnKeyDown" Loaded="OnLoaded">
|
||||
<UserControl.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="pack://application:,,,/Titano;component/Editor/EditorStyles.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</UserControl.Resources>
|
||||
|
||||
<Grid>
|
||||
<ui:EmptyState Message="{l:Loc Editor.Empty}" Visibility="{Binding IsEmpty, Converter={StaticResource BoolToVisibility}}">
|
||||
<Button Style="{StaticResource Button.Primary}" Content="{l:Loc Shell.Nav.Library}" Command="{Binding OpenLibraryCommand}" ToolTip="{l:Loc Tooltips.Nav.Library}" />
|
||||
</ui:EmptyState>
|
||||
<ProgressBar Width="240" IsIndeterminate="True" VerticalAlignment="Center" Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibility}}"
|
||||
AutomationProperties.Name="{l:Loc Editor.Loading}" />
|
||||
|
||||
<!-- Editor.html: header, then the preview, filmstrip and section tabs on the left and the section's settings on the right. -->
|
||||
<Grid x:Name="EditorRoot" DataContext="{Binding Editor}" Margin="16,0,16,12" IsVisibleChanged="OnEditorVisibleChanged"
|
||||
Visibility="{Binding Converter={x:Static ui:NullToCollapsedConverter.Instance}}">
|
||||
<Grid.InputBindings>
|
||||
<KeyBinding Key="Z" Modifiers="Control" Command="{Binding UndoCommand}" />
|
||||
<KeyBinding Key="Y" Modifiers="Control" Command="{Binding RedoCommand}" />
|
||||
<KeyBinding Key="D0" Modifiers="Control" Command="{Binding FitCommand}" />
|
||||
<KeyBinding Key="D1" Modifiers="Control" Command="{Binding ActualSizeCommand}" />
|
||||
<KeyBinding Key="R" Modifiers="Control" Command="{Binding RenderCommand}" />
|
||||
<KeyBinding Key="A" Modifiers="Control+Shift" Command="{Binding AnalyseCommand}" />
|
||||
</Grid.InputBindings>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="52" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Button Style="{StaticResource Button.IconSubtle}" ui:ButtonIcon.Glyph="{StaticResource Icon.Back}" Command="{Binding BackCommand}"
|
||||
ToolTip="{l:Loc Tooltips.Editor.Back}" AutomationProperties.Name="{l:Loc Editor.Back}" Margin="-6,0,0,0" />
|
||||
<TextBlock Grid.Column="1" Text="{Binding Title}" Style="{StaticResource Text.Title}" VerticalAlignment="Center" Margin="6,0,12,0" MaxWidth="320"
|
||||
TextTrimming="CharacterEllipsis" />
|
||||
<TextBlock Grid.Column="2" Text="{Binding Summary}" Style="{StaticResource Text.Secondary}" FontSize="{StaticResource Size.Body}" VerticalAlignment="Center"
|
||||
TextTrimming="CharacterEllipsis" Typography.NumeralAlignment="Tabular" ToolTip="{Binding Summary}" />
|
||||
<StackPanel Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Center" Margin="12,0,0,0">
|
||||
<Button Content="{l:Loc Editor.Analyse}" Command="{Binding AnalyseCommand}" ToolTip="{Binding AnalyseToolTip}" ToolTipService.ShowOnDisabled="True" />
|
||||
<Button Content="{l:Loc Editor.OptimiseAll}" Command="{Binding OptimiseAllCommand}" ToolTip="{Binding OptimiseAllToolTip}"
|
||||
ToolTipService.ShowOnDisabled="True" Margin="12,0,0,0" />
|
||||
<v:SplitButton x:Name="RenderButton" IsPrimary="True" Margin="12,0,0,0" Content="{Binding RenderText}" Command="{Binding RenderCommand}"
|
||||
ItemsSource="{Binding RenderChoices}" DisplayMemberPath="Label" SelectedItem="{Binding SelectedRenderChoice}" ToolTip="{Binding RenderToolTip}"
|
||||
ToolTipService.ShowOnDisabled="True" DropDownToolTip="{l:Loc Tooltips.Editor.RenderChoice}"
|
||||
AutomationProperties.Name="{l:Loc Editor.RenderButton}" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="{StaticResource Editor.Grid.Panel}" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid Margin="0,0,16,0">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition MinHeight="160">
|
||||
<RowDefinition.Style>
|
||||
<Style TargetType="RowDefinition">
|
||||
<Setter Property="Height" Value="{StaticResource Editor.Grid.Preview}" />
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsExportTab}" Value="True">
|
||||
<Setter Property="Height" Value="{StaticResource Editor.Grid.ExportPreview}" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</RowDefinition.Style>
|
||||
</RowDefinition>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition MinHeight="150">
|
||||
<RowDefinition.Style>
|
||||
<Style TargetType="RowDefinition">
|
||||
<Setter Property="Height" Value="{StaticResource Editor.Grid.Measure}" />
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsExportTab}" Value="True">
|
||||
<Setter Property="Height" Value="{StaticResource Editor.Grid.ExportMeasure}" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</RowDefinition.Style>
|
||||
</RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<v:PreviewViewport x:Name="Preview" Source="{Binding PreviewImage}" OriginalSource="{Binding OriginalImage}" IsCompareEnabled="{Binding IsCompare}"
|
||||
WipeLabel="{Binding WipeLabel}" ZoomMode="{Binding ZoomMode, Mode=TwoWay}" ShowVectors="{Binding ViewVectors}"
|
||||
MotionVectors="{Binding MotionVectors}" VectorScale="{Binding VectorScale}" ShowRegions="{Binding ViewRegions}"
|
||||
RegionBoundary="{Binding RegionBoundary}" ShowCropMask="{Binding ShowCropMask}" CropRect="{Binding CropRect}"
|
||||
CropMaskMode="{Binding CropMaskMode}" InfoText="{Binding InfoText}" ToolTip="{l:Loc Tooltips.Editor.Preview.View}"
|
||||
AutomationProperties.Name="{l:Loc Editor.Preview}" SizeChanged="OnPreviewSizeChanged">
|
||||
<v:PreviewViewport.ToolsContent>
|
||||
<!-- Tools give the keyboard back to the preview after a click, so Space keeps playing (OnToolClicked). -->
|
||||
<StackPanel Orientation="Horizontal" DataContext="{Binding DataContext, ElementName=EditorRoot}" ButtonBase.Click="OnToolClicked">
|
||||
<ToggleButton Style="{StaticResource ToggleButton.PreviewTool}" IsChecked="{Binding IsCompare}" ui:ButtonIcon.Glyph="{StaticResource Icon.Compare}"
|
||||
Content="{l:Loc Editor.Preview.Compare}" ToolTip="{l:Loc Tooltips.Editor.Preview.Compare}" />
|
||||
<ToggleButton Visibility="{Binding IsExportTab, Converter={x:Static ui:InverseBoolToVisibilityConverter.Instance}}" Style="{StaticResource ToggleButton.PreviewTool}"
|
||||
IsChecked="{Binding ShowVectors}" ui:ButtonIcon.Glyph="{StaticResource Icon.Vectors}" Content="{l:Loc Editor.Preview.Vectors}"
|
||||
ToolTip="{l:Loc Tooltips.Editor.Preview.Vectors}" />
|
||||
<ToggleButton Visibility="{Binding IsExportTab, Converter={x:Static ui:InverseBoolToVisibilityConverter.Instance}}" Style="{StaticResource ToggleButton.PreviewTool}"
|
||||
IsChecked="{Binding ShowRegions}" ui:ButtonIcon.Glyph="{StaticResource Icon.Regions}" Content="{l:Loc Editor.Preview.Regions}"
|
||||
ToolTip="{l:Loc Tooltips.Editor.Preview.Regions}" />
|
||||
<ToggleButton Style="{StaticResource ToggleButton.PreviewTool}" IsChecked="{Binding ShowCropMask}" ui:ButtonIcon.Glyph="{StaticResource Icon.CropMask}"
|
||||
Content="{l:Loc Editor.Preview.CropMask}" ToolTip="{l:Loc Tooltips.Editor.Preview.CropMask}" />
|
||||
<!-- "What I lose" (dimmed) or "how it will look" (hidden) outside the crop (BRIEF §12.3). -->
|
||||
<ToggleButton Visibility="{Binding ShowCropMask, Converter={StaticResource BoolToVisibility}}" Style="{StaticResource ToggleButton.PreviewToolIcon}"
|
||||
IsChecked="{Binding IsCropResult}" ui:ButtonIcon.Glyph="{StaticResource Icon.Eye}" ToolTip="{Binding CropModeToolTip}"
|
||||
AutomationProperties.Name="{l:Loc Editor.Preview.CropResult}" />
|
||||
<ToggleButton Visibility="{Binding IsExportTab, Converter={x:Static ui:InverseBoolToVisibilityConverter.Instance}}" Style="{StaticResource ToggleButton.PreviewTool}"
|
||||
IsChecked="{Binding IsActualSize}" Content="{l:Loc Editor.Preview.ActualSize}" ToolTip="{l:Loc Tooltips.Editor.Preview.ActualSize}" />
|
||||
<Button Visibility="{Binding IsExportTab, Converter={x:Static ui:InverseBoolToVisibilityConverter.Instance}}" Style="{StaticResource Button.PreviewToolIcon}"
|
||||
ui:ButtonIcon.Glyph="{StaticResource Icon.Fullscreen}" Click="OnFullScreen" ToolTip="{l:Loc Tooltips.Editor.Preview.FullScreen}"
|
||||
AutomationProperties.Name="{l:Loc Editor.Preview.FullScreen}" />
|
||||
<Button Visibility="{Binding IsExportTab, Converter={x:Static ui:InverseBoolToVisibilityConverter.Instance}}" Style="{StaticResource Button.PlayTool}"
|
||||
Command="{Binding PlayCommand}" ToolTip="{Binding PlayToolTip}" AutomationProperties.Name="{Binding PlayName}" />
|
||||
<Button Content="{l:Loc Editor.Preview.Clip}" Command="{Binding PreviewClipCommand}" ToolTip="{Binding ClipToolTip}"
|
||||
Visibility="{Binding IsExportTab, Converter={StaticResource BoolToVisibility}}">
|
||||
<Button.Style>
|
||||
<Style TargetType="Button" BasedOn="{StaticResource Button.PreviewTool}">
|
||||
<Setter Property="ui:ButtonIcon.Glyph" Value="{StaticResource Icon.Play}" />
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsPlaying}" Value="True">
|
||||
<Setter Property="ui:ButtonIcon.Glyph" Value="{StaticResource Icon.Pause}" />
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Button.Style>
|
||||
</Button>
|
||||
</StackPanel>
|
||||
</v:PreviewViewport.ToolsContent>
|
||||
</v:PreviewViewport>
|
||||
<!-- The frame on screen cannot be read: one sentence instead of another frame's picture. -->
|
||||
<TextBlock Text="{Binding PreviewMessage}" Style="{StaticResource Text.Secondary}" HorizontalAlignment="Center" VerticalAlignment="Center"
|
||||
IsHitTestVisible="False" Visibility="{Binding PreviewMessage, Converter={x:Static ui:NullToCollapsedConverter.Instance}}" />
|
||||
|
||||
<v:Filmstrip Grid.Row="1" Margin="0,10,0,0" Source="{Binding Thumbnails}" CurrentIndex="{Binding CurrentIndex, Mode=TwoWay}" Markers="{Binding Markers}"
|
||||
ToolTip="{l:Loc Tooltips.Editor.Filmstrip}" AutomationProperties.Name="{l:Loc Editor.Filmstrip}" />
|
||||
|
||||
<TabControl Grid.Row="2" Margin="0,10,0,0" ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}" AutomationProperties.Name="{l:Loc Editor.Sections}">
|
||||
<TabControl.Resources>
|
||||
<ResourceDictionary Source="pack://application:,,,/Titano;component/Editor/EditorMeasures.xaml" />
|
||||
</TabControl.Resources>
|
||||
<TabControl.ItemContainerStyle>
|
||||
<Style TargetType="TabItem" BasedOn="{StaticResource {x:Type TabItem}}">
|
||||
<Setter Property="Header" Value="{Binding Header}" />
|
||||
<Setter Property="ToolTip" Value="{Binding ToolTip}" />
|
||||
<Setter Property="AutomationProperties.Name" Value="{Binding Header}" />
|
||||
</Style>
|
||||
</TabControl.ItemContainerStyle>
|
||||
</TabControl>
|
||||
</Grid>
|
||||
|
||||
<ContentControl Grid.Column="1" Content="{Binding SelectedTab}" Focusable="False">
|
||||
<ContentControl.Resources>
|
||||
<ResourceDictionary Source="pack://application:,,,/Titano;component/Editor/EditorPanels.xaml" />
|
||||
</ContentControl.Resources>
|
||||
</ContentControl>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -1,8 +1,129 @@
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using Titano.App.Editor;
|
||||
using Titano.App.Shell;
|
||||
using Titano.Core.Diagnostics;
|
||||
|
||||
namespace Titano.App.Sections;
|
||||
|
||||
public partial class SequenceView : UserControl
|
||||
{
|
||||
public SequenceView() => InitializeComponent();
|
||||
/// <summary>Set on a window once its closing waits for the editor's last save.</summary>
|
||||
private static readonly DependencyProperty SavesBeforeClosingProperty =
|
||||
DependencyProperty.RegisterAttached("SavesBeforeClosing", typeof(bool), typeof(SequenceView), new PropertyMetadata(false));
|
||||
|
||||
public SequenceView()
|
||||
{
|
||||
InitializeComponent();
|
||||
// A drag, a click or a key press is one undo step even when it moves a slider many times (ProjectHistory runs);
|
||||
// handled events too, because sliders and repeat buttons mark theirs handled.
|
||||
EditorRoot.AddHandler(Thumb.DragCompletedEvent, new DragCompletedEventHandler((_, _) => EndEditRun()), handledEventsToo: true);
|
||||
EditorRoot.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler((_, _) => EndEditRun()), handledEventsToo: true);
|
||||
EditorRoot.AddHandler(KeyUpEvent, new KeyEventHandler((_, _) => EndEditRun()), handledEventsToo: true);
|
||||
}
|
||||
|
||||
private EditorViewModel? Editor => (DataContext as SequenceViewModel)?.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// The project is saved a moment after each edit; closing the window first stores the last edit, and asks before
|
||||
/// discarding one that cannot be stored (once per window, so it holds even when another section is showing).
|
||||
/// </summary>
|
||||
private void OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
FocusPreview();
|
||||
if (Window.GetWindow(this) is not { } window || (bool)window.GetValue(SavesBeforeClosingProperty)) return;
|
||||
window.SetValue(SavesBeforeClosingProperty, true);
|
||||
var (saved, asking, closed) = (false, false, false);
|
||||
window.Closing += OnClosing;
|
||||
window.Closed += (_, _) => closed = true;
|
||||
|
||||
async void OnClosing(object? s, CancelEventArgs args)
|
||||
{
|
||||
if (saved || args.Cancel || window.DataContext is not ShellViewModel { Sequence: { HasPendingSave: true } section }) return;
|
||||
args.Cancel = true;
|
||||
if (asking) return;
|
||||
asking = true;
|
||||
var close = true;
|
||||
try
|
||||
{
|
||||
close = await section.ConfirmCloseAsync();
|
||||
if (close) await section.CloseAsync();
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
Log.Warning("editor", "The last edit was not saved before closing", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
asking = false;
|
||||
}
|
||||
// "Keep editing": the window stays open with the edit still pending.
|
||||
if (!close) return;
|
||||
saved = true;
|
||||
// An application shutdown closes the window without waiting.
|
||||
if (!closed) window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Space plays or pauses, arrows step a frame (BRIEF §12.3) — unless a text field or a control that uses the key has it.</summary>
|
||||
private void OnKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Handled || Editor is not { } editor || Keyboard.Modifiers != ModifierKeys.None || e.OriginalSource is TextBoxBase) return;
|
||||
var command = e.Key switch
|
||||
{
|
||||
Key.Space => editor.PlayCommand,
|
||||
Key.Left => editor.StepBackCommand,
|
||||
Key.Right => editor.StepForwardCommand,
|
||||
_ => null,
|
||||
};
|
||||
if (command is null) return;
|
||||
command.Execute(null);
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
/// <summary>An opened editor takes the keyboard, so its shortcuts work without clicking into it first.</summary>
|
||||
private void OnEditorVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.NewValue is true) FocusPreview();
|
||||
}
|
||||
|
||||
/// <summary>A tool clicked with the mouse hands the keyboard back to the preview (Space keeps playing); keyboard users keep their focus.</summary>
|
||||
private void OnToolClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (InputManager.Current.MostRecentInputDevice is MouseDevice) Preview.Focus();
|
||||
}
|
||||
|
||||
private void FocusPreview()
|
||||
{
|
||||
if (Editor is null) return;
|
||||
Dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
if (Preview.IsVisible && !IsKeyboardFocusWithin) Keyboard.Focus(Preview);
|
||||
}, DispatcherPriority.Input);
|
||||
}
|
||||
|
||||
private void EndEditRun() => Editor?.Session.EndEditRun();
|
||||
|
||||
/// <summary>The preview renders at the size it is shown, in device pixels.</summary>
|
||||
private void OnPreviewSizeChanged(object sender, SizeChangedEventArgs e)
|
||||
{
|
||||
if (Editor is not { } editor) return;
|
||||
var scale = VisualTreeHelper.GetDpi(this).DpiScaleX;
|
||||
editor.PreviewEdge = (int)(double.Max(e.NewSize.Width, e.NewSize.Height) * scale);
|
||||
}
|
||||
|
||||
private void OnFullScreen(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (Editor is not { } editor) return;
|
||||
var restore = editor.PreviewEdge;
|
||||
var window = new FullScreenPreview(editor) { Owner = Window.GetWindow(this) };
|
||||
window.ShowDialog();
|
||||
editor.PreviewEdge = restore;
|
||||
Preview.Focus();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,170 @@
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Input;
|
||||
using Titano.App.Editor;
|
||||
using Titano.App.Infrastructure;
|
||||
using Titano.App.Shell;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Ui.Mvvm;
|
||||
|
||||
namespace Titano.App.Sections;
|
||||
|
||||
/// <summary>Sequence editor (BRIEF §12.3).</summary>
|
||||
public sealed class SequenceViewModel(ShellViewModel shell) : SectionViewModel(SectionKey.Sequence, "Icon.Sequence")
|
||||
/// <summary>
|
||||
/// The Sequence section (BRIEF §12.3): opens the sequence the Library (or Jobs) chose into an <see cref="EditorViewModel"/>
|
||||
/// and closes the previous one after saving it; without a sequence it shows the empty state. A failed open is reported
|
||||
/// and leaves the section usable; an edit that cannot be saved is never dropped without the user's confirmation.
|
||||
/// </summary>
|
||||
public sealed class SequenceViewModel : SectionViewModel
|
||||
{
|
||||
public ShellViewModel Shell { get; } = shell;
|
||||
public ICommand OpenLibraryCommand => new RelayCommand(() => Shell.Navigate(SectionKey.Library));
|
||||
private EditorViewModel? _editor;
|
||||
private Guid? _openId;
|
||||
private bool _isLoading;
|
||||
private Task _switch = Task.CompletedTask;
|
||||
|
||||
public SequenceViewModel(ShellViewModel shell)
|
||||
: base(SectionKey.Sequence, "Icon.Sequence")
|
||||
{
|
||||
Shell = shell;
|
||||
OpenLibraryCommand = new RelayCommand(() => Shell.Navigate(SectionKey.Library));
|
||||
shell.PropertyChanged += OnShellChanged;
|
||||
}
|
||||
|
||||
public ShellViewModel Shell { get; }
|
||||
|
||||
public ICommand OpenLibraryCommand { get; }
|
||||
|
||||
/// <summary>The open sequence; null shows the empty state.</summary>
|
||||
public EditorViewModel? Editor
|
||||
{
|
||||
get => _editor;
|
||||
private set
|
||||
{
|
||||
if (!Set(ref _editor, value)) return;
|
||||
OnPropertyChanged(nameof(IsEmpty));
|
||||
OnPropertyChanged(nameof(Title));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsLoading { get => _isLoading; private set => Set(ref _isLoading, value); }
|
||||
|
||||
public bool IsEmpty => _editor is null && !_isLoading;
|
||||
|
||||
/// <summary>The open sequence has an edit not stored yet (the save follows each change after a moment, and retries after a failure).</summary>
|
||||
public bool HasPendingSave => _editor?.Session.HasPendingSave == true;
|
||||
|
||||
/// <summary>"Titano — Harbour sunset" while a sequence is open.</summary>
|
||||
public override string Title => _editor?.Title ?? Label;
|
||||
|
||||
/// <summary>Opens <paramref name="sequenceId"/> (the editor of another sequence is saved and closed first).</summary>
|
||||
public Task OpenAsync(Guid sequenceId)
|
||||
{
|
||||
_switch = SwitchAsync(_switch, sequenceId);
|
||||
return _switch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores the open sequence's last edit. When the save fails (archive offline, read-only, busy), asks whether to
|
||||
/// discard the edit (BRIEF §12.1: modals only for destructive confirmations).
|
||||
/// </summary>
|
||||
/// <returns>True when the editor may close: everything is stored or the user chose to discard.</returns>
|
||||
public async Task<bool> ConfirmCloseAsync()
|
||||
{
|
||||
if (Editor is not { } editor || await editor.Session.FlushAsync()) return true;
|
||||
return Shell.Services.Dialogs.Confirm(new ConfirmRequest
|
||||
{
|
||||
Heading = Loc.L("Editor.Unsaved.Title"),
|
||||
Message = Loc.L("Editor.Unsaved.Message", editor.Title, editor.Session.LastSaveError?.Message ?? string.Empty),
|
||||
SafeText = Loc.L("Editor.Unsaved.Keep"),
|
||||
SafeToolTip = Loc.L("Tooltips.Editor.Unsaved.Keep"),
|
||||
ActionText = Loc.L("Editor.Unsaved.Discard"),
|
||||
ActionToolTip = Loc.L("Tooltips.Editor.Unsaved.Discard"),
|
||||
IsDestructive = true,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Closes the open sequence (after <see cref="ConfirmCloseAsync"/>, which stores its last edit).</summary>
|
||||
public async Task CloseAsync()
|
||||
{
|
||||
await Settled(_switch);
|
||||
if (Editor is not { } editor) return;
|
||||
Editor = null;
|
||||
_openId = null;
|
||||
await editor.DisposeAsync();
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Shell.PropertyChanged -= OnShellChanged;
|
||||
Editor?.Dispose();
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
protected override void OnActivated()
|
||||
{
|
||||
if (Shell.CurrentSequenceId is { } id && id != _openId) _ = OpenAsync(id);
|
||||
}
|
||||
|
||||
private void OnShellChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(ShellViewModel.CurrentSequenceId) && IsSelected) OnActivated();
|
||||
}
|
||||
|
||||
/// <summary>Waits for <paramref name="task"/>; its failure was reported where it happened.</summary>
|
||||
private static async Task Settled(Task task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
Log.Debug("editor", $"An earlier open of the editor failed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SwitchAsync(Task previous, Guid sequenceId)
|
||||
{
|
||||
await Settled(previous);
|
||||
if (sequenceId == _openId) return;
|
||||
// The open sequence keeps its editor while its last edit is neither stored nor deliberately discarded.
|
||||
if (!await ConfirmCloseAsync()) return;
|
||||
var services = Shell.Services;
|
||||
IsLoading = true;
|
||||
OnPropertyChanged(nameof(IsEmpty));
|
||||
try
|
||||
{
|
||||
if (Editor is { } old)
|
||||
{
|
||||
Editor = null;
|
||||
await old.DisposeAsync();
|
||||
}
|
||||
_openId = sequenceId;
|
||||
if (services.Database is not { } db) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"));
|
||||
var processing = services.Processing?.Options;
|
||||
var source = new LocalSequenceSource(db, sequenceId, processing?.Archive.Decoder ?? DecoderFactory.CreateClient(null), services.Engine,
|
||||
processing?.Archive);
|
||||
var content = await source.LoadAsync(CancellationToken.None);
|
||||
var encoders = processing?.Encoders ?? [];
|
||||
var session = new EditorSession(source, content, [.. encoders.SelectMany(e => e.Capabilities)]);
|
||||
var editor = new EditorViewModel(Shell, session, encoders, processing?.Host ?? EncoderHost.Client);
|
||||
Editor = editor;
|
||||
await editor.AttachJobsAsync();
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// Whatever failed (index, decoder set-up, advisor, the job list), the section stays usable: an editor that
|
||||
// opened stays open, otherwise the next Open tries again.
|
||||
if (Editor is null) _openId = null;
|
||||
Log.Warning("editor", $"Sequence {sequenceId} could not be opened", ex);
|
||||
Shell.ReportError(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoading = false;
|
||||
OnPropertyChanged(nameof(IsEmpty));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user