2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Controls.Primitives;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Media;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using AvaloniaEdit;
|
|
|
|
using AvaloniaEdit.Document;
|
|
|
|
using AvaloniaEdit.Editing;
|
|
|
|
using AvaloniaEdit.TextMate;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
|
|
|
public class RevisionTextFileView : TextEditor
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
protected override Type StyleKeyOverride => typeof(TextEditor);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public RevisionTextFileView() : base(new TextArea(), new TextDocument())
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
IsReadOnly = true;
|
|
|
|
ShowLineNumbers = true;
|
|
|
|
WordWrap = false;
|
|
|
|
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
|
|
|
|
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
|
|
|
|
|
|
|
|
TextArea.LeftMargins[0].Margin = new Thickness(8, 0);
|
|
|
|
TextArea.TextView.Margin = new Thickness(4, 0);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnLoaded(e);
|
|
|
|
|
|
|
|
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
|
|
|
_textMate = Models.TextMateHelper.CreateForEditor(this);
|
|
|
|
if (DataContext is Models.RevisionTextFile source)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
|
|
|
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
|
|
|
if (_textMate != null)
|
|
|
|
{
|
|
|
|
_textMate.Dispose();
|
|
|
|
_textMate = null;
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
GC.Collect();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
|
|
|
|
var source = DataContext as Models.RevisionTextFile;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (source != null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
Text = source.Content;
|
2024-03-20 04:49:01 -07:00
|
|
|
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
Models.TextMateHelper.SetThemeByApp(_textMate);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var selected = SelectedText;
|
|
|
|
if (string.IsNullOrEmpty(selected)) return;
|
|
|
|
|
|
|
|
var icon = new Avalonia.Controls.Shapes.Path();
|
|
|
|
icon.Width = 10;
|
|
|
|
icon.Height = 10;
|
|
|
|
icon.Stretch = Stretch.Uniform;
|
|
|
|
icon.Data = App.Current?.FindResource("Icons.Copy") as StreamGeometry;
|
|
|
|
|
|
|
|
var copy = new MenuItem();
|
|
|
|
copy.Header = App.Text("Copy");
|
|
|
|
copy.Icon = icon;
|
2024-03-17 18:37:06 -07:00
|
|
|
copy.Click += (o, ev) =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
App.CopyText(selected);
|
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
menu.Items.Add(copy);
|
|
|
|
menu.Open(TextArea.TextView);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private TextMate.Installation _textMate = null;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class RevisionFiles : UserControl
|
|
|
|
{
|
|
|
|
public RevisionFiles()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTreeViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var detail = DataContext as ViewModels.CommitDetail;
|
|
|
|
var node = detail.SelectedRevisionFileNode;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!node.IsFolder)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var menu = detail.CreateRevisionFileContextMenu(node.Backend as Models.Object);
|
|
|
|
menu.Open(sender as Control);
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|