|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +using Avalonia.Interactivity; |
| 5 | + |
| 6 | +namespace SourceGit.Views |
| 7 | +{ |
| 8 | + public partial class CommitMessageEditor : ChromelessWindow |
| 9 | + { |
| 10 | + public CommitMessageEditor() |
| 11 | + { |
| 12 | + InitializeComponent(); |
| 13 | + } |
| 14 | + |
| 15 | + public void AsStandalone(string file) |
| 16 | + { |
| 17 | + _onSave = msg => File.WriteAllText(file, msg); |
| 18 | + _shouldExitApp = true; |
| 19 | + |
| 20 | + var content = File.ReadAllText(file).ReplaceLineEndings("\n").Trim(); |
| 21 | + var firstLineEnd = content.IndexOf('\n', StringComparison.Ordinal); |
| 22 | + if (firstLineEnd == -1) |
| 23 | + { |
| 24 | + Editor.SubjectEditor.Text = content; |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + Editor.SubjectEditor.Text = content.Substring(0, firstLineEnd); |
| 29 | + Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1).Trim(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public void AsBuiltin(string msg, Action<string> onSave) |
| 34 | + { |
| 35 | + _onSave = onSave; |
| 36 | + _shouldExitApp = false; |
| 37 | + |
| 38 | + var firstLineEnd = msg.IndexOf('\n', StringComparison.Ordinal); |
| 39 | + if (firstLineEnd == -1) |
| 40 | + { |
| 41 | + Editor.SubjectEditor.Text = msg; |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + Editor.SubjectEditor.Text = msg.Substring(0, firstLineEnd); |
| 46 | + Editor.DescriptionEditor.Text = msg.Substring(firstLineEnd + 1).Trim(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected override void OnClosed(EventArgs e) |
| 51 | + { |
| 52 | + base.OnClosed(e); |
| 53 | + |
| 54 | + if (_shouldExitApp) |
| 55 | + App.Quit(_exitCode); |
| 56 | + } |
| 57 | + |
| 58 | + private void SaveAndClose(object _1, RoutedEventArgs _2) |
| 59 | + { |
| 60 | + _onSave?.Invoke(Editor.Text); |
| 61 | + _exitCode = 0; |
| 62 | + Close(); |
| 63 | + } |
| 64 | + |
| 65 | + private Action<string> _onSave = null; |
| 66 | + private bool _shouldExitApp = true; |
| 67 | + private int _exitCode = -1; |
| 68 | + } |
| 69 | +} |
0 commit comments