-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTedApp.cs
More file actions
317 lines (264 loc) · 11.5 KB
/
TedApp.cs
File metadata and controls
317 lines (264 loc) · 11.5 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// This is a demo of ted, the Terminal.Gui.Editor example.
using System.Text;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;
using Terminal.Gui.Drawing;
using Terminal.Gui.Editor;
using Terminal.Gui.Editor.Document;
using Terminal.Gui.Editor.Document.Folding;
using Terminal.Gui.Editor.Indentation;
using Terminal.Gui.Input;
using Terminal.Gui.Resources;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
namespace Ted;
/// <summary>
/// Top-level <see cref="Window" /> for the <c>ted</c> demo. MenuBar at top,
/// <see cref="Editor" /> in the middle, StatusBar at the bottom. Single-file —
/// no tabs (compare to Terminal.Gui's Notepad scenario).
/// </summary>
public sealed partial class TedApp : Window
{
// Per-instance config path. Defaults to the real ~/.tui location; tests inject a temp path so they
// never touch the developer's real config (and stay parallel-safe — no env/static mutation).
private readonly string _configPath;
private readonly Shortcut _fileNameShortcut;
private readonly MenuItem _previewMarkdownMenuItem;
/// <summary>Initializes a new <see cref="TedApp" />.</summary>
/// <param name="readOnly">Opens the editor read-only.</param>
/// <param name="configPath">
/// Overrides where view settings persist. <see langword="null" /> uses
/// <see cref="EditorSettings.GetConfigPath" /> (the real <c>~/.tui/ted.config.json</c>).
/// </param>
public TedApp (bool readOnly = false, string? configPath = null)
{
_configPath = configPath ?? EditorSettings.GetConfigPath ();
Title = "ted — Terminal.Gui.Editor demo";
BorderStyle = LineStyle.None;
// Editor first so menu/status-bar shortcuts can pull their hotkeys directly from
// Editor's KeyBindings (any commands the editor doesn't claim fall back to Application).
Editor = new Editor
{
ConvertTabsToSpaces = EditorSettings.ConvertTabsToSpaces,
IndentationSize = EditorSettings.IndentSize,
WordWrap = EditorSettings.WordWrap,
ShowTabs = EditorSettings.ShowTabs,
ReadOnly = readOnly,
CompletionProvider = EditorSettings.AutoComplete ? new WordCompletionProvider () : null,
ViewportSettings = EditorSettings.Scrollbars
? ViewportSettingsFlags.HasScrollBars
: ViewportSettingsFlags.None
};
GutterOptions initialGutter = GutterOptions.None;
if (EditorSettings.LineNumbers)
{
initialGutter |= GutterOptions.LineNumbers;
}
if (EditorSettings.FoldIndicators)
{
initialGutter |= GutterOptions.Folding;
}
Editor.GutterOptions = initialGutter;
Editor.IndentationStrategy =
EditorSettings.AutoIndent ? new DefaultIndentationStrategy () : null;
// Enable brace-based folding. The editor handles the full lifecycle automatically.
Editor.FoldingStrategy = new BraceFoldingStrategy ();
ShowOpenDialog = ShowDefaultOpenDialog;
ShowSaveDialog = ShowDefaultSaveDialog;
ShowSaveChangesDialog = ShowDefaultSaveChangesDialog;
// --- EditorMenuBar: pre-wired File/Edit/View menus ---
_previewMarkdownMenuItem = new MenuItem
{
Title = ToggleTitle (false, "_Preview Markdown"),
Enabled = false
};
_previewMarkdownMenuItem.Action = () =>
{
if (PreviewCheckBox.Visible)
{
PreviewCheckBox.Value = PreviewCheckBox.Value == CheckState.Checked
? CheckState.UnChecked
: CheckState.Checked;
}
};
PreviewCheckBox.ValueChanged += (_, e) =>
{
ToggleMarkdownPreview ();
_previewMarkdownMenuItem.Title = ToggleTitle (e.NewValue == CheckState.Checked, "_Preview Markdown");
};
Menu = new EditorMenuBar (Editor)
{
ShowOpenDialog = () => ShowOpenDialog (),
ShowSaveDialog = () => ShowSaveDialog ()
};
Menu.NewRequested += (_, _) => New ();
Menu.Opening += (_, e) =>
{
if (!ConfirmSaveChanges ())
{
e.Cancel = true;
}
};
Menu.OpenRequested += (_, e) =>
{
CurrentLoadTask = OpenFileAsync (e.FilePath, true);
};
Menu.SaveRequested += (_, _) => Save ();
Menu.SaveAsRequested += (_, e) => { _ = SaveFileAsAsync (e.FilePath, true); };
Menu.QuitRequested += (_, _) => Quit ();
Menu.ViewSettingsChanged += (_, _) => SaveViewSettings ();
// Ted-specific extra menus: View extras, Options, Help, file-name shortcut
Menu.ViewMenu.PopoverMenu!.Root!.Add (_previewMarkdownMenuItem);
Menu.Add (new MenuBarItem ("_Options",
[new MenuItem ("_Settings...", string.Empty, ShowSettingsDialog)]));
Menu.Add (new MenuBarItem (Strings.menuHelp,
[new MenuItem ("_About", "About ted", ShowAboutDialog)]));
_fileNameShortcut = new Shortcut (Key.Empty, "<untitled>", Open)
{
MouseHighlightStates = MouseState.None,
SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Dialog)
};
Menu.Add (_fileNameShortcut);
// --- EditorStatusBar: pre-wired indicators ---
StatusBar = new EditorStatusBar (Editor);
Editor.Y = Pos.Bottom (Menu);
Editor.Width = Dim.Fill ();
Editor.Height = Dim.Fill (StatusBar);
Add (Menu, Editor, StatusBar);
// Ted-specific event handlers
Editor.ModifiedChanged += (_, _) => UpdateModifiedStatus ();
Editor.ContentChanged += (_, e) => UpdateContentSizeStatus (e);
Editor.FindRequested += (_, _) => ShowFindReplaceDialog (false);
Editor.ReplaceRequested += (_, _) => ShowFindReplaceDialog (true);
}
/// <summary>The editor View at the centre of the app. Exposed for tests and future commands.</summary>
public Editor Editor { get; }
/// <summary>The pre-wired menu bar. Exposed for tests.</summary>
public EditorMenuBar Menu { get; }
/// <summary>The pre-wired status bar. Exposed for tests.</summary>
public EditorStatusBar StatusBar { get; }
/// <summary>The status-bar shortcut that displays the current syntax-highlighting language name.</summary>
public Shortcut LanguageShortcut => StatusBar.LanguageShortcut;
/// <summary>The status-bar dropdown that selects <see cref="ThemeManager.Theme" />.</summary>
public DropDownList ThemeDropDown => StatusBar.ThemeDropDown;
/// <summary>The spinner view shown while streaming file load/save is running.</summary>
public SpinnerView LoadStatusSpinner => StatusBar.LoadStatusSpinner;
/// <summary>The status-bar shortcut that hosts <see cref="LoadStatusSpinner" />.</summary>
public Shortcut LoadSpinnerShortcut => StatusBar.LoadSpinnerShortcut;
/// <summary>
/// The status-bar shortcut that mirrors the editor's caret position. Both line and column are
/// 1-based. Updated whenever <see cref="Editor.CaretChanged" /> fires (user-driven movement
/// and document edits that shift the caret).
/// </summary>
public Shortcut LocShortcut => StatusBar.LocShortcut;
private void ShowAboutDialog ()
{
Dialog dialog = new ()
{
Title = "About ted",
Buttons = [new Button { Title = Strings.btnOk, IsDefault = true }]
};
dialog.Border.Settings &= ~BorderSettings.Title;
Version? tgVersion = typeof (Application).Assembly.GetName ().Version;
Label tagline = new ()
{
Text = $"A terminal text editor built with Terminal.Gui {tgVersion}",
TextAlignment = Alignment.Center,
X = Pos.Center (),
Width = Dim.Auto (DimAutoStyle.Text),
Height = Dim.Auto (DimAutoStyle.Text)
};
TedLogo logo = new () { X = Pos.Center (), Y = Pos.Bottom (tagline) + 1 };
Version? editorVersion = typeof (Editor).Assembly.GetName ().Version;
View version = new ()
{
Width = Dim.Auto (),
Height = Dim.Auto (),
Text = $"Terminal.Gui.Editor {editorVersion}",
X = Pos.Center (),
Y = Pos.Bottom (logo) + 1
};
Link link = new ()
{
Text = "https://github.com/gui-cs/Editor",
Url = "https://github.com/gui-cs/Editor",
X = Pos.Center (),
Y = Pos.Bottom (version) + 1
};
dialog.Add (tagline, logo, version, link);
dialog.Buttons.ElementAt (0).SetFocus ();
App?.Run (dialog);
dialog.Dispose ();
}
private void UpdateFileNameShortcut ()
{
_fileNameShortcut.Title = CurrentFilePath ?? "<untitled>";
_fileNameShortcut.SetNeedsDraw ();
}
private void UpdateModifiedStatus ()
{
// Don't override the status while a streaming load/save is in progress --
// the streaming operation owns the spinner and operation-id sequence.
if (LoadStatusSpinner.AutoSpin)
{
return;
}
var verb = Editor.IsModified ? "Modified" : _lastStatusVerb;
var status = FormatCompletedProgress (verb, _lastFileByteSize);
// Set directly rather than going through CompleteStreamingStatus, which would
// bump the operation-id and potentially invalidate a pending streaming completion.
// ModifiedChanged always fires on the UI thread, so no marshalling is needed.
LoadSpinnerShortcut.Title = status;
LoadSpinnerShortcut.HelpText = status;
LoadSpinnerShortcut.SetNeedsDraw ();
}
private void UpdateContentSizeStatus (DocumentChangeEventArgs e)
{
if (LoadStatusSpinner.AutoSpin)
{
return;
}
if (_lastFileByteSize is not { } currentSize)
{
return;
}
Encoding encoding = Editor.Document?.Encoding ?? Encoding.UTF8;
long insertedBytes = encoding.GetByteCount (e.InsertedText.Text);
long removedBytes = encoding.GetByteCount (e.RemovedText.Text);
_lastFileByteSize = Math.Max (0, currentSize + insertedBytes - removedBytes);
UpdateModifiedStatus ();
}
private static string ToggleTitle (bool on, string label)
{
return on ? $"✓ {label}" : $" {label}";
}
private void SaveViewSettings ()
{
EditorSettings.LineNumbers = Editor.GutterOptions.HasFlag (GutterOptions.LineNumbers);
EditorSettings.FoldIndicators = Editor.GutterOptions.HasFlag (GutterOptions.Folding);
EditorSettings.WordWrap = Editor.WordWrap;
EditorSettings.ShowTabs = Editor.ShowTabs;
EditorSettings.IndentSize = Editor.IndentationSize;
EditorSettings.ConvertTabsToSpaces = Editor.ConvertTabsToSpaces;
EditorSettings.AutoIndent = Editor.IndentationStrategy is not null;
EditorSettings.AutoComplete = Editor.CompletionProvider is not null;
EditorSettings.Scrollbars =
Editor.ViewportSettings.HasFlag (ViewportSettingsFlags.HasScrollBars);
EditorSettings.Save (_configPath);
}
private void ShowSettingsDialog ()
{
if (App is null)
{
throw new InvalidOperationException ("ted must be running before showing settings.");
}
EditorSettingsDialog dialog = new (Editor);
App.Run (dialog);
if (dialog.WasAccepted)
{
dialog.ApplyTo (Editor);
SaveViewSettings ();
}
dialog.Dispose ();
}
}