-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Added experimental support for flattening folders #15992
Merged
Merged
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
250ad3e
Feature: Add support for flattening folders
devin-slothower e8f9117
Merge branch 'files-community:main' into main
devin-slothower 1676208
Add resources to be translated
devin-slothower 6d2403f
Merge branch 'main' of https://github.com/devin-slothower/Files
devin-slothower dbb54bf
Copyright header
devin-slothower 386836b
Delete src/Files.App/Properties/Resources.Designer.cs
devin-slothower 99041e8
Delete src/Files.App/Properties/Resources.resx
devin-slothower 155e20d
Revert changes to resource files
yaira2 b1846c4
Merge branch 'files-community:main' into main
devin-slothower 8e957ca
Code Review: Changed to synchronous execution
devin-slothower 1937828
Code Review: Flatten option defaulted to false
devin-slothower b964919
Code Review: Flyout Icon
devin-slothower f0e9fd2
Cleanup
yaira2 bf9b68a
Update CommandManager.cs
yaira2 4cf6ce5
Update Resources.resw
yaira2 87ec819
Update CommandCodes.cs
yaira2 108bf27
Rename
yaira2 a63a797
Update Resources.resw
yaira2 f6d5b54
Update src/Files.App/Actions/FileSystem/FlattenToRootAction.cs
yaira2 76b73a2
Update IsExecutable
yaira2 136dfa8
Merge actions
yaira2 182b1b4
Update FlattenToRootAction.cs
yaira2 8598d64
Update Resources.resw
yaira2 9c35da2
Update FlattenToRootAction.cs
yaira2 819473d
Update Resources.resw
yaira2 e0d7ec6
Strings
yaira2 52f55d8
Update FlattenFolderAction.cs
yaira2 96d6912
Update Resources.resw
yaira2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/Files.App/Actions/FileSystem/FlattenToRootAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) 2024 Files Community | ||
// Licensed under the MIT License. See the LICENSE. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using System.IO; | ||
using Windows.Storage; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Windows.Foundation.Metadata; | ||
using Files.Shared.Helpers; | ||
|
||
namespace Files.App.Actions | ||
{ | ||
internal sealed class FlattenToRootAction : ObservableObject, IAction | ||
{ | ||
private readonly IContentPageContext context; | ||
private readonly IGeneralSettingsService GeneralSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>(); | ||
|
||
public string Label | ||
=> "FlattenToRoot".GetLocalizedResource(); | ||
|
||
public string Description | ||
=> "FlattenToRootDescription".GetLocalizedResource(); | ||
|
||
public RichGlyph Glyph | ||
=> new(themedIconStyle: "App.ThemedIcons.Folder"); | ||
|
||
public bool IsExecutable => | ||
GeneralSettingsService.ShowFlattenOptions && | ||
context.ShellPage is not null && | ||
context.HasSelection && | ||
context.SelectedItems.Count is 1 && | ||
context.SelectedItem is not null && | ||
context.SelectedItem.PrimaryItemAttribute is StorageItemTypes.Folder; | ||
|
||
public FlattenToRootAction() | ||
{ | ||
context = Ioc.Default.GetRequiredService<IContentPageContext>(); | ||
|
||
context.PropertyChanged += Context_PropertyChanged; | ||
GeneralSettingsService.PropertyChanged += GeneralSettingsService_PropertyChanged; | ||
} | ||
|
||
public async Task ExecuteAsync(object? parameter = null) | ||
{ | ||
if (context.SelectedItem is null) | ||
return; | ||
|
||
var optionsDialog = new ContentDialog() | ||
{ | ||
Title = "FlattenFolderDialogTitle".GetLocalizedResource(), | ||
Content = "FlattenFolderDialogContent".GetLocalizedResource(), | ||
PrimaryButtonText = "Flatten".GetLocalizedResource(), | ||
SecondaryButtonText = "Cancel".GetLocalizedResource(), | ||
DefaultButton = ContentDialogButton.Primary | ||
}; | ||
|
||
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) | ||
optionsDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; | ||
|
||
var result = await optionsDialog.TryShowAsync(); | ||
if (result != ContentDialogResult.Primary) | ||
return; | ||
|
||
FlattenFolder(context.SelectedItem.ItemPath); | ||
} | ||
|
||
private void FlattenFolder(string path) | ||
{ | ||
var containedFolders = Directory.GetDirectories(path); | ||
var containedFiles = Directory.GetFiles(path); | ||
|
||
foreach (var containedFolder in containedFolders) | ||
{ | ||
FlattenFolder(containedFolder); | ||
|
||
var folderName = Path.GetFileName(containedFolder); | ||
var destinationPath = Path.Combine(context?.SelectedItem?.ItemPath ?? string.Empty, folderName); | ||
|
||
if (Directory.Exists(destinationPath)) | ||
continue; | ||
|
||
try | ||
{ | ||
Directory.Move(containedFolder, destinationPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
App.Logger.LogWarning(ex.Message, $"Folder '{folderName}' already exists in the destination folder."); | ||
} | ||
} | ||
|
||
foreach (var containedFile in containedFiles) | ||
{ | ||
var fileName = Path.GetFileName(containedFile); | ||
var destinationPath = Path.Combine(context?.SelectedItem?.ItemPath ?? string.Empty, fileName); | ||
|
||
if (File.Exists(destinationPath)) | ||
continue; | ||
|
||
try | ||
{ | ||
File.Move(containedFile, destinationPath); | ||
} | ||
catch (Exception ex) | ||
{ | ||
App.Logger.LogWarning(ex.Message, $"Failed to move file '{fileName}'."); | ||
} | ||
} | ||
|
||
if (Directory.GetFiles(path).Length == 0 && Directory.GetDirectories(path).Length == 0) | ||
{ | ||
try | ||
{ | ||
Directory.Delete(path); | ||
} | ||
catch (Exception ex) | ||
{ | ||
App.Logger.LogWarning(ex.Message, $"Failed to delete folder '{path}'."); | ||
} | ||
} | ||
} | ||
|
||
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
{ | ||
switch (e.PropertyName) | ||
{ | ||
case nameof(IContentPageContext.HasSelection): | ||
case nameof(IContentPageContext.SelectedItem): | ||
OnPropertyChanged(nameof(IsExecutable)); | ||
break; | ||
} | ||
} | ||
|
||
private void GeneralSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
{ | ||
if (e.PropertyName is nameof(IGeneralSettingsService.ShowFlattenOptions)) | ||
OnPropertyChanged(nameof(IsExecutable)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.