From ccdd35b2310f0614c7793b0e86763a4832c89f54 Mon Sep 17 00:00:00 2001 From: antonioseet Date: Mon, 20 Nov 2023 00:35:45 -0800 Subject: [PATCH 1/5] Allow for files to be dragged and dropped onto python files --- .../Helpers/Navigation/NavigationHelpers.cs | 7 +++++-- .../Helpers/FileExtensionHelpers.cs | 20 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index d6cb86a7e012..53cb0b6b0874 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -407,8 +407,11 @@ private static async Task OpenFile(string path, IShellPage ass // Now launch file with options. var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask()); - if (storageItem is not null) - launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); + if (!FileExtensionHelpers.IsPythonFile(storageItem.Path)) + { + if (storageItem is not null) + launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); + } } if (!launchSuccess) diff --git a/src/Files.Shared/Helpers/FileExtensionHelpers.cs b/src/Files.Shared/Helpers/FileExtensionHelpers.cs index 1bd0230cbeed..d44a412c961b 100644 --- a/src/Files.Shared/Helpers/FileExtensionHelpers.cs +++ b/src/Files.Shared/Helpers/FileExtensionHelpers.cs @@ -34,7 +34,7 @@ public static bool IsImageFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".gif", ".tiff", ".tif"); } - + /// /// Check if the file extension is an audio file. /// @@ -44,7 +44,7 @@ public static bool IsAudioFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".mp3", ".m4a", ".wav", ".wma", ".aac", ".adt", ".adts", ".cda", ".flac"); } - + /// /// Check if the file extension is a video file. /// @@ -144,13 +144,13 @@ public static bool IsShortcutOrUrlFile(string? filePathToCheck) /// /// The file path to check. /// true if the filePathToCheck is an executable file; otherwise, false. - /// /// Executable file types are; exe, bat, cmd + /// /// Executable file types are; exe, bat, cmd, py public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false) { return exeOnly ? HasExtension(filePathToCheck, ".exe") - : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd"); + : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd", ".py"); } /// @@ -173,7 +173,7 @@ public static bool IsVhdFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".vhd", ".vhdx"); } - + /// /// Check if the file extension is a screen saver file. /// @@ -206,5 +206,15 @@ public static bool IsCertificateFile(string? filePathToCheck) { return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx"); } + + /// + /// Check if the file extension is a python file. + /// + /// + /// true if the filePathToCheck is a python file; otherwise, false. + public static bool IsPythonFile(string? filePathToCheck) + { + return HasExtension(filePathToCheck, ".py"); + } } } From 321a3cc11828c949688173b4b72a93618d8a0aac Mon Sep 17 00:00:00 2001 From: antonioseet Date: Mon, 20 Nov 2023 00:41:02 -0800 Subject: [PATCH 2/5] Revert "Allow for files to be dragged and dropped onto python files" This reverts commit ccdd35b2310f0614c7793b0e86763a4832c89f54. --- .../Helpers/Navigation/NavigationHelpers.cs | 7 ++----- .../Helpers/FileExtensionHelpers.cs | 20 +++++-------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index 53cb0b6b0874..d6cb86a7e012 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -407,11 +407,8 @@ private static async Task OpenFile(string path, IShellPage ass // Now launch file with options. var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask()); - if (!FileExtensionHelpers.IsPythonFile(storageItem.Path)) - { - if (storageItem is not null) - launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); - } + if (storageItem is not null) + launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); } if (!launchSuccess) diff --git a/src/Files.Shared/Helpers/FileExtensionHelpers.cs b/src/Files.Shared/Helpers/FileExtensionHelpers.cs index d44a412c961b..1bd0230cbeed 100644 --- a/src/Files.Shared/Helpers/FileExtensionHelpers.cs +++ b/src/Files.Shared/Helpers/FileExtensionHelpers.cs @@ -34,7 +34,7 @@ public static bool IsImageFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".gif", ".tiff", ".tif"); } - + /// /// Check if the file extension is an audio file. /// @@ -44,7 +44,7 @@ public static bool IsAudioFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".mp3", ".m4a", ".wav", ".wma", ".aac", ".adt", ".adts", ".cda", ".flac"); } - + /// /// Check if the file extension is a video file. /// @@ -144,13 +144,13 @@ public static bool IsShortcutOrUrlFile(string? filePathToCheck) /// /// The file path to check. /// true if the filePathToCheck is an executable file; otherwise, false. - /// /// Executable file types are; exe, bat, cmd, py + /// /// Executable file types are; exe, bat, cmd public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false) { return exeOnly ? HasExtension(filePathToCheck, ".exe") - : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd", ".py"); + : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd"); } /// @@ -173,7 +173,7 @@ public static bool IsVhdFile(string? fileExtensionToCheck) { return HasExtension(fileExtensionToCheck, ".vhd", ".vhdx"); } - + /// /// Check if the file extension is a screen saver file. /// @@ -206,15 +206,5 @@ public static bool IsCertificateFile(string? filePathToCheck) { return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx"); } - - /// - /// Check if the file extension is a python file. - /// - /// - /// true if the filePathToCheck is a python file; otherwise, false. - public static bool IsPythonFile(string? filePathToCheck) - { - return HasExtension(filePathToCheck, ".py"); - } } } From 46a692b0359790ec2a025f15e2234786886cee2e Mon Sep 17 00:00:00 2001 From: antonioseet Date: Mon, 20 Nov 2023 14:37:16 -0800 Subject: [PATCH 3/5] Dropping files onto python files executes the python file with the path parameter of files dropped into them. --- .../Helpers/Navigation/NavigationHelpers.cs | 7 +++++-- src/Files.Shared/Helpers/FileExtensionHelpers.cs | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index d6cb86a7e012..53cb0b6b0874 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -407,8 +407,11 @@ private static async Task OpenFile(string path, IShellPage ass // Now launch file with options. var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask()); - if (storageItem is not null) - launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); + if (!FileExtensionHelpers.IsPythonFile(storageItem.Path)) + { + if (storageItem is not null) + launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); + } } if (!launchSuccess) diff --git a/src/Files.Shared/Helpers/FileExtensionHelpers.cs b/src/Files.Shared/Helpers/FileExtensionHelpers.cs index 1bd0230cbeed..519bbe429f28 100644 --- a/src/Files.Shared/Helpers/FileExtensionHelpers.cs +++ b/src/Files.Shared/Helpers/FileExtensionHelpers.cs @@ -144,13 +144,13 @@ public static bool IsShortcutOrUrlFile(string? filePathToCheck) /// /// The file path to check. /// true if the filePathToCheck is an executable file; otherwise, false. - /// /// Executable file types are; exe, bat, cmd + /// /// Executable file types are; exe, bat, cmd, py public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false) { return exeOnly ? HasExtension(filePathToCheck, ".exe") - : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd"); + : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd", ".py"); } /// @@ -206,5 +206,16 @@ public static bool IsCertificateFile(string? filePathToCheck) { return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx"); } + + /// + /// Check if the file extension is a Python file. + /// + /// + /// true if the filePathToCheck is a python file; otherwise, false. + public static bool IsPythonFile(string? filePathToCheck) + { + return HasExtension(filePathToCheck, ".py"); + } + } } From 43e1884cbf15cb1123fdfeec2d328f0944a20a84 Mon Sep 17 00:00:00 2001 From: antonioseet Date: Mon, 20 Nov 2023 21:38:29 -0800 Subject: [PATCH 4/5] remove python files as part of the executables list. --- src/Files.App/Data/Items/ListedItem.cs | 1 + .../Helpers/Navigation/NavigationHelpers.cs | 15 +++++++++++++++ .../Utils/Storage/Operations/FilesystemHelpers.cs | 8 +++++++- .../Storage/Operations/IFilesystemHelpers.cs | 2 +- src/Files.App/Views/LayoutModes/BaseLayout.cs | 6 +++--- src/Files.Shared/Helpers/FileExtensionHelpers.cs | 4 ++-- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Files.App/Data/Items/ListedItem.cs b/src/Files.App/Data/Items/ListedItem.cs index f3a95ef8671d..bc2c64babe73 100644 --- a/src/Files.App/Data/Items/ListedItem.cs +++ b/src/Files.App/Data/Items/ListedItem.cs @@ -374,6 +374,7 @@ public override string ToString() public bool IsAlternateStream => this is AlternateStreamItem; public bool IsGitItem => this is GitItem; public virtual bool IsExecutable => FileExtensionHelpers.IsExecutableFile(ItemPath); + public virtual bool IsPythonFile => FileExtensionHelpers.IsPythonFile(ItemPath); public bool IsPinned => App.QuickAccessManager.Model.FavoriteItems.Contains(itemPath); public bool IsDriveRoot => ItemPath == PathNormalization.GetPathRoot(ItemPath); public bool IsElevated => CheckElevationRights(); diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index 53cb0b6b0874..a68f6c79c0a3 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -104,6 +104,21 @@ public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInsta } } + public static async Task OpenItemsWithPythonAsync(IShellPage associatedInstance, IEnumerable items, string pythonScriptPath) + { + // Don't open files and folders inside recycle bin + if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || + associatedInstance.SlimContentPage is null) + { + return; + } + + foreach (var item in items) + { + await Win32Helpers.InvokeWin32ComponentAsync(pythonScriptPath, associatedInstance, arguments: $"\"{item.Path}\""); + } + } + /// /// Navigates to a directory or opens file /// diff --git a/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs b/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs index e9b0a3109b35..8f4650c4dbb2 100644 --- a/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs +++ b/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs @@ -231,7 +231,8 @@ public async Task PerformOperationTypeAsync( string destination, bool showDialog, bool registerHistory, - bool isTargetExecutable = false) + bool isTargetExecutable = false, + bool isTargetPythonFile = false) { try { @@ -259,6 +260,11 @@ public async Task PerformOperationTypeAsync( var items = await GetDraggedStorageItems(packageView); NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination); return ReturnResult.Success; + }else if(isTargetPythonFile) + { + var items = await GetDraggedStorageItems(packageView); + NavigationHelpers.OpenItemsWithPythonAsync(associatedInstance, items, destination); + return ReturnResult.Success; } else { diff --git a/src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs b/src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs index 083e323b4c2f..204e592b7434 100644 --- a/src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs +++ b/src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs @@ -114,7 +114,7 @@ public interface IFilesystemHelpers : IDisposable /// The is NOT fullPath /// Determines whether is saved /// of performed operation - Task PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false); + Task PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false, bool isDestinationPython = false); #region Copy diff --git a/src/Files.App/Views/LayoutModes/BaseLayout.cs b/src/Files.App/Views/LayoutModes/BaseLayout.cs index 688c8159dbce..e20d0eca8f7c 100644 --- a/src/Files.App/Views/LayoutModes/BaseLayout.cs +++ b/src/Files.App/Views/LayoutModes/BaseLayout.cs @@ -1032,7 +1032,7 @@ private async void Item_DragOver(object sender, DragEventArgs e) { e.DragUIOverride.IsCaptionVisible = true; - if (item.IsExecutable) + if (item.IsExecutable || item.IsPythonFile) { e.DragUIOverride.Caption = $"{"OpenWith".GetLocalizedResource()} {item.Name}"; e.AcceptedOperation = DataPackageOperation.Link; @@ -1110,7 +1110,7 @@ private async void Item_Drop(object sender, DragEventArgs e) var item = GetItemFromElement(sender); if (item is not null) - await ParentShellPageInstance!.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (item as ShortcutItem)?.TargetPath ?? item.ItemPath, false, true, item.IsExecutable); + await ParentShellPageInstance!.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (item as ShortcutItem)?.TargetPath ?? item.ItemPath, false, true, item.IsExecutable, item.IsPythonFile); deferral.Complete(); } @@ -1256,7 +1256,7 @@ protected void InitializeDrag(UIElement container, ListedItem item) return; UninitializeDrag(container); - if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable) + if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable || item.IsPythonFile) { container.AllowDrop = true; container.AddHandler(UIElement.DragOverEvent, Item_DragOverEventHandler, true); diff --git a/src/Files.Shared/Helpers/FileExtensionHelpers.cs b/src/Files.Shared/Helpers/FileExtensionHelpers.cs index 519bbe429f28..99a7ef93367e 100644 --- a/src/Files.Shared/Helpers/FileExtensionHelpers.cs +++ b/src/Files.Shared/Helpers/FileExtensionHelpers.cs @@ -144,13 +144,13 @@ public static bool IsShortcutOrUrlFile(string? filePathToCheck) /// /// The file path to check. /// true if the filePathToCheck is an executable file; otherwise, false. - /// /// Executable file types are; exe, bat, cmd, py + /// /// Executable file types are; exe, bat, cmd public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false) { return exeOnly ? HasExtension(filePathToCheck, ".exe") - : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd", ".py"); + : HasExtension(filePathToCheck, ".exe", ".bat", ".cmd"); } /// From 1478f87197b293b65919fd535685c5c98d4123b0 Mon Sep 17 00:00:00 2001 From: antonioseet Date: Tue, 21 Nov 2023 11:55:30 -0800 Subject: [PATCH 5/5] applied suggestions --- src/Files.App/Helpers/Navigation/NavigationHelpers.cs | 11 ++++------- .../Utils/Storage/Operations/FilesystemHelpers.cs | 3 ++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs index a68f6c79c0a3..e718cb75fc5f 100644 --- a/src/Files.App/Helpers/Navigation/NavigationHelpers.cs +++ b/src/Files.App/Helpers/Navigation/NavigationHelpers.cs @@ -83,7 +83,7 @@ public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, b public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInstance, IEnumerable items, string executable) { - // Don't open files and folders inside recycle bin + // Don't open files and folders inside recycle bin if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || associatedInstance.SlimContentPage is null) { @@ -106,7 +106,7 @@ public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInsta public static async Task OpenItemsWithPythonAsync(IShellPage associatedInstance, IEnumerable items, string pythonScriptPath) { - // Don't open files and folders inside recycle bin + // Don't open files and folders inside recycle bin if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) || associatedInstance.SlimContentPage is null) { @@ -422,11 +422,8 @@ private static async Task OpenFile(string path, IShellPage ass // Now launch file with options. var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask()); - if (!FileExtensionHelpers.IsPythonFile(storageItem.Path)) - { - if (storageItem is not null) - launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); - } + if (storageItem is not null) + launchSuccess = await Launcher.LaunchFileAsync(storageItem, options); } if (!launchSuccess) diff --git a/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs b/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs index 8f4650c4dbb2..22fc6d54b7fd 100644 --- a/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs +++ b/src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs @@ -260,7 +260,8 @@ public async Task PerformOperationTypeAsync( var items = await GetDraggedStorageItems(packageView); NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination); return ReturnResult.Success; - }else if(isTargetPythonFile) + } + else if (isTargetPythonFile) { var items = await GetDraggedStorageItems(packageView); NavigationHelpers.OpenItemsWithPythonAsync(associatedInstance, items, destination);