Browse Source

Added SelectAll and made OnSelectionChanged protected

tznind 4 years ago
parent
commit
f21f21522f
2 changed files with 87 additions and 45 deletions
  1. 35 1
      Terminal.Gui/Views/TreeView.cs
  2. 52 44
      UICatalog/Scenarios/ClassExplorer.cs

+ 35 - 1
Terminal.Gui/Views/TreeView.cs

@@ -312,7 +312,7 @@ namespace Terminal.Gui {
 				selectedObject = value; 
 
 				if(!ReferenceEquals(oldValue,value))
-					SelectionChanged?.Invoke(this,new SelectionChangedEventArgs<T>(this,oldValue,value));
+					OnSelectionChanged(new SelectionChangedEventArgs<T>(this,oldValue,value));
 			}
 		}
 
@@ -645,6 +645,9 @@ namespace Terminal.Gui {
 				case Key.PageDown | Key.ShiftMask:
 					AdjustSelection(Bounds.Height,keyEvent.Key.HasFlag(Key.ShiftMask));
 				break;
+				case Key.A | Key.CtrlMask:
+					SelectAll();
+					break;
 				case Key.Home:
 					GoToFirst();
 				break;
@@ -1042,6 +1045,37 @@ namespace Terminal.Gui {
 				}	
 			}
 		}
+
+		/// <summary>
+		/// Selects all objects in the tree when <see cref="MultiSelect"/> is enabled otherwise does nothing
+		/// </summary>
+		public void SelectAll()
+		{
+			if(!MultiSelect)
+				return;
+
+			_multiSelectedRegions.Clear();
+
+			var map = BuildLineMap();
+
+			if(map.Length == 0)
+				return;
+
+			_multiSelectedRegions.Push(new TreeSelection<T>(map[0],map.Length,map));
+			SetNeedsDisplay();
+			
+			OnSelectionChanged(new SelectionChangedEventArgs<T>(this,SelectedObject,SelectedObject));
+		}
+
+
+		/// <summary>
+		/// Raises the SelectionChanged event
+		/// </summary>
+		/// <param name="e"></param>
+		protected virtual void OnSelectionChanged (SelectionChangedEventArgs<T> e)
+		{
+			SelectionChanged?.Invoke(this,e);
+		}
 	}
 
 	class TreeSelection<T> where T : class {

+ 52 - 44
UICatalog/Scenarios/ClassExplorer.cs

@@ -117,62 +117,70 @@ namespace UICatalog.Scenarios {
 		private void TreeView_SelectionChanged (object sender, SelectionChangedEventArgs<object> e)
 		{
 			var val = e.NewValue;
+			var all = treeView.GetAllSelectedObjects().ToArray();
 
 			if(val == null || val is ShowForType)
 				return;
 			try {
 				
-				StringBuilder sb = new StringBuilder();
+				if(all.Length > 1){
 
-				// tell the user about the currently selected tree node
-				sb.AppendLine(e.NewValue.GetType().Name);
-				
-				if(val is Assembly ass) {
-					sb.AppendLine($"Location:{ass.Location}");
-					sb.AppendLine($"FullName:{ass.FullName}");
+					textView.Text = all.Length  + " Objects";
 				}
+				else
+				{
+					StringBuilder sb = new StringBuilder();
 
-				if(val is PropertyInfo p) {
-					sb.AppendLine($"Name:{p.Name}");
-					sb.AppendLine($"Type:{p.PropertyType}");
-					sb.AppendLine($"CanWrite:{p.CanWrite}");
-					sb.AppendLine($"CanRead:{p.CanRead}");
-				}
-			
-				if(val is FieldInfo f) {
-					sb.AppendLine($"Name:{f.Name}");
-					sb.AppendLine($"Type:{f.FieldType}");
-				}
-						
-				if(val is EventInfo ev) {
-					sb.AppendLine($"Name:{ev.Name}");
-					sb.AppendLine($"Parameters:");
-					foreach(var parameter in ev.EventHandlerType.GetMethod("Invoke").GetParameters()) {
-						sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
-					}
-				}
-					
-				if(val is MethodInfo method) {
-					sb.AppendLine($"Name:{method.Name}");
-					sb.AppendLine($"IsPublic:{method.IsPublic}");
-					sb.AppendLine($"IsStatic:{method.IsStatic}");
-					sb.AppendLine($"Parameters:{(method.GetParameters().Any() ? "":"None")}");
-					foreach(var parameter in method.GetParameters()) {
-						sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
-					}
-				}
-			
+					// tell the user about the currently selected tree node
+					sb.AppendLine(e.NewValue.GetType().Name);
 					
-				if(val is ConstructorInfo ctor) {
-					sb.AppendLine($"Name:{ctor.Name}");
-					sb.AppendLine($"Parameters:{(ctor.GetParameters().Any() ? "":"None")}");
-					foreach(var parameter in ctor.GetParameters()) {
-						sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
+					if(val is Assembly ass) {
+						sb.AppendLine($"Location:{ass.Location}");
+						sb.AppendLine($"FullName:{ass.FullName}");
 					}
-				}
 
-				textView.Text = sb.ToString().Replace("\r\n","\n");
+					if(val is PropertyInfo p) {
+						sb.AppendLine($"Name:{p.Name}");
+						sb.AppendLine($"Type:{p.PropertyType}");
+						sb.AppendLine($"CanWrite:{p.CanWrite}");
+						sb.AppendLine($"CanRead:{p.CanRead}");
+					}
+				
+					if(val is FieldInfo f) {
+						sb.AppendLine($"Name:{f.Name}");
+						sb.AppendLine($"Type:{f.FieldType}");
+					}
+							
+					if(val is EventInfo ev) {
+						sb.AppendLine($"Name:{ev.Name}");
+						sb.AppendLine($"Parameters:");
+						foreach(var parameter in ev.EventHandlerType.GetMethod("Invoke").GetParameters()) {
+							sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
+						}
+					}
+						
+					if(val is MethodInfo method) {
+						sb.AppendLine($"Name:{method.Name}");
+						sb.AppendLine($"IsPublic:{method.IsPublic}");
+						sb.AppendLine($"IsStatic:{method.IsStatic}");
+						sb.AppendLine($"Parameters:{(method.GetParameters().Any() ? "":"None")}");
+						foreach(var parameter in method.GetParameters()) {
+							sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
+						}
+					}
+				
+						
+					if(val is ConstructorInfo ctor) {
+						sb.AppendLine($"Name:{ctor.Name}");
+						sb.AppendLine($"Parameters:{(ctor.GetParameters().Any() ? "":"None")}");
+						foreach(var parameter in ctor.GetParameters()) {
+							sb.AppendLine($"  {parameter.ParameterType} {parameter.Name}");
+						}
+					}
 
+					textView.Text = sb.ToString().Replace("\r\n","\n");
+				}
+								
 			} catch (Exception ex) {
 
 				textView.Text = ex.Message;