-
Notifications
You must be signed in to change notification settings - Fork 831
Fix startup for Microsoft Store #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+188
−4
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4eb7697
Fix launch at startup for app in Microsoft Store
mor39a 4ee8bc3
Added Launch at Startup option when not installed from Microsoft Store.
mor39a 547fd04
Apply suggestion from @Kyome22
mor39a 2d0c9b7
Apply suggestion from @Kyome22
mor39a de8d3fb
Return the name from RunCat365 to RunCat 365
mor39a e9566e0
Refactoring startup
mor39a 89eddaf
Refactoring LaunchAtStartupManager
mor39a b64b4f6
UI is separated from the logic, of LaunchAtStartupManager
mor39a 9b52575
Keep StartupTask static
mor39a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.