Add experimental debug inspector

This commit is contained in:
John Lewin 2017-09-18 10:05:04 -07:00
parent 9721f93266
commit fa170f0a6b
5 changed files with 419 additions and 78 deletions

99
InspectForm.Designer.cs generated Normal file
View file

@ -0,0 +1,99 @@
namespace MatterHackers.MatterControl
{
partial class InspectForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.treeView1 = new System.Windows.Forms.TreeView();
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.propertyGrid1);
this.splitContainer1.Size = new System.Drawing.Size(1426, 972);
this.splitContainer1.SplitterDistance = 886;
this.splitContainer1.TabIndex = 0;
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.FullRowSelect = true;
this.treeView1.HideSelection = false;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(886, 972);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// propertyGrid1
//
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ControlDark;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(536, 972);
this.propertyGrid1.TabIndex = 0;
//
// InspectForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1426, 972);
this.Controls.Add(this.splitContainer1);
this.Name = "InspectForm";
this.Text = "InspectForm";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.PropertyGrid propertyGrid1;
}
}

179
InspectForm.cs Normal file
View file

@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl
{
public partial class InspectForm : Form
{
private TreeNode activeTreeNode;
private GuiWidget inspectedWidget;
private GuiWidget InspectedWidget
{
get => inspectedWidget;
set
{
if (inspectedWidget != null)
{
inspectedWidget.DebugShowBounds = false;
}
inspectedWidget = value;
inspectedWidget.DebugShowBounds = true;
if (inspectedWidget != null)
{
propertyGrid1.SelectedObject = inspectedWidget;
}
if (activeTreeNode != null)
{
activeTreeNode.Checked = false;
}
if (treeNodes.TryGetValue(inspectedWidget, out TreeNode treeNode))
{
treeView1.SelectedNode = treeNode;
activeTreeNode = treeNode;
activeTreeNode.Checked = true;
}
inspectedWidget.Invalidate();
}
}
bool showNamesUnderMouse = true;
private GuiWidget inspectedSystemWindow;
private Vector2 mousePosition;
Dictionary<GuiWidget, TreeNode> treeNodes = new Dictionary<GuiWidget, TreeNode>();
public InspectForm(GuiWidget inspectionSource)
{
InitializeComponent();
inspectionSource.MouseMove += (s, e) =>
{
mousePosition = e.Position;
};
inspectionSource.AfterDraw += (s, e) =>
{
if (showNamesUnderMouse && !inspectionSource.HasBeenClosed)
{
var namedChildren = new List<GuiWidget.WidgetAndPosition>();
inspectedSystemWindow.FindNamedChildrenRecursive(
"",
namedChildren,
new RectangleDouble(mousePosition.x, mousePosition.y, mousePosition.x + 1, mousePosition.y + 1),
GuiWidget.SearchType.Partial,
allowDisabledOrHidden: false);
// If the context changed, update the UI
if (namedChildren.LastOrDefault()?.widget is GuiWidget firstUnderMouse
&& firstUnderMouse != this.InspectedWidget)
{
RebuildUI(namedChildren);
this.InspectedWidget = firstUnderMouse;
}
}
};
this.inspectedSystemWindow = inspectionSource;
inspectionSource.Invalidate();
}
private void AddItem(GuiWidget widget, string text, TreeNode childNode = null)
{
if (treeNodes.TryGetValue(widget, out TreeNode existingNode))
{
existingNode.Nodes.Add(childNode);
existingNode.Expand();
}
else
{
var node = new TreeNode(text)
{
Tag = widget
};
if (childNode != null)
{
node.Nodes.Add(childNode);
node.Expand();
}
treeNodes.Add(widget, node);
var parent = widget.Parent;
if (parent == null)
{
treeView1.Nodes.Add(node);
}
else
{
AddItem(parent, parent.Text, node);
}
}
}
public void RebuildUI(List<GuiWidget.WidgetAndPosition> namedChildren)
{
treeView1.Nodes.Clear();
treeNodes.Clear();
treeView1.SuspendLayout();
for (int i = 0; i < namedChildren.Count; i++)
{
var child = namedChildren[i];
AddItem(child.widget, BuildName(child.widget));
}
treeView1.ResumeLayout();
}
private string BuildName(GuiWidget widget)
{
string nameToWrite = inspectedWidget == widget ? "* " : "";
if (!string.IsNullOrEmpty(widget.Name))
{
nameToWrite += $"{widget.GetType().Name} --- {widget.Name}";
}
else
{
nameToWrite += $"{widget.GetType().Name}";
}
return nameToWrite;
}
int selectionIndex;
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.F2)
{
selectionIndex++;
}
else if (e.KeyCode == System.Windows.Forms.Keys.F2)
{
selectionIndex--;
}
base.OnKeyDown(e);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
this.InspectedWidget = e.Node.Tag as GuiWidget;
}
}
}

120
InspectForm.resx Normal file
View file

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -143,6 +143,12 @@
<Compile Include="CustomWidgets\RadioImageWidget.cs" />
<Compile Include="CustomWidgets\RadioPanelWidget.cs" />
<Compile Include="CustomWidgets\ValueDisplayInfo.cs" />
<Compile Include="InspectForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="InspectForm.Designer.cs">
<DependentUpon>InspectForm.cs</DependentUpon>
</Compile>
<Compile Include="Library\ContentProviders\GCodeContentProvider.cs" />
<Compile Include="Library\ContentProviders\IContentProvider.cs" />
<Compile Include="ConfigurationPage\ApplicationSettings\ApplicationSettingsView.cs" />
@ -490,6 +496,9 @@
<EmbeddedResource Include="config.json">
<LogicalName>config.json</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="InspectForm.resx">
<DependentUpon>InspectForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="App.config" />
<None Include="Library\LibraryProviders.cd" />
<None Include="Library\Widgets\ListView\ListView.cd" />

View file

@ -557,9 +557,6 @@ namespace MatterHackers.MatterControl
IsLoading = false;
#if DEBUG
AfterDraw += ShowNamesUnderMouse;
#endif
base.OnLoad(args);
}
@ -626,14 +623,6 @@ namespace MatterHackers.MatterControl
dropWasOnChild = true;
}
#if DEBUG
if (showNamesUnderMouse)
{
mousePosition = mouseEvent.Position;
Invalidate();
}
#endif
if (GuiWidget.DebugBoundsUnderMouse)
{
Invalidate();
@ -758,65 +747,8 @@ namespace MatterHackers.MatterControl
#endif
}
private int extraInfoOffsetFromLast = 0;
private bool showNamesUnderMouse = false;
private GuiWidget inspectedWidget = null;
#if DEBUG
Vector2 mousePosition;
private void ShowNamesUnderMouse(object sender, DrawEventArgs e)
{
if (showNamesUnderMouse)
{
if (inspectedWidget != null)
{
inspectedWidget.DebugShowBounds = false;
}
List<WidgetAndPosition> namedChildren = new List<WidgetAndPosition>();
this.FindNamedChildrenRecursive("", namedChildren, new RectangleDouble(mousePosition.x, mousePosition.y, mousePosition.x + 1 , mousePosition.y + 1), SearchType.Partial, allowDisabledOrHidden: false);
Vector2 start = new Vector2(10, 50);
int lineHeight = 20;
e.graphics2D.FillRectangle(start, start + new Vector2(500, namedChildren.Count * lineHeight), new RGBA_Bytes(RGBA_Bytes.Black, 120));
// Make sure we are in range of the current list
extraInfoOffsetFromLast = Math.Max(0, Math.Min(namedChildren.Count - 1, extraInfoOffsetFromLast));
for(int i=0; i< namedChildren.Count; i++)
{
var child = namedChildren[i];
if (i == (namedChildren.Count-1) - extraInfoOffsetFromLast)
{
inspectedWidget = child.widget;
}
string nameToWrite = inspectedWidget == child.widget ? "* " : "";
if (child.name != null)
{
nameToWrite += $"{child.widget.GetType().Name} --- {child.name}";
}
else
{
nameToWrite += $"{child.widget.GetType().Name} -- -";
}
if (inspectedWidget == child.widget)
{
nameToWrite += $" | H:{child.widget.HAnchor}, V:{child.widget.VAnchor}";
}
e.graphics2D.DrawString(nameToWrite, start.x, start.y, backgroundColor: RGBA_Bytes.White, drawFromHintedCach: true);
start.y += lineHeight;
}
if (inspectedWidget != null)
{
inspectedWidget.DebugShowBounds = true;
}
}
}
InspectForm inspectForm = null;
public override void OnKeyDown(KeyEventArgs keyEvent)
{
@ -827,18 +759,20 @@ namespace MatterHackers.MatterControl
{
if (keyEvent.KeyCode == Keys.F1)
{
showNamesUnderMouse = !showNamesUnderMouse;
}
else if (keyEvent.KeyCode == Keys.F2)
{
extraInfoOffsetFromLast++;
}
else if (keyEvent.KeyCode == Keys.F3)
{
extraInfoOffsetFromLast--;
//showNamesUnderMouse = !showNamesUnderMouse;
if (inspectForm == null)
{
inspectForm = new InspectForm(this);
inspectForm.FormClosed += (s, e2) =>
{
inspectForm = null;
};
inspectForm.Show();
}
}
}
}
#endif
public static void CheckKnownAssemblyConditionalCompSymbols()
{