Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Reflection;
- using Newtonsoft.Json;
- using static MonitorHelper;
- public static class SaveLoad {
- [Serializable]
- public class SavedStuff {
- public TVSettings MonitorSettings_Enabled;
- public TVSettings MonitorSettings_Disabled;
- }
- public static void SaveEverything() {
- var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\SavedData.txt";
- if (File.Exists(path)) { File.Delete(path); }
- using (FileStream fs = File.Open(path, FileMode.CreateNew))
- using (StreamWriter sw = new StreamWriter(fs))
- using (JsonWriter jw = new JsonTextWriter(sw)) {
- jw.Formatting = Formatting.Indented;
- SavedStuff stuff = new SavedStuff();
- stuff.MonitorSettings_Enabled = Monitors.MonitorHelper.Enabled;
- stuff.MonitorSettings_Disabled = Monitors.MonitorHelper.Disabled;
- JsonSerializer serializer = new JsonSerializer();
- serializer.Serialize(jw, stuff);
- }
- }
- public static void LoadEverything() {
- var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\SavedData.txt";
- if (!File.Exists(path)) { return; }
- var x = File.ReadAllText(path);
- SavedStuff stuff = JsonConvert.DeserializeObject<SavedStuff>(x);
- Monitors.MonitorHelper.Enabled = stuff.MonitorSettings_Enabled;
- Monitors.MonitorHelper.Disabled = stuff.MonitorSettings_Disabled;
- foreach (var item in stuff.Tasks) { item.UpdateChildren(); }
- MainWindow.RefreshAll();
- }
- }
- public static class ContextMenu {
- static System.Windows.Forms.NotifyIcon icon;
- public static void InitializeContextMenu() {
- icon = new System.Windows.Forms.NotifyIcon();
- icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().ManifestModule.Name);
- icon.Visible = true;
- icon.ContextMenu = new System.Windows.Forms.ContextMenu();
- icon.ContextMenu.MenuItems.Add("Save TV State", SaveScreenSettings);
- icon.ContextMenu.MenuItems.Add("Enable TV", ChangeScreenSettings);
- icon.ContextMenu.MenuItems.Add("Exit", Exit);
- }
- static void SaveScreenSettings(object Sender, EventArgs e) => Monitors.MonitorHelper.ChangeTVState(false, true);
- static void ChangeScreenSettings(object Sender, EventArgs e) => Monitors.MonitorHelper.ChangeTVState(true);
- static void Exit(object Sender, EventArgs e) {
- icon.Visible = false;
- SaveLoad.SaveEverything();
- System.Windows.Application.Current.Shutdown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement