-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathTerminalView.xaml.cs
202 lines (168 loc) · 6 KB
/
TerminalView.xaml.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using CommunityToolkit.WinUI;
using Files.App.Data.Items;
using Files.App.Terminal;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.IO;
using Windows.ApplicationModel;
namespace Files.App.UserControls
{
/// <summary>
/// Disclaimer: code from https://github.com/felixse/FluentTerminal
/// </summary>
public sealed partial class TerminalView : UserControl, IxtermEventListener
{
// Members related to initialization
private readonly TaskCompletionSource<object> _tcsConnected = new TaskCompletionSource<object>();
private readonly TaskCompletionSource<object> _tcsNavigationCompleted = new TaskCompletionSource<object>();
public event EventHandler<object> OnOutput;
public event EventHandler<string> OnPaste;
public event EventHandler<string> OnSessionRestart;
public WebView2 WebView => WebViewControl;
private Terminal.Terminal _terminal { get; set; }
public TerminalView()
{
InitializeComponent();
}
private async void WebViewControl_LoadedAsync(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
await WebViewControl.EnsureCoreWebView2Async();
WebViewControl.CoreWebView2.OpenDevToolsWindow();
WebViewControl.NavigationCompleted += WebViewControl_NavigationCompleted;
WebViewControl.NavigationStarting += WebViewControl_NavigationStarting;
WebViewControl.CoreWebView2.SetVirtualHostNameToFolderMapping(
"terminal.files",
Path.Combine(Package.Current.InstalledLocation.Path, "Files.App", "Terminal", "UI"),
CoreWebView2HostResourceAccessKind.DenyCors);
WebViewControl.Source = new Uri("http://terminal.files/index.html");
// Waiting for WebView.NavigationCompleted event to happen
await _tcsNavigationCompleted.Task.ConfigureAwait(false);
var provider = new DefaultValueProvider();
var options = provider.GetDefaultTerminalOptions();
var keyBindings = provider.GetCommandKeyBindings();
var theme = provider.GetPreInstalledThemes().First();
var profile = provider.GetPreinstalledShellProfiles().First();
var size = await CreateXtermViewAsync(options, theme.Colors,
keyBindings.Values.SelectMany(k => k)).ConfigureAwait(false);
// Waiting for IxtermEventListener.OnInitialized() call to happen
await _tcsConnected.Task;
StartShellProcess(size, profile);
}
private Task<TerminalSize> CreateXtermViewAsync(TerminalOptions options, TerminalColors theme, IEnumerable<KeyBinding> keyBindings)
{
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var serializedOptions = JsonConvert.SerializeObject(options, serializerSettings);
var serializedTheme = JsonConvert.SerializeObject(theme);
var serializedKeyBindings = JsonConvert.SerializeObject(keyBindings);
return ExecuteScriptAsync(
$"createTerminal('{serializedOptions}', '{serializedTheme}', '{serializedKeyBindings}')")
.ContinueWith(t => JsonConvert.DeserializeObject<TerminalSize>(t.Result));
}
private void WebViewControl_NavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
{
var _terminalBridge = new TerminalBridge(this);
_ = WebViewControl.AddWebAllowedObject("terminalBridge", _terminalBridge);
}
private void WebViewControl_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
{
_tcsNavigationCompleted.TrySetResult(null);
}
void IxtermEventListener.OnKeyboardCommand(string command)
{
if (Enum.TryParse(command, true, out Command commandValue))
{
}
else if (Guid.TryParse(command, out Guid shellProfileId))
{
}
}
void IxtermEventListener.OnMouseClick(MouseButton mouseButton, int x, int y, bool hasSelection, string hoveredUri)
{
var action = MouseAction.None;
switch (mouseButton)
{
case MouseButton.Middle:
break;
case MouseButton.Right:
break;
}
if (action == MouseAction.Paste)
{
((IxtermEventListener)this).OnKeyboardCommand(nameof(Command.Paste));
}
else if (action == MouseAction.CopySelectionOrPaste)
{
if (hasSelection)
{
((IxtermEventListener)this).OnKeyboardCommand(nameof(Command.Copy));
}
else
{
((IxtermEventListener)this).OnKeyboardCommand(nameof(Command.Paste));
}
}
}
async void IxtermEventListener.OnSelectionChanged(string selection)
{
if (!string.IsNullOrEmpty(selection))
{
await ExecuteScriptAsync("term.clearSelection()").ConfigureAwait(false);
}
}
void IxtermEventListener.OnTerminalResized(int columns, int rows)
{
_terminal.Resize(columns, rows);
}
void IxtermEventListener.OnInitialized()
{
_tcsConnected.TrySetResult(null);
}
void IxtermEventListener.OnTitleChanged(string title)
{
}
private void Terminal_OutputReceived(object sender, byte[] e)
{
OnOutput?.Invoke(this, e);
}
void IxtermEventListener.OnError(string error)
{
}
public void OnInput(byte[] data)
{
_terminal.WriteToPseudoConsole(data);
}
private async Task<string> ExecuteScriptAsync(string script)
{
try
{
var scriptTask = DispatcherQueue.EnqueueAsync(() => WebViewControl.InvokeScriptAsync("eval", new[] { script }))
.ConfigureAwait(false);
return await scriptTask;
}
catch (Exception e)
{
}
return string.Empty;
}
private void StartShellProcess(TerminalSize size, ShellProfile profile)
{
var sessionType = SessionType.ConPty;
var ShellExecutableName = Path.GetFileNameWithoutExtension(profile.Location);
var cwd = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var args = !string.IsNullOrWhiteSpace(profile.Location)
? $"\"{profile.Location}\" {profile.Arguments}"
: profile.Arguments;
BufferedReader _reader;
_terminal = new Terminal.Terminal();
_terminal.OutputReady += (s, e) =>
{
_reader = new BufferedReader(_terminal.ConsoleOutStream, b => Terminal_OutputReceived(this, b), true);
};
Task.Factory.StartNew(() => _terminal.Start(args, cwd,
80, 30));
}
}
}