-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cs
More file actions
113 lines (85 loc) · 4.42 KB
/
FormMain.cs
File metadata and controls
113 lines (85 loc) · 4.42 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
using System.Reflection;
namespace LinesOfCodeCounter;
public partial class FormMain : Form
{
string FolderToExamine { get; set; }
DataGridColorHelper dataGridColorHelper;
HashSet<string> acceptedFileTypes = new() {".cs", ".py", ".shader", ".cpp", ".h"};
HashSet<string> excludedFilePaths = new() {@"\bin", @"\obj", @"\Properties", @"\Debug", @"\Release", @"\Plugins", @"\LeanTween", @"\ThirdParty", @"\Third Party", @"\TextMesh Pro" };
HashSet<CodeFile> codeFiles = new();
public FormMain() => InitializeComponent();
void Form1_Load(object sender, EventArgs e) => SetupUI();
void SetupUI()
{
labelFilezCounted.Text = labelTotalLines.Text = labelTotalChars.Text = labelAvgLines.Text = labelAvgChars.Text = string.Empty;
UserInputConverter.FillTextBoxesWithSettings(acceptedFileTypes, excludedFilePaths, richTextBoxAllowedFiles, richTextBoxExcludedFiles);
SetupDataGridView();
void SetupDataGridView()
{
EnableDoubleBufferedDataGrid();
dataGridColorHelper = new(dataGridView1, this.Font);
dataGridView1.ForeColor = dataGridColorHelper.TextColor;
void EnableDoubleBufferedDataGrid() => typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
}
}
void buttonSelectFolder_Click(object sender, EventArgs e)
{
using(var folderBrowser = new FolderBrowserDialog())
{
DialogResult result = folderBrowser.ShowDialog();
if(result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath))
{
FolderToExamine = folderBrowser.SelectedPath;
labelTimeTaken.Text = "";
}
}
labelSelectedFolder.Text = "Examining Folder: " + FolderToExamine;
}
void buttonDoCaluclation_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(FolderToExamine)) return;
UpdateSettings();
if(TryGetResults(out CodeAnalysisResult? result))
UpdateUILabels(result!);
void UpdateSettings()
{
UserInputConverter.ConvertUserInputsToRealSettings(ref acceptedFileTypes, ref excludedFilePaths, richTextBoxAllowedFiles, richTextBoxExcludedFiles);
var timeTaken = DataGridViewFiller.GenerateAndFillDataGridView(ref codeFiles, dataGridView1, FolderToExamine, acceptedFileTypes, excludedFilePaths);
labelTimeTaken.Text = $"Analysis finished in {timeTaken.TotalSeconds.ToString("F2")} seconds.";
}
bool TryGetResults(out CodeAnalysisResult? result)
{
if(codeFiles?.Count <= 0)
{
MessageBox.Show("No matches found. Assure file extensions you want are inlcuded.");
result = null;
return false;
}
result = new CodeAnalysisResult(codeFiles!);
return true;
}
void UpdateUILabels(CodeAnalysisResult result)
{
if(result == null) return;
labelFilezCounted.Text = result.TotalFiles.ToString("#,0");
labelTotalLines.Text = result.TotalLines.ToString("#,0");
labelTotalChars.Text = result.TotalCharacters.ToString("#,0");
labelAvgLines.Text = result.AverageLinesPerFile.ToString("#,0.##");
labelAvgChars.Text = result.AverageCharactersPerLine.ToString("#,0.##");
}
}
void panelMenuBar_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
Native.ReleaseCapture();
Native.SendMessage(Handle, Native.WM_NCLBUTTONDOWN, Native.HT_CAPTION, 0);
}
}
void buttonQuit_Click(object sender, EventArgs e) => Environment.Exit(0);
void buttonMinimize_Click(object sender, EventArgs e) => this.WindowState = FormWindowState.Minimized;
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) => dataGridColorHelper.DoRowBackColor(e);
void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) => dataGridColorHelper.DoRowHeaderColor(sender, e);
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) => dataGridColorHelper.DoSelectAllColor(e);
void dataGridView1_Paint(object sender, PaintEventArgs e) => dataGridColorHelper.DoColumnHeaderColor(e);
}