forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIAssistant.axaml.cs
More file actions
176 lines (143 loc) · 5.32 KB
/
Copy pathAIAssistant.axaml.cs
File metadata and controls
176 lines (143 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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;
using AvaloniaEdit.Rendering;
using AvaloniaEdit.TextMate;
namespace SourceGit.Views
{
public class AIResponseView : TextEditor
{
public class LineStyleTransformer : DocumentColorizingTransformer
{
protected override void ColorizeLine(DocumentLine line)
{
var content = CurrentContext.Document.GetText(line);
if (content.StartsWith("Read changes in file: ", StringComparison.Ordinal))
{
ChangeLinePart(line.Offset + 22, line.EndOffset, v =>
{
v.TextRunProperties.SetForegroundBrush(Brushes.DeepSkyBlue);
v.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
});
}
}
}
public static readonly StyledProperty<string> ContentProperty =
AvaloniaProperty.Register<AIResponseView, string>(nameof(Content), string.Empty);
public string Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
protected override Type StyleKeyOverride => typeof(TextEditor);
public AIResponseView() : base(new TextArea(), new TextDocument())
{
IsReadOnly = true;
ShowLineNumbers = false;
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
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;
if (_textMate == null)
{
_textMate = Models.TextMateHelper.CreateForEditor(this);
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md");
TextArea.TextView.LineTransformers.Add(new LineStyleTransformer());
}
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
GC.Collect();
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ContentProperty)
Text = Content;
}
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
{
if (DataContext is not ViewModels.AIAssistant vm)
return;
var selected = SelectedText;
if (string.IsNullOrEmpty(selected))
return;
var apply = new MenuItem() { Header = App.Text("AIAssistant.Use") };
apply.Icon = this.CreateMenuIcon("Icons.Check");
apply.Click += (_, ev) =>
{
vm.Use(selected);
ev.Handled = true;
};
var copy = new MenuItem() { Header = App.Text("Copy") };
copy.Icon = this.CreateMenuIcon("Icons.Copy");
copy.Click += async (_, ev) =>
{
await this.CopyTextAsync(selected);
ev.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(apply);
menu.Items.Add(copy);
menu.Open(TextArea.TextView);
e.Handled = true;
}
private TextMate.Installation _textMate = null;
}
public partial class AIAssistant : ChromelessWindow
{
public AIAssistant()
{
CloseOnESC = true;
InitializeComponent();
}
protected override async void OnOpened(EventArgs e)
{
base.OnOpened(e);
if (DataContext is ViewModels.AIAssistant vm)
await vm.GenAsync();
}
protected override void OnClosing(WindowClosingEventArgs e)
{
base.OnClosing(e);
(DataContext as ViewModels.AIAssistant)?.Cancel();
}
private async void OnModelChanged(object sender, SelectionChangedEventArgs e)
{
if (DataContext is ViewModels.AIAssistant vm && IsLoaded)
await vm.GenAsync();
e.Handled = true;
}
private void OnUseClicked(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.AIAssistant vm && !string.IsNullOrEmpty(vm.Response))
vm.Use(vm.Response);
e.Handled = true;
}
private async void OnRegenClicked(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.AIAssistant vm)
await vm.GenAsync();
e.Handled = true;
}
}
}