-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsWindow.cs
171 lines (132 loc) · 7.48 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Text.Json;
using System.Threading;
namespace Video_Encoder__NET_Core_Version_ {
public partial class SettingsWindow : Form {
public SettingsWindow(Form _mainWindow) {
InitializeComponent();
/// List of fields that will be saved and loaded from the savefile.
savedFields.AddRange(new List<Control>
{ textBox_ffmpegLoc, checkBox_CopyDate, checkBox_Shadowplay });
mainWindow = _mainWindow;
loadSettings();
this.ActiveControl = label_Title; /// Set focus to label to draw focus away from buttons
}
#region /// Draggable Form ///
/// System for making form draggable. SRC: https://stackoverflow.com/a/1592899/8233240
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ReleaseCapture();
private void SettingsWindow_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
#endregion
public string settingsLocation = Path.GetDirectoryName
($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\VideoEncoder\\");
public List<Control> savedFields = new();
private readonly Form mainWindow;
public void loadSettings() {
Dictionary<string, string> jsonDict = new();
try {
jsonDict = (Dictionary<string, string>) /// Explicit cast as return type of Deserialize() is object
JsonSerializer.Deserialize(File.ReadAllText($"{settingsLocation}\\settings.json"), jsonDict.GetType());
//foreach (KeyValuePair<string, string> p in jsonDict) {
// Debug.WriteLine($"Setting: {p.Key}; Value: {p.Value}");
// if (bool.TryParse(p.Value, out bool result)) { Debug.WriteLine(result); }
//}
foreach (Control c in savedFields) {
if (jsonDict.TryGetValue(c.Tag.ToString(), out string settingValue)) {
//Debug.WriteLine($"{c.Name}: {settingValue}");
/// Conditional expression unsuitable for this function. Will replace with if statement
//c.GetType() != typeof(CheckBox) ? c.Text = settingValue : ((CheckBox)c).Checked = bool.Parse(settingValue);
/// Clunky if statement
//if (c.GetType() == typeof(CheckBox)) {
// ((CheckBox)c).Checked = bool.Parse(settingValue);
//} else {
// c.Text = settingValue;
//}
/// Cleaner if statement; uses 'is' operator and pattern matching
if (c is CheckBox cb) { cb.Checked = bool.Parse(settingValue); } else { c.Text = settingValue; }
}
}
((MainWindow)mainWindow).ffmpegPath = textBox_ffmpegLoc.Text;
Debug.WriteLine($"Settings loaded from {settingsLocation}\\settings.json .");
} catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) { /// If file not found
Debug.WriteLine($"Settings file not found at {settingsLocation}. One will be generated later.");
} catch (Exception e) { /// Any other error
Debug.WriteLine($"An error occurred while the settings file was being read. \n {e}");
}
}
private void button_Close_Click(object sender, EventArgs e) {
this.Close();
}
private void button_download_ffmpeg_Click(object sender, EventArgs e) {
using (var downloadwindow = new DownloadWindow()) {
downloadwindow.StartPosition = FormStartPosition.CenterParent;
downloadwindow.ShowInTaskbar = false;
downloadwindow.settingsWindow = this;
downloadwindow.ShowDialog();
}
}
private void button_Save_Click(object sender, EventArgs e) {
Dictionary<string, string> jsonDict = new();
foreach (Control c in savedFields) {
/// Print out tag text and then the control text, unless it's a checkbox, in which case checked state.
//Debug.WriteLine($"{c.Tag}: { (c.GetType() != typeof(CheckBox) ? c.Text : ((CheckBox)c).Checked) }");
jsonDict.Add(c.Tag.ToString(), c.GetType() != typeof(CheckBox) ? c.Text : ((CheckBox)c).Checked.ToString());
}
JsonSerializerOptions jsonOptionsPretty = new() { WriteIndented = true };
string jsonText = JsonSerializer.Serialize(jsonDict, jsonOptionsPretty);
//Debug.WriteLine(jsonText);
SaveSettings:
try {
File.WriteAllText($"{settingsLocation}\\settings.json", jsonText);
Debug.WriteLine($"Settings saved to {settingsLocation}\\settings.json .");
button_Save.Text = "Saved!";
button_Save.Update();
} catch (DirectoryNotFoundException) { /// If directory doesn't exist
try {
Debug.WriteLine("Error in saving file; directory non-existent. Attempting creation.");
Directory.CreateDirectory(settingsLocation); /// Try to create
goto SaveSettings; /// Really shouldn't be using goto, it's a little dirty but it works.
} catch (Exception exc) { /// If error in creation
Debug.WriteLine("Error in creation of directory. \n" + exc.Message);
}
} catch (Exception ex) { /// If any other error other than non-existent directory
Debug.WriteLine($"Other error in saving settings to file. \n" + ex.Message);
}
((MainWindow)mainWindow).ffmpegPath = textBox_ffmpegLoc.Text;
Thread.Sleep(500); /// Artifical wait of 500ms
button_Save.Text = "Save Changes";
this.Close();
}
private void button_select_ffmpegLoc_Click(object sender, EventArgs e) {
using OpenFileDialog openFileDialog = new();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFileDialog.Filter = "FFmpeg|*.exe"; ///TODO: Consider adding name specific filter.
openFileDialog.FilterIndex = 0;
openFileDialog.Title = "Find FFmpeg";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK) {
string filePath = openFileDialog.FileName;
textBox_ffmpegLoc.Text = filePath;
}
}
private void SettingsWindow_DragEnter(object sender, DragEventArgs e) {
///TODO: Come up with filtering OTF for dragging files in. Use code from Main as reference
}
private void SettingsWindow_DragDrop(object sender, DragEventArgs e) {
///TODO: Manage extraction of path and putting into textbox
}
}
}