forked from TheJoeFin/Caffeinated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
206 lines (174 loc) · 6.52 KB
/
SettingsForm.cs
File metadata and controls
206 lines (174 loc) · 6.52 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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Caffeinated.Helpers;
using Windows.ApplicationModel;
namespace Caffeinated;
public partial class SettingsForm : BaseForm {
readonly BindingList<Duration> Durations;
readonly AppSettings appSettings;
public SettingsForm(AppSettings passedAppSettings) : base() {
InitializeComponent();
appSettings = passedAppSettings;
var durations = from i in appSettings.Durations
select new Duration { Minutes = i };
this.Durations = new BindingList<Duration>(durations.ToList());
var defaultItem = this.Durations.Where(
d => d.Minutes == appSettings.DefaultDuration
).FirstOrDefault();
if (appSettings.ShowMessageOnLaunch)
SettingsAtLaunchChkBox.Checked = true;
if (appSettings.ActivateOnLaunch)
ActivateChkBox.Checked = true;
DefaultDurationBox.DataSource = this.Durations;
DefaultDurationBox.DisplayMember = "Description";
DefaultDurationBox.ValueMember = "Minutes";
DefaultDurationBox.SelectedItem = defaultItem;
ToolStripMenuItem deleteMI = new("Delete Duration");
deleteMI.Click += DeleteMI_Click;
ContextMenuStrip durationCM = new();
durationCM.Items.Add(deleteMI);
DefaultDurationBox.ContextMenuStrip = durationCM;
setStartupCheckBox();
setRadioButtons();
}
private void setRadioButtons() {
switch (appSettings.Icon) {
case TrayIcon.Mug:
mugRDBTN.Checked = true;
break;
case TrayIcon.EyeWithZzz:
eyeZZZRDBTN.Checked = true;
break;
default:
defaultRDBTN.Checked = true;
break;
}
}
private void DeleteMI_Click(object? sender, EventArgs e) {
if (DefaultDurationBox.SelectedItem is not Duration durationToDelete)
return;
DialogResult result = MessageBox.Show(
$"Delete {durationToDelete.Description}?",
"Caffeinated",
MessageBoxButtons.YesNo
);
switch (result) {
case DialogResult.None:
break;
case DialogResult.OK:
break;
case DialogResult.Cancel:
break;
case DialogResult.Abort:
break;
case DialogResult.Retry:
break;
case DialogResult.Ignore:
break;
case DialogResult.Yes:
Durations.Remove(durationToDelete);
appSettings.Durations.Remove(durationToDelete.Minutes);
// appSettings.Durations = appSettings.Durations;
break;
case DialogResult.No:
break;
default:
break;
}
}
private async void setStartupCheckBox() {
StartupTask startupTask = await StartupTask.GetAsync("StartCaffeinated");
Debug.WriteLine("Startup is " + startupTask.State.ToString());
switch (startupTask.State) {
case StartupTaskState.Disabled:
// Task is disabled but can be enabled.
StartupChkBox.Checked = false;
break;
case StartupTaskState.DisabledByUser:
// Task is disabled and user must enable it manually.
StartupChkBox.Checked = false;
StartupChkBox.Enabled = false;
StartupChkBox.Text += "\nDisabled in Task Manager";
break;
case StartupTaskState.Enabled:
StartupChkBox.Checked = true;
break;
}
}
private void okBtn_Click(object sender, EventArgs e) {
this.Close();
}
private void cancelBtn_Click(object sender, EventArgs e) {
this.Close();
}
private void DefaultDurationBox_SelectedIndexChanged(object sender,EventArgs e) {
if (DefaultDurationBox.SelectedItem is Duration item) {
appSettings.DefaultDuration = item.Minutes;
}
}
private async void StartupChkBox_CheckedChanged(object sender, EventArgs e) {
StartupTask startupTask = await StartupTask.GetAsync("StartCaffeinated");
switch (StartupChkBox.Checked) {
case true:
StartupTaskState newState = await startupTask.RequestEnableAsync();
Debug.WriteLine("Request to enable startup, result = {0}", newState);
break;
case false:
startupTask.Disable();
Debug.WriteLine("Disabled startup task");
break;
}
}
private void addCustomDurationBTN_Click(object sender, EventArgs e) {
bool didParse = int.TryParse(CustomDurationTXBX.Text, out int newDuration);
if (didParse == false)
return;
if ( newDuration < 0) {
CustomDurationTXBX.Text = "";
MessageBox.Show(
"Enter a positive number.",
"Caffeinated",
MessageBoxButtons.OK
);
return;
}
if (appSettings.Durations.Contains(newDuration)) {
CustomDurationTXBX.Text = "";
MessageBox.Show(
$"{newDuration} is already a duration.",
"Caffeinated",
MessageBoxButtons.OK
);
return;
}
Duration newCustomDuration = new() {
Minutes = newDuration
};
Durations.Add(newCustomDuration);
var sortedDurations = Durations.OrderByDescending(i => i).ToList();
Durations.Clear();
foreach (var item in sortedDurations) {
Durations.Add(item);
}
appSettings.Durations.Add(newDuration);
CustomDurationTXBX.Text = "";
}
private void defaultRDBTN_Click(object sender, EventArgs e) {
appSettings.Icon = TrayIcon.Default;
}
private void eyeZZZRDBTN_Click(object sender, EventArgs e) {
appSettings.Icon = TrayIcon.EyeWithZzz;
}
private void mugRDBTN_Click(object sender, EventArgs e) {
appSettings.Icon = TrayIcon.Mug;
}
private void ActivateChkBox_CheckedChanged(object sender, EventArgs e) {
appSettings.ActivateOnLaunch = ActivateChkBox.Checked;
}
private void SettingsAtLaunchChkBox_CheckedChanged(object sender, EventArgs e) {
appSettings.ShowMessageOnLaunch = SettingsAtLaunchChkBox.Checked;
}
}