|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Interactivity; |
| 3 | +using Avalonia.Markup.Xaml; |
| 4 | +using System; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace SubtitleEdit.Plugins.TypewriterEffect; |
| 8 | + |
| 9 | +public partial class MainWindow : Window |
| 10 | +{ |
| 11 | + private readonly PluginRequest _request; |
| 12 | + |
| 13 | + // Parameterless constructor only exists for the XAML designer. |
| 14 | + public MainWindow() : this(new PluginRequest()) { } |
| 15 | + |
| 16 | + public MainWindow(PluginRequest request) |
| 17 | + { |
| 18 | + InitializeComponent(); |
| 19 | + _request = request; |
| 20 | + |
| 21 | + var selectedCount = request.SelectedIndices.Count; |
| 22 | + InfoLabel.Text = selectedCount > 0 |
| 23 | + ? $"Each of the {selectedCount} selected line(s) will be split into several short lines that progressively reveal the text." |
| 24 | + : "Every line will be split into several short lines that progressively reveal the text."; |
| 25 | + |
| 26 | + EndDelayInput.Value = (decimal)LoadEndDelaySetting(request.Settings, defaultValue: 0.5); |
| 27 | + } |
| 28 | + |
| 29 | + private void InitializeComponent() => AvaloniaXamlLoader.Load(this); |
| 30 | + |
| 31 | + private void OnCancel(object? sender, RoutedEventArgs e) |
| 32 | + { |
| 33 | + App.Response = new PluginResponse { Status = "cancelled" }; |
| 34 | + Close(); |
| 35 | + } |
| 36 | + |
| 37 | + private void OnOk(object? sender, RoutedEventArgs e) |
| 38 | + { |
| 39 | + var endDelaySec = (double)(EndDelayInput.Value ?? 0m); |
| 40 | + |
| 41 | + try |
| 42 | + { |
| 43 | + var blocks = SubRipParser.Parse(_request.Subtitle.SubRip); |
| 44 | + var selected = new HashSet<int>(_request.SelectedIndices); |
| 45 | + var (resultBlocks, changed) = TypewriterEngine.Apply(blocks, selected, endDelaySec); |
| 46 | + |
| 47 | + App.Response = new PluginResponse |
| 48 | + { |
| 49 | + Status = "ok", |
| 50 | + Message = changed == 0 |
| 51 | + ? "No lines changed." |
| 52 | + : $"Typewriter applied to {changed} line(s).", |
| 53 | + UndoDescription = "Typewriter effect", |
| 54 | + Subtitle = new PluginSubtitle |
| 55 | + { |
| 56 | + Format = "SubRip", |
| 57 | + Native = SubRipParser.Serialize(resultBlocks), |
| 58 | + }, |
| 59 | + Settings = BuildSettings(endDelaySec), |
| 60 | + }; |
| 61 | + } |
| 62 | + catch (Exception ex) |
| 63 | + { |
| 64 | + App.Response = new PluginResponse { Status = "error", Message = ex.Message }; |
| 65 | + } |
| 66 | + |
| 67 | + Close(); |
| 68 | + } |
| 69 | + |
| 70 | + private static double LoadEndDelaySetting(JsonElement? settings, double defaultValue) |
| 71 | + { |
| 72 | + if (settings is null || settings.Value.ValueKind != JsonValueKind.Object) |
| 73 | + { |
| 74 | + return defaultValue; |
| 75 | + } |
| 76 | + if (settings.Value.TryGetProperty("endDelay", out var value) && |
| 77 | + value.ValueKind == JsonValueKind.Number && |
| 78 | + value.TryGetDouble(out var parsed)) |
| 79 | + { |
| 80 | + return parsed; |
| 81 | + } |
| 82 | + return defaultValue; |
| 83 | + } |
| 84 | + |
| 85 | + private static JsonElement BuildSettings(double endDelay) |
| 86 | + { |
| 87 | + using var doc = JsonDocument.Parse($"{{\"endDelay\":{endDelay.ToString("0.###", System.Globalization.CultureInfo.InvariantCulture)}}}"); |
| 88 | + return doc.RootElement.Clone(); |
| 89 | + } |
| 90 | +} |
0 commit comments