-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
236 lines (208 loc) · 7.66 KB
/
Copy pathMainForm.cs
File metadata and controls
236 lines (208 loc) · 7.66 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Drawing;
using System.Windows.Forms;
using ProgramUpdater.Services;
using ProgramUpdater.Extensions;
namespace ProgramUpdater
{
public partial class MainForm : Form
{
private TableLayoutPanel mainLayout;
private Label titleLabel;
private Label statusLabel;
private ProgressBar progressBar;
private Button cancelButton;
private RichTextBox logTextBox;
private UpdateService _updateService;
private readonly ConfigurationService _configService;
private readonly SettingsService _settingsService;
public MainForm(ConfigurationService configService, UpdateService updateService, SettingsService settingsService)
{
InitializeComponent();
InitializeCustomComponents();
SetupEventHandlers();
_configService = configService;
_updateService = updateService;
_settingsService = settingsService;
LoadSettings();
}
private void LoadSettings()
{
try
{
this.Text = _settingsService.WindowTitle;
titleLabel.Text = _settingsService.TitleText;
}
catch (Exception ex)
{
LogMessage($"Failed to load settings: {ex.Message}", LogLevel.Error);
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new SizeF(96F, 96F);
this.AutoScaleMode = AutoScaleMode.Dpi;
this.ClientSize = new Size(600, 400);
this.MinimumSize = new Size(600, 400);
this.Text = "Program Updater";
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.ResumeLayout(false);
}
private void InitializeCustomComponents()
{
// Main layout
mainLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(20),
RowCount = 5,
ColumnCount = 1
};
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 40F)); // Title
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); // Status
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); // Progress Bar
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); // Log
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 40F)); // Button
// Title Label
titleLabel = new Label
{
Text = "Program Update in Progress",
Font = new Font("Segoe UI", 14F, FontStyle.Bold),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleLeft
};
// Status Label
statusLabel = new Label
{
Text = "Initializing...",
Font = new Font("Segoe UI", 9F),
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleLeft
};
// Progress Bar
progressBar = new ProgressBar
{
Dock = DockStyle.Fill,
Style = ProgressBarStyle.Continuous,
Height = 23
};
// Log TextBox
logTextBox = new RichTextBox
{
Dock = DockStyle.Fill,
ReadOnly = true,
BackColor = Color.White,
Font = new Font("Consolas", 9F),
BorderStyle = BorderStyle.FixedSingle
};
// Cancel Button
cancelButton = new Button
{
Text = "Cancel",
Font = new Font("Consolas", 9F),
Dock = DockStyle.Right,
Width = 100,
Height = 45,
BackColor = Color.FromArgb(230, 230, 230)
};
// Add controls to layout
mainLayout.Controls.Add(titleLabel, 0, 0);
mainLayout.Controls.Add(statusLabel, 0, 1);
mainLayout.Controls.Add(progressBar, 0, 2);
mainLayout.Controls.Add(logTextBox, 0, 3);
mainLayout.Controls.Add(cancelButton, 0, 4);
this.Controls.Add(mainLayout);
}
private void SetupEventHandlers()
{
this.Load += MainForm_Load;
cancelButton.Click += CancelButton_Click;
}
private async void MainForm_Load(object sender, EventArgs e)
{
try
{
bool updateSuccess = await _updateService.PerformUpdate();
if (updateSuccess)
{
ChangeCancelButtonToClose();
}
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
LogMessage($"Update was cancelled by user: {ex.Message}", LogLevel.Warning);
}
else
{
LogMessage($"Update failed: {ex.Message}", LogLevel.Error);
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ChangeCancelButtonToClose();
}
}
private void ChangeCancelButtonToClose()
{
if (this.InvokeRequired)
{
this.Invoke(new Action(ChangeCancelButtonToClose));
return;
}
cancelButton.Text = "Close";
cancelButton.Click -= CancelButton_Click;
cancelButton.Click += (s, e) => Close();
}
private void CancelButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show(
"Are you sure you want to cancel the update process?",
"Confirm Cancellation",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_updateService?.RequestCancellation();
cancelButton.Enabled = false;
statusLabel.Text = "Cancelling...";
}
}
public void UpdateProgress(int percentage, string status)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => UpdateProgress(percentage, status)));
return;
}
progressBar.Value = percentage;
statusLabel.Text = status;
}
public void LogMessage(string message, LogLevel level)
{
if (logTextBox.InvokeRequired)
{
logTextBox.Invoke(new Action(() => LogMessage(message, level)));
return;
}
Color textColor = level switch
{
LogLevel.Error => Color.Red,
LogLevel.Warning => Color.Orange,
LogLevel.Success => Color.Green,
_ => logTextBox.ForeColor
};
logTextBox.SelectionStart = logTextBox.TextLength;
logTextBox.SelectionLength = 0;
logTextBox.SelectionColor = textColor;
logTextBox.AppendText($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{level}] {message}{Environment.NewLine}");
logTextBox.SelectionColor = logTextBox.ForeColor;
logTextBox.ScrollToCaret();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
}
}
}