Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 4f2d950

Browse files
committed
Open changed file when double clicked.
Only very basic implementation: file is always opened irrespective of what the `OpenChangedFileAction` is and whether the local file is up-to-date. This needs fleshing out.
1 parent 2c87346 commit 4f2d950

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

src/GitHub.App/SampleData/PullRequestDetailViewModelDesigner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public PullRequestDetailViewModelDesigner()
7373

7474
public ReactiveCommand<Unit> Checkout { get; }
7575
public ReactiveCommand<object> OpenOnGitHub { get; }
76+
public ReactiveCommand<object> OpenFile { get; }
7677
public ReactiveCommand<object> ToggleChangedFilesView { get; }
7778
public ReactiveCommand<object> ToggleOpenChangedFileAction { get; }
79+
public ReactiveCommand<object> ViewOpenFile { get; }
7880
}
7981
}

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public PullRequestDetailViewModel(
9797
Checkout = ReactiveCommand.CreateAsyncObservable(canCheckout, DoCheckout);
9898

9999
OpenOnGitHub = ReactiveCommand.Create();
100+
OpenFile = ReactiveCommand.Create();
101+
OpenFile.Subscribe(x => DoOpenFile((IPullRequestFileViewModel)x));
100102

101103
ToggleChangedFilesView = ReactiveCommand.Create();
102104
ToggleChangedFilesView.Subscribe(_ =>
@@ -111,6 +113,8 @@ public PullRequestDetailViewModel(
111113
OpenChangedFileAction = OpenChangedFileAction == OpenChangedFileAction.Diff ?
112114
OpenChangedFileAction.Open : OpenChangedFileAction.Diff;
113115
});
116+
117+
ViewOpenFile = ReactiveCommand.Create();
114118
}
115119

116120
/// <summary>
@@ -269,6 +273,16 @@ public string CheckoutDisabledMessage
269273
/// </summary>
270274
public ReactiveCommand<object> OpenOnGitHub { get; }
271275

276+
/// <summary>
277+
/// Gets a command that opens the specified file according to <see cref="ChangedFilesView"/>.
278+
/// </summary>
279+
/// <remarks>
280+
/// An <see cref="IPullRequestFileViewModel"/> should be passed as the parameter to this command's
281+
/// Execute method. It triggers <see cref="ViewOpenFile"/> or <see cref="ViewCompareFiles"/> which
282+
/// should be handled by the view to implement opening or comparing the file.
283+
/// </remarks>
284+
public ReactiveCommand<object> OpenFile { get; }
285+
272286
/// <summary>
273287
/// Gets a command that toggles the <see cref="ChangedFilesView"/> property.
274288
/// </summary>
@@ -279,6 +293,14 @@ public string CheckoutDisabledMessage
279293
/// </summary>
280294
public ReactiveCommand<object> ToggleOpenChangedFileAction { get; }
281295

296+
/// <summary>
297+
/// Gets a command that informs the view that the specified file should be opened.
298+
/// </summary>
299+
/// <remarks>
300+
/// The parameter passed is a string detailing the full path to the file.
301+
/// </remarks>
302+
public ReactiveCommand<object> ViewOpenFile { get; }
303+
282304
/// <summary>
283305
/// Initializes the view model with new data.
284306
/// </summary>
@@ -491,5 +513,12 @@ IObservable<Unit> DoCheckout(object unused)
491513
return Observable.Empty<Unit>();
492514
});
493515
}
516+
517+
void DoOpenFile(IPullRequestFileViewModel x)
518+
{
519+
// TODO: Handle diffing files accoring to OpenChangedFileAction and getting a file from another branch etc.
520+
var fullPath = Path.Combine(repository.LocalPath, x.Path, x.FileName);
521+
ViewOpenFile.Execute(fullPath);
522+
}
494523
}
495524
}

src/GitHub.Exports.Reactive/ViewModels/IPullRequestDetailViewModel.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
176176
/// </summary>
177177
ReactiveCommand<object> OpenOnGitHub { get; }
178178

179+
/// <summary>
180+
/// Gets a command that opens the specified file according to <see cref="ChangedFilesView"/>.
181+
/// </summary>
182+
/// <remarks>
183+
/// An <see cref="IPullRequestFileViewModel"/> should be passed as the parameter to this command's
184+
/// Execute method. It triggers <see cref="ViewOpenFile"/> or <see cref="ViewCompareFiles"/> which
185+
/// should be handled by the view to implement opening or comparing the file.
186+
/// </remarks>
187+
ReactiveCommand<object> OpenFile { get; }
188+
179189
/// <summary>
180190
/// Gets a command that toggles the <see cref="ChangedFilesView"/> property.
181191
/// </summary>
@@ -185,5 +195,13 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
185195
/// Gets a command that toggles the <see cref="OpenChangedFileAction"/> property.
186196
/// </summary>
187197
ReactiveCommand<object> ToggleOpenChangedFileAction { get; }
198+
199+
/// <summary>
200+
/// Gets a command that informs the view that the specified file should be opened.
201+
/// </summary>
202+
/// <remarks>
203+
/// The parameter passed is a string detailing the full path to the file.
204+
/// </remarks>
205+
ReactiveCommand<object> ViewOpenFile { get; }
188206
}
189207
}

src/GitHub.VisualStudio/UI/Views/PullRequestDetailView.xaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Background="{DynamicResource GitHubVsToolWindowBackground}"
1414
DataContext="{Binding ViewModel}"
1515
d:DesignWidth="356"
16-
d:DesignHeight="640"
16+
d:DesignHeight="780"
1717
mc:Ignorable="d"
1818
Style="{DynamicResource BusyStateView}">
1919
<d:DesignProperties.DataContext>
@@ -400,7 +400,8 @@
400400
<TreeView ItemsSource="{Binding ChangedFilesTree}"
401401
Background="Transparent"
402402
BorderThickness="0"
403-
Margin="0 6 0 0">
403+
Margin="0 6 0 0"
404+
MouseDoubleClick="FileListMouseDoubleClick">
404405
<TreeView.Style>
405406
<Style TargetType="TreeView">
406407
<Style.Triggers>
@@ -422,13 +423,13 @@
422423
<StackPanel Orientation="Horizontal">
423424
<!-- TODO: Use the right icon here -->
424425
<ui:OcticonImage Icon="briefcase" Foreground="{DynamicResource GitHubVsWindowText}"/>
425-
<TextBlock Text="{Binding DirectoryName}" Margin="4 0" VerticalAlignment="Center"/>
426+
<TextBlock Text="{Binding DirectoryName}" Margin="4 2" VerticalAlignment="Center"/>
426427
</StackPanel>
427428
</HierarchicalDataTemplate>
428429
<DataTemplate DataType="{x:Type vm:PullRequestFileViewModel}">
429430
<StackPanel Orientation="Horizontal">
430431
<ui:OcticonImage Icon="file_code" Foreground="{DynamicResource GitHubVsWindowText}"/>
431-
<TextBlock Text="{Binding FileName}" Margin="4 0" VerticalAlignment="Center"/>
432+
<TextBlock Text="{Binding FileName}" Margin="4 2" VerticalAlignment="Center"/>
432433
</StackPanel>
433434
</DataTemplate>
434435
</TreeView.Resources>
@@ -438,7 +439,8 @@
438439
<ListView ItemsSource="{Binding ChangedFilesList}"
439440
Background="Transparent"
440441
BorderThickness="0"
441-
Margin="0 6 0 0" >
442+
Margin="0 6 0 0"
443+
MouseDoubleClick="FileListMouseDoubleClick">
442444
<ListView.Style>
443445
<Style TargetType="ListView">
444446
<Style.Triggers>

src/GitHub.VisualStudio/UI/Views/PullRequestDetailView.xaml.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public PullRequestDetailView()
4040
this.WhenActivated(d =>
4141
{
4242
d(ViewModel.OpenOnGitHub.Subscribe(_ => DoOpenOnGitHub()));
43+
d(ViewModel.ViewOpenFile.Subscribe(x => DoOpenFile((string)x)));
4344
});
4445

4546
OpenChangesOptionsMenu = ReactiveCommand.Create();
@@ -109,5 +110,20 @@ void DoOpenChangesOptionsMenu(dynamic o)
109110
menu.VerticalOffset = o.MenuY;
110111
menu.IsOpen = true;
111112
}
113+
114+
void DoOpenFile(string fileName)
115+
{
116+
Services.Dte.ItemOperations.OpenFile(fileName);
117+
}
118+
119+
private void FileListMouseDoubleClick(object sender, MouseButtonEventArgs e)
120+
{
121+
var file = (e.OriginalSource as FrameworkElement)?.DataContext as IPullRequestFileViewModel;
122+
123+
if (file != null)
124+
{
125+
ViewModel.OpenFile.Execute(file);
126+
}
127+
}
112128
}
113129
}

0 commit comments

Comments
 (0)