|
| 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 | +} |
0 commit comments