Skip to content

Commit 9dbebd7

Browse files
author
heku
committed
Add preview window support
1 parent 9885545 commit 9dbebd7

17 files changed

Lines changed: 60 additions & 40 deletions

Kool.VsDiff.Linked/VSPackage.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Kool.VsDiff.Linked/VSPackage.en.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
<data name="OptionsPage_EnableDiffClipboardWithFile" xml:space="preserve">
146146
<value>Enable compare Clipboard content with selected file.</value>
147147
</data>
148+
<data name="OptionsPage_PreferToUsePreviewWindow" xml:space="preserve">
149+
<value>Prefer to use preview window (if supports).</value>
150+
</data>
148151
<data name="OptionsPage_TestButtonContent" xml:space="preserve">
149152
<value>TEST</value>
150153
</data>

Kool.VsDiff.Linked/VSPackage.zh-Hans.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@
145145
<data name="OptionsPage_EnableDiffClipboardWithFile" xml:space="preserve">
146146
<value>启用选中文件与剪贴板内容的比较功能</value>
147147
</data>
148+
<data name="OptionsPage_PreferToUsePreviewWindow" xml:space="preserve">
149+
<value>优先使用预览窗口 (如果支持的话)</value>
150+
</data>
148151
<data name="OptionsPage_TestButtonContent" xml:space="preserve">
149152
<value>测试</value>
150153
</data>

Kool.VsDiff.Shared/Kool.VsDiff.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="$(MSBuildThisFileDirectory)Commands\DiffClipboardWithFileCommand.cs" />
1818
<Compile Include="$(MSBuildThisFileDirectory)Commands\DiffSelectedFilesCommand.cs" />
1919
<Compile Include="$(MSBuildThisFileDirectory)Ids.cs" />
20+
<Compile Include="$(MSBuildThisFileDirectory)Models\InverseBooleanConverter.cs" />
2021
<Compile Include="$(MSBuildThisFileDirectory)Models\ClipboardHelper.cs" />
2122
<Compile Include="$(MSBuildThisFileDirectory)Models\CustomDiffTool.cs" />
2223
<Compile Include="$(MSBuildThisFileDirectory)Models\DiffToolFactory.cs" />

Kool.VsDiff.Shared/Models/CustomDiffTool.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
using System;
22
using System.Diagnostics;
3+
using static Kool.VsDiff.VsDiffPackage;
34

45
namespace Kool.VsDiff.Models
56
{
67
internal sealed class CustomDiffTool : IDiffTool
78
{
8-
private readonly string _command;
9-
private readonly string _args;
10-
11-
public CustomDiffTool(string command, string args)
12-
{
13-
_command = command ?? throw new ArgumentNullException(nameof(command));
14-
_args = args ?? throw new ArgumentNullException(nameof(args));
15-
}
16-
179
public void Diff(string file1, string file2, Action<string, string> callback)
1810
{
19-
var args = _args.Replace("$FILE1", file1).Replace("$FILE2", file2);
11+
var args = Options.CustomDiffToolArgs.Replace("$FILE1", file1).Replace("$FILE2", file2);
2012
var process = new Process
2113
{
2214
EnableRaisingEvents = true,
23-
StartInfo = new ProcessStartInfo(_command, args)
15+
StartInfo = new ProcessStartInfo(Options.CustomDiffToolPath, args)
2416
};
2517
process.Exited += (_, _) => callback?.Invoke(file1, file2);
2618
process.Start();

Kool.VsDiff.Shared/Models/DiffToolFactory.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ internal static class DiffToolFactory
66
{
77
private static IDiffTool CachedDiffTool;
88

9-
public static IDiffTool CreateDiffTool()
10-
{
11-
if (CachedDiffTool == null)
12-
{
13-
CachedDiffTool = Options.UseCustomDiffTool
14-
? new CustomDiffTool(Options.CustomDiffToolPath, Options.CustomDiffToolArgs)
15-
: new VsDiffTool(Instance);
16-
}
17-
18-
return CachedDiffTool;
19-
}
9+
public static IDiffTool CreateDiffTool() => CachedDiffTool ??= Options.UseCustomDiffTool ? new CustomDiffTool() : new VsDiffTool();
2010

2111
public static void ClearCache() => CachedDiffTool = null;
2212
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace Kool.VsDiff.Models
6+
{
7+
public sealed class InverseBooleanConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => !(bool)value;
10+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
11+
}
12+
}

Kool.VsDiff.Shared/Models/VsDiffTool.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
using Microsoft;
2+
using Microsoft.VisualStudio;
3+
using Microsoft.VisualStudio.Shell;
24
using Microsoft.VisualStudio.Shell.Interop;
35
using System;
46
using System.IO;
7+
using static Kool.VsDiff.VsDiffPackage;
58

69
namespace Kool.VsDiff.Models
710
{
811
internal class VsDiffTool : IDiffTool
912
{
1013
private readonly IVsDifferenceService _diffService;
1114

12-
public VsDiffTool(VsDiffPackage package)
15+
public VsDiffTool()
1316
{
14-
var sp = package as IServiceProvider;
17+
var sp = Instance as IServiceProvider;
1518
_diffService = (IVsDifferenceService)sp.GetService(typeof(SVsDifferenceService));
1619
Assumes.Present(_diffService);
1720
}
@@ -23,7 +26,17 @@ public void Diff(string file1, string file2, Action<string, string> callback)
2326
var caption = $"{name1} vs {name2}";
2427
var tooltip = file1 + Environment.NewLine + file2;
2528

26-
_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
29+
if (Options.PreferToUsePreviewWindow)
30+
{
31+
using (new NewDocumentStateScope(__VSNEWDOCUMENTSTATE2.NDS_TryProvisional, VSConstants.NewDocumentStateReason.Navigation))
32+
{
33+
_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
34+
}
35+
}
36+
else
37+
{
38+
_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
39+
}
2740

2841
callback?.Invoke(file1, file2);
2942
}

Kool.VsDiff.Shared/Pages/VsDiffOptions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ internal sealed class VsDiffOptions : UIElementDialogPage
1616

1717
public VsDiffOptions() => SetDefaults();
1818

19-
public bool DiffClipboardWithCodeEnabled { get; set; }
20-
public bool DiffClipboardWithFileEnabled { get; set; }
21-
public bool DiffClipboardWithDocumentEnabled { get; set; }
22-
2319
public bool UseCustomDiffTool { get; set; }
2420
public string CustomDiffToolPath { get; set; }
2521
public string CustomDiffToolArgs { get; set; }
2622

23+
public bool DiffClipboardWithCodeEnabled { get; set; }
24+
public bool DiffClipboardWithFileEnabled { get; set; }
25+
public bool DiffClipboardWithDocumentEnabled { get; set; }
26+
public bool PreferToUsePreviewWindow { get; set; }
27+
2728
protected override UIElement Child => _page ??= new DiffToolOptionsPage(this);
2829

2930
protected override void OnActivate(CancelEventArgs e)
@@ -57,12 +58,13 @@ public override void ResetSettings()
5758

5859
private void SetDefaults()
5960
{
60-
DiffClipboardWithCodeEnabled = true;
61-
DiffClipboardWithFileEnabled = true;
62-
DiffClipboardWithDocumentEnabled = true;
6361
UseCustomDiffTool = false;
6462
CustomDiffToolPath = @"%ProgramFiles(x86)%\WinMerge\WinMergeU.exe";
6563
CustomDiffToolArgs = "-e -u \"$FILE1\" \"$FILE2\"";
64+
DiffClipboardWithCodeEnabled = true;
65+
DiffClipboardWithFileEnabled = true;
66+
DiffClipboardWithDocumentEnabled = true;
67+
PreferToUsePreviewWindow = true;
6668
}
6769
}
6870
}

Kool.VsDiff.Shared/Pages/VsDiffOptionsPage.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:kv="clr-namespace:Kool.VsDiff"
8+
xmlns:kvm="clr-namespace:Kool.VsDiff.Models"
89
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"
910
FontFamily="{DynamicResource VsFont.EnvironmentFontFamily}"
1011
FontSize="{DynamicResource VsFont.EnvironmentFontSize}">
12+
<UserControl.Resources>
13+
<kvm:InverseBooleanConverter x:Key="InverseBooleanConverter" />
14+
</UserControl.Resources>
1115
<StackPanel>
1216
<GroupBox Padding="0,5">
1317
<GroupBox.Header>
@@ -37,5 +41,6 @@
3741
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithCodeEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithCode}" />
3842
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithFileEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithFile}" />
3943
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithDocumentEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithDocument}" />
44+
<CheckBox Margin="0,5,0,0" IsChecked="{Binding PreferToUsePreviewWindow}" Content="{x:Static kv:VSPackage.OptionsPage_PreferToUsePreviewWindow}" IsEnabled="{Binding UseCustomDiffTool, Mode=OneWay, Converter={StaticResource InverseBooleanConverter}}" />
4045
</StackPanel>
4146
</UserControl>

0 commit comments

Comments
 (0)