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

View file

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