-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsWindow.cs
145 lines (124 loc) · 6.6 KB
/
SettingsWindow.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
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace BatteryWatch
{
public partial class SettingsWindow : Form
{
private int lowestBatteryPercent = 0;
private int highestBatteryPercent = 0;
public SettingsWindow()
{
InitializeComponent();
}
private void lowestBatteryPercentTextBox_TextChanged(object sender, System.EventArgs e)
{
int previousCaretPosition = RemoveNonNumericalCharacters(lowestBatteryPercentTextBox);
// add visual percentage
if (!lowestBatteryPercentTextBox.Text.EndsWith("%") && lowestBatteryPercentTextBox.Text.Length > 0)
AddVisualPercentage(lowestBatteryPercentTextBox, previousCaretPosition);
// check if the input is longer than 2 numbers
if (lowestBatteryPercentTextBox.Text.Length > 3) // bigger than three because of the % at the end
{
HandleTooLongInput(lowestBatteryPercentTextBox);
}
}
private void LowestBatteryPercentTextBox_SelectionChanged(object sender, System.EventArgs e)
{
if (lowestBatteryPercentTextBox.SelectionStart == lowestBatteryPercentTextBox.Text.Length)
// we cannot go past the % symbol
lowestBatteryPercentTextBox.SelectionStart -= 1;
else if (lowestBatteryPercentTextBox.SelectedText.Contains("%"))
// we cannot select the % symbol
lowestBatteryPercentTextBox.SelectionLength -= 1;
}
private void highestBatteryPercentTextBox_TextChanged(object sender, System.EventArgs e)
{
int previousCaretPosition = RemoveNonNumericalCharacters(highestBatteryPercentTextBox);
// add visual percentage
if (!highestBatteryPercentTextBox.Text.EndsWith("%") && highestBatteryPercentTextBox.Text.Length > 0)
AddVisualPercentage(highestBatteryPercentTextBox, previousCaretPosition);
// check if the input is longer than 2 numbers
if (highestBatteryPercentTextBox.Text.Length > 3) // bigger than three because of the % at the end
{
HandleTooLongInput(highestBatteryPercentTextBox);
}
}
private void HandleTooLongInput(RichTextBox textBox)
{
/* this method is called whenever the user tries to enter input that is too long,
because the maximum permitted length is two numbers */
int previousCaretPosition = textBox.SelectionStart; // get the caret position before removing the excess numbers
textBox.Text = textBox.Text.Remove(previousCaretPosition - 1, 1); // remove the unecessary number
textBox.SelectionStart = previousCaretPosition - 1; // move the caret where it was before the user tried to add a number
}
private void HighestBatteryPercentTextBox_SelectionChanged(object sender, System.EventArgs e)
{
if (highestBatteryPercentTextBox.SelectionStart == highestBatteryPercentTextBox.Text.Length)
// we cannot go past the % symbol
highestBatteryPercentTextBox.SelectionStart -= 1;
else if (highestBatteryPercentTextBox.SelectedText.Contains("%"))
// we cannot select the % symbol
highestBatteryPercentTextBox.SelectionLength -= 1;
}
private int RemoveNonNumericalCharacters(RichTextBox textBox)
{
/* this method removes every non-numerical character, entered in a text box.
it is called whenever the text boxes are edited and before the % symbol is added
Returns the previous caret position*/
int previousCaretPosition = 0;
if (!textBox.Text.EndsWith("%")) // if it's the first text entered (copy-pasted numbers longer than 2 digits)
previousCaretPosition = textBox.Text.Length - 1;
else
previousCaretPosition = textBox.SelectionStart == 0 ? 0 : textBox.SelectionStart - 1;
textBox.Text = Regex.Replace(textBox.Text, @"[^0-9]", "");
return previousCaretPosition;
}
private void AddVisualPercentage(RichTextBox textBox, int previousCaretPosition)
{
/* this methods adds a % symbol to the end of the textbox's text */
textBox.Text += '%';
// move the cursor where it should be
if (previousCaretPosition + 1 != textBox.Text.Length)
textBox.SelectionStart = previousCaretPosition + 1;
else
textBox.SelectionStart = previousCaretPosition;
}
private void closeSettingsButton_Click(object sender, System.EventArgs e)
{
// parse the text
int.TryParse(this.lowestBatteryPercentTextBox.Text.Replace("%", ""), out this.lowestBatteryPercent);
int.TryParse(this.highestBatteryPercentTextBox.Text.Replace("%", ""), out this.highestBatteryPercent);
if (InputIsValid(this.lowestBatteryPercent, this.highestBatteryPercent))
{
this.Hide();
// create the tray icon and start the watcher loop
var bwac = new BatteryWatchApplicationContext(this.lowestBatteryPercent, this.highestBatteryPercent);
}
else
{
InvalidInputWindow invalidInputWindow = new InvalidInputWindow();
invalidInputWindow.Show();
}
}
private bool InputIsValid(int minimumPercentage, int maximumPercentage)
{
/* returns if the input that has been entered is valid. Must be numbers from 2-99 and the minimumPercentage must
be 5 less than the maximum */
bool validMinPercentage = (minimumPercentage >= 2 && minimumPercentage <= 94);
bool validMaxPercentage = (maximumPercentage >= 7 && maximumPercentage <= 99);
bool differenceIsBiggerThanFive = (maximumPercentage - minimumPercentage) >= 5;
return validMinPercentage && validMaxPercentage && differenceIsBiggerThanFive;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Disablind Ctrl-V (pasting) in the textbox
switch (keyData)
{
case Keys.Control | Keys.V:
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
}