Options.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 class SettingsJson
  12. {
  13. public Color Theme { get; set; }
  14. public string AppsFolder { get; set; }
  15. public bool EnableTray { get; set; }
  16. public bool ShowHelp { get; set; }
  17. public bool AutoStart { get; set; }
  18. public LanguageCode LanguageCode { get; set; }
  19. // universal
  20. public bool EnablePerformanceTweaks { get; set; }
  21. public bool DisableNetworkThrottling { get; set; }
  22. public bool DisableWindowsDefender { get; set; }
  23. public bool DisableSystemRestore { get; set; }
  24. public bool DisablePrintService { get; set; }
  25. public bool DisableMediaPlayerSharing { get; set; }
  26. public bool DisableErrorReporting { get; set; }
  27. public bool DisableHomeGroup { get; set; }
  28. public bool DisableSuperfetch { get; set; }
  29. public bool DisableTelemetryTasks { get; set; }
  30. public bool DisableCompatibilityAssistant { get; set; }
  31. public bool DisableFaxService { get; set; }
  32. public bool DisableSmartScreen { get; set; }
  33. public bool DisableCloudClipboard { get; set; }
  34. public bool DisableStickyKeys { get; set; }
  35. public bool DisableHibernation { get; set; }
  36. public bool DisableSMB1 { get; set; }
  37. public bool DisableSMB2 { get; set; }
  38. public bool DisableNTFSTimeStamp { 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 DisableFeatureUpdates { 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. }
  80. internal static class Options
  81. {
  82. const int CONTRAST_THRESHOLD = 149;
  83. internal static Color ForegroundColor = Color.FromArgb(153, 102, 204);
  84. internal static Color ForegroundAccentColor = Color.FromArgb(134, 89, 179);
  85. internal static Color BackgroundColor = Color.FromArgb(10, 10, 10);
  86. internal static Color BackAccentColor = Color.FromArgb(40, 40, 40);
  87. internal static Color TextColor;
  88. readonly static string _themeFlag = "themeable";
  89. internal readonly static string SettingsFile = Required.CoreFolder + "\\Optimizer.json";
  90. internal static SettingsJson CurrentOptions = new SettingsJson();
  91. internal static dynamic TranslationList;
  92. internal static Color GetContrastColor(Color c)
  93. {
  94. double brightness = c.R * 0.299 + c.G * 0.587 + c.B * 0.114;
  95. return brightness > CONTRAST_THRESHOLD ? Color.Black : Color.White;
  96. }
  97. internal static void ApplyTheme(Form f)
  98. {
  99. SetTheme(f, CurrentOptions.Theme, ColorHelper.ChangeColorBrightness(CurrentOptions.Theme, 0.7));
  100. }
  101. private static void SetTheme(Form f, Color c1, Color c2)
  102. {
  103. dynamic c;
  104. ForegroundColor = c1;
  105. ForegroundAccentColor = c2;
  106. TextColor = GetContrastColor(CurrentOptions.Theme);
  107. Utilities.GetSelfAndChildrenRecursive(f).ToList().ForEach(x =>
  108. {
  109. c = x;
  110. if (x is Button)
  111. {
  112. c.ForeColor = TextColor;
  113. c.BackColor = c1;
  114. c.FlatAppearance.BorderColor = c1;
  115. c.FlatAppearance.MouseDownBackColor = c2;
  116. c.FlatAppearance.MouseOverBackColor = c2;
  117. c.FlatAppearance.BorderSize = 0;
  118. }
  119. if (x is LinkLabel)
  120. {
  121. if ((string)c.Tag == _themeFlag)
  122. {
  123. c.LinkColor = c1;
  124. c.VisitedLinkColor = c1;
  125. c.ActiveLinkColor = c2;
  126. }
  127. }
  128. if (x is CheckBox || x is RadioButton || x is Label)
  129. {
  130. if ((string)c.Tag == _themeFlag)
  131. {
  132. c.ForeColor = c1;
  133. }
  134. }
  135. c.Invalidate();
  136. });
  137. }
  138. internal static void LegacyCheck()
  139. {
  140. if (File.Exists(SettingsFile))
  141. {
  142. if (File.ReadAllText(SettingsFile).Contains("FirstRun"))
  143. {
  144. File.Delete(SettingsFile);
  145. }
  146. }
  147. }
  148. internal static void SaveSettings()
  149. {
  150. if (File.Exists(SettingsFile))
  151. {
  152. string jsonFile = File.ReadAllText(SettingsFile);
  153. string jsonMemory = JsonConvert.SerializeObject(CurrentOptions);
  154. // check to see if no changes have been made
  155. if (JToken.DeepEquals(JObject.Parse(jsonFile), JObject.Parse(jsonMemory))) return;
  156. File.Delete(SettingsFile);
  157. using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
  158. using (StreamWriter sw = new StreamWriter(fs))
  159. using (JsonWriter jw = new JsonTextWriter(sw))
  160. {
  161. jw.Formatting = Formatting.Indented;
  162. JsonSerializer serializer = new JsonSerializer();
  163. serializer.Serialize(jw, CurrentOptions);
  164. }
  165. }
  166. }
  167. internal static void LoadSettings()
  168. {
  169. if (!File.Exists(SettingsFile) || File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  170. {
  171. // settings migration for new color picker
  172. if (File.Exists(SettingsFile) && File.ReadAllText(SettingsFile).Contains("\"Color\":"))
  173. {
  174. SettingsJson tmpJson = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  175. tmpJson.Theme = Color.FromArgb(153, 102, 204);
  176. CurrentOptions = tmpJson;
  177. }
  178. else
  179. {
  180. // DEFAULT OPTIONS
  181. CurrentOptions.Theme = Color.FromArgb(153, 102, 204);
  182. CurrentOptions.AppsFolder = Path.Combine(Application.StartupPath, "Optimizer Downloads");
  183. Directory.CreateDirectory(Options.CurrentOptions.AppsFolder);
  184. CurrentOptions.EnableTray = false;
  185. CurrentOptions.ShowHelp = true;
  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.DisableFeatureUpdates = 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.DisableVisualStudioTelemetry = false;
  229. CurrentOptions.DisableFirefoxTemeletry = false;
  230. CurrentOptions.DisableChromeTelemetry = false;
  231. CurrentOptions.DisableNVIDIATelemetry = false;
  232. CurrentOptions.DisableOneDrive = false;
  233. CurrentOptions.TaskbarToLeft = false;
  234. CurrentOptions.DisableSnapAssist = false;
  235. CurrentOptions.DisableWidgets = false;
  236. CurrentOptions.DisableChat = false;
  237. CurrentOptions.TaskbarSmaller = false;
  238. CurrentOptions.ClassicRibbon = false;
  239. CurrentOptions.ClassicMenu = false;
  240. CurrentOptions.DisableTPMCheck = false;
  241. CurrentOptions.CompactMode = false;
  242. CurrentOptions.DisableStickers = false;
  243. using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
  244. using (StreamWriter sw = new StreamWriter(fs))
  245. using (JsonWriter jw = new JsonTextWriter(sw))
  246. {
  247. jw.Formatting = Formatting.Indented;
  248. JsonSerializer serializer = new JsonSerializer();
  249. serializer.Serialize(jw, CurrentOptions);
  250. }
  251. }
  252. }
  253. else
  254. {
  255. CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  256. }
  257. LoadTranslation();
  258. }
  259. internal static void LoadTranslation()
  260. {
  261. // load proper translation list
  262. try
  263. {
  264. if (CurrentOptions.LanguageCode == LanguageCode.EN) TranslationList = JObject.Parse(Properties.Resources.EN);
  265. if (CurrentOptions.LanguageCode == LanguageCode.RU) TranslationList = JObject.Parse(Properties.Resources.RU);
  266. if (CurrentOptions.LanguageCode == LanguageCode.EL) TranslationList = JObject.Parse(Properties.Resources.EL);
  267. if (CurrentOptions.LanguageCode == LanguageCode.TR) TranslationList = JObject.Parse(Properties.Resources.TR);
  268. if (CurrentOptions.LanguageCode == LanguageCode.DE) TranslationList = JObject.Parse(Properties.Resources.DE);
  269. if (CurrentOptions.LanguageCode == LanguageCode.ES) TranslationList = JObject.Parse(Properties.Resources.ES);
  270. if (CurrentOptions.LanguageCode == LanguageCode.PT) TranslationList = JObject.Parse(Properties.Resources.PT);
  271. if (CurrentOptions.LanguageCode == LanguageCode.FR) TranslationList = JObject.Parse(Properties.Resources.FR);
  272. if (CurrentOptions.LanguageCode == LanguageCode.IT) TranslationList = JObject.Parse(Properties.Resources.IT);
  273. if (CurrentOptions.LanguageCode == LanguageCode.CN) TranslationList = JObject.Parse(Properties.Resources.CN);
  274. if (CurrentOptions.LanguageCode == LanguageCode.CZ) TranslationList = JObject.Parse(Properties.Resources.CZ);
  275. if (CurrentOptions.LanguageCode == LanguageCode.TW) TranslationList = JObject.Parse(Properties.Resources.TW);
  276. if (CurrentOptions.LanguageCode == LanguageCode.KO) TranslationList = JObject.Parse(Properties.Resources.KO);
  277. if (CurrentOptions.LanguageCode == LanguageCode.PL) TranslationList = JObject.Parse(Properties.Resources.PL);
  278. if (CurrentOptions.LanguageCode == LanguageCode.AR) TranslationList = JObject.Parse(Properties.Resources.AR);
  279. if (CurrentOptions.LanguageCode == LanguageCode.KU) TranslationList = JObject.Parse(Properties.Resources.KU);
  280. if (CurrentOptions.LanguageCode == LanguageCode.HU) TranslationList = JObject.Parse(Properties.Resources.HU);
  281. if (CurrentOptions.LanguageCode == LanguageCode.RO) TranslationList = JObject.Parse(Properties.Resources.RO);
  282. if (CurrentOptions.LanguageCode == LanguageCode.NL) TranslationList = JObject.Parse(Properties.Resources.NL);
  283. ;
  284. }
  285. catch (Exception ex)
  286. {
  287. ErrorLogger.LogError("Options.LoadTranslation", ex.Message, ex.StackTrace);
  288. TranslationList = JObject.Parse(Properties.Resources.EN);
  289. }
  290. }
  291. }
  292. }