-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettings.vb
221 lines (198 loc) · 9.4 KB
/
Settings.vb
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
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Public Class Settings
Private _settingsPath As String
Public ReadOnly Property Loaded As Boolean = False
Public Sub Init()
Dim configFileName As String = "PropertiesDotNet.xml"
If Environment.GetEnvironmentVariable("OS") = "Windows_NT" Then
If Not Directory.Exists(Path.Combine(Environment.GetEnvironmentVariable("AppData"), "WalkmanOSS")) Then
Directory.CreateDirectory(Path.Combine(Environment.GetEnvironmentVariable("AppData"), "WalkmanOSS"))
End If
_settingsPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), "WalkmanOSS", configFileName)
Else
If Not Directory.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "WalkmanOSS")) Then
Directory.CreateDirectory(Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "WalkmanOSS"))
End If
_settingsPath = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "WalkmanOSS", configFileName)
End If
If File.Exists(Path.Combine(Application.StartupPath, configFileName)) Then
_settingsPath = Path.Combine(Application.StartupPath, configFileName)
ElseIf File.Exists(configFileName) Then
_settingsPath = New FileInfo(configFileName).FullName
End If
cbxTheme.Items.AddRange([Enum].GetNames(GetType(ThemeNames)))
_Loaded = True
If File.Exists(_settingsPath) Then
LoadSettings()
Else
' set initial settings
chkUseSystemDefault.Checked = True
chkShowOpenWithWarning.Checked = True
chkEnableAutoResize.Checked = True
chkEnableUpdateCheck.Checked = True
cbxDriveInfo.SelectedIndex = 2 ' Show on Drives
cbxDefaultSize.SelectedIndex = 11 ' Auto (Decimal)
Dim darkThemeEnabled As Boolean? = WalkmanLib.GetDarkThemeEnabled()
cbxTheme.SelectedIndex = If(darkThemeEnabled.HasValue AndAlso darkThemeEnabled.Value, ThemeNames.Dark, ThemeNames.Default)
End If
End Sub
Private Sub MeVisibleChanged() Handles Me.VisibleChanged
If Me.Visible Then Me.CenterToParent()
End Sub
Public Enum DriveInfoVisibility
AlwaysVisible
AlwaysHidden
AutoVisibility
End Enum
Public Enum ThemeNames
[Default]
Inverted
SystemDark
Dark
Test
End Enum
Public Function GetTheme() As WalkmanLib.Theme
Select Case Theme
Case ThemeNames.Default
Return WalkmanLib.Theme.Default
Case ThemeNames.Inverted
Return WalkmanLib.Theme.Inverted
Case ThemeNames.SystemDark
Return WalkmanLib.Theme.SystemDark
Case ThemeNames.Dark
Return WalkmanLib.Theme.Dark
Case ThemeNames.Test
Return WalkmanLib.Theme.Test
Case Else
Throw New ApplicationException("Invalid Theme Name: " & Theme.ToString())
End Select
End Function
Public Sub ApplyTheme()
Dim theme As WalkmanLib.Theme = GetTheme()
WalkmanLib.ApplyTheme(theme, Me, True)
If components IsNot Nothing Then WalkmanLib.ApplyTheme(theme, components.Components, True)
PropertiesDotNet.ApplyTheme(theme)
End Sub
#Region "Properties"
Public ReadOnly Property DefaultUseSystemState As Boolean
Public ReadOnly Property ShowOpenWithWarning As Boolean
Public ReadOnly Property AutoResize As Boolean
Public ReadOnly Property UpdateCheck As Boolean
Public ReadOnly Property ShowDriveInfo As DriveInfoVisibility
Public ReadOnly Property DefaultSizeSelection As Integer
Public ReadOnly Property Theme As ThemeNames
#End Region
#Region "GUI Methods"
Private Sub chkUseSystemDefault_CheckedChanged() Handles chkUseSystemDefault.CheckedChanged
_DefaultUseSystemState = chkUseSystemDefault.Checked
SaveSettings()
End Sub
Private Sub chkShowOpenWithWarning_CheckedChanged() Handles chkShowOpenWithWarning.CheckedChanged
_ShowOpenWithWarning = chkShowOpenWithWarning.Checked
SaveSettings()
End Sub
Private Sub chkEnableAutoResize_CheckedChanged() Handles chkEnableAutoResize.CheckedChanged
_AutoResize = chkEnableAutoResize.Checked
SaveSettings()
End Sub
Private Sub chkEnableUpdateCheck_CheckedChanged() Handles chkEnableUpdateCheck.CheckedChanged
_UpdateCheck = chkEnableUpdateCheck.Checked
SaveSettings()
End Sub
Private Sub cbxDriveInfo_SelectedIndexChanged() Handles cbxDriveInfo.SelectedIndexChanged
_ShowDriveInfo = DirectCast(cbxDriveInfo.SelectedIndex, DriveInfoVisibility)
SaveSettings()
End Sub
Private Sub cbxDefaltSize_SelectedIndexChanged() Handles cbxDefaultSize.SelectedIndexChanged
_DefaultSizeSelection = cbxDefaultSize.SelectedIndex
SaveSettings()
End Sub
Private Sub cbxTheme_SelectedIndexChanged() Handles cbxTheme.SelectedIndexChanged
_Theme = DirectCast(cbxTheme.SelectedIndex, ThemeNames)
SaveSettings()
ApplyTheme()
End Sub
Private Sub btnClose_Click() Handles btnClose.Click
Me.Hide()
End Sub
Private Sub btnShowSettingsFile_Click() Handles btnShowSettingsFile.Click
Process.Start("explorer.exe", "/select, " & _settingsPath)
End Sub
#End Region
#Region "Settings Saving & Loading"
Private Sub LoadSettings() Handles btnReload.Click
If Not Loaded Then Return
_loading = True
Try
Using reader As XmlReader = XmlReader.Create(_settingsPath)
Try
reader.Read()
Catch ex As XmlException
Return
End Try
If reader.IsStartElement() AndAlso reader.Name = "PropertiesDotNet" Then
If reader.Read() AndAlso reader.IsStartElement() AndAlso reader.Name = "Settings" AndAlso reader.Read() Then
While reader.IsStartElement
Select Case reader.Name
Case "DefaultUseSystemState"
reader.Read()
Boolean.TryParse(reader.Value, chkUseSystemDefault.Checked)
Case "ShowOpenWithWarning"
reader.Read()
Boolean.TryParse(reader.Value, chkShowOpenWithWarning.Checked)
Case "EnableAutoResize"
reader.Read()
Boolean.TryParse(reader.Value, chkEnableAutoResize.Checked)
Case "EnableUpdateCheck"
reader.Read()
Boolean.TryParse(reader.Value, chkEnableUpdateCheck.Checked)
Case "ShowDriveInfo"
reader.Read()
Dim selected As DriveInfoVisibility
[Enum].TryParse(reader.Value, selected)
cbxDriveInfo.SelectedIndex = selected
Case "DefaultSizeSelection"
reader.Read()
Integer.TryParse(reader.Value, cbxDefaultSize.SelectedIndex)
Case "Theme"
reader.Read()
Dim out As ThemeNames
[Enum].TryParse(reader.Value, out)
cbxTheme.SelectedIndex = out
Case Else
reader.Read() ' skip unknown values
End Select
reader.Read() : reader.Read()
End While
End If
End If
End Using
Finally
_loading = False
End Try
End Sub
Private _loading As Boolean = False ' so that we don't save while loading settings
Friend Sub SaveSettings() Handles btnSave.Click
If Not Loaded OrElse _loading Then Return
Using writer As XmlWriter = XmlWriter.Create(_settingsPath, New XmlWriterSettings With {.Indent = True})
writer.WriteStartDocument()
writer.WriteStartElement("PropertiesDotNet")
writer.WriteStartElement("Settings")
writer.WriteElementString("DefaultUseSystemState", DefaultUseSystemState.ToString())
writer.WriteElementString("ShowOpenWithWarning", ShowOpenWithWarning.ToString())
writer.WriteElementString("EnableAutoResize", AutoResize.ToString())
writer.WriteElementString("EnableUpdateCheck", UpdateCheck.ToString())
writer.WriteElementString("ShowDriveInfo", ShowDriveInfo.ToString())
writer.WriteElementString("DefaultSizeSelection", DefaultSizeSelection.ToString())
writer.WriteElementString("Theme", Theme.ToString())
writer.WriteEndElement() ' Settings
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
End Sub
#End Region
End Class