Skip to content
Closed
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
88 changes: 85 additions & 3 deletions RunCat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ static void Main()

public class RunCatApplicationContext : ApplicationContext
{
private const int CPU_TIMER_DEFAULT_INTERVAL = 3000;
private const int ANIMATE_TIMER_DEFAULT_INTERVAL = 200;
private PerformanceCounter cpuUsage;
private ToolStripMenuItem runnerMenu;
private ToolStripMenuItem themeMenu;
Expand Down Expand Up @@ -117,13 +119,23 @@ public RunCatApplicationContext()
StartObserveCPU();
current = 1;
}

/// <summary>
/// Event that is triggered when the application is closed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnApplicationExit(object sender, EventArgs e)
{
UserSettings.Default.Runner = runner;
UserSettings.Default.Theme = manualTheme;
UserSettings.Default.Save();
}

/// <summary>
/// Checks if the startup-option is already enabled.
/// </summary>
/// <returns></returns>
private bool IsStartupEnabled()
{
string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
Expand All @@ -133,6 +145,10 @@ private bool IsStartupEnabled()
}
}

/// <summary>
/// Checks the globally selected theme.
/// </summary>
/// <returns></returns>
private string GetAppsUseTheme()
{
string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize";
Expand All @@ -149,6 +165,9 @@ private string GetAppsUseTheme()
}
}

/// <summary>
/// Sets the icon.
/// </summary>
private void SetIcons()
{
string prefix = 0 < manualTheme.Length ? manualTheme : systemTheme;
Expand All @@ -162,6 +181,11 @@ private void SetIcons()
icons = list.ToArray();
}

/// <summary>
/// Checks whether the individual elements of the menu are selected or not.
/// </summary>
/// <param name="sender"></param>
/// <param name="menu"></param>
private void UpdateCheckedState(ToolStripMenuItem sender, ToolStripMenuItem menu)
{
foreach (ToolStripMenuItem item in menu.DropDownItems)
Expand All @@ -171,6 +195,11 @@ private void UpdateCheckedState(ToolStripMenuItem sender, ToolStripMenuItem menu
sender.Checked = true;
}

/// <summary>
/// Sets the runner.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetRunner(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Expand All @@ -179,6 +208,11 @@ private void SetRunner(object sender, EventArgs e)
SetIcons();
}

/// <summary>
/// Sets theme icons.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetThemeIcons(object sender, EventArgs e)
{
UpdateCheckedState((ToolStripMenuItem)sender, themeMenu);
Expand All @@ -187,6 +221,9 @@ private void SetThemeIcons(object sender, EventArgs e)
SetIcons();
}

/// <summary>
/// Updates the theme icons, if necessary.
/// </summary>
private void UpdateThemeIcons()
{
if (0 < manualTheme.Length)
Expand All @@ -200,24 +237,45 @@ private void UpdateThemeIcons()
SetIcons();
}

/// <summary>
/// Sets the icons to light theme.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetLightIcons(object sender, EventArgs e)
{
UpdateCheckedState((ToolStripMenuItem)sender, themeMenu);
manualTheme = "light";
SetIcons();
}

/// <summary>
/// Sets the icons to dark theme.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetDarkIcons(object sender, EventArgs e)
{
UpdateCheckedState((ToolStripMenuItem)sender, themeMenu);
manualTheme = "dark";
SetIcons();
}

/// <summary>
/// Gets called, whenever a user preference has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.General) UpdateThemeIcons();
}

/// <summary>
/// Sets the startup option.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetStartup(object sender, EventArgs e)
{
startupMenu.Checked = !startupMenu.Checked;
Expand All @@ -236,6 +294,11 @@ private void SetStartup(object sender, EventArgs e)
}
}

/// <summary>
/// Exits the application.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Exit(object sender, EventArgs e)
{
cpuUsage.Close();
Expand All @@ -245,37 +308,56 @@ private void Exit(object sender, EventArgs e)
Application.Exit();
}

/// <summary>
/// Gets called, whenever the animation timer has elapsed and updates the icon.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AnimationTick(object sender, EventArgs e)
{
if (icons.Length <= current) current = 0;
notifyIcon.Icon = icons[current];
current = (current + 1) % icons.Length;
}

/// <summary>
/// Sets the animation properties.
/// </summary>
private void SetAnimation()
{
animateTimer.Interval = 200;
animateTimer.Interval = ANIMATE_TIMER_DEFAULT_INTERVAL;
animateTimer.Tick += new EventHandler(AnimationTick);
}

/// <summary>
/// Gets the cpu usage, writes it to the icon and updates the animation timer.
/// </summary>
private void CPUTick()
{
float s = cpuUsage.NextValue();
notifyIcon.Text = $"CPU: {s:f1}%";
s = 200.0f / (float)Math.Max(1.0f, Math.Min(20.0f, s / 5.0f));
s = ANIMATE_TIMER_DEFAULT_INTERVAL / (float)Math.Max(1.0f, Math.Min(20.0f, s / 5.0f));
animateTimer.Stop();
animateTimer.Interval = (int)s;
animateTimer.Start();
}

/// <summary>
/// Gets called, whenever the cpu timer has elapsed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ObserveCPUTick(object sender, EventArgs e)
{
CPUTick();
}

/// <summary>
/// Sets the cpu timer properties.
/// </summary>
private void StartObserveCPU()
{
cpuTimer.Interval = 3000;
cpuTimer.Interval = CPU_TIMER_DEFAULT_INTERVAL;
cpuTimer.Tick += new EventHandler(ObserveCPUTick);
cpuTimer.Start();
}
Expand Down