Files
Terminal.Gui/UICatalog/Scenarios/InteractiveTree.cs
Tig 16055c53b0 Fixes #3039. Fix View.HotKey (#3249)
* Added View.DefaultCommand etc... Started on dedicated scenario

* Fixed un-shifted hotkeys -> Fixed Key Equals. Fixed WindowsDriver passing wrong key. Etc.

* Fixed Key Bindings and HotKeys

* Fixed Key Bindings and HotKeys

* Label now correctly supports hotkey

* Disabled unix hot keys because they are annoying and get in the way

* Updated nuget. fixed warnings

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Changed TextChangingEventArgs to inherit from CancelEventArgs

* TextChangingEventArgs -> TextEventArgs

* Simplified Text events by having only on args class

* Fixed unit tests fail

* Simplified by removing TitleEventArgs

* POC of Title being primary for hotkey. Label and Button hacked to work

* POC of Title being primary for hotkey. Label and Button hacked to work - all unit tests pass

* Dropped Microsoft.NETFramework.ReferenceAssemblies

* Fixed Dialogs scenario hotkeys

* Fixed build warnings

* Fixed Border Title render bug

* Regiggering default command handling

* Regiggering default command handling

* Checkbox clean up

* Added StateEventArgs POC

* Command.Default -> Command.HotKey

* Command.Default -> Command.HotKey - fixed TableView

* Command.Default -> Command.HotKey - fixed TableView

* Updated reactive example

* Fixed Toplevel.BringOverlappedTopToFront - was reordering SubViews when it shouldn't

* WIP - broke

* Finished impl of StateEventArgs

* Deleted ToggleEventArgs.cs. Added StateEventArgs.cs

* XML doc fix

* Removed old code

* Removed commented out code

* Label.Clicked -> Label.Accept (missed this before)

* Removed Labels as Buttons scenario as it's not really  useful

* Moved SubView tests to own file

* Moved SubView tests to own file

* Simplified Text test

* Added OnAccept test

* Deleted DefaultCommand

* Modernized CheckBox

* New button test

* Cleaned up RadioGroup; added tests

* KeyCode->Key in ListView

* Added ListView unit tests

* ListView now does Accept correctly

* TreeView now does Accept correctly

* Cleaned up some TextField tests

* TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Fixed ComboBox to deal with TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Removed un-needed using statement
2024-02-22 15:11:26 -07:00

170 lines
5.3 KiB
C#

using System.Linq;
using Terminal.Gui;
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 Setup ()
{
Win.Title = GetName ();
Win.Y = 1; // menu
Win.Height = Dim.Fill (1); // status bar
var menu = new MenuBar
{
Menus =
[
new MenuBarItem ("_File", new MenuItem [] { new ("_Quit", "", Quit) })
]
};
Application.Top.Add (menu);
_treeView = new TreeView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill (1) };
_treeView.KeyDown += TreeView_KeyPress;
Win.Add (_treeView);
var statusBar = new StatusBar (
new StatusItem []
{
new (
Application.QuitKey,
$"{Application.QuitKey} to Quit",
Quit
),
new (
KeyCode.CtrlMask | KeyCode.C,
"~^C~ Add Child",
AddChildNode
),
new (
KeyCode.CtrlMask | KeyCode.T,
"~^T~ Add Root",
AddRootNode
),
new (
KeyCode.CtrlMask | KeyCode.R,
"~^R~ Rename Node",
RenameNode
)
}
);
Application.Top.Add (statusBar);
}
private void AddChildNode ()
{
ITreeNode node = _treeView.SelectedObject;
if (node != null)
{
if (GetText ("Text", "Enter text for node:", "", out string entered))
{
node.Children.Add (new TreeNode (entered));
_treeView.RefreshObject (node);
}
}
}
private void AddRootNode ()
{
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;
var ok = new Button { Text = "Ok", IsDefault = true };
ok.Accept += (s, e) =>
{
okPressed = true;
Application.RequestStop ();
};
var cancel = new Button { Text = "Cancel" };
cancel.Accept += (s, e) => Application.RequestStop ();
var d = new Dialog { Title = title, Buttons = [ok, cancel] };
var lbl = new Label { X = 0, Y = 1, Text = label };
var tf = new TextField { Text = initialText, X = 0, Y = 2, Width = Dim.Fill () };
d.Add (lbl, tf);
tf.SetFocus ();
Application.Run (d);
enteredText = okPressed ? tf.Text : null;
return okPressed;
}
private void Quit () { Application.RequestStop (); }
private void RenameNode ()
{
ITreeNode node = _treeView.SelectedObject;
if (node != null)
{
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 (obj.KeyCode == KeyCode.Delete)
{
ITreeNode toDelete = _treeView.SelectedObject;
if (toDelete == 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 == null)
{
MessageBox.ErrorQuery (
"Could not delete",
$"Parent of '{toDelete}' was unexpectedly null",
"Ok"
);
}
else
{
//update the model
parent.Children.Remove (toDelete);
//refresh the tree
_treeView.RefreshObject (parent);
}
}
}
}
}