Skip to content

Commit 0f72cf8

Browse files
authored
Fixes #4425 - ApplicationImpl internal (#4426)
* Pulled from v2_release * Refactor migration guide for Terminal.Gui v2 Restructured and expanded the migration guide to provide a comprehensive resource for transitioning from Terminal.Gui v1 to v2. Key updates include: - Added a Table of Contents for easier navigation. - Summarized major architectural changes in v2, including the instance-based application model, IRunnable architecture, and 24-bit TrueColor support. - Updated examples to reflect new patterns, such as initializers replacing constructors and explicit disposal using `IDisposable`. - Documented changes to the layout system, including the removal of `Absolute`/`Computed` styles and the introduction of `Viewport`. - Standardized event patterns to use `object sender, EventArgs args`. - Detailed updates to the Keyboard, Mouse, and Navigation APIs, including configurable key bindings and viewport-relative mouse coordinates. - Replaced legacy components like `ScrollView` and `ContextMenu` with built-in scrolling and `PopoverMenu`. - Clarified disposal rules and introduced best practices for resource management. - Provided a complete migration example and a summary of breaking changes. This update aims to simplify the migration process by addressing breaking changes, introducing new features, and aligning with modern .NET conventions. * Refactor to use Application.Instance for lifecycle management Replaced all occurrences of `ApplicationImpl.Instance` with the new `Application.Instance` property across the codebase to align with the updated application lifecycle model. Encapsulated the `ApplicationImpl` class by making it `internal`, ensuring it is no longer directly accessible outside its assembly. Introduced the `[Obsolete]` `Application.Instance` property as a backward-compatible singleton for the legacy static `Application` model, while encouraging the use of `Application.Create()` for new code. Updated `MessageBox` methods to use `Application.Instance` for consistent modal dialog management. Improved documentation to reflect these changes and emphasize the transition to the instance-based application model. Performed code cleanup in multiple classes to ensure consistency and maintainability. These changes maintain backward compatibility while preparing the codebase for the eventual removal of the legacy `ApplicationImpl` class. * Fix doc bug * - Removed obsolete `.cd` class diagram files. - Introduced `IRunnable` interface for decoupling component execution. - Added fluent API for running dialogs and retrieving results. - Enhanced `View` with `App` and `Driver` properties for better decoupling. - Improved testability with support for mock and real applications. - Implemented `IDisposable` for proper resource cleanup. - Replaced `RunnableSessionStack` with `SessionStack` for session management. - Updated driver architecture to align with the new model. - Scoped `IKeyboard` to application contexts for modularity. - Updated documentation with migration strategies and best practices. These changes modernize the library, improve maintainability, and align with current development practices.
1 parent 8e92327 commit 0f72cf8

34 files changed

+278
-833
lines changed

Examples/UICatalog/Scenarios/ContextMenus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void AppWindowOnInitialized (object? sender, EventArgs e)
4949
var text = "Context Menu";
5050
var width = 20;
5151

52-
CreateWinContextMenu (ApplicationImpl.Instance);
52+
CreateWinContextMenu (Application.Instance);
5353

5454
var label = new Label
5555
{

Examples/UICatalog/Scenarios/CsvEditor.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private void AddColumn ()
215215
_tableView.Table.Columns
216216
);
217217

218-
int? result = MessageBox.Query (ApplicationImpl.Instance,
218+
int? result = MessageBox.Query (Application.Instance,
219219
"Column Type",
220220
"Pick a data type for the column",
221221
"Date",
@@ -308,7 +308,7 @@ private void DeleteColum ()
308308

309309
if (_tableView.SelectedColumn == -1)
310310
{
311-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No Column", "No column selected", "Ok");
311+
MessageBox.ErrorQuery (Application.Instance, "No Column", "No column selected", "Ok");
312312

313313
return;
314314
}
@@ -320,7 +320,7 @@ private void DeleteColum ()
320320
}
321321
catch (Exception ex)
322322
{
323-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Could not remove column", ex.Message, "Ok");
323+
MessageBox.ErrorQuery (Application.Instance, "Could not remove column", ex.Message, "Ok");
324324
}
325325
}
326326

@@ -342,7 +342,7 @@ private void EditCurrentCell (object? sender, CellActivatedEventArgs e)
342342
}
343343
catch (Exception ex)
344344
{
345-
MessageBox.ErrorQuery (ApplicationImpl.Instance, 60, 20, "Failed to set text", ex.Message, "Ok");
345+
MessageBox.ErrorQuery (Application.Instance, 60, 20, "Failed to set text", ex.Message, "Ok");
346346
}
347347

348348
_tableView.Update ();
@@ -388,7 +388,7 @@ private void MoveColumn ()
388388

389389
if (_tableView.SelectedColumn == -1)
390390
{
391-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No Column", "No column selected", "Ok");
391+
MessageBox.ErrorQuery (Application.Instance, "No Column", "No column selected", "Ok");
392392

393393
return;
394394
}
@@ -413,7 +413,7 @@ private void MoveColumn ()
413413
}
414414
catch (Exception ex)
415415
{
416-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error moving column", ex.Message, "Ok");
416+
MessageBox.ErrorQuery (Application.Instance, "Error moving column", ex.Message, "Ok");
417417
}
418418
}
419419

@@ -426,7 +426,7 @@ private void MoveRow ()
426426

427427
if (_tableView.SelectedRow == -1)
428428
{
429-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No Rows", "No row selected", "Ok");
429+
MessageBox.ErrorQuery (Application.Instance, "No Rows", "No row selected", "Ok");
430430

431431
return;
432432
}
@@ -462,15 +462,15 @@ private void MoveRow ()
462462
}
463463
catch (Exception ex)
464464
{
465-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error moving column", ex.Message, "Ok");
465+
MessageBox.ErrorQuery (Application.Instance, "Error moving column", ex.Message, "Ok");
466466
}
467467
}
468468

469469
private bool NoTableLoaded ()
470470
{
471471
if (_tableView?.Table is null)
472472
{
473-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No Table Loaded", "No table has currently be opened", "Ok");
473+
MessageBox.ErrorQuery (Application.Instance, "No Table Loaded", "No table has currently be opened", "Ok");
474474

475475
return true;
476476
}
@@ -582,7 +582,7 @@ private void Open (string filename)
582582
}
583583
catch (Exception ex)
584584
{
585-
MessageBox.ErrorQuery (ApplicationImpl.Instance,
585+
MessageBox.ErrorQuery (Application.Instance,
586586
"Open Failed",
587587
$"Error on line {lineNumber}{Environment.NewLine}{ex.Message}",
588588
"Ok"
@@ -612,7 +612,7 @@ private void Save ()
612612
{
613613
if (_tableView?.Table is null || string.IsNullOrWhiteSpace (_currentFile) || _currentTable is null)
614614
{
615-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No file loaded", "No file is currently loaded", "Ok");
615+
MessageBox.ErrorQuery (Application.Instance, "No file loaded", "No file is currently loaded", "Ok");
616616

617617
return;
618618
}
@@ -674,7 +674,7 @@ private void SetFormat ()
674674

675675
if (col.DataType == typeof (string))
676676
{
677-
MessageBox.ErrorQuery (ApplicationImpl.Instance,
677+
MessageBox.ErrorQuery (Application.Instance,
678678
"Cannot Format Column",
679679
"String columns cannot be Formatted, try adding a new column to the table with a date/numerical Type",
680680
"Ok"
@@ -711,7 +711,7 @@ private void Sort (bool asc)
711711

712712
if (_tableView.SelectedColumn == -1)
713713
{
714-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "No Column", "No column selected", "Ok");
714+
MessageBox.ErrorQuery (Application.Instance, "No Column", "No column selected", "Ok");
715715

716716
return;
717717
}

Examples/UICatalog/Scenarios/DynamicStatusBar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void UpdateTarget ()
7979
}
8080
catch (Exception ex)
8181
{
82-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Binding Error", $"Binding failed: {ex}.", "Ok");
82+
MessageBox.ErrorQuery (Application.Instance, "Binding Error", $"Binding failed: {ex}.", "Ok");
8383
}
8484
}
8585
}
@@ -140,7 +140,7 @@ public DynamicStatusBarDetails ()
140140
public TextView TextAction { get; }
141141
public TextField TextShortcut { get; }
142142
public TextField TextTitle { get; }
143-
public Action CreateAction (DynamicStatusItem item) { return () => MessageBox.ErrorQuery (ApplicationImpl.Instance, item.Title, item.Action, "Ok"); }
143+
public Action CreateAction (DynamicStatusItem item) { return () => MessageBox.ErrorQuery (Application.Instance, item.Title, item.Action, "Ok"); }
144144

145145
public void EditStatusItem (Shortcut statusItem)
146146
{

Examples/UICatalog/Scenarios/Editor.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private bool CanCloseFile ()
201201
Debug.Assert (_textView.IsDirty);
202202

203203
int? r = MessageBox.ErrorQuery (
204-
ApplicationImpl.Instance,
204+
Application.Instance,
205205
"Save File",
206206
$"Do you want save changes in {_appWindow.Title}?",
207207
"Yes",
@@ -236,7 +236,7 @@ private void CloseFile ()
236236
}
237237
catch (Exception ex)
238238
{
239-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error", ex.Message, "Ok");
239+
MessageBox.ErrorQuery (Application.Instance, "Error", ex.Message, "Ok");
240240
}
241241
}
242242

@@ -315,11 +315,11 @@ private void ContinueFind (bool next = true, bool replace = false)
315315

316316
if (!found)
317317
{
318-
MessageBox.Query (ApplicationImpl.Instance, "Find", $"The following specified text was not found: '{_textToFind}'", "Ok");
318+
MessageBox.Query (Application.Instance, "Find", $"The following specified text was not found: '{_textToFind}'", "Ok");
319319
}
320320
else if (gaveFullTurn)
321321
{
322-
MessageBox.Query (ApplicationImpl.Instance,
322+
MessageBox.Query (Application.Instance,
323323
"Find",
324324
$"No more occurrences were found for the following specified text: '{_textToFind}'",
325325
"Ok"
@@ -895,15 +895,15 @@ private void ReplaceAll ()
895895

896896
if (_textView.ReplaceAllText (_textToFind, _matchCase, _matchWholeWord, _textToReplace))
897897
{
898-
MessageBox.Query (ApplicationImpl.Instance,
898+
MessageBox.Query (Application.Instance,
899899
"Replace All",
900900
$"All occurrences were replaced for the following specified text: '{_textToReplace}'",
901901
"Ok"
902902
);
903903
}
904904
else
905905
{
906-
MessageBox.Query (ApplicationImpl.Instance,
906+
MessageBox.Query (Application.Instance,
907907
"Replace All",
908908
$"None of the following specified text was found: '{_textToFind}'",
909909
"Ok"
@@ -1155,7 +1155,7 @@ private bool SaveAs ()
11551155
{
11561156
if (File.Exists (path))
11571157
{
1158-
if (MessageBox.Query (ApplicationImpl.Instance,
1158+
if (MessageBox.Query (Application.Instance,
11591159
"Save File",
11601160
"File already exists. Overwrite any way?",
11611161
"No",
@@ -1194,11 +1194,11 @@ private bool SaveFile (string title, string file)
11941194
_originalText = Encoding.Unicode.GetBytes (_textView.Text);
11951195
_saved = true;
11961196
_textView.ClearHistoryChanges ();
1197-
MessageBox.Query (ApplicationImpl.Instance, "Save File", "File was successfully saved.", "Ok");
1197+
MessageBox.Query (Application.Instance, "Save File", "File was successfully saved.", "Ok");
11981198
}
11991199
catch (Exception ex)
12001200
{
1201-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error", ex.Message, "Ok");
1201+
MessageBox.ErrorQuery (Application.Instance, "Error", ex.Message, "Ok");
12021202

12031203
return false;
12041204
}

Examples/UICatalog/Scenarios/FileDialogExamples.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public override void Main ()
133133
}
134134
catch (Exception ex)
135135
{
136-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error", ex.ToString (), "_Ok");
136+
MessageBox.ErrorQuery (Application.Instance, "Error", ex.ToString (), "_Ok");
137137
}
138138
finally
139139
{
@@ -153,7 +153,7 @@ private void ConfirmOverwrite (object sender, FilesSelectedEventArgs e)
153153
{
154154
if (File.Exists (e.Dialog.Path))
155155
{
156-
int? result = MessageBox.Query (ApplicationImpl.Instance, "Overwrite?", "File already exists", "_Yes", "_No");
156+
int? result = MessageBox.Query (Application.Instance, "Overwrite?", "File already exists", "_Yes", "_No");
157157
e.Cancel = result == 1;
158158
}
159159
}
@@ -248,23 +248,23 @@ private void CreateDialog ()
248248

249249
if (canceled)
250250
{
251-
MessageBox.Query (ApplicationImpl.Instance,
251+
MessageBox.Query (Application.Instance,
252252
"Canceled",
253253
"You canceled navigation and did not pick anything",
254254
"Ok"
255255
);
256256
}
257257
else if (_cbAllowMultipleSelection.CheckedState == CheckState.Checked)
258258
{
259-
MessageBox.Query (ApplicationImpl.Instance,
259+
MessageBox.Query (Application.Instance,
260260
"Chosen!",
261261
"You chose:" + Environment.NewLine + string.Join (Environment.NewLine, multiSelected.Select (m => m)),
262262
"Ok"
263263
);
264264
}
265265
else
266266
{
267-
MessageBox.Query (ApplicationImpl.Instance,
267+
MessageBox.Query (Application.Instance,
268268
"Chosen!",
269269
"You chose:" + Environment.NewLine + path,
270270
"Ok"

Examples/UICatalog/Scenarios/Generic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void Main ()
2929
{
3030
// When Accepting is handled, set e.Handled to true to prevent further processing.
3131
e.Handled = true;
32-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Error", "You pressed the button!", "_Ok");
32+
MessageBox.ErrorQuery (Application.Instance, "Error", "You pressed the button!", "_Ok");
3333
};
3434

3535
appWindow.Add (button);

Examples/UICatalog/Scenarios/HexEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private void _hexView_PositionChanged (object? sender, HexViewEventArgs obj)
181181
}
182182
}
183183

184-
private void Copy () { MessageBox.ErrorQuery (ApplicationImpl.Instance, "Not Implemented", "Functionality not yet implemented.", "Ok"); }
184+
private void Copy () { MessageBox.ErrorQuery (Application.Instance, "Not Implemented", "Functionality not yet implemented.", "Ok"); }
185185

186186
private void CreateDemoFile (string fileName)
187187
{
@@ -208,15 +208,15 @@ private void CreateUnicodeDemoFile (string fileName)
208208
ms.Close ();
209209
}
210210

211-
private void Cut () { MessageBox.ErrorQuery (ApplicationImpl.Instance, "Not Implemented", "Functionality not yet implemented.", "Ok"); }
211+
private void Cut () { MessageBox.ErrorQuery (Application.Instance, "Not Implemented", "Functionality not yet implemented.", "Ok"); }
212212

213213
private Stream LoadFile ()
214214
{
215215
var stream = new MemoryStream ();
216216

217217
if (!_saved && _hexView!.Edits.Count > 0 && _hexView.Source is {})
218218
{
219-
if (MessageBox.ErrorQuery (ApplicationImpl.Instance,
219+
if (MessageBox.ErrorQuery (Application.Instance,
220220
"Save",
221221
"The changes were not saved. Want to open without saving?",
222222
"_Yes",
@@ -267,7 +267,7 @@ private void Open ()
267267
d.Dispose ();
268268
}
269269

270-
private void Paste () { MessageBox.ErrorQuery (ApplicationImpl.Instance, "Not Implemented", "Functionality not yet implemented.", "_Ok"); }
270+
private void Paste () { MessageBox.ErrorQuery (Application.Instance, "Not Implemented", "Functionality not yet implemented.", "_Ok"); }
271271
private void Quit () { Application.RequestStop (); }
272272

273273
private void Save ()

Examples/UICatalog/Scenarios/Images.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void BtnStartFireOnAccept (object sender, CommandEventArgs e)
183183

184184
if (!_sixelSupportResult.SupportsTransparency)
185185
{
186-
if (MessageBox.Query (ApplicationImpl.Instance,
186+
if (MessageBox.Query (Application.Instance,
187187
"Transparency Not Supported",
188188
"It looks like your terminal does not support transparent sixel backgrounds. Do you want to try anyway?",
189189
"Yes",
@@ -288,7 +288,7 @@ private void OpenImage (object sender, CommandEventArgs e)
288288
}
289289
catch (Exception ex)
290290
{
291-
MessageBox.ErrorQuery (ApplicationImpl.Instance, "Could not open file", ex.Message, "Ok");
291+
MessageBox.ErrorQuery (Application.Instance, "Could not open file", ex.Message, "Ok");
292292

293293
return;
294294
}
@@ -492,7 +492,7 @@ private void OutputSixelButtonClick (object sender, CommandEventArgs e)
492492
{
493493
if (_imageView.FullResImage == null)
494494
{
495-
MessageBox.Query (ApplicationImpl.Instance, "No Image Loaded", "You must first open an image. Use the 'Open Image' button above.", "Ok");
495+
MessageBox.Query (Application.Instance, "No Image Loaded", "You must first open an image. Use the 'Open Image' button above.", "Ok");
496496

497497
return;
498498
}

Examples/UICatalog/Scenarios/InteractiveTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void TreeView_KeyPress (object? sender, Key obj)
173173

174174
if (parent is null)
175175
{
176-
MessageBox.ErrorQuery (ApplicationImpl.Instance,
176+
MessageBox.ErrorQuery (Application.Instance,
177177
"Could not delete",
178178
$"Parent of '{toDelete}' was unexpectedly null",
179179
"Ok"

Examples/UICatalog/Scenarios/KeyBindings.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ public KeyBindingsDemo ()
164164

165165
AddCommand (Command.Save, ctx =>
166166
{
167-
MessageBox.Query (ApplicationImpl.Instance, $"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
167+
MessageBox.Query (Application.Instance, $"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
168168
return true;
169169
});
170170
AddCommand (Command.New, ctx =>
171171
{
172-
MessageBox.Query (ApplicationImpl.Instance, $"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
172+
MessageBox.Query (Application.Instance, $"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
173173
return true;
174174
});
175175
AddCommand (Command.HotKey, ctx =>
176176
{
177-
MessageBox.Query (ApplicationImpl.Instance, $"{ctx.Command}", $"Ctx: {ctx}\nCommand: {ctx.Command}", buttons: "Ok");
177+
MessageBox.Query (Application.Instance, $"{ctx.Command}", $"Ctx: {ctx}\nCommand: {ctx.Command}", buttons: "Ok");
178178
SetFocus ();
179179
return true;
180180
});
@@ -189,7 +189,7 @@ public KeyBindingsDemo ()
189189
{
190190
return false;
191191
}
192-
MessageBox.Query (ApplicationImpl.Instance, $"{keyCommandContext.Binding}", $"Key: {keyCommandContext.Binding.Key}\nCommand: {ctx.Command}", buttons: "Ok");
192+
MessageBox.Query (Application.Instance, $"{keyCommandContext.Binding}", $"Key: {keyCommandContext.Binding.Key}\nCommand: {ctx.Command}", buttons: "Ok");
193193
Application.RequestStop ();
194194
return true;
195195
});

0 commit comments

Comments
 (0)