|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Files.App.Actions |
| 5 | +{ |
| 6 | + internal sealed partial class OpenInIDEAction : ObservableObject, IAction |
| 7 | + { |
| 8 | + private readonly IDevToolsSettingsService _devToolsSettingsService; |
| 9 | + |
| 10 | + private readonly IContentPageContext _context; |
| 11 | + |
| 12 | + public string Label |
| 13 | + => string.Format( |
| 14 | + "OpenInIDE".GetLocalizedResource(), |
| 15 | + _devToolsSettingsService.IDEName); |
| 16 | + |
| 17 | + public string Description |
| 18 | + => string.Format( |
| 19 | + "OpenInIDEDescription".GetLocalizedResource(), |
| 20 | + _devToolsSettingsService.IDEName); |
| 21 | + |
| 22 | + public bool IsExecutable => |
| 23 | + _context.Folder is not null && |
| 24 | + !string.IsNullOrWhiteSpace(_devToolsSettingsService.IDEPath); |
| 25 | + |
| 26 | + public OpenInIDEAction() |
| 27 | + { |
| 28 | + _devToolsSettingsService = Ioc.Default.GetRequiredService<IDevToolsSettingsService>(); |
| 29 | + _context = Ioc.Default.GetRequiredService<IContentPageContext>(); |
| 30 | + _context.PropertyChanged += Context_PropertyChanged; |
| 31 | + _devToolsSettingsService.PropertyChanged += DevSettings_PropertyChanged; |
| 32 | + } |
| 33 | + |
| 34 | + public async Task ExecuteAsync(object? parameter = null) |
| 35 | + { |
| 36 | + var res = await Win32Helper.RunPowershellCommandAsync( |
| 37 | + $"& \'{_devToolsSettingsService.IDEPath}\' \'{_context.ShellPage?.ShellViewModel.WorkingDirectory}\'", |
| 38 | + PowerShellExecutionOptions.Hidden |
| 39 | + ); |
| 40 | + |
| 41 | + if (!res) |
| 42 | + await DynamicDialogFactory.ShowFor_IDEErrorDialog(_devToolsSettingsService.IDEName); |
| 43 | + } |
| 44 | + |
| 45 | + private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 46 | + { |
| 47 | + if (e.PropertyName == nameof(IContentPageContext.Folder)) |
| 48 | + OnPropertyChanged(nameof(IsExecutable)); |
| 49 | + } |
| 50 | + |
| 51 | + private void DevSettings_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 52 | + { |
| 53 | + if (e.PropertyName == nameof(IDevToolsSettingsService.IDEPath)) |
| 54 | + { |
| 55 | + OnPropertyChanged(nameof(IsExecutable)); |
| 56 | + } |
| 57 | + else if (e.PropertyName == nameof(IDevToolsSettingsService.IDEName)) |
| 58 | + { |
| 59 | + OnPropertyChanged(nameof(Label)); |
| 60 | + OnPropertyChanged(nameof(Description)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments