mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* 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.
194 lines
5.2 KiB
C#
194 lines
5.2 KiB
C#
#nullable enable
|
|
|
|
namespace UICatalog.Scenarios;
|
|
|
|
[ScenarioMetadata ("Interactive Tree", "Create nodes and child nodes in TreeView.")]
|
|
[ScenarioCategory ("Controls")]
|
|
[ScenarioCategory ("TreeView")]
|
|
public class InteractiveTree : Scenario
|
|
{
|
|
private TreeView? _treeView;
|
|
|
|
public override void Main ()
|
|
{
|
|
Application.Init ();
|
|
|
|
Window appWindow = new ()
|
|
{
|
|
Title = GetName (),
|
|
BorderStyle = LineStyle.None
|
|
};
|
|
|
|
// MenuBar
|
|
MenuBar menu = new ();
|
|
|
|
menu.Add (
|
|
new MenuBarItem (
|
|
"_File",
|
|
[
|
|
new MenuItem
|
|
{
|
|
Title = "_Quit",
|
|
Action = Quit
|
|
}
|
|
]
|
|
)
|
|
);
|
|
|
|
_treeView = new ()
|
|
{
|
|
X = 0,
|
|
Y = Pos.Bottom (menu),
|
|
Width = Dim.Fill (),
|
|
Height = Dim.Fill (1)
|
|
};
|
|
_treeView.KeyDown += TreeView_KeyPress;
|
|
|
|
// StatusBar
|
|
StatusBar statusBar = new (
|
|
[
|
|
new (Application.QuitKey, "Quit", Quit),
|
|
new (Key.C.WithCtrl, "Add Child", AddChildNode),
|
|
new (Key.T.WithCtrl, "Add Root", AddRootNode),
|
|
new (Key.R.WithCtrl, "Rename Node", RenameNode)
|
|
]
|
|
);
|
|
|
|
appWindow.Add (menu, _treeView, statusBar);
|
|
|
|
Application.Run (appWindow);
|
|
appWindow.Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
private void AddChildNode ()
|
|
{
|
|
if (_treeView is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ITreeNode? node = _treeView.SelectedObject;
|
|
|
|
if (node is { })
|
|
{
|
|
if (GetText ("Text", "Enter text for node:", "", out string entered))
|
|
{
|
|
node.Children.Add (new TreeNode (entered));
|
|
_treeView.RefreshObject (node);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddRootNode ()
|
|
{
|
|
if (_treeView is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (GetText ("Text", "Enter text for node:", "", out string entered))
|
|
{
|
|
_treeView.AddObject (new TreeNode (entered));
|
|
}
|
|
}
|
|
|
|
private bool GetText (string title, string label, string initialText, out string enteredText)
|
|
{
|
|
var okPressed = false;
|
|
|
|
Button ok = new () { Text = "Ok", IsDefault = true };
|
|
|
|
ok.Accepting += (s, e) =>
|
|
{
|
|
okPressed = true;
|
|
Application.RequestStop ();
|
|
};
|
|
Button cancel = new () { Text = "Cancel" };
|
|
cancel.Accepting += (s, e) => Application.RequestStop ();
|
|
Dialog d = new () { Title = title, Buttons = [ok, cancel] };
|
|
|
|
Label lbl = new () { X = 0, Y = 1, Text = label };
|
|
|
|
TextField tf = new () { Text = initialText, X = 0, Y = 2, Width = Dim.Fill () };
|
|
|
|
d.Add (lbl, tf);
|
|
tf.SetFocus ();
|
|
|
|
Application.Run (d);
|
|
d.Dispose ();
|
|
|
|
enteredText = okPressed ? tf.Text : string.Empty;
|
|
|
|
return okPressed;
|
|
}
|
|
|
|
private void Quit () { Application.RequestStop (); }
|
|
|
|
private void RenameNode ()
|
|
{
|
|
if (_treeView is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ITreeNode? node = _treeView.SelectedObject;
|
|
|
|
if (node is { })
|
|
{
|
|
if (GetText ("Text", "Enter text for node:", node.Text, out string entered))
|
|
{
|
|
node.Text = entered;
|
|
_treeView.RefreshObject (node);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void TreeView_KeyPress (object? sender, Key obj)
|
|
{
|
|
if (_treeView is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (obj.KeyCode == Key.Delete)
|
|
{
|
|
ITreeNode? toDelete = _treeView.SelectedObject;
|
|
|
|
if (toDelete is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
obj.Handled = true;
|
|
|
|
// if it is a root object remove it
|
|
if (_treeView.Objects.Contains (toDelete))
|
|
{
|
|
_treeView.Remove (toDelete);
|
|
}
|
|
else
|
|
{
|
|
ITreeNode? parent = _treeView.GetParent (toDelete);
|
|
|
|
if (parent is null)
|
|
{
|
|
MessageBox.ErrorQuery (Application.Instance,
|
|
"Could not delete",
|
|
$"Parent of '{toDelete}' was unexpectedly null",
|
|
"Ok"
|
|
);
|
|
}
|
|
else
|
|
{
|
|
//update the model
|
|
parent.Children.Remove (toDelete);
|
|
|
|
//refresh the tree
|
|
_treeView.RefreshObject (parent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|