forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitExtensionsUserControl.cs
132 lines (109 loc) · 4.06 KB
/
GitExtensionsUserControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using GitCommands;
namespace ResourceManager
{
/// <summary>Provides translation and hotkey plumbing for GitEx <see cref="UserControl"/>s.</summary>
public class GitExtensionsControl : UserControl, ITranslate
{
public GitExtensionsControl()
{
Font = AppSettings.Font;
Load += GitExtensionsControl_Load;
}
public int GetCurrentDeviceDpi()
{
#if TARGETING_DOTNET47
int deviceDpi = DeviceDpi;
#else
int deviceDpi;
using (Graphics g = this.CreateGraphics())
{
deviceDpi = (int)g.DpiX;
}
#endif
return deviceDpi;
}
[Browsable(false)] // because we always read from settings
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override Font Font
{
get { return base.Font; }
set { base.Font = value; }
}
private static bool CheckComponent(object value)
{
bool isComponentInDesignMode = false;
var component = value as IComponent;
if (component != null)
{
ISite site = component.Site;
if ((site != null) && site.DesignMode)
isComponentInDesignMode = true;
}
return isComponentInDesignMode;
}
protected virtual void OnRuntimeLoad(EventArgs e)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!CheckComponent(this))
OnRuntimeLoad(e);
}
private bool translated;
void GitExtensionsControl_Load(object sender, EventArgs e)
{
// find out if the value is a component and is currently in design mode
bool isComponentInDesignMode = CheckComponent(this);
if (!translated && !isComponentInDesignMode)
throw new Exception("The control " + GetType().Name + " is not translated in the constructor. You need to call Translate() right after InitializeComponent().");
}
/// <summary>Translates the <see cref="UserControl"/>'s elements.</summary>
protected void Translate()
{
Translator.Translate(this, GitCommands.AppSettings.CurrentTranslation);
translated = true;
}
public virtual void AddTranslationItems(ITranslation translation)
{
TranslationUtils.AddTranslationItemsFromFields(Name, this, translation);
}
public virtual void TranslateItems(ITranslation translation)
{
TranslationUtils.TranslateItemsFromFields(Name, this, translation);
}
#region Hotkeys
/// <summary>Gets or sets a value that specifies if the hotkeys are used</summary>
protected bool HotkeysEnabled { get; set; }
/// <summary>Gets or sets the hotkeys</summary>
protected IEnumerable<HotkeyCommand> Hotkeys { get; set; }
/// <summary>Checks if a hotkey wants to handle the key before letting the message propagate.</summary>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (HotkeysEnabled && this.Hotkeys != null)
foreach (var hotkey in this.Hotkeys)
{
if (hotkey != null && hotkey.KeyData == keyData)
{
return ExecuteCommand(hotkey.CommandCode);
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// Override this method to handle form-specific Hotkey commands.
/// <remarks>This base method does nothing and returns false.</remarks>
/// </summary>
/// <param name="command"></param>
protected virtual bool ExecuteCommand(int command)
{
return false;
}
#endregion
}
}