|
7 | 7 | using System.Reactive.Disposables; |
8 | 8 | using System.Reactive.Linq; |
9 | 9 | using System.Reactive.Subjects; |
| 10 | +using System.Reactive.Threading.Tasks; |
10 | 11 | using System.Threading.Tasks; |
| 12 | +using System.Windows.Input; |
11 | 13 | using GitHub.Extensions; |
12 | 14 | using GitHub.Models; |
13 | 15 | using GitHub.Services; |
@@ -50,7 +52,7 @@ public PullRequestFilesViewModel( |
50 | 52 | DiffFileWithWorkingDirectory = ReactiveCommand.CreateAsyncTask( |
51 | 53 | isBranchCheckedOut, |
52 | 54 | x => (Task)editorService.OpenDiff(pullRequestSession, ((IPullRequestFileNode)x).RelativePath)); |
53 | | - OpenFileInWorkingDirectory = ReactiveCommand.CreateAsyncTask( |
| 55 | + OpenFileInWorkingDirectory = new NonDeletedFileCommand( |
54 | 56 | isBranchCheckedOut, |
55 | 57 | x => (Task)editorService.OpenFile(pullRequestSession, ((IPullRequestFileNode)x).RelativePath, true)); |
56 | 58 |
|
@@ -198,5 +200,37 @@ async Task<IInlineCommentThreadModel> GetFirstCommentThread(IPullRequestFileNode |
198 | 200 |
|
199 | 201 | return threads.FirstOrDefault(); |
200 | 202 | } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Implements the <see cref="OpenFileInWorkingDirectory"/> command. |
| 206 | + /// </summary> |
| 207 | + /// <remarks> |
| 208 | + /// We need to "Open File in Solution" when the parameter passed to the command parameter |
| 209 | + /// represents a deleted file. ReactiveCommand doesn't allow us to change the CanExecute |
| 210 | + /// state depending on the parameter, so we override |
| 211 | + /// <see cref="ICommand.CanExecute(object)"/> to do this ourselves. |
| 212 | + /// </remarks> |
| 213 | + class NonDeletedFileCommand : ReactiveCommand<Unit>, ICommand |
| 214 | + { |
| 215 | + public NonDeletedFileCommand( |
| 216 | + IObservable<bool> canExecute, |
| 217 | + Func<object, Task> executeAsync) |
| 218 | + : base(canExecute, x => executeAsync(x).ToObservable()) |
| 219 | + { |
| 220 | + } |
| 221 | + |
| 222 | + bool ICommand.CanExecute(object parameter) |
| 223 | + { |
| 224 | + if (parameter is IPullRequestFileNode node) |
| 225 | + { |
| 226 | + if (node.Status == PullRequestFileStatus.Removed) |
| 227 | + { |
| 228 | + return false; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + return CanExecute(parameter); |
| 233 | + } |
| 234 | + } |
201 | 235 | } |
202 | 236 | } |
0 commit comments