Skip to content

Commit 27e0ae3

Browse files
added run setting to the shortcut properties dialog
1 parent 16f39b6 commit 27e0ae3

File tree

15 files changed

+92
-17
lines changed

15 files changed

+92
-17
lines changed

src/Files.App/Data/Items/ListedItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Drawing;
1010
using System.IO;
1111
using System.Text;
12+
using Vanara.PInvoke;
1213
using Windows.Storage;
1314

1415
#pragma warning disable CS0618 // Type or member is obsolete
@@ -526,6 +527,7 @@ public override string Name
526527
public string Arguments { get; set; }
527528
public string WorkingDirectory { get; set; }
528529
public bool RunAsAdmin { get; set; }
530+
public ShowWindowCommand ShowWindowCommand { get; set; }
529531
public bool IsUrl { get; set; }
530532
public bool IsSymLink { get; set; }
531533
public override bool IsExecutable => FileExtensionHelpers.IsExecutableFile(TargetPath, true);

src/Files.App/Data/Items/ShellLinkItem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Vanara.PInvoke;
5+
46
namespace Files.App.Data.Items
57
{
68
public sealed class ShellLinkItem : ShellFileItem
@@ -15,6 +17,8 @@ public sealed class ShellLinkItem : ShellFileItem
1517

1618
public bool InvalidTarget { get; set; }
1719

20+
public ShowWindowCommand ShowWindowCommand { get; set; }
21+
1822
public ShellLinkItem()
1923
{
2024
}

src/Files.App/Data/Models/SelectedItemsPropertiesViewModel.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Files.Shared.Helpers;
66
using System.Windows.Input;
77
using TagLib;
8+
using Vanara.PInvoke;
89

910
namespace Files.App.Data.Models
1011
{
@@ -526,7 +527,6 @@ public bool IsItemSelected
526527

527528
public SelectedItemsPropertiesViewModel()
528529
{
529-
530530
}
531531

532532
private bool isSelectedItemImage = false;
@@ -782,6 +782,42 @@ public bool RunAsAdminEnabled
782782
set => SetProperty(ref runAsAdminEnabled, value);
783783
}
784784

785+
private static readonly IReadOnlyDictionary<ShowWindowCommand, string> showWindowCommandTypes = new Dictionary<ShowWindowCommand, string>()
786+
{
787+
{ ShowWindowCommand.SW_NORMAL, Strings.NormalWindow.GetLocalizedResource() },
788+
{ ShowWindowCommand.SW_SHOWMINNOACTIVE, Strings.Minimized.GetLocalizedResource() },
789+
{ ShowWindowCommand.SW_MAXIMIZE, Strings.Maximized.GetLocalizedResource() }
790+
}.AsReadOnly();
791+
public IReadOnlyDictionary<ShowWindowCommand, string> ShowWindowCommandTypes { get => showWindowCommandTypes; }
792+
793+
public string SelectedShowWindowCommand
794+
{
795+
get => ShowWindowCommandTypes.GetValueOrDefault(ShowWindowCommandEditedValue)!;
796+
set => ShowWindowCommandEditedValue = ShowWindowCommandTypes.First(e => e.Value == value).Key;
797+
}
798+
799+
private ShowWindowCommand showWindowCommand;
800+
public ShowWindowCommand ShowWindowCommand
801+
{
802+
get => showWindowCommand;
803+
set
804+
{
805+
if (SetProperty(ref showWindowCommand, value))
806+
ShowWindowCommandEditedValue = value;
807+
}
808+
}
809+
810+
private ShowWindowCommand showWindowCommandEditedValue;
811+
public ShowWindowCommand ShowWindowCommandEditedValue
812+
{
813+
get => showWindowCommandEditedValue;
814+
set
815+
{
816+
if (SetProperty(ref showWindowCommandEditedValue, value))
817+
OnPropertyChanged(nameof(SelectedShowWindowCommand));
818+
}
819+
}
820+
785821
private bool isPropertiesLoaded;
786822
public bool IsPropertiesLoaded
787823
{

src/Files.App/Helpers/UI/UIFilesystemHelpers.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Net;
88
using System.Text;
9+
using Vanara.PInvoke;
910
using Windows.ApplicationModel.DataTransfer;
1011
using Windows.Storage;
1112

@@ -245,17 +246,13 @@ public static async Task<bool> HandleShortcutCannotBeCreated(string shortcutName
245246
/// <summary>
246247
/// Updates ListedItem properties for a shortcut
247248
/// </summary>
248-
/// <param name="item"></param>
249-
/// <param name="targetPath"></param>
250-
/// <param name="arguments"></param>
251-
/// <param name="workingDir"></param>
252-
/// <param name="runAsAdmin"></param>
253-
public static void UpdateShortcutItemProperties(ShortcutItem item, string targetPath, string arguments, string workingDir, bool runAsAdmin)
249+
public static void UpdateShortcutItemProperties(ShortcutItem item, string targetPath, string arguments, string workingDir, bool runAsAdmin, ShowWindowCommand showWindowCommand)
254250
{
255251
item.TargetPath = Environment.ExpandEnvironmentVariables(targetPath);
256252
item.Arguments = arguments;
257253
item.WorkingDirectory = workingDir;
258254
item.RunAsAdmin = runAsAdmin;
255+
item.ShowWindowCommand = showWindowCommand;
259256
}
260257

261258
public async static Task<StorageCredential> RequestPassword(IPasswordProtectedItem sender)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,18 @@
18381838
<data name="CompatibilityRegisterThisProgramForRestart" xml:space="preserve">
18391839
<value>Register this program for restart</value>
18401840
</data>
1841+
<data name="Run" xml:space="preserve">
1842+
<value>Run</value>
1843+
</data>
1844+
<data name="NormalWindow" xml:space="preserve">
1845+
<value>Normal window</value>
1846+
</data>
1847+
<data name="Minimized" xml:space="preserve">
1848+
<value>Minimized</value>
1849+
</data>
1850+
<data name="Maximized" xml:space="preserve">
1851+
<value>Maximized</value>
1852+
</data>
18411853
<data name="RunAsAdministrator" xml:space="preserve">
18421854
<value>Run as administrator</value>
18431855
</data>

src/Files.App/Utils/Shell/ShellFolderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public static ShellLinkItem GetShellLinkItem(ShellLink linkItem)
127127
{
128128
IsFolder = !string.IsNullOrEmpty(linkItem.TargetPath) && linkItem.Target.IsFolder,
129129
RunAsAdmin = linkItem.RunAsAdministrator,
130+
ShowWindowCommand = linkItem.ShowState,
130131
Arguments = linkItem.Arguments,
131132
WorkingDirectory = Environment.ExpandEnvironmentVariables(linkItem.WorkingDirectory),
132133
TargetPath = Environment.ExpandEnvironmentVariables(linkItem.TargetPath)

src/Files.App/Utils/Storage/Enumerators/UniversalStorageEnumerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ public static async Task<ListedItem> AddFolderAsync(
193193
TargetPath = linkFolder.TargetPath,
194194
Arguments = linkFolder.Arguments,
195195
WorkingDirectory = linkFolder.WorkingDirectory,
196-
RunAsAdmin = linkFolder.RunAsAdmin
196+
RunAsAdmin = linkFolder.RunAsAdmin,
197+
ShowWindowCommand = linkFolder.ShowWindowCommand
197198
};
198199
}
199200
else if (folder is BinStorageFolder binFolder)
@@ -293,6 +294,7 @@ public static async Task<ListedItem> AddFileAsync(
293294
Arguments = linkFile.Arguments,
294295
WorkingDirectory = linkFile.WorkingDirectory,
295296
RunAsAdmin = linkFile.RunAsAdmin,
297+
ShowWindowCommand = linkFile.ShowWindowCommand,
296298
IsUrl = isUrl,
297299
};
298300
}

src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ CancellationToken cancellationToken
324324
Arguments = shInfo.Arguments,
325325
WorkingDirectory = shInfo.WorkingDirectory,
326326
RunAsAdmin = shInfo.RunAsAdmin,
327+
ShowWindowCommand = shInfo.ShowWindowCommand,
327328
IsUrl = isUrl,
328329
};
329330
}

src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ public static void TryCancelOperation(string operationId)
733733
}
734734
}
735735

736-
public static Task<bool> CreateOrUpdateLinkAsync(string linkSavePath, string targetPath, string arguments = "", string workingDirectory = "", bool runAsAdmin = false)
736+
public static Task<bool> CreateOrUpdateLinkAsync(string linkSavePath, string targetPath, string arguments = "", string workingDirectory = "", bool runAsAdmin = false, ShowWindowCommand showWindowCommand = ShowWindowCommand.SW_NORMAL)
737737
{
738738
try
739739
{
@@ -746,6 +746,10 @@ public static Task<bool> CreateOrUpdateLinkAsync(string linkSavePath, string tar
746746
newLink.RunAsAdministrator = runAsAdmin;
747747

748748
newLink.SaveAs(linkSavePath); // Overwrite if exists
749+
750+
// ShowState has to be set after SaveAs has been called, otherwise an UnauthorizedAccessException gets thrown in some cases
751+
newLink.ShowState = showWindowCommand;
752+
749753
return Task.FromResult(true);
750754
}
751755
else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath))

src/Files.App/Utils/Storage/StorageItems/ShellStorageFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System.Runtime.InteropServices.WindowsRuntime;
5+
using Vanara.PInvoke;
56
using Windows.Foundation;
67
using Windows.Storage;
78
using Windows.Storage.FileProperties;
@@ -16,13 +17,15 @@ public sealed class ShortcutStorageFile : ShellStorageFile, IShortcutStorageItem
1617
public string Arguments { get; }
1718
public string WorkingDirectory { get; }
1819
public bool RunAsAdmin { get; }
20+
public ShowWindowCommand ShowWindowCommand { get; set; }
1921

2022
public ShortcutStorageFile(ShellLinkItem item) : base(item)
2123
{
2224
TargetPath = item.TargetPath;
2325
Arguments = item.Arguments;
2426
WorkingDirectory = item.WorkingDirectory;
2527
RunAsAdmin = item.RunAsAdmin;
28+
ShowWindowCommand = item.ShowWindowCommand;
2629
}
2730
}
2831

0 commit comments

Comments
 (0)