sourcegit/src/Views/RevisionFiles.axaml.cs

96 lines
2.7 KiB
C#
Raw Normal View History

using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Media;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
namespace SourceGit.Views
{
public class RevisionTextFileView : TextEditor
{
protected override Type StyleKeyOverride => typeof(TextEditor);
public RevisionTextFileView() : base(new TextArea(), new TextDocument())
{
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);
TextArea.TextView.Options.EnableHyperlinks = false;
TextArea.TextView.Options.EnableEmailHyperlinks = false;
}
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
GC.Collect();
}
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
var source = DataContext as Models.RevisionTextFile;
if (source != null)
Text = source.Content;
else
Text = string.Empty;
}
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
{
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;
copy.Click += (o, ev) =>
{
App.CopyText(selected);
ev.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(copy);
TextArea.TextView.OpenContextMenu(menu);
e.Handled = true;
}
}
public partial class RevisionFiles : UserControl
{
public RevisionFiles()
{
InitializeComponent();
}
}
}