-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlocker.cs
152 lines (132 loc) · 4.72 KB
/
Blocker.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using NLog;
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DeploymentToolkit.Blocker
{
public partial class Blocker : Form
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private bool _allowClose = false;
private byte _step;
private Timer _timer;
public Blocker()
{
_logger.Trace("Blocker initializing...");
InitializeComponent();
_logger.Trace("Setting blocker settings...");
this.FormBorderStyle = FormBorderStyle.None;
this.KeyDown += Blocker_KeyDown;
this.FormClosing += Blocker_Closing;
// Needed for transparency to work
_logger.Trace("Switching parents...");
this.PictureLogo.Parent = this.PictureBackground;
this.LabelInstallation.Parent = this.PictureBackground;
if (File.Exists("Logo.png"))
{
_logger.Trace("Custom logo found. Creating logo...");
try
{
this.PictureLogo.Image = Image.FromFile("Logo.png");
this.PictureLogo.SizeMode = PictureBoxSizeMode.Zoom;
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to create logo");
}
}
if(File.Exists("Background.png"))
{
_logger.Trace("Custom background found. Creating background...");
try
{
this.PictureBackground.Image = Image.FromFile("Background.png");
this.PictureBackground.SizeMode = PictureBoxSizeMode.StretchImage;
}
catch(Exception ex)
{
_logger.Error(ex, "Failed to create background image");
}
}
else if(!string.IsNullOrEmpty(Program.Settings.BackgroundColor))
{
_logger.Info("Custom background color specified in Settings.xml. Setting colors...");
var color = GetColor(Program.Settings.BackgroundColor);
if (color != default(Color))
this.PictureBackground.BackColor = color;
else
_logger.Warn($"Unknown color specified ({Program.Settings.BackgroundColor})");
}
if(!string.IsNullOrEmpty(Program.Settings.ForegroundColor))
{
_logger.Info("Custom foreground color specified in Settings.xml. Setting colors...");
var color = GetColor(Program.Settings.ForegroundColor);
if (color != default(Color))
this.LabelInstallation.ForeColor = color;
else
_logger.Warn($"Unknown color specified ({Program.Settings.BackgroundColor})");
}
_logger.Info("Creating and starting timer...");
_timer = new Timer
{
Interval = 1000
};
_timer.Tick += Tick;
_timer.Start();
_logger.Info("Blocker successfully initialized");
}
private void Tick(object sender, EventArgs e)
{
var text = new StringBuilder("Installation running\n");
for (var i = 0; i < _step; i++)
{
text.Append(".");
}
this.LabelInstallation.Text = text.ToString();
if (++_step > 3)
_step = 0;
}
private void Blocker_KeyDown(object sender, KeyEventArgs e)
{
#if DEBUG
//Closing over key should only be possible in debug mode
if (e.KeyCode == Keys.Q)
{
_logger.Warn("Closing allowed because of DEBUG build");
_allowClose = true;
this.Close();
}
#endif
}
private void Blocker_Closing(object sender, FormClosingEventArgs e)
{
if (!_allowClose)
{
_logger.Trace("User tried to close application. Preventing...");
e.Cancel = true;
}
}
private Color GetColor(string color)
{
var result = default(Color);
try
{
if (color.StartsWith("#"))
{
result = ColorTranslator.FromHtml(color);
}
else
{
result = Color.FromName(color);
}
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to parse color ({color})");
}
return result;
}
}
}