/
mainForm.cs
502 lines (421 loc) · 15.6 KB
/
mainForm.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using WinLess.Models;
using WinLess.Helpers;
using WinLess.Less;
namespace WinLess
{
public partial class mainForm : Form
{
private static mainForm activeOrInActiveMainForm = null;
public static mainForm ActiveOrInActiveMainForm
{
get
{
return activeOrInActiveMainForm;
}
}
private delegate void AddCompileResultDelegate(Models.CompileResult result);
private bool finishedLoading;
#region mainForm init and shutdown
public mainForm()
{
try
{
finishedLoading = false;
activeOrInActiveMainForm = this;
Program.Settings = Settings.LoadSettings();
Program.Settings.DirectoryList.Initialize();
InitializeComponent();
initFilesDataGridViewCheckAllCheckBox();
foldersListBox.DataSource = Program.Settings.DirectoryList.Directories;
compileResultsDataGridView.DataSource = new List<Models.CompileResult>();
}
catch (Exception e)
{
ExceptionHandler.LogException(e);
}
}
public void LoadDirectories(CommandLineArguments args)
{
if (args.ClearDirectories)
{
Program.Settings.DirectoryList.ClearDirectories();
}
//load directories specified in arguments
foreach (string directoryPath in args.DirectoryPaths)
{
if(System.IO.Directory.Exists(directoryPath))
{
Models.Directory directory = Program.Settings.DirectoryList.AddDirectory(directoryPath);
foreach (Models.File file in directory.Files)
{
file.Minify = args.Minify;
if (args.InitialCompile)
{
file.Compile();
}
}
}
}
foldersListBox_DataChanged();
SelectDirectory();
Program.Settings.SaveSettings();
}
private void mainForm_Activated(object sender, EventArgs e)
{
if (!this.finishedLoading)
{
this.finishedLoading = true;
if (Program.Settings.StartMinified)
{
MinimizeToTray();
}
}
}
private void mainForm_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
CommandLineArguments commandLineArgs = new CommandLineArguments(args);
if (commandLineArgs.HasArguments)
{
LoadDirectories(commandLineArgs);
}
}
private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Program.Settings.SaveSettings();
}
#endregion
#region filesTabPage
#region foldersListBox
private void foldersListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy; // Okay
}
else
{
e.Effect = DragDropEffects.None; // Unknown data, ignore it
}
}
private void foldersListBox_DragDrop(object sender, DragEventArgs e)
{
string[] fullPaths = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string fullPath in fullPaths)
{
DirectoryInfo directoryInfo = new DirectoryInfo(fullPath);
if (directoryInfo.Exists && !foldersListBox.Items.Contains(directoryInfo.FullName))
{
Program.Settings.DirectoryList.AddDirectory(directoryInfo.FullName);
foldersListBox_DataChanged();
SelectDirectory();
Program.Settings.SaveSettings();
}
}
}
private void foldersListBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
RemoveDirectory();
}
}
private void foldersListBox_SelectedValueChanged(object sender, EventArgs e)
{
SelectDirectory();
}
private void foldersListBox_DataChanged()
{
((CurrencyManager)foldersListBox.BindingContext[foldersListBox.DataSource]).Refresh();
filesDataGridView_DataChanged();
}
private void RemoveDirectory()
{
if (foldersListBox.SelectedItem != null)
{
Program.Settings.DirectoryList.RemoveDirectory((Models.Directory)foldersListBox.SelectedItem);
foldersListBox_DataChanged();
filesDataGridView.DataSource = new List<Models.File>();
filesDataGridView_DataChanged();
Program.Settings.SaveSettings();
}
}
private void SelectDirectory()
{
Models.Directory directory = (Models.Directory)foldersListBox.SelectedItem;
if (directory != null)
{
filesDataGridView.DataSource = directory.Files;
}
else
{
filesDataGridView.DataSource = new List<Models.File>();
}
filesDataGridView_DataChanged();
}
#endregion
#region filesDataGridView
private void filesDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
Program.Settings.SaveSettings();
}
private void filesDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
filesDataGridView_OpenSelectedFile();
}
private void filesDataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
filesDataGridView.CurrentCell = filesDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
}
private void initFilesDataGridViewCheckAllCheckBox()
{
// add checkbox header
Rectangle rect = filesDataGridView.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = 10;
rect.Y = 4;
CheckBox checkAllFilesCheckbox = new CheckBox();
checkAllFilesCheckbox.Name = "checkboxHeader";
checkAllFilesCheckbox.Size = new Size(15, 15);
checkAllFilesCheckbox.Location = rect.Location;
checkAllFilesCheckbox.CheckedChanged += new EventHandler(checkAllFilesCheckbox_CheckedChanged);
filesDataGridView.Controls.Add(checkAllFilesCheckbox);
}
private void checkAllFilesCheckbox_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < filesDataGridView.RowCount; i++)
{
filesDataGridView[0, i].Value = ((CheckBox)filesDataGridView.Controls.Find("checkboxHeader", true)[0]).Checked;
}
filesDataGridView.EndEdit();
}
private void filesDataGridView_DataChanged()
{
List<Models.File> files = (List<Models.File>)filesDataGridView.DataSource;
files.Sort((x, y) => string.Compare(x.FullPath, y.FullPath));
((CurrencyManager)filesDataGridView.BindingContext[filesDataGridView.DataSource]).Refresh();
}
private void filesDataGridView_OpenSelectedFile()
{
DataGridViewCell cell = filesDataGridView.SelectedCells[0];
Models.File file = (Models.File)cell.OwningRow.DataBoundItem;
string filePath;
if (cell.ColumnIndex == 1)
{
filePath = file.FullPath;
}
else
{
filePath = file.OutputPath;
}
if (System.IO.File.Exists(filePath))
{
Process process = new Process();
process.StartInfo.FileName = filePath;
process.Start();
}
}
private void openFiletoolStripMenuItem_Click(object sender, EventArgs e)
{
filesDataGridView_OpenSelectedFile();
}
private void fileOpenFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
DataGridViewCell cell = filesDataGridView.SelectedCells[0];
Models.File file = (Models.File)cell.OwningRow.DataBoundItem;
string filePath;
if (cell.ColumnIndex == 1)
{
filePath = file.FullPath;
}
else
{
filePath = file.OutputPath;
}
FileInfo fileInfo = new FileInfo(filePath);
string directoryPath = fileInfo.DirectoryName;
if (System.IO.Directory.Exists(directoryPath))
{
Process process = new Process();
process.StartInfo.FileName = directoryPath;
process.Start();
}
}
private void fileSelectOutputToolStripMenuItem_Click(object sender, EventArgs e)
{
DataGridViewCell cell = filesDataGridView.SelectedCells[0];
Models.File file = (Models.File)cell.OwningRow.DataBoundItem;
FileInfo fileInfo = new FileInfo(file.OutputPath);
outputFolderBrowserDialog.SelectedPath = fileInfo.DirectoryName;
if (outputFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
file.OutputPath = string.Format("{0}\\{1}", outputFolderBrowserDialog.SelectedPath, fileInfo.Name);
filesDataGridView_DataChanged();
Program.Settings.SaveSettings();
}
}
#endregion
#region Buttons
private void addDirectoryButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
Program.Settings.DirectoryList.AddDirectory(folderBrowserDialog.SelectedPath);
foldersListBox_DataChanged();
Program.Settings.SaveSettings();
}
}
private void removeDirectoryButton_Click(object sender, EventArgs e)
{
RemoveDirectory();
}
private void refreshDirectoryButton_Click(object sender, EventArgs e)
{
if (foldersListBox.SelectedItem != null)
{
Models.Directory directory = (Models.Directory)foldersListBox.SelectedItem;
directory.Refresh();
SelectDirectory();
Program.Settings.SaveSettings();
}
}
private void compileSelectedButton_Click(object sender, EventArgs e)
{
CompileSelectedFiles();
}
private void compileToolStripMenuItem_Click(object sender, EventArgs e)
{
CompileSelectedFiles();
}
#endregion
#endregion
#region compilerTabPage
#region compileResultsDataGridView
public void AddCompileResult(Models.CompileResult result)
{
if (InvokeRequired)
{
this.Invoke(new AddCompileResultDelegate(AddCompileResult), new object[] { result });
return;
}
List<Models.CompileResult> compileResults = (List<Models.CompileResult>)compileResultsDataGridView.DataSource;
compileResults.Insert(0, result);
compileResultsDataGridView_DataChanged();
if (string.Compare(result.ResultText, "success", StringComparison.InvariantCultureIgnoreCase) != 0)
{
ShowErrorNotification("Compile error", result.ResultText);
}
else if (Program.Settings.ShowSuccessMessages)
{
ShowSuccessNotification("Successful compile", result.FullPath);
}
}
private void compileResultsDataGridView_DataChanged()
{
((CurrencyManager)compileResultsDataGridView.BindingContext[compileResultsDataGridView.DataSource]).Refresh();
}
#endregion
#region Buttons
private void clearCompileResultsButton_Click(object sender, EventArgs e)
{
compileResultsDataGridView.DataSource = new List<Models.CompileResult>();
compileResultsDataGridView_DataChanged();
}
#endregion
#endregion
#region notifyIcon
private void mainForm_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
this.Hide();
}
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
RestoreFromTray();
}
else
{
MinimizeToTray();
}
}
private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
RestoreFromTray();
tabControl.SelectTab(compilerTabPage);
}
private void MinimizeToTray()
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
}
public void RestoreFromTray()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void ShowSuccessNotification(string title, string message){
notifyIcon.ShowBalloonTip(500, title, message, ToolTipIcon.Info);
}
private void ShowErrorNotification(string title, string message)
{
notifyIcon.ShowBalloonTip(500, title, message, ToolTipIcon.Error);
}
private void notifyIconMenuOpen_Click(object sender, EventArgs e)
{
RestoreFromTray();
}
private void notifyIconMenuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
#region menu
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
settingsForm form = new settingsForm();
form.ShowDialog(this);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
aboutForm form = new aboutForm();
form.ShowDialog(this);
}
#endregion
#region Helper Methods
/// <summary>
/// Compiles a list of selected LESS files
/// </summary>
private void CompileSelectedFiles()
{
// Retrieve list of files from data grid
List<Models.File> files = (List<Models.File>)filesDataGridView.DataSource;
// Compile files one by one
foreach (Models.File file in files)
{
if (file.Enabled)
{
Compiler.CompileLessFile(file.FullPath, file.OutputPath, file.Minify);
}
}
}
#endregion
}
}