Advertisement
Guest User

Context Menu & Save/Load function for the MonitorHelper

a guest
Jul 18th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Newtonsoft.Json;
  5. using static MonitorHelper;
  6.  
  7. public static class SaveLoad {
  8.         [Serializable]
  9.         public class SavedStuff {
  10.             public TVSettings MonitorSettings_Enabled;
  11.             public TVSettings MonitorSettings_Disabled;
  12.         }
  13.  
  14.         public static void SaveEverything() {
  15.             var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\SavedData.txt";
  16.             if (File.Exists(path)) { File.Delete(path); }
  17.             using (FileStream fs = File.Open(path, FileMode.CreateNew))
  18.             using (StreamWriter sw = new StreamWriter(fs))
  19.             using (JsonWriter jw = new JsonTextWriter(sw)) {
  20.                 jw.Formatting = Formatting.Indented;
  21.  
  22.                 SavedStuff stuff = new SavedStuff();
  23.                 stuff.MonitorSettings_Enabled = Monitors.MonitorHelper.Enabled;
  24.                 stuff.MonitorSettings_Disabled = Monitors.MonitorHelper.Disabled;
  25.  
  26.                 JsonSerializer serializer = new JsonSerializer();
  27.                 serializer.Serialize(jw, stuff);
  28.             }
  29.         }
  30.  
  31.         public static void LoadEverything() {
  32.             var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\SavedData.txt";
  33.             if (!File.Exists(path)) { return; }
  34.             var x = File.ReadAllText(path);
  35.  
  36.             SavedStuff stuff = JsonConvert.DeserializeObject<SavedStuff>(x);
  37.             Monitors.MonitorHelper.Enabled = stuff.MonitorSettings_Enabled;
  38.             Monitors.MonitorHelper.Disabled = stuff.MonitorSettings_Disabled;
  39.  
  40.             foreach (var item in stuff.Tasks) { item.UpdateChildren(); }
  41.             MainWindow.RefreshAll();
  42.         }
  43. }
  44.  
  45. public static class ContextMenu {
  46.  
  47.     static System.Windows.Forms.NotifyIcon icon;
  48.  
  49.     public static void InitializeContextMenu() {
  50.         icon = new System.Windows.Forms.NotifyIcon();
  51.         icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().ManifestModule.Name);
  52.         icon.Visible = true;
  53.  
  54.         icon.ContextMenu = new System.Windows.Forms.ContextMenu();
  55.  
  56.         icon.ContextMenu.MenuItems.Add("Save TV State", SaveScreenSettings);
  57.         icon.ContextMenu.MenuItems.Add("Enable TV", ChangeScreenSettings);
  58.  
  59.         icon.ContextMenu.MenuItems.Add("Exit", Exit);
  60.  
  61.     }
  62.  
  63.     static void SaveScreenSettings(object Sender, EventArgs e) => Monitors.MonitorHelper.ChangeTVState(false, true);
  64.     static void ChangeScreenSettings(object Sender, EventArgs e) => Monitors.MonitorHelper.ChangeTVState(true);
  65.  
  66.     static void Exit(object Sender, EventArgs e) {
  67.         icon.Visible = false;
  68.         SaveLoad.SaveEverything();
  69.         System.Windows.Application.Current.Shutdown();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement