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
29 changes: 28 additions & 1 deletion RunCat365/ContextMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ internal ContextMenuManager(
Action<Theme> setManualTheme,
Func<FPSMaxLimit> getFPSMaxLimit,
Action<FPSMaxLimit> setFPSMaxLimit,
Func<bool> getLaunchAtStartup,
Func<bool, bool> toggleLaunchAtStartup,
Action openRepository,
Action onExit
)
Expand Down Expand Up @@ -91,10 +93,17 @@ Action onExit
_ => null
);

var launchAtStartupMenu = new CustomToolStripMenuItem("Launch at startup")
{
Checked = getLaunchAtStartup()
};
launchAtStartupMenu.Click += (sender, e) => HandleStartupMenuClick(sender, toggleLaunchAtStartup);

var settingsMenu = new CustomToolStripMenuItem("Settings");
settingsMenu.DropDownItems.AddRange(
themeMenu,
fpsMaxLimitMenu
fpsMaxLimitMenu,
launchAtStartupMenu
);

var endlessGameMenu = new CustomToolStripMenuItem("Endless Game");
Expand Down Expand Up @@ -187,6 +196,24 @@ internal void SetIcons(Theme systemTheme, Theme manualTheme, Runner runner)
icons.AddRange(list);
}

private static void HandleStartupMenuClick(object? sender, Func<bool, bool> toggleLaunchAtStartup)
{
if (sender is null) return;
var item = (ToolStripMenuItem)sender;
try
{
if (toggleLaunchAtStartup(item.Checked))
{
item.Checked = !item.Checked;
}
}
catch (InvalidOperationException ex)
{
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

}

private void ShowOrActivateGameWindow(Func<Theme> getSystemTheme)
{
if (endlessGameForm is null)
Expand Down
141 changes: 141 additions & 0 deletions RunCat365/LaunchAtStartupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2025 Takuto Nakamura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.Win32;
using Windows.ApplicationModel;

namespace RunCat365
{

internal interface ILaunchAtStartupManager
{
bool GetEnabled();
bool SetEnabled(bool enabled);
}

internal sealed class PackagedLaunchAtStartupManager : ILaunchAtStartupManager
{
private static StartupTask? startupTask;

public bool GetEnabled()
{
if (startupTask is null) startupTask = Task.Run(async () => await StartupTask.GetAsync("RunCatStartup")).Result;
if (startupTask is null) return false;
if (startupTask.State == StartupTaskState.Enabled) return true;
return false;
}

public bool SetEnabled(bool enabled)
{
var changeCheck = false;
if (startupTask is null) startupTask = Task.Run(async () => await StartupTask.GetAsync("RunCatStartup")).Result;
if (!enabled)
{
switch (startupTask.State)
{
case StartupTaskState.Enabled:
changeCheck = true;
break;
case StartupTaskState.Disabled:
StartupTaskState newStartupState = Task.Run(async () => await startupTask.RequestEnableAsync()).Result;
if (newStartupState == StartupTaskState.Enabled)
{
changeCheck = true;
}
else
{
throw new InvalidOperationException("Launch at Startup could not be activated.");
}
break;
case StartupTaskState.DisabledByUser:
throw new InvalidOperationException("Launch at startup was disabled by the user, enable it in Task Manager > Startup, search RunCat 365 and enable it.");
case StartupTaskState.DisabledByPolicy:
throw new InvalidOperationException("Launch at startup was disabled by policy.");
}
}
else
{
if (startupTask.State == StartupTaskState.Enabled) startupTask.Disable();
changeCheck = true;
}
return changeCheck;
}
}

internal sealed class UnpackagedLaunchAtStartupManager : ILaunchAtStartupManager
{
public bool GetEnabled()
{
var keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
using var rKey = Registry.CurrentUser.OpenSubKey(keyName);
if (rKey is null) return false;
var value = (rKey.GetValue(Application.ProductName) is not null);
rKey.Close();
return value;
}

public bool SetEnabled(bool enabled)
{
var productName = Application.ProductName;
if (productName is null) return false;
var keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
using var rKey = Registry.CurrentUser.OpenSubKey(keyName, true);
if (rKey is null) return false;
if (enabled)
{
rKey.DeleteValue(productName, false);
}
else
{
var fileName = Environment.ProcessPath;
if (fileName != null)
{
rKey.SetValue(productName, fileName);
}
}
rKey.Close();
return true;
}
}

internal class LaunchAtStartupManager
{

private readonly ILaunchAtStartupManager _launchAtStartupManager;

public LaunchAtStartupManager()
{
_launchAtStartupManager = IsRunningAsPackaged()
? new PackagedLaunchAtStartupManager()
: new UnpackagedLaunchAtStartupManager();
}

public bool GetStartup() => _launchAtStartupManager.GetEnabled();

public bool SetStartup(bool enabled) => _launchAtStartupManager.SetEnabled(enabled);

private bool IsRunningAsPackaged()
{
try
{
var _ = Package.Current;
return true;
}
catch
{
return false;
}
}
}
}
8 changes: 6 additions & 2 deletions RunCat365/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Takuto Nakamura
// Copyright 2020 Takuto Nakamura
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using FormsTimer = System.Windows.Forms.Timer;
using Microsoft.Win32;
using RunCat365.Properties;
using System.Diagnostics;
using FormsTimer = System.Windows.Forms.Timer;

namespace RunCat365
{
Expand Down Expand Up @@ -49,6 +49,7 @@ public class RunCat365ApplicationContext : ApplicationContext
private readonly CPURepository cpuRepository;
private readonly MemoryRepository memoryRepository;
private readonly StorageRepository storageRepository;
private readonly LaunchAtStartupManager launchAtStartupManager;
private readonly ContextMenuManager contextMenuManager;
private readonly FormsTimer fetchTimer;
private readonly FormsTimer animateTimer;
Expand All @@ -70,6 +71,7 @@ public RunCat365ApplicationContext()
cpuRepository = new CPURepository();
memoryRepository = new MemoryRepository();
storageRepository = new StorageRepository();
launchAtStartupManager = new LaunchAtStartupManager();

contextMenuManager = new ContextMenuManager(
() => runner,
Expand All @@ -79,6 +81,8 @@ public RunCat365ApplicationContext()
t => manualTheme = t,
() => fpsMaxLimit,
f => fpsMaxLimit = f,
() => launchAtStartupManager.GetStartup(),
s => launchAtStartupManager.SetStartup(s),
() => OpenRepository(),
() => Exit()
);
Expand Down
14 changes: 13 additions & 1 deletion WapForStore/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">
IgnorableNamespaces="uap uap5 desktop rescap">
Comment thread
mor39a marked this conversation as resolved.

<Identity
Name="StudioKyome.RunCat"
Expand Down Expand Up @@ -39,6 +41,16 @@
<uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" Square71x71Logo="Images\SmallTile.png" Square310x310Logo="Images\LargeTile.png"/>
<uap:SplashScreen Image="Images\SplashScreen.png" />
</uap:VisualElements>
<Extensions>
<uap5:Extension
Category="windows.startupTask">
<uap5:StartupTask
TaskId="RunCatStartup"
DisplayName="RunCat 365"/>
</uap5:Extension>
<desktop:Extension
Category="windows.fullTrustProcess" />
</Extensions>
</Application>
</Applications>

Expand Down