Optimized the GreenshotForm by removing the designer support in release mode.

This commit is contained in:
Robin Krom 2022-01-25 11:37:13 +01:00
commit 10e3049087
No known key found for this signature in database
GPG key ID: BCC01364F1371490
2 changed files with 121 additions and 104 deletions

View file

@ -19,11 +19,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
#if DEBUG
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
#endif
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using Greenshot.Base.Core;
@ -40,24 +43,33 @@ namespace Greenshot.Base.Controls
private static readonly ILog LOG = LogManager.GetLogger(typeof(GreenshotForm));
protected static CoreConfiguration coreConfiguration;
private static readonly IDictionary<Type, FieldInfo[]> reflectionCache = new Dictionary<Type, FieldInfo[]>();
#if DEBUG
private IComponentChangeService m_changeService;
private bool _isDesignModeLanguageSet;
private bool _applyLanguageManually;
private bool _storeFieldsManually;
private IDictionary<string, Control> _designTimeControls;
private IDictionary<string, ToolStripItem> _designTimeToolStripItems;
#endif
private bool _applyLanguageManually;
private bool _storeFieldsManually;
static GreenshotForm()
{
#if DEBUG
if (!IsInDesignMode)
{
#endif
coreConfiguration = IniConfig.GetIniSection<CoreConfiguration>();
#if DEBUG
}
#endif
}
#if DEBUG
[Category("Greenshot"), DefaultValue(null), Description("Specifies key of the language file to use when displaying the text.")]
#endif
public string LanguageKey { get; set; }
#if DEBUG
/// <summary>
/// Used to check the designmode during a constructor
/// </summary>
@ -71,6 +83,7 @@ namespace Greenshot.Base.Controls
(Application.ExecutablePath.IndexOf("wdexpress.exe", StringComparison.OrdinalIgnoreCase) > -1));
}
}
#endif
protected bool ManualLanguageApply
{
@ -89,6 +102,7 @@ namespace Greenshot.Base.Controls
/// </summary>
protected bool ToFront { get; set; }
#if DEBUG
/// <summary>
/// Code to initialize the language etc during design time
/// </summary>
@ -106,8 +120,8 @@ namespace Greenshot.Base.Controls
// this "type"
Assembly currentAssembly = GetType().Assembly;
if (typeResService != null)
{
if (typeResService == null) return;
string assemblyPath = typeResService.GetPathOfAssembly(currentAssembly.GetName());
string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
if (assemblyDirectory != null && !Language.AddLanguageFilePath(Path.Combine(assemblyDirectory, @"..\..\Greenshot\Languages\")))
@ -120,7 +134,6 @@ namespace Greenshot.Base.Controls
Language.AddLanguageFilePath(Path.Combine(assemblyDirectory, @"..\..\..\Languages\"));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
@ -151,14 +164,17 @@ namespace Greenshot.Base.Controls
base.OnPaint(e);
}
#endif
protected override void OnLoad(EventArgs e)
{
// Every GreenshotForm should have it's default icon
// And it might not ne needed for a Tool Window, but still for the task manager / switcher it's important
Icon = GreenshotResources.GetGreenshotIcon();
#if DEBUG
if (!DesignMode)
{
#endif
if (!_applyLanguageManually)
{
ApplyLanguage();
@ -166,6 +182,7 @@ namespace Greenshot.Base.Controls
FillFields();
base.OnLoad(e);
#if DEBUG
}
else
{
@ -174,6 +191,7 @@ namespace Greenshot.Base.Controls
base.OnLoad(e);
ApplyLanguage();
}
#endif
}
/// <summary>
@ -207,6 +225,7 @@ namespace Greenshot.Base.Controls
base.OnClosed(e);
}
#if DEBUG
/// <summary>
/// This override allows the control to register event handlers for IComponentChangeService events
/// at the time the control is sited, which happens only in design mode.
@ -322,6 +341,7 @@ namespace Greenshot.Base.Controls
base.Dispose(disposing);
}
#endif
protected void ApplyLanguage(ToolStripItem applyTo, string languageKey)
{
@ -454,7 +474,7 @@ namespace Greenshot.Base.Controls
ApplyLanguage(applyToControl);
}
}
#if DEBUG
if (DesignMode)
{
foreach (Control designControl in _designTimeControls.Values)
@ -467,6 +487,7 @@ namespace Greenshot.Base.Controls
ApplyLanguage(designToolStripItem);
}
}
#endif
}
finally
{
@ -515,11 +536,11 @@ namespace Greenshot.Base.Controls
{
var controlObject = field.GetValue(this);
IGreenshotConfigBindable configBindable = controlObject as IGreenshotConfigBindable;
if (!string.IsNullOrEmpty(configBindable?.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName))
{
if (string.IsNullOrEmpty(configBindable?.SectionName) || string.IsNullOrEmpty(configBindable.PropertyName)) continue;
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null)
{
if (section == null) continue;
if (!section.Values.TryGetValue(configBindable.PropertyName, out var iniValue))
{
LOG.DebugFormat("Wrong property '{0}' configured for field '{1}'", configBindable.PropertyName, field.Name);
@ -566,8 +587,6 @@ namespace Greenshot.Base.Controls
comboxBox.Enabled = !iniValue.IsFixed;
}
}
}
}
OnFieldsFilled();
}
@ -587,11 +606,11 @@ namespace Greenshot.Base.Controls
var controlObject = field.GetValue(this);
IGreenshotConfigBindable configBindable = controlObject as IGreenshotConfigBindable;
if (!string.IsNullOrEmpty(configBindable?.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName))
{
if (string.IsNullOrEmpty(configBindable?.SectionName) || string.IsNullOrEmpty(configBindable.PropertyName)) continue;
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null)
{
if (section == null) continue;
if (!section.Values.TryGetValue(configBindable.PropertyName, out var iniValue))
{
continue;
@ -631,8 +650,6 @@ namespace Greenshot.Base.Controls
iniDirty = true;
}
}
}
}
if (iniDirty)
{

View file

@ -16,7 +16,7 @@
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
<maxversiontested Id="10.0.18363.0"/>
<maxversiontested Id="10.0.19044.0"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!--Windows 8 -->