Options.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace Optimizer
  9. {
  10. [Serializable]
  11. public sealed class SettingsJson
  12. {
  13. public Color Theme { get; set; }
  14. public string AppsFolder { get; set; }
  15. public bool EnableTray { get; set; }
  16. public bool AutoStart { get; set; }
  17. public LanguageCode LanguageCode { get; set; }
  18. // universal
  19. public bool EnablePerformanceTweaks { get; set; }
  20. public bool DisableNetworkThrottling { get; set; }
  21. public bool DisableWindowsDefender { get; set; }
  22. public bool DisableSystemRestore { get; set; }
  23. public bool DisablePrintService { get; set; }
  24. public bool DisableMediaPlayerSharing { get; set; }
  25. public bool DisableErrorReporting { get; set; }
  26. public bool DisableHomeGroup { get; set; }
  27. public bool DisableSuperfetch { get; set; }
  28. public bool DisableTelemetryTasks { get; set; }
  29. public bool DisableCompatibilityAssistant { get; set; }
  30. public bool DisableFaxService { get; set; }
  31. public bool DisableSmartScreen { get; set; }
  32. public bool DisableCloudClipboard { get; set; }
  33. public bool DisableStickyKeys { get; set; }
  34. public bool DisableHibernation { get; set; }
  35. public bool DisableSMB1 { get; set; }
  36. public bool DisableSMB2 { get; set; }
  37. public bool DisableNTFSTimeStamp { get; set; }
  38. public bool DisableSearch { get; set; }
  39. // apps telemetry
  40. public bool DisableOffice2016Telemetry { get; set; }
  41. public bool DisableVisualStudioTelemetry { get; set; }
  42. public bool DisableFirefoxTemeletry { get; set; }
  43. public bool DisableChromeTelemetry { get; set; }
  44. public bool DisableNVIDIATelemetry { get; set; }
  45. // windows 8
  46. public bool DisableOneDrive { get; set; }
  47. // windows 10
  48. public bool EnableLegacyVolumeSlider { get; set; }
  49. public bool DisableQuickAccessHistory { get; set; }
  50. public bool DisableStartMenuAds { get; set; }
  51. public bool UninstallOneDrive { get; set; }
  52. public bool DisableMyPeople { get; set; }
  53. public bool DisableAutomaticUpdates { get; set; }
  54. public bool ExcludeDrivers { get; set; }
  55. public bool DisableTelemetryServices { get; set; }
  56. public bool DisablePrivacyOptions { get; set; }
  57. public bool DisableCortana { get; set; }
  58. public bool DisableSensorServices { get; set; }
  59. public bool DisableWindowsInk { get; set; }
  60. public bool DisableSpellingTyping { get; set; }
  61. public bool DisableXboxLive { get; set; }
  62. public bool DisableGameBar { get; set; }
  63. public bool DisableInsiderService { get; set; }
  64. public bool DisableStoreUpdates { get; set; }
  65. public bool EnableLongPaths { get; set; }
  66. public bool RemoveCastToDevice { get; set; }
  67. public bool EnableGamingMode { get; set; }
  68. // windows 11
  69. public bool TaskbarToLeft { get; set; }
  70. public bool DisableSnapAssist { get; set; }
  71. public bool DisableWidgets { get; set; }
  72. public bool DisableChat { get; set; }
  73. public bool TaskbarSmaller { get; set; }
  74. public bool ClassicRibbon { get; set; }
  75. public bool ClassicMenu { get; set; }
  76. public bool DisableTPMCheck { get; set; }
  77. public bool CompactMode { get; set; }
  78. public bool DisableStickers { get; set; }
  79. public bool DisableVBS { get; set; }
  80. }
  81. internal static class Options
  82. {
  83. const int CONTRAST_THRESHOLD = 149;
  84. internal static Color ForegroundColor = Color.FromArgb(153, 102, 204);
  85. internal static Color ForegroundAccentColor = Color.FromArgb(134, 89, 179);
  86. internal static Color BackgroundColor = Color.FromArgb(10, 10, 10);
  87. internal static Color BackAccentColor = Color.FromArgb(40, 40, 40);
  88. internal static Color TextColor;
  89. readonly static string _themeFlag = "themeable";
  90. internal readonly static string SettingsFile = Required.CoreFolder + "\\Optimizer.json";
  91. internal static SettingsJson CurrentOptions = new SettingsJson();
  92. internal static dynamic TranslationList;
  93. internal static Color GetContrastColor(Color c)
  94. {
  95. double brightness = c.R * 0.299 + c.G * 0.587 + c.B * 0.114;
  96. return brightness > CONTRAST_THRESHOLD ? Color.Black : Color.White;
  97. }
  98. internal static void ApplyTheme(Form f)
  99. {
  100. SetTheme(f, CurrentOptions.Theme, ColorHelper.ChangeColorBrightness(CurrentOptions.Theme, 0.7));
  101. }
  102. private static void SetTheme(Form f, Color c1, Color c2)
  103. {
  104. dynamic c;
  105. ForegroundColor = c1;
  106. ForegroundAccentColor = c2;
  107. TextColor = GetContrastColor(CurrentOptions.Theme);
  108. Utilities.GetSelfAndChildrenRecursive(f).ToList().ForEach(x =>
  109. {
  110. c = x;
  111. if (x is Button)
  112. {
  113. c.ForeColor = TextColor;
  114. c.BackColor = c1;
  115. c.FlatAppearance.BorderColor = c1;
  116. c.FlatAppearance.MouseDownBackColor = c2;
  117. c.FlatAppearance.MouseOverBackColor = c2;
  118. c.FlatAppearance.BorderSize = 0;
  119. }
  120. if (x is LinkLabel)
  121. {
  122. if ((string)c.Tag == _themeFlag)
  123. {
  124. c.LinkColor = c1;
  125. c.VisitedLinkColor = c1;
  126. c.ActiveLinkColor = c2;
  127. }
  128. }
  129. if (x is CheckBox || x is RadioButton || x is Label)
  130. {
  131. if ((string)c.Tag == _themeFlag)
  132. {
  133. c.ForeColor = c1;
  134. }
  135. }
  136. c.Invalidate();
  137. });
  138. }
  139. internal static void LegacyCheck()
  140. {
  141. if (File.Exists(SettingsFile))
  142. {
  143. if (File.ReadAllText(SettingsFile).Contains("FirstRun"))
  144. {
  145. File.Delete(SettingsFile);
  146. }
  147. }
  148. }
  149. internal static void SaveSettings()
  150. {
  151. if (File.Exists(SettingsFile))
  152. {
  153. string jsonFile = File.ReadAllText(SettingsFile);
  154. string jsonMemory = JsonConvert.SerializeObject(CurrentOptions);
  155. // check to see if no changes have been made
  156. if (JToken.DeepEquals(JObject.Parse(jsonFile), JObject.Parse(jsonMemory))) return;
  157. File.Delete(SettingsFile);
  158. using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
  159. using (StreamWriter sw = new StreamWriter(fs))
  160. using (JsonWriter jw = new JsonTextWriter(sw))
  161. {
  162. jw.Formatting = Formatting.Indented;
  163. JsonSerializer serializer = new JsonSerializer();
  164. serializer.Serialize(jw, CurrentOptions);
  165. }
  166. }
  167. }
  168. internal static void LoadSettings()
  169. {
  170. if (!File.Exists(SettingsFile) || File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  171. {
  172. // settings migration for new color picker
  173. if (File.Exists(SettingsFile) && File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  174. {
  175. SettingsJson tmpJson = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  176. tmpJson.Theme = Color.FromArgb(153, 102, 204);
  177. CurrentOptions = tmpJson;
  178. }
  179. else
  180. {
  181. // DEFAULT OPTIONS
  182. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  183. CurrentOptions.AppsFolder = Path.Combine(Application.StartupPath, "Optimizer Downloads");
  184. Directory.CreateDirectory(Options.CurrentOptions.AppsFolder);
  185. CurrentOptions.EnableTray = false;
  186. CurrentOptions.AutoStart = false;
  187. CurrentOptions.LanguageCode = LanguageCode.EN;
  188. CurrentOptions.EnablePerformanceTweaks = false;
  189. CurrentOptions.DisableNetworkThrottling = false;
  190. CurrentOptions.DisableWindowsDefender = false;
  191. CurrentOptions.DisableSystemRestore = false;
  192. CurrentOptions.DisablePrintService = false;
  193. CurrentOptions.DisableMediaPlayerSharing = false;
  194. CurrentOptions.DisableErrorReporting = false;
  195. CurrentOptions.DisableHomeGroup = false;
  196. CurrentOptions.DisableSuperfetch = false;
  197. CurrentOptions.DisableTelemetryTasks = false;
  198. CurrentOptions.DisableOffice2016Telemetry = false;
  199. CurrentOptions.DisableCompatibilityAssistant = false;
  200. CurrentOptions.DisableFaxService = false;
  201. CurrentOptions.DisableSmartScreen = false;
  202. CurrentOptions.DisableStickyKeys = false;
  203. CurrentOptions.EnableGamingMode = false;
  204. CurrentOptions.EnableLegacyVolumeSlider = false;
  205. CurrentOptions.DisableQuickAccessHistory = false;
  206. CurrentOptions.DisableStartMenuAds = false;
  207. CurrentOptions.UninstallOneDrive = false;
  208. CurrentOptions.DisableMyPeople = false;
  209. CurrentOptions.DisableAutomaticUpdates = false;
  210. CurrentOptions.ExcludeDrivers = false;
  211. CurrentOptions.DisableTelemetryServices = false;
  212. CurrentOptions.DisablePrivacyOptions = false;
  213. CurrentOptions.DisableCortana = false;
  214. CurrentOptions.DisableSensorServices = false;
  215. CurrentOptions.DisableWindowsInk = false;
  216. CurrentOptions.DisableSpellingTyping = false;
  217. CurrentOptions.DisableXboxLive = false;
  218. CurrentOptions.DisableGameBar = false;
  219. CurrentOptions.DisableInsiderService = false;
  220. CurrentOptions.DisableStoreUpdates = false;
  221. CurrentOptions.DisableCloudClipboard = false;
  222. CurrentOptions.EnableLongPaths = false;
  223. CurrentOptions.RemoveCastToDevice = false;
  224. CurrentOptions.DisableHibernation = false;
  225. CurrentOptions.DisableSMB1 = false;
  226. CurrentOptions.DisableSMB2 = false;
  227. CurrentOptions.DisableNTFSTimeStamp = false;
  228. CurrentOptions.DisableSearch = false;
  229. CurrentOptions.DisableVisualStudioTelemetry = false;
  230. CurrentOptions.DisableFirefoxTemeletry = false;
  231. CurrentOptions.DisableChromeTelemetry = false;
  232. CurrentOptions.DisableNVIDIATelemetry = false;
  233. CurrentOptions.DisableOneDrive = false;
  234. CurrentOptions.TaskbarToLeft = false;
  235. CurrentOptions.DisableSnapAssist = false;
  236. CurrentOptions.DisableWidgets = false;
  237. CurrentOptions.DisableChat = false;
  238. CurrentOptions.TaskbarSmaller = false;
  239. CurrentOptions.ClassicRibbon = false;
  240. CurrentOptions.ClassicMenu = false;
  241. CurrentOptions.DisableTPMCheck = false;
  242. CurrentOptions.CompactMode = false;
  243. CurrentOptions.DisableStickers = false;
  244. CurrentOptions.DisableVBS = false;
  245. using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
  246. using (StreamWriter sw = new StreamWriter(fs))
  247. using (JsonWriter jw = new JsonTextWriter(sw))
  248. {
  249. jw.Formatting = Formatting.Indented;
  250. JsonSerializer serializer = new JsonSerializer();
  251. serializer.Serialize(jw, CurrentOptions);
  252. }
  253. }
  254. }
  255. else
  256. {
  257. CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  258. }
  259. if (CurrentOptions.Theme == Color.Empty || CurrentOptions.Theme == Color.FromArgb(0, 0, 0))
  260. {
  261. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  262. }
  263. LoadTranslation();
  264. }
  265. internal static void LoadTranslation()
  266. {
  267. // load proper translation list
  268. try
  269. {
  270. if (CurrentOptions.LanguageCode == LanguageCode.EN) TranslationList = JObject.Parse(Properties.Resources.EN);
  271. if (CurrentOptions.LanguageCode == LanguageCode.RU) TranslationList = JObject.Parse(Properties.Resources.RU);
  272. if (CurrentOptions.LanguageCode == LanguageCode.EL) TranslationList = JObject.Parse(Properties.Resources.EL);
  273. if (CurrentOptions.LanguageCode == LanguageCode.TR) TranslationList = JObject.Parse(Properties.Resources.TR);
  274. if (CurrentOptions.LanguageCode == LanguageCode.DE) TranslationList = JObject.Parse(Properties.Resources.DE);
  275. if (CurrentOptions.LanguageCode == LanguageCode.ES) TranslationList = JObject.Parse(Properties.Resources.ES);
  276. if (CurrentOptions.LanguageCode == LanguageCode.PT) TranslationList = JObject.Parse(Properties.Resources.PT);
  277. if (CurrentOptions.LanguageCode == LanguageCode.FR) TranslationList = JObject.Parse(Properties.Resources.FR);
  278. if (CurrentOptions.LanguageCode == LanguageCode.IT) TranslationList = JObject.Parse(Properties.Resources.IT);
  279. if (CurrentOptions.LanguageCode == LanguageCode.CN) TranslationList = JObject.Parse(Properties.Resources.CN);
  280. if (CurrentOptions.LanguageCode == LanguageCode.CZ) TranslationList = JObject.Parse(Properties.Resources.CZ);
  281. if (CurrentOptions.LanguageCode == LanguageCode.TW) TranslationList = JObject.Parse(Properties.Resources.TW);
  282. if (CurrentOptions.LanguageCode == LanguageCode.KO) TranslationList = JObject.Parse(Properties.Resources.KO);
  283. if (CurrentOptions.LanguageCode == LanguageCode.PL) TranslationList = JObject.Parse(Properties.Resources.PL);
  284. if (CurrentOptions.LanguageCode == LanguageCode.AR) TranslationList = JObject.Parse(Properties.Resources.AR);
  285. if (CurrentOptions.LanguageCode == LanguageCode.KU) TranslationList = JObject.Parse(Properties.Resources.KU);
  286. if (CurrentOptions.LanguageCode == LanguageCode.HU) TranslationList = JObject.Parse(Properties.Resources.HU);
  287. if (CurrentOptions.LanguageCode == LanguageCode.RO) TranslationList = JObject.Parse(Properties.Resources.RO);
  288. if (CurrentOptions.LanguageCode == LanguageCode.NL) TranslationList = JObject.Parse(Properties.Resources.NL);
  289. if (CurrentOptions.LanguageCode == LanguageCode.UA) TranslationList = JObject.Parse(Properties.Resources.UA);
  290. }
  291. catch (Exception ex)
  292. {
  293. ErrorLogger.LogError("Options.LoadTranslation", ex.Message, ex.StackTrace);
  294. TranslationList = JObject.Parse(Properties.Resources.EN);
  295. }
  296. }
  297. }
  298. }