Skip to content

Commit

Permalink
Support changing theme via CLI and refactor arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Aug 24, 2024
1 parent f533006 commit 2f0d9b2
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 51 deletions.
51 changes: 4 additions & 47 deletions src/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
116 changes: 114 additions & 2 deletions src/IpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinDynamicDesktop
{
Expand All @@ -30,7 +31,7 @@ public void Dispose()
namedPipeServer?.Dispose();
}

public void ListenForArgs(Action<string[]> listener)
public void ListenForArgs(ApplicationContext app)
{
Task.Factory.StartNew(() =>
{
Expand All @@ -44,7 +45,8 @@ public void ListenForArgs(Action<string[]> listener)
namedPipeServer.WaitForConnection();
}

listener(reader.ReadToEnd().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries));
ProcessArgs(app, reader.ReadToEnd().Split(Environment.NewLine,
StringSplitOptions.RemoveEmptyEntries));
namedPipeServer.Disconnect();
}
}, TaskCreationOptions.LongRunning);
Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/SolarScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion src/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ThemeManager

public static bool downloadMode = false;
public static bool importMode = false;
public static List<string> importPaths;
public static List<string> importPaths = new List<string>();
public static List<ThemeConfig> importedThemes = new List<ThemeConfig>();

public static string[] defaultThemes;
Expand Down

0 comments on commit 2f0d9b2

Please sign in to comment.