Skip to content

Commit 3e0f70f

Browse files
authored
Add support for creating google docs files from the "new" menu (#8784)
1 parent c554fe1 commit 3e0f70f

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

src/Files.Launcher/Helpers/ShellNewMenuHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ private static async Task<ShellNewEntry> GetShellNewRegistryEntries(RegistryKey
6969
private static async Task<ShellNewEntry> ParseShellNewRegistryEntry(RegistryKey key, RegistryKey root)
7070
{
7171
var valueNames = key.GetValueNames();
72-
if (!valueNames.Contains("NullFile") &&
73-
!valueNames.Contains("ItemName") &&
74-
!valueNames.Contains("FileName"))
72+
if (!valueNames.Contains("NullFile", StringComparer.OrdinalIgnoreCase) &&
73+
!valueNames.Contains("ItemName", StringComparer.OrdinalIgnoreCase) &&
74+
!valueNames.Contains("FileName", StringComparer.OrdinalIgnoreCase) &&
75+
!valueNames.Contains("Command", StringComparer.OrdinalIgnoreCase))
7576
{
7677
return null;
7778
}

src/Files.Uwp/CommandLine/CommandLineParser.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal class CommandLineParser
1010
{
1111
public static ParsedCommands ParseUntrustedCommands(string cmdLineString)
1212
{
13-
var parsedArgs = Parse(SplitArguments(cmdLineString));
13+
var parsedArgs = Parse(SplitArguments(cmdLineString, true));
1414
return ParseSplitArguments(parsedArgs);
1515
}
1616

@@ -76,7 +76,7 @@ private static ParsedCommands ParseSplitArguments(List<KeyValuePair<string, stri
7676
return commands;
7777
}
7878

79-
private static string[] SplitArguments(string commandLine)
79+
public static string[] SplitArguments(string commandLine, bool trimQuotes = false)
8080
{
8181
char[] commandLineCharArray = commandLine.ToCharArray();
8282
bool isInQuote = false;
@@ -94,7 +94,14 @@ private static string[] SplitArguments(string commandLine)
9494
}
9595
}
9696

97-
return new string(commandLineCharArray).Replace("\"", "", StringComparison.Ordinal).Split('\n');
97+
if (trimQuotes)
98+
{
99+
return new string(commandLineCharArray).Replace("\"", "", StringComparison.Ordinal).Split('\n');
100+
}
101+
else
102+
{
103+
return new string(commandLineCharArray).Split('\n');
104+
}
98105
}
99106

100107
public static List<KeyValuePair<string, string>> Parse(string[] args = null)

src/Files.Uwp/Extensions/ShellNewEntryExtensions.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Windows.ApplicationModel.AppService;
1111
using Windows.Foundation.Collections;
1212
using Windows.Storage;
13+
using System.Linq;
1314

1415
namespace Files.Extensions
1516
{
@@ -60,19 +61,39 @@ public static async Task<FilesystemResult<BaseStorageFile>> Create(this ShellNew
6061
var parentFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(filePath));
6162
if (parentFolder)
6263
{
63-
return await Create(shellEntry, parentFolder, Path.GetFileName(filePath));
64+
return await Create(shellEntry, parentFolder, filePath);
6465
}
6566
return new FilesystemResult<BaseStorageFile>(null, parentFolder.ErrorCode);
6667
}
6768

68-
public static async Task<FilesystemResult<BaseStorageFile>> Create(this ShellNewEntry shellEntry, BaseStorageFolder parentFolder, string fileName)
69+
public static async Task<FilesystemResult<BaseStorageFile>> Create(this ShellNewEntry shellEntry, BaseStorageFolder parentFolder, string filePath)
6970
{
7071
FilesystemResult<BaseStorageFile> createdFile = null;
72+
var fileName = Path.GetFileName(filePath);
7173
if (!fileName.EndsWith(shellEntry.Extension, StringComparison.Ordinal))
7274
{
7375
fileName += shellEntry.Extension;
7476
}
75-
if (shellEntry.Template == null)
77+
if (shellEntry.Command != null)
78+
{
79+
var args = CommandLine.CommandLineParser.SplitArguments(shellEntry.Command);
80+
if (args.Any())
81+
{
82+
var connection = await AppServiceConnectionHelper.Instance;
83+
if (connection != null)
84+
{
85+
_ = await connection.SendMessageForResponseAsync(new ValueSet()
86+
{
87+
{ "Arguments", "LaunchApp" },
88+
{ "WorkingDirectory", PathNormalization.GetParentDir(filePath) },
89+
{ "Application", args[0].Replace("\"", "", StringComparison.Ordinal) },
90+
{ "Parameters", string.Join(" ", args.Skip(1)).Replace("%1", filePath) }
91+
});
92+
}
93+
}
94+
createdFile = new FilesystemResult<BaseStorageFile>(null, Shared.Enums.FileSystemStatusCode.Success);
95+
}
96+
else if (shellEntry.Template == null)
7697
{
7798
createdFile = await FilesystemTasks.Wrap(() => parentFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName).AsTask());
7899
}

src/Files.Uwp/Helpers/UIFilesystemHelpers.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,22 @@ public static async Task<IStorageItem> CreateFileFromDialogResultTypeForResult(A
313313
}
314314
}
315315

316-
// Show rename dialog
317-
DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog();
318-
await dialog.ShowAsync();
319-
320-
if (dialog.DynamicResult != DynamicDialogResult.Primary)
316+
// Skip rename dialog when ShellNewEntry has a Command (e.g. ".accdb", ".gdoc")
317+
string userInput = null;
318+
if (itemType != AddItemDialogItemType.File || itemInfo?.Command == null)
321319
{
322-
return null;
320+
DynamicDialog dialog = DynamicDialogFactory.GetFor_RenameDialog();
321+
await dialog.ShowAsync(); // Show rename dialog
322+
323+
if (dialog.DynamicResult != DynamicDialogResult.Primary)
324+
{
325+
return null;
326+
}
327+
328+
userInput = dialog.ViewModel.AdditionalData as string;
323329
}
324330

325331
// Create file based on dialog result
326-
string userInput = dialog.ViewModel.AdditionalData as string;
327332
var folderRes = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(currentPath);
328333
var created = new FilesystemResult<(ReturnResult, IStorageItem)>((ReturnResult.Failed, null), FileSystemStatusCode.Generic);
329334
if (folderRes)

0 commit comments

Comments
 (0)