Skip to content

Commit 40e288c

Browse files
authored
RichCommand: Open File Location (#12111)
1 parent 74f3262 commit 40e288c

File tree

9 files changed

+79
-44
lines changed

9 files changed

+79
-44
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Commands;
4+
using Files.App.Contexts;
5+
using Files.App.Extensions;
6+
using Files.App.Filesystem;
7+
using Files.App.Helpers;
8+
using Files.App.Views;
9+
using Files.Shared.Enums;
10+
using System;
11+
using System.ComponentModel;
12+
using System.IO;
13+
using System.Threading.Tasks;
14+
15+
namespace Files.App.Actions
16+
{
17+
internal class OpenFileLocationAction : ObservableObject, IAction
18+
{
19+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
20+
21+
public string Label => "OpenFileLocation".GetLocalizedResource();
22+
23+
public string Description => "OpenFileLocationDescription".GetLocalizedResource();
24+
25+
public RichGlyph Glyph => new(baseGlyph: "\uE8DA");
26+
27+
public bool IsExecutable => context.HasSelection && context.SelectedItem is ShortcutItem;
28+
29+
public OpenFileLocationAction()
30+
{
31+
context.PropertyChanged += Context_PropertyChanged;
32+
}
33+
34+
public async Task ExecuteAsync()
35+
{
36+
var item = context.SelectedItem as ShortcutItem;
37+
38+
if (string.IsNullOrWhiteSpace(item?.TargetPath))
39+
return;
40+
41+
// Check if destination path exists
42+
var folderPath = Path.GetDirectoryName(item.TargetPath);
43+
var destFolder = await context.ShellPage?.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);
44+
45+
if (destFolder)
46+
{
47+
context.ShellPage?.NavigateWithArguments(context.ShellPage?.InstanceViewModel.FolderSettings.GetLayoutType(folderPath), new NavigationArguments()
48+
{
49+
NavPathParam = folderPath,
50+
SelectItems = new[] { Path.GetFileName(item.TargetPath.TrimPath()) },
51+
AssociatedTabInstance = context.ShellPage
52+
});
53+
}
54+
else if (destFolder == FileSystemStatusCode.NotFound)
55+
{
56+
await DialogDisplayHelper.ShowDialogAsync("FileNotFoundDialog/Title".GetLocalizedResource(), "FileNotFoundDialog/Text".GetLocalizedResource());
57+
}
58+
else
59+
{
60+
await DialogDisplayHelper.ShowDialogAsync("InvalidItemDialogTitle".GetLocalizedResource(),
61+
string.Format("InvalidItemDialogContent".GetLocalizedResource(), Environment.NewLine, destFolder.ErrorCode.ToString()));
62+
}
63+
}
64+
65+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
66+
{
67+
if (e.PropertyName is nameof(IContentPageContext.HasSelection))
68+
OnPropertyChanged(nameof(IsExecutable));
69+
}
70+
}
71+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public enum CommandCodes
3838
OpenItem,
3939
OpenItemWithApplicationPicker,
4040
OpenParentFolder,
41+
OpenFileLocation,
4142
RefreshItems,
4243
Rename,
4344

src/Files.App/Commands/Manager/CommandManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ internal class CommandManager : ICommandManager
137137
public IRichCommand CloseSelectedTab => commands[CommandCodes.CloseSelectedTab];
138138
public IRichCommand OpenNewPane => commands[CommandCodes.OpenNewPane];
139139
public IRichCommand ClosePane => commands[CommandCodes.ClosePane];
140+
public IRichCommand OpenFileLocation => commands[CommandCodes.OpenFileLocation];
140141
public IRichCommand PlayAll => commands[CommandCodes.PlayAll];
141142

142143
public CommandManager()
@@ -281,6 +282,7 @@ public CommandManager()
281282
[CommandCodes.CloseSelectedTab] = new CloseSelectedTabAction(),
282283
[CommandCodes.OpenNewPane] = new OpenNewPaneAction(),
283284
[CommandCodes.ClosePane] = new ClosePaneAction(),
285+
[CommandCodes.OpenFileLocation] = new OpenFileLocationAction(),
284286
[CommandCodes.PlayAll] = new PlayAllAction(),
285287
};
286288

src/Files.App/Commands/Manager/ICommandManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
4545
IRichCommand OpenItem { get; }
4646
IRichCommand OpenItemWithApplicationPicker { get; }
4747
IRichCommand OpenParentFolder { get; }
48+
IRichCommand OpenFileLocation { get; }
4849
IRichCommand RefreshItems { get; }
4950
IRichCommand Rename { get; }
5051

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
257257
ShowInSearchPage = true,
258258
ShowItem = itemsSelected && showOpenItemWith
259259
},
260-
new ContextMenuFlyoutItemViewModel()
261-
{
262-
Text = "OpenFileLocation".GetLocalizedResource(),
263-
Glyph = "\uE8DA",
264-
Command = commandsViewModel.OpenFileLocationCommand,
265-
ShowItem = itemsSelected && selectedItems.All(i => i.IsShortcut),
266-
ShowInSearchPage = true,
267-
},
260+
new ContextMenuFlyoutItemViewModelBuilder(commands.OpenFileLocation).Build(),
268261
new ContextMenuFlyoutItemViewModel()
269262
{
270263
Text = "OpenInNewTab".GetLocalizedResource(),

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,6 @@ private void OpenPropertiesFromBaseContextMenuFlyout(object sender, object e)
9696
FilePropertiesHelpers.ShowProperties(associatedInstance);
9797
}
9898

99-
public virtual async void OpenFileLocation(RoutedEventArgs e)
100-
{
101-
ShortcutItem item = SlimContentPage.SelectedItem as ShortcutItem;
102-
103-
if (string.IsNullOrWhiteSpace(item?.TargetPath))
104-
return;
105-
106-
// Check if destination path exists
107-
string folderPath = Path.GetDirectoryName(item.TargetPath);
108-
FilesystemResult<StorageFolderWithPath> destFolder = await associatedInstance.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);
109-
110-
if (destFolder)
111-
{
112-
associatedInstance.NavigateWithArguments(associatedInstance.InstanceViewModel.FolderSettings.GetLayoutType(folderPath), new NavigationArguments()
113-
{
114-
NavPathParam = folderPath,
115-
SelectItems = new[] { Path.GetFileName(item.TargetPath.TrimPath()) },
116-
AssociatedTabInstance = associatedInstance
117-
});
118-
}
119-
else if (destFolder == FileSystemStatusCode.NotFound)
120-
{
121-
await DialogDisplayHelper.ShowDialogAsync("FileNotFoundDialog/Title".GetLocalizedResource(), "FileNotFoundDialog/Text".GetLocalizedResource());
122-
}
123-
else
124-
{
125-
await DialogDisplayHelper.ShowDialogAsync("InvalidItemDialogTitle".GetLocalizedResource(),
126-
string.Format("InvalidItemDialogContent".GetLocalizedResource(), Environment.NewLine, destFolder.ErrorCode.ToString()));
127-
}
128-
}
129-
13099
public virtual async void OpenDirectoryInNewTab(RoutedEventArgs e)
131100
{
132101
foreach (ListedItem listedItem in SlimContentPage.SelectedItems)

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public BaseLayoutCommandsViewModel(IBaseLayoutCommandImplementationModel command
2727
private void InitializeCommands()
2828
{
2929
ShowPropertiesCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.ShowProperties);
30-
OpenFileLocationCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenFileLocation);
3130
OpenDirectoryInNewTabCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenDirectoryInNewTab);
3231
OpenDirectoryInNewPaneCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenDirectoryInNewPane);
3332
OpenInNewWindowItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenInNewWindowItem);
@@ -46,8 +45,6 @@ private void InitializeCommands()
4645

4746
public ICommand ShowPropertiesCommand { get; private set; }
4847

49-
public ICommand OpenFileLocationCommand { get; private set; }
50-
5148
public ICommand OpenDirectoryInNewTabCommand { get; private set; }
5249

5350
public ICommand OpenDirectoryInNewPaneCommand { get; private set; }

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
1010
{
1111
void ShowProperties(RoutedEventArgs e);
1212

13-
void OpenFileLocation(RoutedEventArgs e);
14-
1513
void OpenDirectoryInNewTab(RoutedEventArgs e);
1614

1715
void OpenDirectoryInNewPane(RoutedEventArgs e);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,6 +3178,9 @@
31783178
<data name="ShowCheckboxesWhenSelectingItems" xml:space="preserve">
31793179
<value>Show checkboxes when selecting items</value>
31803180
</data>
3181+
<data name="OpenFileLocationDescription" xml:space="preserve">
3182+
<value>Open the item's location</value>
3183+
</data>
31813184
<data name="DeletePermanently" xml:space="preserve">
31823185
<value>Delete permanently</value>
31833186
</data>

0 commit comments

Comments
 (0)