From be6b1fa6e4576e1b1af68219b3e46053361ceedc Mon Sep 17 00:00:00 2001
From: adelikat <adelikat@tasvideos.org>
Date: Sat, 29 Aug 2020 11:27:00 -0500
Subject: [PATCH] consolidate propertygrid heigh changing logic

---
 .../Extensions/ControlExtensions.cs           | 45 +++++++++++--------
 .../config/GenericCoreConfig.cs               | 36 +--------------
 .../tools/TAStudio/GreenzoneSettings.cs       |  2 +-
 3 files changed, 29 insertions(+), 54 deletions(-)

diff --git a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs
index 6f71632472..11fd2feba9 100644
--- a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs
+++ b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs
@@ -335,34 +335,41 @@ namespace BizHawk.Client.EmuHawk
 		public static bool IsCtrlShift(this KeyEventArgs e, Keys key)
 			=> !e.Alt && e.Control && e.Shift && e.KeyCode == key;
 
-		// For inexplicable reasons, property grid does not expose a way to do this
-		// https://www.codeproject.com/Articles/28193/Change-the-height-of-a-PropertyGrid-s-description?msg=3379905#xx3379905xx
-		public static void SetDescriptionRowHeight(this PropertyGrid grid, int numRows)
+		/// <summary>
+		/// Changes the description heigh area to match the rows needed for the largest description in the list
+		/// </summary>
+		public static void AdjustDescriptionHeightToFit(this PropertyGrid grid)
 		{
 			try
 			{
-				var controlsProp = grid.GetType().GetProperty("Controls");
-				var controlsCollection = (Control.ControlCollection)controlsProp.GetValue(grid, null);
+				int maxLength = 0;
+				string desc = "";
 
-				foreach(Control c in controlsCollection)
+				foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(grid.SelectedObject))
 				{
-					Type ct = c.GetType();
-					string sName = ct.Name;
-
-					if (sName == "DocComment")
+					if (property.Description?.Length > maxLength)
 					{
-						var controlsProp2 = ct.GetProperty("Lines");
-						controlsProp2.SetValue(c, numRows, null);
-
-						FieldInfo fi = ct.BaseType.GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic);
-						fi.SetValue(c, true);
+						maxLength = property.Description.Length;
+						desc = property.Description;
 					}
 				}
-			 }
-			 catch (Exception ex)
-			 {
+
+				foreach (Control control in grid.Controls)
+				{
+					if (control.GetType().Name == "DocComment")
+					{
+						FieldInfo field = control.GetType().GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic);
+						field?.SetValue(control, true);
+						int height = (int)Graphics.FromHwnd(control.Handle).MeasureString(desc, control.Font, grid.Width).Height;
+						control.Height = Math.Max(20, height) + 16; // magic for now
+						return;
+					}
+				}
+			}
+			catch
+			{
 				// Eat it
-			 }
+			}
 		}
 	}
 }
diff --git a/src/BizHawk.Client.EmuHawk/config/GenericCoreConfig.cs b/src/BizHawk.Client.EmuHawk/config/GenericCoreConfig.cs
index b1ca482fd8..850c400d77 100644
--- a/src/BizHawk.Client.EmuHawk/config/GenericCoreConfig.cs
+++ b/src/BizHawk.Client.EmuHawk/config/GenericCoreConfig.cs
@@ -35,7 +35,7 @@ namespace BizHawk.Client.EmuHawk
 			if (_s != null)
 			{
 				propertyGrid1.SelectedObject = _s;
-				ChangeDescriptionHeight(propertyGrid1);
+				propertyGrid1.AdjustDescriptionHeightToFit();
 			}
 			else
 			{
@@ -45,7 +45,7 @@ namespace BizHawk.Client.EmuHawk
 			if (_ss != null)
 			{
 				propertyGrid2.SelectedObject = _ss;
-				ChangeDescriptionHeight(propertyGrid2);
+				propertyGrid2.AdjustDescriptionHeightToFit();
 			}
 			else
 			{
@@ -58,38 +58,6 @@ namespace BizHawk.Client.EmuHawk
 			}
 		}
 
-		private static void ChangeDescriptionHeight(PropertyGrid grid)
-		{
-			if (grid == null)
-			{
-				throw new ArgumentNullException(nameof(grid));
-			}
-
-			int maxLength = 0;
-			string desc = "";
-
-			foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(grid.SelectedObject))
-			{
-				if (property.Description?.Length > maxLength)
-				{
-					maxLength = property.Description.Length;
-					desc = property.Description;
-				}
-			}
-
-			foreach (Control control in grid.Controls)
-			{
-				if (control.GetType().Name == "DocComment")
-				{
-					FieldInfo field = control.GetType().GetField("userSized", BindingFlags.Instance | BindingFlags.NonPublic);
-					field?.SetValue(control, true);
-					int height = (int)System.Drawing.Graphics.FromHwnd(control.Handle).MeasureString(desc, control.Font, grid.Width).Height;
-					control.Height = Math.Max(20, height) + 16; // magic for now
-					return;
-				}
-			}
-		}
-
 		private void OkBtn_Click(object sender, EventArgs e)
 		{
 			if (_s != null && _settingsChanged)
diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/GreenzoneSettings.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/GreenzoneSettings.cs
index 4fa63f3805..6e750012aa 100644
--- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/GreenzoneSettings.cs
+++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/GreenzoneSettings.cs
@@ -26,7 +26,7 @@ namespace BizHawk.Client.EmuHawk
 
 		private void GreenzoneSettings_Load(object sender, EventArgs e)
 		{
-			SettingsPropertyGrid.SetDescriptionRowHeight(9);
+			SettingsPropertyGrid.AdjustDescriptionHeightToFit();
 		}
 
 		private void OkBtn_Click(object sender, EventArgs e)