Skip to content

Commit 7e67dc4

Browse files
authored
Feature: Added an action to copy the path of the current directory (#16406)
1 parent 5672540 commit 7e67dc4

File tree

9 files changed

+140
-21
lines changed

9 files changed

+140
-21
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Windows.ApplicationModel.DataTransfer;
5+
6+
namespace Files.App.Actions
7+
{
8+
internal sealed class CopyItemPathAction : IAction
9+
{
10+
private readonly IContentPageContext context;
11+
12+
public string Label
13+
=> Strings.CopyPath.GetLocalizedResource();
14+
15+
public string Description
16+
=> Strings.CopyItemPathDescription.GetLocalizedResource();
17+
18+
public RichGlyph Glyph
19+
=> new RichGlyph(themedIconStyle: "App.ThemedIcons.CopyAsPath");
20+
21+
public HotKey HotKey
22+
=> new(Keys.C, KeyModifiers.CtrlShift);
23+
24+
public bool IsExecutable
25+
=> context.HasSelection;
26+
27+
public CopyItemPathAction()
28+
{
29+
context = Ioc.Default.GetRequiredService<IContentPageContext>();
30+
}
31+
32+
public Task ExecuteAsync(object? parameter = null)
33+
{
34+
if (context.ShellPage?.SlimContentPage is not null)
35+
{
36+
var path = context.ShellPage.SlimContentPage.SelectedItems is not null
37+
? context.ShellPage.SlimContentPage.SelectedItems.Select(x => x.ItemPath).Aggregate((accum, current) => accum + "\n" + current)
38+
: context.ShellPage.ShellViewModel.WorkingDirectory;
39+
40+
if (FtpHelpers.IsFtpPath(path))
41+
path = path.Replace("\\", "/", StringComparison.Ordinal);
42+
43+
SafetyExtensions.IgnoreExceptions(() =>
44+
{
45+
DataPackage data = new();
46+
data.SetText(path);
47+
48+
Clipboard.SetContent(data);
49+
Clipboard.Flush();
50+
});
51+
}
52+
53+
return Task.CompletedTask;
54+
}
55+
}
56+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Windows.ApplicationModel.DataTransfer;
5+
6+
namespace Files.App.Actions
7+
{
8+
internal sealed class CopyItemPathWithQuotesAction : IAction
9+
{
10+
private readonly IContentPageContext context;
11+
12+
public string Label
13+
=> Strings.CopyItemPathWithQuotes.GetLocalizedResource();
14+
15+
public string Description
16+
=> Strings.CopyItemPathWithQuotesDescription.GetLocalizedResource();
17+
18+
public RichGlyph Glyph
19+
=> new RichGlyph(themedIconStyle: "App.ThemedIcons.CopyAsPath");
20+
21+
public HotKey HotKey
22+
=> new(Keys.C, KeyModifiers.CtrlAlt);
23+
24+
public bool IsExecutable
25+
=> context.HasSelection;
26+
27+
public CopyItemPathWithQuotesAction()
28+
{
29+
context = Ioc.Default.GetRequiredService<IContentPageContext>();
30+
}
31+
32+
public Task ExecuteAsync(object? parameter = null)
33+
{
34+
if (context.ShellPage?.SlimContentPage is not null)
35+
{
36+
var selectedItems = context.ShellPage.SlimContentPage.SelectedItems;
37+
var path = selectedItems is not null
38+
? string.Join("\n", selectedItems.Select(item => $"\"{item.ItemPath}\""))
39+
: context.ShellPage.ShellViewModel.WorkingDirectory;
40+
41+
if (FtpHelpers.IsFtpPath(path))
42+
path = path.Replace("\\", "/", StringComparison.Ordinal);
43+
44+
SafetyExtensions.IgnoreExceptions(() =>
45+
{
46+
DataPackage data = new();
47+
data.SetText(path);
48+
49+
Clipboard.SetContent(data);
50+
Clipboard.Flush();
51+
});
52+
}
53+
54+
return Task.CompletedTask;
55+
}
56+
}
57+
}

src/Files.App/Actions/FileSystem/CopyPathAction.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ internal sealed class CopyPathAction : IAction
1010
private readonly IContentPageContext context;
1111

1212
public string Label
13-
=> "CopyPath".GetLocalizedResource();
13+
=> Strings.CopyPath.GetLocalizedResource();
1414

1515
public string Description
16-
=> "CopyPathDescription".GetLocalizedResource();
16+
=> Strings.CopyPathDescription.GetLocalizedResource();
1717

1818
public RichGlyph Glyph
1919
=> new RichGlyph(themedIconStyle: "App.ThemedIcons.CopyAsPath");
2020

21-
public HotKey HotKey
22-
=> new(Keys.C, KeyModifiers.CtrlShift);
23-
2421
public bool IsExecutable
25-
=> context.HasSelection;
22+
=> context.PageType != ContentPageTypes.Home && context.PageType != ContentPageTypes.RecycleBin;
2623

2724
public CopyPathAction()
2825
{
@@ -33,9 +30,7 @@ public Task ExecuteAsync(object? parameter = null)
3330
{
3431
if (context.ShellPage?.SlimContentPage is not null)
3532
{
36-
var path = context.ShellPage.SlimContentPage.SelectedItems is not null
37-
? context.ShellPage.SlimContentPage.SelectedItems.Select(x => x.ItemPath).Aggregate((accum, current) => accum + "\n" + current)
38-
: context.ShellPage.ShellViewModel.WorkingDirectory;
33+
var path = context.ShellPage.ShellViewModel.WorkingDirectory;
3934

4035
if (FtpHelpers.IsFtpPath(path))
4136
path = path.Replace("\\", "/", StringComparison.Ordinal);

src/Files.App/Actions/FileSystem/CopyPathWithQuotesAction.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ internal sealed class CopyPathWithQuotesAction : IAction
1010
private readonly IContentPageContext context;
1111

1212
public string Label
13-
=> "CopyPathWithQuotes".GetLocalizedResource();
13+
=> Strings.CopyPathWithQuotes.GetLocalizedResource();
1414

1515
public string Description
16-
=> "CopyPathWithQuotesDescription".GetLocalizedResource();
16+
=> Strings.CopyPathWithQuotesDescription.GetLocalizedResource();
1717

1818
public RichGlyph Glyph
1919
=> new RichGlyph(themedIconStyle: "App.ThemedIcons.CopyAsPath");
2020

21-
public HotKey HotKey
22-
=> new(Keys.C, KeyModifiers.CtrlAlt);
23-
2421
public bool IsExecutable
25-
=> context.HasSelection;
22+
=> context.PageType != ContentPageTypes.Home && context.PageType != ContentPageTypes.RecycleBin;
2623

2724
public CopyPathWithQuotesAction()
2825
{
@@ -33,10 +30,7 @@ public Task ExecuteAsync(object? parameter = null)
3330
{
3431
if (context.ShellPage?.SlimContentPage is not null)
3532
{
36-
var selectedItems = context.ShellPage.SlimContentPage.SelectedItems;
37-
var path = selectedItems is not null
38-
? string.Join("\n", selectedItems.Select(item => $"\"{item.ItemPath}\""))
39-
: context.ShellPage.ShellViewModel.WorkingDirectory;
33+
var path = "\"" + context.ShellPage.ShellViewModel.WorkingDirectory + "\"";
4034

4135
if (FtpHelpers.IsFtpPath(path))
4236
path = path.Replace("\\", "/", StringComparison.Ordinal);

src/Files.App/Data/Commands/Manager/CommandCodes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public enum CommandCodes
2929

3030
// File System
3131
CopyItem,
32+
CopyItemPath,
3233
CopyPath,
34+
CopyItemPathWithQuotes,
3335
CopyPathWithQuotes,
3436
CutItem,
3537
PasteItem,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public IRichCommand this[HotKey hotKey]
8181
public IRichCommand SetAsLockscreenBackground => commands[CommandCodes.SetAsLockscreenBackground];
8282
public IRichCommand SetAsAppBackground => commands[CommandCodes.SetAsAppBackground];
8383
public IRichCommand CopyItem => commands[CommandCodes.CopyItem];
84+
public IRichCommand CopyItemPath => commands[CommandCodes.CopyItemPath];
8485
public IRichCommand CopyPath => commands[CommandCodes.CopyPath];
86+
public IRichCommand CopyItemPathWithQuotes => commands[CommandCodes.CopyItemPathWithQuotes];
8587
public IRichCommand CopyPathWithQuotes => commands[CommandCodes.CopyPathWithQuotes];
8688
public IRichCommand CutItem => commands[CommandCodes.CutItem];
8789
public IRichCommand PasteItem => commands[CommandCodes.PasteItem];
@@ -273,7 +275,9 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
273275
[CommandCodes.SetAsLockscreenBackground] = new SetAsLockscreenBackgroundAction(),
274276
[CommandCodes.SetAsAppBackground] = new SetAsAppBackgroundAction(),
275277
[CommandCodes.CopyItem] = new CopyItemAction(),
278+
[CommandCodes.CopyItemPath] = new CopyItemPathAction(),
276279
[CommandCodes.CopyPath] = new CopyPathAction(),
280+
[CommandCodes.CopyItemPathWithQuotes] = new CopyItemPathWithQuotesAction(),
277281
[CommandCodes.CopyPathWithQuotes] = new CopyPathWithQuotesAction(),
278282
[CommandCodes.CutItem] = new CutItemAction(),
279283
[CommandCodes.PasteItem] = new PasteItemAction(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public interface ICommandManager : IEnumerable<IRichCommand>
3333
IRichCommand ToggleToolbar { get; }
3434

3535
IRichCommand CopyItem { get; }
36+
IRichCommand CopyItemPath { get; }
3637
IRichCommand CopyPath { get; }
38+
IRichCommand CopyItemPathWithQuotes { get; }
3739
IRichCommand CopyPathWithQuotes { get; }
3840
IRichCommand CutItem { get; }
3941
IRichCommand PasteItem { get; }

src/Files.App/Data/Factories/ContentPageContextFlyoutFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
459459
IsPrimary = true,
460460
IsVisible = true,
461461
}.Build(),
462-
new ContextMenuFlyoutItemViewModelBuilder(Commands.CopyPath)
462+
new ContextMenuFlyoutItemViewModelBuilder(Commands.CopyItemPath)
463463
{
464464
IsVisible = UserSettingsService.GeneralSettingsService.ShowCopyPath
465465
&& itemsSelected

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
<data name="CopyPathWithQuotes" xml:space="preserve">
127127
<value>Copy path with quotes</value>
128128
</data>
129+
<data name="CopyItemPathWithQuotes" xml:space="preserve">
130+
<value>Copy selected item path with quotes</value>
131+
</data>
129132
<data name="Browse" xml:space="preserve">
130133
<value>Browse</value>
131134
</data>
@@ -2391,11 +2394,17 @@
23912394
<value>Copy item(s) to clipboard</value>
23922395
</data>
23932396
<data name="CopyPathDescription" xml:space="preserve">
2397+
<value>Copy path of the current directory to the clipboard</value>
2398+
</data>
2399+
<data name="CopyItemPathDescription" xml:space="preserve">
23942400
<value>Copy path of selected items to the clipboard</value>
23952401
</data>
2396-
<data name="CopyPathWithQuotesDescription" xml:space="preserve">
2402+
<data name="CopyItemPathWithQuotesDescription" xml:space="preserve">
23972403
<value>Copy path of selected items with quotes to the clipboard</value>
23982404
</data>
2405+
<data name="CopyPathWithQuotesDescription" xml:space="preserve">
2406+
<value>Copy path of the current directory with quotes to the clipboard</value>
2407+
</data>
23992408
<data name="CutItemDescription" xml:space="preserve">
24002409
<value>Cut item(s) to clipboard</value>
24012410
</data>

0 commit comments

Comments
 (0)