|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +namespace Files.App.Actions |
| 5 | +{ |
| 6 | + internal sealed class PasteItemAsShortcutAction : ObservableObject, IAction |
| 7 | + { |
| 8 | + private readonly IContentPageContext context; |
| 9 | + |
| 10 | + public string Label |
| 11 | + => Strings.PasteShortcut.GetLocalizedResource(); |
| 12 | + |
| 13 | + public string Description |
| 14 | + => Strings.PasteShortcutDescription.GetLocalizedResource(); |
| 15 | + |
| 16 | + public RichGlyph Glyph |
| 17 | + => new(themedIconStyle: "App.ThemedIcons.Paste"); |
| 18 | + |
| 19 | + public bool IsExecutable |
| 20 | + => GetIsExecutable(); |
| 21 | + |
| 22 | + public PasteItemAsShortcutAction() |
| 23 | + { |
| 24 | + context = Ioc.Default.GetRequiredService<IContentPageContext>(); |
| 25 | + |
| 26 | + context.PropertyChanged += Context_PropertyChanged; |
| 27 | + App.AppModel.PropertyChanged += AppModel_PropertyChanged; |
| 28 | + } |
| 29 | + |
| 30 | + public Task ExecuteAsync(object? parameter = null) |
| 31 | + { |
| 32 | + if (context.ShellPage is null) |
| 33 | + return Task.CompletedTask; |
| 34 | + |
| 35 | + string path = context.ShellPage.ShellViewModel.WorkingDirectory; |
| 36 | + return UIFilesystemHelpers.PasteItemAsShortcutAsync(path, context.ShellPage); |
| 37 | + } |
| 38 | + |
| 39 | + public bool GetIsExecutable() |
| 40 | + { |
| 41 | + return |
| 42 | + App.AppModel.IsPasteEnabled && |
| 43 | + context.PageType != ContentPageTypes.Home && |
| 44 | + context.PageType != ContentPageTypes.RecycleBin && |
| 45 | + context.PageType != ContentPageTypes.SearchResults; |
| 46 | + } |
| 47 | + |
| 48 | + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 49 | + { |
| 50 | + if (e.PropertyName is nameof(IContentPageContext.PageType)) |
| 51 | + OnPropertyChanged(nameof(IsExecutable)); |
| 52 | + } |
| 53 | + |
| 54 | + private void AppModel_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 55 | + { |
| 56 | + if (e.PropertyName is nameof(AppModel.IsPasteEnabled)) |
| 57 | + OnPropertyChanged(nameof(IsExecutable)); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments