Skip to content

Commit

Permalink
Run Collapse after login using Task Scheduler (#317)
Browse files Browse the repository at this point in the history
* Early implementation for startup settings

* Move the task creator to its own method

WARNING: IsStartupToTray set method is not yet implemented

* Move stub finder to its own method

+ Dispose all Tasks
also finalize the IsStartupToTray

* Defaults to disable on task creation

* FInalize both backend and frontend for startup task

* Localize options

* Use the created task if the task does not exist

* Run task with highest privileges

As discussed with @bagusnl, the task should run automatically as the highest privileged process on start-up

---------

Co-authored-by: Shatyuka <[email protected]>
Co-authored-by: Kemal Setya Adhi <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2023
1 parent 86fe095 commit d72fefc
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 120 deletions.
1 change: 1 addition & 0 deletions CollapseLauncher/CollapseLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@
OnContent="{x:Bind helper:Locale.Lang._Misc.Enabled}"
OffContent="{x:Bind helper:Locale.Lang._Misc.Disabled}"
IsOn="{x:Bind IsMinimizeToTaskbar, Mode=TwoWay}"/>
<ToggleSwitch
x:Name="StartupToggle"
Margin="0,4"
Header="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_LaunchOnStartup}"
OnContent="{x:Bind helper:Locale.Lang._Misc.Enabled}"
OffContent="{x:Bind helper:Locale.Lang._Misc.Disabled}"
IsOn="{x:Bind IsLaunchOnStartup, Mode=TwoWay}"/>
<ToggleSwitch
x:Name="StartupToTrayToggle"
Margin="0,4"
Header="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_StartupToTray}"
OnContent="{x:Bind helper:Locale.Lang._Misc.Enabled}"
OffContent="{x:Bind helper:Locale.Lang._Misc.Disabled}"
IsOn="{x:Bind IsStartupToTray, Mode=TwoWay}"
Visibility="Collapsed"/>
</StackPanel>
<TextBlock
Margin="0,0,0,16"
Expand Down
108 changes: 106 additions & 2 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
using Hi3Helper.Shared.Region;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Connectivity;
using static CollapseLauncher.InnerLauncherConfig;
using static CollapseLauncher.RegionResourceListHelper;
Expand All @@ -28,6 +28,11 @@ namespace CollapseLauncher.Pages
{
public sealed partial class SettingsPage : Page
{
#region Properties
private readonly string _collapseStartupTaskName = "CollapseLauncherStartupTask";
#endregion

#region Settings Page Handler
public SettingsPage()
{
this.InitializeComponent();
Expand Down Expand Up @@ -68,7 +73,9 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
{
BackgroundImgChanger.ToggleBackground(true);
}
#endregion

#region Settings Methods
private async void RelocateFolder(object sender, RoutedEventArgs e)
{
switch (await Dialog_RelocateFolder(Content))
Expand Down Expand Up @@ -309,6 +316,42 @@ private void Egg(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e
HerLegacy.Visibility = Visibility.Visible;
}

private Task CreateScheduledTask(string taskName)
{
string collapseStartupTarget = FindCollapseStubPath();

using TaskService ts = new TaskService();

TaskDefinition taskDefinition = TaskService.Instance.NewTask();
taskDefinition.RegistrationInfo.Author = "CollapseLauncher";
taskDefinition.RegistrationInfo.Description = "Run Collapse Launcher automatically when computer starts";
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Settings.Enabled = false;
taskDefinition.Triggers.Add(new LogonTrigger());
taskDefinition.Actions.Add(new ExecAction(collapseStartupTarget, null, null));

Task task = TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, taskDefinition);
taskDefinition.Dispose();
return task;
}

public string FindCollapseStubPath()
{
var collapseExecName = "CollapseLauncher.exe";
var collapseMainPath = Process.GetCurrentProcess().MainModule.FileName;
var collapseStubPath = Path.Combine(Directory.GetParent(Path.GetDirectoryName(collapseMainPath)).FullName, collapseExecName);
if (File.Exists(collapseStubPath))
{
LogWriteLine($"Found stub at {collapseStubPath}", LogType.Default, true);
return collapseStubPath;
}
LogWriteLine($"Collapse stub does not exist, returning current executable path!\r\n\t{collapseStubPath}", LogType.Default, true);
return collapseMainPath;
}
#endregion

#region Settings UI Backend
private bool IsBGCustom
{
get
Expand Down Expand Up @@ -547,7 +590,7 @@ private int SelectedWindowSizeProfile
CurrentWindowSizeName = WindowSizeProfilesKey[value];
var delayedDragAreaChange = async () =>
{
await Task.Delay(250);
await System.Threading.Tasks.Task.Delay(250);
ChangeTitleDragArea.Change(DragAreaTemplate.Default);
};
delayedDragAreaChange();
Expand Down Expand Up @@ -632,6 +675,67 @@ private bool IsMinimizeToTaskbar
set => SetAndSaveConfigValue("MinimizeToTray", value);
}

private bool IsLaunchOnStartup
{
get
{
using TaskService ts = new TaskService();

Task task = ts.GetTask(_collapseStartupTaskName);
if (task == null) task = CreateScheduledTask(_collapseStartupTaskName);

bool value = task.Definition.Settings.Enabled;
task.Dispose();

if (value) StartupToTrayToggle.Visibility = Visibility.Visible;
else StartupToTrayToggle.Visibility = Visibility.Collapsed;

return value;
}
set
{
using TaskService ts = new TaskService();

Task task = ts.GetTask(_collapseStartupTaskName);
task.Definition.Settings.Enabled = value;
task.RegisterChanges();
task.Dispose();

if (value) StartupToTrayToggle.Visibility = Visibility.Visible;
else StartupToTrayToggle.Visibility = Visibility.Collapsed;
}
}

private bool IsStartupToTray
{
get
{
using TaskService ts = new TaskService();

Task task = ts.GetTask(_collapseStartupTaskName);
if (task == null) task = CreateScheduledTask(_collapseStartupTaskName);

bool? value = false;
if (task.Definition.Actions[0] is ExecAction execAction)
value = execAction.Arguments?.Trim().Contains("tray", StringComparison.CurrentCultureIgnoreCase);

task.Dispose();
return value ?? false;
}
set
{
string collapseStartupTarget = FindCollapseStubPath();
using TaskService ts = new TaskService();

Task task = ts.GetTask(_collapseStartupTaskName);
task.Definition.Actions.Clear();
task.Definition.Actions.Add(new ExecAction(collapseStartupTarget, value ? "tray" : null, null));
task.RegisterChanges();
task.Dispose();
}
}
#endregion

#region Keyboard Shortcuts
private async void ShowKbScList_Click(Object sender, RoutedEventArgs e) => await Dialogs.KeyboardShortcuts.Dialog_ShowKbShortcuts(this);

Expand Down
110 changes: 72 additions & 38 deletions CollapseLauncher/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@
"System.Runtime": "4.3.1"
}
},
"TaskScheduler": {
"type": "Direct",
"requested": "[2.10.1, )",
"resolved": "2.10.1",
"contentHash": "12b19oq9SOGbBq8745jQVQ08jAjgnsGXidQfclaZ94UZCtJRPkNfM8pz0D7pj04RHcQJQdZZnAUuC93reALqNg==",
"dependencies": {
"Microsoft.Win32.Registry": "5.0.0",
"System.Diagnostics.EventLog": "5.0.0",
"System.Security.AccessControl": "5.0.0"
}
},
"ColorCode.Core": {
"type": "Transitive",
"resolved": "2.0.13",
Expand Down Expand Up @@ -355,8 +366,8 @@
},
"Microsoft.NETCore.Platforms": {
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "TMBuzAHpTenGbGgk0SMTwyEkyijY/Eae4ZGsFNYJvAr/LDn1ku3Etp3FPxChmDp5HHF3kzJuoaa08N0xjqAJfQ=="
"resolved": "5.0.0",
"contentHash": "VyPlqzH2wavqquTcYpkIIAQ6WdenuKoFN0BdYBbCWsclXacSOHNQn66Gt4z5NBqEYW0FAPm5rlvki9ZiCij5xQ=="
},
"Microsoft.NETCore.Targets": {
"type": "Transitive",
Expand All @@ -373,6 +384,15 @@
"System.Runtime": "4.3.0"
}
},
"Microsoft.Win32.Registry": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==",
"dependencies": {
"System.Security.AccessControl": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"Microsoft.Win32.SystemEvents": {
"type": "Transitive",
"resolved": "8.0.0",
Expand Down Expand Up @@ -617,6 +637,16 @@
"System.Threading": "4.3.0"
}
},
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "FHkCwUfsTs+/5tsK+c0egLfacUgbhvcwi3wUFWSEEArSXao343mYqcpOVVFMlcCkdNtjU4YwAWaKYwal6f02og==",
"dependencies": {
"Microsoft.NETCore.Platforms": "5.0.0",
"Microsoft.Win32.Registry": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Diagnostics.Tools": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -983,6 +1013,15 @@
"System.Runtime.Extensions": "4.3.0"
}
},
"System.Security.AccessControl": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==",
"dependencies": {
"Microsoft.NETCore.Platforms": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.Cryptography.Algorithms": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -1127,6 +1166,11 @@
"runtime.native.System.Security.Cryptography.OpenSsl": "4.3.0"
}
},
"System.Security.Principal.Windows": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA=="
},
"System.Text.Encoding": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -1332,6 +1376,15 @@
"runtime.win.Microsoft.Win32.Primitives": "4.3.0"
}
},
"Microsoft.Win32.Registry": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "dDoKi0PnDz31yAyETfRntsLArTlVAVzUzCIvvEDsDsucrl33Dl8pIJG06ePTJTI3tGpeyHS9Cq7Foc/s4EeKcg==",
"dependencies": {
"System.Security.AccessControl": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"Microsoft.Win32.SystemEvents": {
"type": "Transitive",
"resolved": "8.0.0",
Expand Down Expand Up @@ -1632,6 +1685,16 @@
"runtime.win.System.Diagnostics.Debug": "4.3.0"
}
},
"System.Diagnostics.EventLog": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "FHkCwUfsTs+/5tsK+c0egLfacUgbhvcwi3wUFWSEEArSXao343mYqcpOVVFMlcCkdNtjU4YwAWaKYwal6f02og==",
"dependencies": {
"Microsoft.NETCore.Platforms": "5.0.0",
"Microsoft.Win32.Registry": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Diagnostics.Tools": {
"type": "Transitive",
"resolved": "4.3.0",
Expand Down Expand Up @@ -1906,18 +1969,13 @@
"runtime.native.System": "4.3.0"
}
},
"System.Security.Claims": {
"System.Security.AccessControl": {
"type": "Transitive",
"resolved": "4.3.0",
"contentHash": "P/+BR/2lnc4PNDHt/TPBAWHVMLMRHsyYZbU1NphW4HIWzCggz8mJbTQQ3MKljFE7LS3WagmVFuBgoLcFzYXlkA==",
"resolved": "5.0.0",
"contentHash": "dagJ1mHZO3Ani8GH0PHpPEe/oYO+rVdbQjvjJkBRNQkX4t0r1iaeGn8+/ybkSLEan3/slM0t59SVdHzuHf2jmw==",
"dependencies": {
"System.Collections": "4.3.0",
"System.Globalization": "4.3.0",
"System.IO": "4.3.0",
"System.Resources.ResourceManager": "4.3.0",
"System.Runtime": "4.3.0",
"System.Runtime.Extensions": "4.3.0",
"System.Security.Principal": "4.3.0"
"Microsoft.NETCore.Platforms": "5.0.0",
"System.Security.Principal.Windows": "5.0.0"
}
},
"System.Security.Cryptography.Algorithms": {
Expand Down Expand Up @@ -2050,34 +2108,10 @@
"runtime.native.System.Security.Cryptography.OpenSsl": "4.3.0"
}
},
"System.Security.Principal": {
"type": "Transitive",
"resolved": "4.3.0",
"contentHash": "I1tkfQlAoMM2URscUtpcRo/hX0jinXx6a/KUtEQoz3owaYwl3qwsO8cbzYVVnjxrzxjHo3nJC+62uolgeGIS9A==",
"dependencies": {
"System.Runtime": "4.3.0"
}
},
"System.Security.Principal.Windows": {
"type": "Transitive",
"resolved": "4.3.0",
"contentHash": "HVL1rvqYtnRCxFsYag/2le/ZfKLK4yMw79+s6FmKXbSCNN0JeAhrYxnRAHFoWRa0dEojsDcbBSpH3l22QxAVyw==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.0",
"Microsoft.Win32.Primitives": "4.3.0",
"System.Collections": "4.3.0",
"System.Diagnostics.Debug": "4.3.0",
"System.Reflection": "4.3.0",
"System.Resources.ResourceManager": "4.3.0",
"System.Runtime": "4.3.0",
"System.Runtime.Extensions": "4.3.0",
"System.Runtime.Handles": "4.3.0",
"System.Runtime.InteropServices": "4.3.0",
"System.Security.Claims": "4.3.0",
"System.Security.Principal": "4.3.0",
"System.Text.Encoding": "4.3.0",
"System.Threading": "4.3.0"
}
"resolved": "5.0.0",
"contentHash": "t0MGLukB5WAVU9bO3MGzvlGnyJPgUlcwerXn1kzBRjwLKixT96XV0Uza41W49gVd8zEMFu9vQEFlv0IOrytICA=="
},
"System.Text.Encoding": {
"type": "Transitive",
Expand Down
Loading

0 comments on commit d72fefc

Please sign in to comment.