Skip to content

Commit 7d3c2f6

Browse files
authored
Feature: Added run as admin toggle to shortcut properties (#10833)
1 parent 3e26ad1 commit 7d3c2f6

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

src/Files.App/Helpers/UIFilesystemHelpers.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public static async Task CreateFolderWithSelectionAsync(IShellPage associatedIns
379379
}
380380

381381
/// <summary>
382-
/// Set a single file or folder to hidden or unhidden an refresh the
382+
/// Set a single file or folder to hidden or unhidden and refresh the
383383
/// view after setting the flag
384384
/// </summary>
385385
/// <param name="item"></param>
@@ -396,5 +396,21 @@ public static async Task CreateShortcutFromDialogAsync(IShellPage associatedInst
396396
var dialogService = Ioc.Default.GetRequiredService<IDialogService>();
397397
await dialogService.ShowDialogAsync(viewModel);
398398
}
399+
400+
/// <summary>
401+
/// Updates ListedItem properties for a shortcut
402+
/// </summary>
403+
/// <param name="item"></param>
404+
/// <param name="targetPath"></param>
405+
/// <param name="arguments"></param>
406+
/// <param name="workingDir"></param>
407+
/// <param name="runAsAdmin"></param>
408+
public static void UpdateShortcutItemProperties(ShortcutItem item, string targetPath, string arguments, string workingDir, bool runAsAdmin)
409+
{
410+
item.TargetPath = targetPath;
411+
item.Arguments = arguments;
412+
item.WorkingDirectory = workingDir;
413+
item.RunAsAdmin = runAsAdmin;
414+
}
399415
}
400416
}

src/Files.App/ViewModels/Properties/FileProperties.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ public override void GetBaseProperties()
6969
ViewModel.ShortcutItemWorkingDirVisibility = Item.IsLinkItem || shortcutItem.IsSymLink ? false : true;
7070
ViewModel.ShortcutItemArguments = shortcutItem.Arguments;
7171
ViewModel.ShortcutItemArgumentsVisibility = Item.IsLinkItem || shortcutItem.IsSymLink ? false : true;
72+
if (isApplication)
73+
ViewModel.RunAsAdmin = shortcutItem.RunAsAdmin;
7274
ViewModel.IsSelectedItemShortcut = FileExtensionHelpers.IsShortcutFile(Item.FileExtension);
7375
ViewModel.ShortcutItemOpenLinkCommand = new RelayCommand(async () =>
7476
{
7577
if (Item.IsLinkItem)
7678
{
7779
var tmpItem = (ShortcutItem)Item;
78-
await Win32Helpers.InvokeWin32ComponentAsync(ViewModel.ShortcutItemPath, AppInstance, ViewModel.ShortcutItemArguments, tmpItem.RunAsAdmin, ViewModel.ShortcutItemWorkingDir);
80+
await Win32Helpers.InvokeWin32ComponentAsync(ViewModel.ShortcutItemPath, AppInstance, ViewModel.ShortcutItemArguments, ViewModel.RunAsAdmin, ViewModel.ShortcutItemWorkingDir);
7981
}
8082
else
8183
{
@@ -311,16 +313,17 @@ private async void ViewModel_PropertyChanged(object sender, System.ComponentMode
311313
}
312314
break;
313315

316+
case "RunAsAdmin":
314317
case "ShortcutItemPath":
315318
case "ShortcutItemWorkingDir":
316319
case "ShortcutItemArguments":
317-
var tmpItem = (ShortcutItem)Item;
318320
if (string.IsNullOrWhiteSpace(ViewModel.ShortcutItemPath))
319321
return;
320322

321-
await FileOperationsHelpers.CreateOrUpdateLinkAsync(Item.ItemPath, ViewModel.ShortcutItemPath, ViewModel.ShortcutItemArguments, ViewModel.ShortcutItemWorkingDir, tmpItem.RunAsAdmin);
323+
await FileOperationsHelpers.CreateOrUpdateLinkAsync(Item.ItemPath, ViewModel.ShortcutItemPath, ViewModel.ShortcutItemArguments, ViewModel.ShortcutItemWorkingDir, ViewModel.RunAsAdmin);
322324
break;
323325
}
324326
}
325327
}
328+
326329
}

src/Files.App/ViewModels/SelectedItemsPropertiesViewModel.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,5 +602,23 @@ public bool IsHidden
602602
get => isHidden;
603603
set => SetProperty(ref isHidden, value);
604604
}
605+
606+
private bool runAsAdmin;
607+
public bool RunAsAdmin
608+
{
609+
get => runAsAdmin;
610+
set
611+
{
612+
RunAsAdminEnabled = true;
613+
SetProperty(ref runAsAdmin, value);
614+
}
615+
}
616+
617+
private bool runAsAdminEnabled;
618+
public bool RunAsAdminEnabled
619+
{
620+
get => runAsAdminEnabled;
621+
set => SetProperty(ref runAsAdminEnabled, value);
622+
}
605623
}
606624
}

src/Files.App/Views/Pages/PropertiesShortcut.xaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ResourceDictionary>
1515
<ResourceDictionary.MergedDictionaries>
1616
<ResourceDictionary Source="ms-appx:///ResourceDictionaries/PropertiesStyles.xaml" />
17+
<ResourceDictionary Source="ms-appx:///ResourceDictionaries/RightAlignedToggleSwitchStyle.xaml" />
1718
</ResourceDictionary.MergedDictionaries>
1819
</ResourceDictionary>
1920
</local:PropertiesTab.Resources>
@@ -127,6 +128,17 @@
127128
Text="{x:Bind ViewModel.ShortcutItemWorkingDir, Mode=TwoWay}" />
128129
</Grid>
129130

131+
<local1:SettingsBlockControl
132+
x:Name="RunAsAdminCheckbox"
133+
Title="{helpers:ResourceString Name=RunAsAdministrator}"
134+
HorizontalAlignment="Stretch"
135+
x:Load="{x:Bind ViewModel.RunAsAdminEnabled, Mode=OneWay}">
136+
<ToggleSwitch
137+
AutomationProperties.Name="{helpers:ResourceString Name=RunAsAdministrator}"
138+
IsOn="{x:Bind ViewModel.RunAsAdmin, Mode=TwoWay}"
139+
Style="{StaticResource RightAlignedToggleSwitchStyle}" />
140+
</local1:SettingsBlockControl>
141+
130142
<local1:SettingsBlockControl
131143
x:Name="OpenFileLocation"
132144
Title="{helpers:ResourceString Name=OpenFileLocation}"

src/Files.App/Views/Pages/PropertiesShortcut.xaml.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using CommunityToolkit.WinUI;
2+
using Files.App.Filesystem;
13
using Files.App.ViewModels.Properties;
4+
using Files.App.Helpers;
25
using System.Threading.Tasks;
36

47
namespace Files.App.Views
@@ -10,9 +13,27 @@ public PropertiesShortcut()
1013
InitializeComponent();
1114
}
1215

13-
public override Task<bool> SaveChangesAsync()
16+
public override async Task<bool> SaveChangesAsync()
1417
{
15-
return Task.FromResult(true);
18+
var shortcutItem = BaseProperties switch
19+
{
20+
FileProperties properties => properties.Item,
21+
FolderProperties properties => properties.Item,
22+
_ => null
23+
} as ShortcutItem;
24+
25+
if (shortcutItem is null)
26+
return true;
27+
28+
await App.Window.DispatcherQueue.EnqueueAsync(() =>
29+
UIFilesystemHelpers.UpdateShortcutItemProperties(shortcutItem,
30+
ViewModel.ShortcutItemPath,
31+
ViewModel.ShortcutItemArguments,
32+
ViewModel.ShortcutItemWorkingDir,
33+
ViewModel.RunAsAdmin)
34+
);
35+
36+
return true;
1637
}
1738

1839
public override void Dispose()

0 commit comments

Comments
 (0)