Skip to content
Merged
Show file tree
Hide file tree
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
47 changes: 46 additions & 1 deletion RunCat365/ContextMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ internal ContextMenuManager(
Action<FPSMaxLimit> setFPSMaxLimit,
Func<bool> getLaunchAtStartup,
Func<bool, bool> toggleLaunchAtStartup,
Func<SpeedSource> getSpeedSource,
Action<SpeedSource> setSpeedSource,
bool isGpuAvailable,
Action openRepository,
Action onExit
)
Expand Down Expand Up @@ -99,11 +102,37 @@ Action onExit
};
launchAtStartupMenu.Click += (sender, e) => HandleStartupMenuClick(sender, toggleLaunchAtStartup);

var speedSourceMenu = new CustomToolStripMenuItem("Speed based on");
var cpuSpeedItem = new CustomToolStripMenuItem("CPU")
{
Checked = getSpeedSource() == SpeedSource.CPU
};
cpuSpeedItem.Click += (sender, e) => HandleSpeedSourceClick(speedSourceMenu, sender, SpeedSource.CPU, setSpeedSource);
speedSourceMenu.DropDownItems.Add(cpuSpeedItem);

if (isGpuAvailable)
{
var gpuSpeedItem = new CustomToolStripMenuItem("GPU")
{
Checked = getSpeedSource() == SpeedSource.GPU
};
gpuSpeedItem.Click += (sender, e) => HandleSpeedSourceClick(speedSourceMenu, sender, SpeedSource.GPU, setSpeedSource);
speedSourceMenu.DropDownItems.Add(gpuSpeedItem);
}

var memorySpeedItem = new CustomToolStripMenuItem("Memory")
{
Checked = getSpeedSource() == SpeedSource.Memory
};
memorySpeedItem.Click += (sender, e) => HandleSpeedSourceClick(speedSourceMenu, sender, SpeedSource.Memory, setSpeedSource);
speedSourceMenu.DropDownItems.Add(memorySpeedItem);

var settingsMenu = new CustomToolStripMenuItem(Strings.Menu_Settings);
settingsMenu.DropDownItems.AddRange(
themeMenu,
fpsMaxLimitMenu,
launchAtStartupMenu
launchAtStartupMenu,
speedSourceMenu
);

var endlessGameMenu = new CustomToolStripMenuItem(Strings.Menu_EndlessGame);
Expand Down Expand Up @@ -175,6 +204,22 @@ Action<T> assignValueAction
}
}

private static void HandleSpeedSourceClick(
ToolStripMenuItem parentMenu,
object? sender,
SpeedSource source,
Action<SpeedSource> setSpeedSource
)
{
if (sender is null) return;
foreach (ToolStripMenuItem childItem in parentMenu.DropDownItems)
{
childItem.Checked = false;
}
((ToolStripMenuItem)sender).Checked = true;
setSpeedSource(source);
}

private static Bitmap? GetRunnerThumbnailBitmap(Theme systemTheme, Runner runner)
{
var iconName = $"{systemTheme.GetString()}_{runner.GetString()}_0".ToLower();
Expand Down
5 changes: 5 additions & 0 deletions RunCat365/GPURepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ struct GPUInfo

internal static class GPUInfoExtension
{
internal static string GetDescription(this GPUInfo gpuInfo)
{
return $"GPU: {gpuInfo.Total:f1}%";
}

internal static List<string> GenerateIndicator(this GPUInfo gpuInfo)
{
var resultLines = new List<string>
Expand Down
5 changes: 5 additions & 0 deletions RunCat365/MemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ struct MemoryInfo

internal static class MemoryInfoExtension
{
internal static string GetDescription(this MemoryInfo memoryInfo)
{
return $"{Strings.SystemInfo_Memory}: {memoryInfo.MemoryLoad}%";
}

internal static List<string> GenerateIndicator(this MemoryInfo memoryInfo)
{
var resultLines = new List<string>
Expand Down
33 changes: 28 additions & 5 deletions RunCat365/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ internal class RunCat365ApplicationContext : ApplicationContext
private Runner runner = Runner.Cat;
private Theme manualTheme = Theme.System;
private FPSMaxLimit fpsMaxLimit = FPSMaxLimit.FPS40;
private SpeedSource speedSource = SpeedSource.CPU;
private int fetchCounter = 5;

public RunCat365ApplicationContext()
Expand All @@ -72,6 +73,7 @@ public RunCat365ApplicationContext()
_ = Enum.TryParse(UserSettings.Default.Runner, out runner);
_ = Enum.TryParse(UserSettings.Default.Theme, out manualTheme);
_ = Enum.TryParse(UserSettings.Default.FPSMaxLimit, out fpsMaxLimit);
_ = Enum.TryParse(UserSettings.Default.SpeedSource, out speedSource);

SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(UserPreferenceChanged);

Expand All @@ -92,6 +94,9 @@ public RunCat365ApplicationContext()
f => ChangeFPSMaxLimit(f),
() => launchAtStartupManager.GetStartup(),
s => launchAtStartupManager.SetStartup(s),
() => speedSource,
s => ChangeSpeedSource(s),
gpuRepository.IsAvailable,
() => OpenRepository(),
() => Application.Exit()
);
Expand Down Expand Up @@ -179,6 +184,13 @@ private void ChangeFPSMaxLimit(FPSMaxLimit f)
UserSettings.Default.Save();
}

private void ChangeSpeedSource(SpeedSource source)
{
speedSource = source;
UserSettings.Default.SpeedSource = source.GetString();
UserSettings.Default.Save();
}

private void AnimationTick(object? sender, EventArgs e)
{
contextMenuManager.AdvanceFrame();
Expand All @@ -192,7 +204,13 @@ private void FetchSystemInfo(
NetworkInfo networkInfo
)
{
contextMenuManager.SetNotifyIconText(cpuInfo.GetDescription());
var tooltipText = speedSource switch
{
SpeedSource.GPU => gpuInfo.GetDescription(),
SpeedSource.Memory => memoryInfo.GetDescription(),
_ => cpuInfo.GetDescription()
};
contextMenuManager.SetNotifyIconText(tooltipText);

var systemInfoValues = new List<string>();
systemInfoValues.AddRange(cpuInfo.GenerateIndicator());
Expand All @@ -203,10 +221,15 @@ NetworkInfo networkInfo
contextMenuManager.SetSystemInfoMenuText(string.Join("\n", [.. systemInfoValues]));
}

private int CalculateInterval(float cpuTotalValue)
private int CalculateInterval(float cpuValue, float gpuValue, float memoryValue)
{
// Range of interval: 25-500 (ms) = 2-40 (fps)
var speed = (float)Math.Max(1.0f, (cpuTotalValue / 5.0f) * fpsMaxLimit.GetRate());
var load = speedSource switch
{
SpeedSource.GPU => gpuValue,
SpeedSource.Memory => memoryValue,
_ => cpuValue
};
var speed = (float)Math.Max(1.0f, (load / 5.0f) * fpsMaxLimit.GetRate());
return (int)(500.0f / speed);
}

Expand All @@ -226,7 +249,7 @@ private void FetchTick(object? state, EventArgs e)
FetchSystemInfo(cpuInfo, gpuInfo, memoryInfo, storageInfo, networkInfo);

animateTimer.Stop();
animateTimer.Interval = CalculateInterval(cpuInfo.Total);
animateTimer.Interval = CalculateInterval(cpuInfo.Total, gpuInfo.Total, memoryInfo.MemoryLoad);
animateTimer.Start();
}

Expand Down
12 changes: 12 additions & 0 deletions RunCat365/Properties/UserSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RunCat365/Properties/UserSettings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<Setting Name="FirstLaunch" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SpeedSource" Type="System.String" Scope="User">
<Value Profile="(Default)">CPU</Value>
</Setting>
</Settings>
</SettingsFile>
17 changes: 17 additions & 0 deletions RunCat365/SpeedSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace RunCat365
{
internal enum SpeedSource
{
CPU,
GPU,
Memory
}

internal static class SpeedSourceExtension
{
internal static string GetString(this SpeedSource source)
{
return source.ToString();
}
}
}