Skip to content

Commit 410ec87

Browse files
authored
Added option to open Files on Windows StartUp (#7309)
1 parent 0e3462b commit 410ec87

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

src/Files.Package/Package.appxmanifest

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
<uap5:ExecutionAlias Alias="files.exe"/>
5959
</uap5:AppExecutionAlias>
6060
</uap5:Extension>
61+
<uap5:Extension
62+
Category="windows.startupTask"
63+
Executable="files.exe"
64+
EntryPoint="files.App">
65+
<uap5:StartupTask
66+
TaskId="3AA55462-A5FA-4933-88C4-712D0B6CDEBB"
67+
Enabled="false"
68+
DisplayName="Files" />
69+
</uap5:Extension>
6170
<Extension Category="windows.updateTask" EntryPoint="BackgroundTasks.UpdateTask" />
6271
<uap:Extension Category="windows.fileTypeAssociation">
6372
<uap:FileTypeAssociation Name="zip">

src/Files/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ async Task PerformNavigation(string payload, string selectItem = null)
462462
SettingsViewModel.ReportIssueOnGitHub();
463463
}
464464
break;
465+
466+
case ActivationKind.StartupTask:
467+
break;
465468
}
466469

467470
rootFrame.Navigate(typeof(MainPage), null, new SuppressNavigationTransitionInfo());

src/Files/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,4 +2686,7 @@ We use App Center to keep track of app usage, find bugs, and fix crashes. All in
26862686
<data name="RecentFiles" xml:space="preserve">
26872687
<value>Recent Files</value>
26882688
</data>
2689+
<data name="SettingsOpenInLogin" xml:space="preserve">
2690+
<value>Open Files on Windows Startup</value>
2691+
</data>
26892692
</root>

src/Files/ViewModels/SettingsViewModels/PreferencesViewModel.cs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Linq;
1717
using System.Threading.Tasks;
1818
using System.Windows.Input;
19+
using Windows.ApplicationModel;
1920
using Windows.Foundation.Collections;
2021
using Windows.Storage;
2122
using Windows.Storage.AccessCache;
@@ -42,6 +43,8 @@ public class PreferencesViewModel : ObservableObject, IDisposable
4243

4344
public ICommand EditTerminalApplicationsCommand { get; }
4445

46+
public ICommand OpenFilesAtStartupCommand { get; }
47+
4548
public PreferencesViewModel()
4649
{
4750
DefaultLanguages = App.AppSettings.DefaultLanguages;
@@ -53,6 +56,7 @@ public PreferencesViewModel()
5356
};
5457

5558
EditTerminalApplicationsCommand = new AsyncRelayCommand(LaunchTerminalsConfigFile);
59+
OpenFilesAtStartupCommand = new AsyncRelayCommand(OpenFilesAtStartup);
5660
App.TerminalController.ModelChanged += ReloadTerminals;
5761

5862
if (UserSettingsService.PreferencesSettingsService.TabsOnStartupList != null)
@@ -75,8 +79,9 @@ public PreferencesViewModel()
7579
recentsItem,
7680
});
7781
}, TaskScheduler.FromCurrentSynchronizationContext());
78-
}
7982

83+
_ = DetectOpenFilesAtStartup();
84+
}
8085

8186
private async Task PopulateRecentItems(MenuFlyoutSubItemViewModel menu)
8287
{
@@ -419,6 +424,85 @@ await connection.SendMessageAsync(new ValueSet()
419424
}
420425
}
421426

427+
private bool openInLogin;
428+
429+
public bool OpenInLogin
430+
{
431+
get => openInLogin;
432+
set => SetProperty(ref openInLogin, value);
433+
}
434+
435+
private bool canOpenInLogin;
436+
437+
public bool CanOpenInLogin
438+
{
439+
get => canOpenInLogin;
440+
set => SetProperty(ref canOpenInLogin, value);
441+
}
442+
443+
public async Task OpenFilesAtStartup()
444+
{
445+
var stateMode = await ReadState();
446+
447+
bool state = stateMode switch
448+
{
449+
StartupTaskState.Enabled => true,
450+
StartupTaskState.EnabledByPolicy => true,
451+
StartupTaskState.DisabledByPolicy => false,
452+
StartupTaskState.DisabledByUser => false,
453+
_ => false,
454+
};
455+
456+
if (state != OpenInLogin)
457+
{
458+
StartupTask startupTask = await StartupTask.GetAsync("3AA55462-A5FA-4933-88C4-712D0B6CDEBB");
459+
if (OpenInLogin)
460+
{
461+
await startupTask.RequestEnableAsync();
462+
}
463+
else
464+
{
465+
startupTask.Disable();
466+
}
467+
await DetectOpenFilesAtStartup();
468+
}
469+
}
470+
471+
public async Task DetectOpenFilesAtStartup()
472+
{
473+
var stateMode = await ReadState();
474+
475+
switch (stateMode)
476+
{
477+
case StartupTaskState.Disabled:
478+
CanOpenInLogin = true;
479+
OpenInLogin = false;
480+
break;
481+
case StartupTaskState.Enabled:
482+
CanOpenInLogin = true;
483+
OpenInLogin = true;
484+
break;
485+
case StartupTaskState.DisabledByPolicy:
486+
CanOpenInLogin = false;
487+
OpenInLogin = false;
488+
break;
489+
case StartupTaskState.DisabledByUser:
490+
CanOpenInLogin = false;
491+
OpenInLogin = false;
492+
break;
493+
case StartupTaskState.EnabledByPolicy:
494+
CanOpenInLogin = false;
495+
OpenInLogin = true;
496+
break;
497+
}
498+
}
499+
500+
public async Task<StartupTaskState> ReadState()
501+
{
502+
var state = await StartupTask.GetAsync("3AA55462-A5FA-4933-88C4-712D0B6CDEBB");
503+
return state.State;
504+
}
505+
422506
public bool AreHiddenItemsVisible
423507
{
424508
get => UserSettingsService.PreferencesSettingsService.AreHiddenItemsVisible;

src/Files/Views/SettingsPages/Preferences.xaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
99
xmlns:datamodels="using:Files.DataModels"
1010
xmlns:helpers="using:Files.Helpers"
11+
xmlns:i="using:Microsoft.Xaml.Interactivity"
12+
xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
1113
xmlns:local="using:Files.UserControls.Settings"
1214
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1315
xmlns:settingsviewmodels="using:Files.ViewModels.SettingsViewModels"
@@ -324,6 +326,23 @@
324326
IsOn="{Binding AlwaysOpenANewInstance, Mode=TwoWay}"
325327
Style="{StaticResource RightAlignedToggleSwitchStyle}" />
326328
</local:SettingsBlockControl>
329+
330+
<local:SettingsBlockControl Title="{helpers:ResourceString Name=SettingsOpenInLogin}" HorizontalAlignment="Stretch">
331+
<local:SettingsBlockControl.Icon>
332+
<FontIcon Glyph="&#xE7E8;" />
333+
</local:SettingsBlockControl.Icon>
334+
<ToggleSwitch
335+
AutomationProperties.Name="{helpers:ResourceString Name=SettingsOpenInLogin}"
336+
IsEnabled="{Binding CanOpenInLogin, Mode=OneWay}"
337+
IsOn="{Binding OpenInLogin, Mode=TwoWay}"
338+
Style="{StaticResource RightAlignedToggleSwitchStyle}">
339+
<i:Interaction.Behaviors>
340+
<icore:EventTriggerBehavior EventName="Toggled">
341+
<icore:InvokeCommandAction Command="{Binding OpenFilesAtStartupCommand, Mode=OneWay}" />
342+
</icore:EventTriggerBehavior>
343+
</i:Interaction.Behaviors>
344+
</ToggleSwitch>
345+
</local:SettingsBlockControl>
327346
</StackPanel>
328347
</local:SettingsBlockControl.ExpandableContent>
329348
</local:SettingsBlockControl>

0 commit comments

Comments
 (0)