Skip to content

Commit 620f411

Browse files
committed
code_style: move RevisionTextFileView
1 parent e033a93 commit 620f411

File tree

2 files changed

+110
-110
lines changed

2 files changed

+110
-110
lines changed

src/Views/RevisionFileContentViewer.axaml.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,117 @@
1+
using System;
2+
3+
using Avalonia;
14
using Avalonia.Controls;
5+
using Avalonia.Controls.Primitives;
6+
using Avalonia.Interactivity;
7+
using Avalonia.Media;
8+
9+
using AvaloniaEdit;
10+
using AvaloniaEdit.Document;
11+
using AvaloniaEdit.Editing;
12+
using AvaloniaEdit.TextMate;
213

314
namespace SourceGit.Views
415
{
16+
public class RevisionTextFileView : TextEditor
17+
{
18+
protected override Type StyleKeyOverride => typeof(TextEditor);
19+
20+
public RevisionTextFileView() : base(new TextArea(), new TextDocument())
21+
{
22+
IsReadOnly = true;
23+
ShowLineNumbers = true;
24+
WordWrap = false;
25+
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
26+
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
27+
28+
TextArea.LeftMargins[0].Margin = new Thickness(8, 0);
29+
TextArea.TextView.Margin = new Thickness(4, 0);
30+
TextArea.TextView.Options.EnableHyperlinks = false;
31+
TextArea.TextView.Options.EnableEmailHyperlinks = false;
32+
}
33+
34+
protected override void OnLoaded(RoutedEventArgs e)
35+
{
36+
base.OnLoaded(e);
37+
38+
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
39+
UpdateTextMate();
40+
}
41+
42+
protected override void OnUnloaded(RoutedEventArgs e)
43+
{
44+
base.OnUnloaded(e);
45+
46+
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
47+
48+
if (_textMate != null)
49+
{
50+
_textMate.Dispose();
51+
_textMate = null;
52+
}
53+
54+
GC.Collect();
55+
}
56+
57+
protected override void OnDataContextChanged(EventArgs e)
58+
{
59+
base.OnDataContextChanged(e);
60+
61+
if (DataContext is Models.RevisionTextFile source)
62+
{
63+
UpdateTextMate();
64+
Text = source.Content;
65+
}
66+
else
67+
{
68+
Text = string.Empty;
69+
}
70+
}
71+
72+
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
73+
{
74+
var selected = SelectedText;
75+
if (string.IsNullOrEmpty(selected))
76+
return;
77+
78+
var copy = new MenuItem() { Header = App.Text("Copy") };
79+
copy.Click += (_, ev) =>
80+
{
81+
App.CopyText(selected);
82+
ev.Handled = true;
83+
};
84+
85+
if (this.FindResource("Icons.Copy") is Geometry geo)
86+
{
87+
copy.Icon = new Avalonia.Controls.Shapes.Path()
88+
{
89+
Width = 10,
90+
Height = 10,
91+
Stretch = Stretch.Uniform,
92+
Data = geo,
93+
};
94+
}
95+
96+
var menu = new ContextMenu();
97+
menu.Items.Add(copy);
98+
menu.Open(TextArea.TextView);
99+
100+
e.Handled = true;
101+
}
102+
103+
private void UpdateTextMate()
104+
{
105+
if (_textMate == null)
106+
_textMate = Models.TextMateHelper.CreateForEditor(this);
107+
108+
if (DataContext is Models.RevisionTextFile file)
109+
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
110+
}
111+
112+
private TextMate.Installation _textMate = null;
113+
}
114+
5115
public partial class RevisionFileContentViewer : UserControl
6116
{
7117
public RevisionFileContentViewer()

src/Views/RevisionFiles.axaml.cs

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,8 @@
1-
using System;
2-
3-
using Avalonia;
41
using Avalonia.Controls;
5-
using Avalonia.Controls.Primitives;
62
using Avalonia.Input;
7-
using Avalonia.Interactivity;
8-
using Avalonia.Media;
9-
10-
using AvaloniaEdit;
11-
using AvaloniaEdit.Document;
12-
using AvaloniaEdit.Editing;
13-
using AvaloniaEdit.TextMate;
143

154
namespace SourceGit.Views
165
{
17-
public class RevisionTextFileView : TextEditor
18-
{
19-
protected override Type StyleKeyOverride => typeof(TextEditor);
20-
21-
public RevisionTextFileView() : base(new TextArea(), new TextDocument())
22-
{
23-
IsReadOnly = true;
24-
ShowLineNumbers = true;
25-
WordWrap = false;
26-
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
27-
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
28-
29-
TextArea.LeftMargins[0].Margin = new Thickness(8, 0);
30-
TextArea.TextView.Margin = new Thickness(4, 0);
31-
TextArea.TextView.Options.EnableHyperlinks = false;
32-
TextArea.TextView.Options.EnableEmailHyperlinks = false;
33-
}
34-
35-
protected override void OnLoaded(RoutedEventArgs e)
36-
{
37-
base.OnLoaded(e);
38-
39-
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
40-
UpdateTextMate();
41-
}
42-
43-
protected override void OnUnloaded(RoutedEventArgs e)
44-
{
45-
base.OnUnloaded(e);
46-
47-
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
48-
49-
if (_textMate != null)
50-
{
51-
_textMate.Dispose();
52-
_textMate = null;
53-
}
54-
55-
GC.Collect();
56-
}
57-
58-
protected override void OnDataContextChanged(EventArgs e)
59-
{
60-
base.OnDataContextChanged(e);
61-
62-
if (DataContext is Models.RevisionTextFile source)
63-
{
64-
UpdateTextMate();
65-
Text = source.Content;
66-
}
67-
else
68-
{
69-
Text = string.Empty;
70-
}
71-
}
72-
73-
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
74-
{
75-
var selected = SelectedText;
76-
if (string.IsNullOrEmpty(selected))
77-
return;
78-
79-
var copy = new MenuItem() { Header = App.Text("Copy") };
80-
copy.Click += (_, ev) =>
81-
{
82-
App.CopyText(selected);
83-
ev.Handled = true;
84-
};
85-
86-
if (this.FindResource("Icons.Copy") is Geometry geo)
87-
{
88-
copy.Icon = new Avalonia.Controls.Shapes.Path()
89-
{
90-
Width = 10,
91-
Height = 10,
92-
Stretch = Stretch.Uniform,
93-
Data = geo,
94-
};
95-
}
96-
97-
var menu = new ContextMenu();
98-
menu.Items.Add(copy);
99-
menu.Open(TextArea.TextView);
100-
101-
e.Handled = true;
102-
}
103-
104-
private void UpdateTextMate()
105-
{
106-
if (_textMate == null)
107-
_textMate = Models.TextMateHelper.CreateForEditor(this);
108-
109-
if (DataContext is Models.RevisionTextFile file)
110-
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
111-
}
112-
113-
private TextMate.Installation _textMate = null;
114-
}
115-
1166
public partial class RevisionFiles : UserControl
1177
{
1188
public RevisionFiles()

0 commit comments

Comments
 (0)