diff --git a/src/AppContext.cs b/src/AppContext.cs index 5254c09..9e65c75 100644 --- a/src/AppContext.cs +++ b/src/AppContext.cs @@ -3,7 +3,6 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. using System; -using System.Linq; using System.Reflection; using System.Windows.Forms; @@ -23,8 +22,8 @@ public AppContext(string[] args) : base(new HiddenForm()) Localization.Initialize(); LoggingHandler.RotateDebugLog(); - ThemeManager.importPaths = args.Where(System.IO.File.Exists).ToList(); - HandleMultiInstance(args); + CheckSingleInstance(args); + ipcManager.ProcessArgs(args); InitializeTrayIcon(); LocationManager.Initialize(); @@ -36,13 +35,13 @@ public AppContext(string[] args) : base(new HiddenForm()) UpdateChecker.Initialize(); } - private void HandleMultiInstance(string[] args) + private void CheckSingleInstance(string[] args) { ipcManager = new IpcManager(); if (ipcManager.isFirstInstance) { - ipcManager.ListenForArgs(OnArgumentsReceived); + ipcManager.ListenForArgs(this); } else { @@ -92,48 +91,6 @@ public static void ToggleTrayIcon() notifyIcon.Visible = !isHidden; } - private void OnArgumentsReceived(string[] args) - { - if (JsonConfig.settings.hideTrayIcon) - { - MainForm.BeginInvoke(ToggleTrayIcon); - } - - foreach (string arg in args) - { - if (arg.StartsWith('/')) - { - switch (arg.ToLower()) - { - case "/refresh": - scheduler.RunAndUpdateLocation(true); - break; - case "/theme:auto": - MainForm.BeginInvoke(SolarScheduler.SetAppearanceMode, AppearanceMode.Automatic); - break; - case "/theme:light": - MainForm.BeginInvoke(SolarScheduler.SetAppearanceMode, AppearanceMode.Light); - break; - case "/theme:dark": - MainForm.BeginInvoke(SolarScheduler.SetAppearanceMode, AppearanceMode.Dark); - break; - default: - Console.WriteLine("Unrecognized command line option: " + arg); - break; - } - } - else - { - ThemeManager.importPaths.Add(arg); - } - } - - if (ThemeManager.importPaths.Count > 0 && !ThemeManager.importMode) - { - MainForm.BeginInvoke(ThemeManager.SelectTheme); - } - } - private void OnNotifyIconMouseUp(object sender, MouseEventArgs e) { // Show context menu when taskbar icon is left clicked diff --git a/src/IpcManager.cs b/src/IpcManager.cs index 3b1a9b5..1faffaf 100644 --- a/src/IpcManager.cs +++ b/src/IpcManager.cs @@ -7,6 +7,7 @@ using System.IO.Pipes; using System.Threading; using System.Threading.Tasks; +using System.Windows.Forms; namespace WinDynamicDesktop { @@ -30,7 +31,7 @@ public void Dispose() namedPipeServer?.Dispose(); } - public void ListenForArgs(Action listener) + public void ListenForArgs(ApplicationContext app) { Task.Factory.StartNew(() => { @@ -44,7 +45,8 @@ public void ListenForArgs(Action listener) namedPipeServer.WaitForConnection(); } - listener(reader.ReadToEnd().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)); + ProcessArgs(app, reader.ReadToEnd().Split(Environment.NewLine, + StringSplitOptions.RemoveEmptyEntries)); namedPipeServer.Disconnect(); } }, TaskCreationOptions.LongRunning); @@ -59,5 +61,115 @@ public void SendArgsToFirstInstance(string[] args) writer.Write(string.Join(Environment.NewLine, args)); writer.Flush(); } + + public void ProcessArgs(string[] initialArgs) + { + foreach (string arg in initialArgs) + { + if (arg.ToLower().StartsWith("/theme") && arg.IndexOf('=') != -1) + { + ProcessThemeArg(arg.ToLower()); + } + else if (arg.StartsWith('/')) + { + switch (arg.ToLower()) + { + case "/refresh": + break; + case "/theme:auto": + case "/theme:light": + case "/theme:dark": + AppearanceMode mode = (AppearanceMode)Enum.Parse(typeof(AppearanceMode), + arg.Substring(7), true); + JsonConfig.settings.appearanceMode = (int)mode; + break; + default: + Console.WriteLine("Unrecognized command line option: " + arg); + break; + } + } + else if (File.Exists(arg)) + { + ThemeManager.importPaths.Add(arg); + } + } + } + + private void ProcessArgs(ApplicationContext app, string[] args) + { + if (JsonConfig.settings.hideTrayIcon) + { + app.MainForm.BeginInvoke(AppContext.ToggleTrayIcon); + } + + foreach (string arg in args) + { + if (arg.ToLower().StartsWith("/theme") && arg.IndexOf('=') != -1) + { + string themeId = ProcessThemeArg(arg.ToLower()); + if (themeId != null) + { + ThemeShuffler.AddThemeToHistory(themeId); + AppContext.scheduler.Run(true); + } + } + else if (arg.StartsWith('/')) + { + switch (arg.ToLower()) + { + case "/refresh": + AppContext.scheduler.RunAndUpdateLocation(true); + break; + case "/theme:auto": + case "/theme:light": + case "/theme:dark": + AppearanceMode mode = (AppearanceMode)Enum.Parse(typeof(AppearanceMode), + arg.Substring(7), true); + app.MainForm.BeginInvoke(SolarScheduler.SetAppearanceMode, mode); + break; + default: + Console.WriteLine("Unrecognized command line option: " + arg); + break; + } + } + else if (File.Exists(arg)) + { + ThemeManager.importPaths.Add(arg); + } + } + + if (ThemeManager.importPaths.Count > 0 && !ThemeManager.importMode) + { + app.MainForm.BeginInvoke(ThemeManager.SelectTheme); + } + } + + private string ProcessThemeArg(string arg) + { + string themeId = arg.Substring(arg.IndexOf('=') + 1); + if (ThemeManager.themeSettings.Find((theme) => theme.themeId == themeId) == null) + { + Console.WriteLine("Failed to set theme - unknown theme ID: " + themeId); + return null; + } + + if (arg.StartsWith("/theme=")) + { + JsonConfig.settings.activeThemes[0] = themeId; + } + else if (arg.StartsWith("/theme:L=")) + { + JsonConfig.settings.lockScreenTheme = themeId; + JsonConfig.settings.lockScreenDisplayIndex = -1; + } + else + { + int displayNumber = int.Parse(arg[7].ToString()); + JsonConfig.settings.activeThemes[displayNumber] = themeId; + JsonConfig.settings.activeThemes[0] = null; + } + + return themeId; + } } } diff --git a/src/SolarScheduler.cs b/src/SolarScheduler.cs index 7ef44b0..51a9a82 100644 --- a/src/SolarScheduler.cs +++ b/src/SolarScheduler.cs @@ -8,7 +8,7 @@ namespace WinDynamicDesktop { - public enum AppearanceMode { Automatic, Light, Dark } + public enum AppearanceMode { Automatic, Auto = Automatic, Light, Dark } public enum DaySegment { Sunrise, Day, Sunset, Night, AlwaysDay, AlwaysNight } diff --git a/src/ThemeManager.cs b/src/ThemeManager.cs index 5fbe29d..965800d 100644 --- a/src/ThemeManager.cs +++ b/src/ThemeManager.cs @@ -19,7 +19,7 @@ class ThemeManager public static bool downloadMode = false; public static bool importMode = false; - public static List importPaths; + public static List importPaths = new List(); public static List importedThemes = new List(); public static string[] defaultThemes;