Added new method HitTest to TreeView to allow for better context menus

This commit is contained in:
Thomas Nind
2022-05-10 15:39:15 +01:00
parent 736d10598b
commit 5c39f6af48
3 changed files with 110 additions and 1 deletions

View File

@@ -77,6 +77,7 @@ namespace UICatalog.Scenarios {
};
treeViewFiles.ObjectActivated += TreeViewFiles_ObjectActivated;
treeViewFiles.MouseClick += TreeViewFiles_MouseClick;
SetupFileTree ();
@@ -88,6 +89,52 @@ namespace UICatalog.Scenarios {
red = Application.Driver.MakeAttribute (Color.Red, Color.Blue);
}
private void TreeViewFiles_MouseClick (View.MouseEventArgs obj)
{
// if user right clicks
if (obj.MouseEvent.Flags.HasFlag(MouseFlags.Button3Clicked)) {
var rightClicked = treeViewFiles.HitTest (new Point (obj.MouseEvent.X, obj.MouseEvent.Y));
// nothing was clicked
if (rightClicked == null)
return;
var menu = new ContextMenu ();
menu.Position = new Point(
obj.MouseEvent.X + treeViewFiles.Frame.X,
obj.MouseEvent.Y + treeViewFiles.Frame.Y +1);
menu.MenuItems = new MenuBarItem (new [] { new MenuItem ("Properties",null,()=> {
MessageBox.Query($"{rightClicked.Name}({rightClicked.GetType().Name})",Describe(rightClicked),"Ok");
}
) });
menu.Show ();
}
}
private string Describe (FileSystemInfo f)
{
try {
if (f is FileInfo fi) {
return "Size:" + fi.Length;
}
if (f is DirectoryInfo d) {
return $@"Parent:{d.Parent}
Attributes:{d.Attributes}";
}
} catch (Exception) {
return "Could not get properties";
}
return null;
}
private void SetupScrollBar ()
{
// When using scroll bar leave the last row of the control free (for over-rendering with scroll bar)