Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions CenterTaskbar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class TrayApplication : ApplicationContext
private const string ShellSecondaryTrayWnd = "Shell_SecondaryTrayWnd";

private static readonly string ExecutablePath = "\"" + Application.ExecutablePath + "\"";
private static readonly string SilentExecutablePath = "\"" + Application.ExecutablePath + "\"" + " -silent";
private static bool _disposed;
private CancellationTokenSource _loopCancellationTokenSource = new CancellationTokenSource();

Expand All @@ -71,6 +72,8 @@ public class TrayApplication : ApplicationContext

private readonly NotifyIcon _trayIcon;

private readonly bool _hidden = false;

// private Thread positionThread;
private readonly Dictionary<AutomationElement, Task> _positionThreads =
new Dictionary<AutomationElement, Task>();
Expand All @@ -80,8 +83,15 @@ public TrayApplication(IReadOnlyList<string> args)
if (args.Count > 0)
try
{
_activeFramerate = int.Parse(args[0]);
Debug.WriteLine("Active refresh rate: " + _activeFramerate);
if (args[0].Equals("-silent"))
{
_hidden = true;
}
else
{
_activeFramerate = int.Parse(args[0]);
Debug.WriteLine("Active refresh rate: " + _activeFramerate);
}
}
catch (FormatException e)
{
Expand All @@ -99,18 +109,26 @@ public TrayApplication(IReadOnlyList<string> args)
};

// Setup Tray Icon
_trayIcon = new NotifyIcon
if (!_hidden)
{
Icon = Resources.TrayIcon,
ContextMenu = new ContextMenu(new[]
if (IsSilentApplicationInStartup())
{
header,
new MenuItem("Scan for screens", Restart),
startup,
new MenuItem("E&xit", Exit)
}),
Visible = true
};
AddApplicationToStartup();
}
_trayIcon = new NotifyIcon
{
Icon = Resources.TrayIcon,
ContextMenu = new ContextMenu(new[]
{
header,
new MenuItem("Scan for screens", Restart),
startup,
new MenuItem("Hide", Hide),
new MenuItem("E&xit", Exit),
}),
Visible = true
};
}

Start();
SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanged;
Expand Down Expand Up @@ -141,6 +159,15 @@ public bool IsApplicationInStartup()
return value is string startValue && startValue.StartsWith(ExecutablePath);
}
}

public bool IsSilentApplicationInStartup()
{
using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true))
{
var value = key?.GetValue(AppName);
return value is string startValue && startValue.StartsWith(SilentExecutablePath);
}
}

public void AddApplicationToStartup()
{
Expand All @@ -149,6 +176,14 @@ public void AddApplicationToStartup()
key?.SetValue(AppName, ExecutablePath);
}
}

public void AddSilentApplicationToStartup()
{
using (var key = Registry.CurrentUser.OpenSubKey(RunRegkey, true))
{
key?.SetValue(AppName, SilentExecutablePath);
}
}

public void RemoveApplicationFromStartup()
{
Expand Down Expand Up @@ -210,6 +245,18 @@ private async void Restart(object sender, EventArgs e)

}

private void Hide(object sender, EventArgs eventArgs)
{
if (IsApplicationInStartup())
{
AddSilentApplicationToStartup();
MessageBox.Show("The tray icon is now hidden. To show the icon again, " +
"kill CenterTaskbar using Task Manager and run CenterTaskbar once again.",
"CenterTaskbar", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
_trayIcon.Visible = false;
}

private void ResetAll()
{
CancelPositionThread();
Expand Down