Dispose of newly-instantiated Forms when populating Tool Box

see #2741, fe6bf7ba1, #2763
This commit is contained in:
YoshiRulz 2021-05-31 00:52:48 +10:00
parent be575c96f0
commit 2704003429
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 13 additions and 8 deletions

View File

@ -59,21 +59,21 @@ namespace BizHawk.Client.EmuHawk
foreach (var t in tools) foreach (var t in tools)
{ {
var instance = Activator.CreateInstance(t); var wasLoaded = Tools.Has(t);
var instance = (Form) Tools.Load(t, focus: false);
var tsb = new ToolStripButton var tsb = new ToolStripButton
{ {
Image = ((Form) instance).Icon.ToBitmap(), Image = instance.Icon.ToBitmap(),
Text = ((Form) instance).Text, Text = instance.Text,
DisplayStyle = ((Form) instance).ShowIcon ? ToolStripItemDisplayStyle.Image : ToolStripItemDisplayStyle.Text DisplayStyle = instance.ShowIcon ? ToolStripItemDisplayStyle.Image : ToolStripItemDisplayStyle.Text
}; };
if (!wasLoaded) instance.Dispose();
tsb.Click += (o, e) => tsb.Click += (o, e) =>
{ {
Tools.Load(t); if (wasLoaded) instance.Focus(); // instance refers to already opened tool, focus it
else Tools.Load(t); // instance was new and has been disposed by now
Close(); Close();
}; };
ToolBoxStrip.Items.Add(tsb); ToolBoxStrip.Items.Add(tsb);
} }
} }

View File

@ -487,6 +487,11 @@ namespace BizHawk.Client.EmuHawk
return _tools.Any(t => t is T && t.IsActive); return _tools.Any(t => t is T && t.IsActive);
} }
/// <returns><see langword="true"/> iff a tool of the given <paramref name="toolType"/> is <see cref="IToolForm.IsActive">active</see></returns>
public bool Has(Type toolType)
=> typeof(IToolForm).IsAssignableFrom(toolType)
&& _tools.Any(t => toolType.IsInstanceOfType(t) && t.IsActive);
/// <summary> /// <summary>
/// Gets the instance of T, or creates and returns a new instance /// Gets the instance of T, or creates and returns a new instance
/// </summary> /// </summary>