Skip to content

Commit

Permalink
Add preview window support
Browse files Browse the repository at this point in the history
  • Loading branch information
heku committed Nov 7, 2021
1 parent 9885545 commit 9dbebd7
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 40 deletions.
1 change: 1 addition & 0 deletions Kool.VsDiff.Linked/VSPackage.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Kool.VsDiff.Linked/VSPackage.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<data name="OptionsPage_EnableDiffClipboardWithFile" xml:space="preserve">
<value>Enable compare Clipboard content with selected file.</value>
</data>
<data name="OptionsPage_PreferToUsePreviewWindow" xml:space="preserve">
<value>Prefer to use preview window (if supports).</value>
</data>
<data name="OptionsPage_TestButtonContent" xml:space="preserve">
<value>TEST</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions Kool.VsDiff.Linked/VSPackage.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<data name="OptionsPage_EnableDiffClipboardWithFile" xml:space="preserve">
<value>启用选中文件与剪贴板内容的比较功能</value>
</data>
<data name="OptionsPage_PreferToUsePreviewWindow" xml:space="preserve">
<value>优先使用预览窗口 (如果支持的话)</value>
</data>
<data name="OptionsPage_TestButtonContent" xml:space="preserve">
<value>测试</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions Kool.VsDiff.Shared/Kool.VsDiff.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Commands\DiffClipboardWithFileCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Commands\DiffSelectedFilesCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Ids.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\InverseBooleanConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\ClipboardHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\CustomDiffTool.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\DiffToolFactory.cs" />
Expand Down
14 changes: 3 additions & 11 deletions Kool.VsDiff.Shared/Models/CustomDiffTool.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
using System;
using System.Diagnostics;
using static Kool.VsDiff.VsDiffPackage;

namespace Kool.VsDiff.Models
{
internal sealed class CustomDiffTool : IDiffTool
{
private readonly string _command;
private readonly string _args;

public CustomDiffTool(string command, string args)
{
_command = command ?? throw new ArgumentNullException(nameof(command));
_args = args ?? throw new ArgumentNullException(nameof(args));
}

public void Diff(string file1, string file2, Action<string, string> callback)
{
var args = _args.Replace("$FILE1", file1).Replace("$FILE2", file2);
var args = Options.CustomDiffToolArgs.Replace("$FILE1", file1).Replace("$FILE2", file2);
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo(_command, args)
StartInfo = new ProcessStartInfo(Options.CustomDiffToolPath, args)
};
process.Exited += (_, _) => callback?.Invoke(file1, file2);
process.Start();
Expand Down
12 changes: 1 addition & 11 deletions Kool.VsDiff.Shared/Models/DiffToolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ internal static class DiffToolFactory
{
private static IDiffTool CachedDiffTool;

public static IDiffTool CreateDiffTool()
{
if (CachedDiffTool == null)
{
CachedDiffTool = Options.UseCustomDiffTool
? new CustomDiffTool(Options.CustomDiffToolPath, Options.CustomDiffToolArgs)
: new VsDiffTool(Instance);
}

return CachedDiffTool;
}
public static IDiffTool CreateDiffTool() => CachedDiffTool ??= Options.UseCustomDiffTool ? new CustomDiffTool() : new VsDiffTool();

public static void ClearCache() => CachedDiffTool = null;
}
Expand Down
12 changes: 12 additions & 0 deletions Kool.VsDiff.Shared/Models/InverseBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Globalization;
using System.Windows.Data;

namespace Kool.VsDiff.Models
{
public sealed class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => !(bool)value;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
}
19 changes: 16 additions & 3 deletions Kool.VsDiff.Shared/Models/VsDiffTool.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.IO;
using static Kool.VsDiff.VsDiffPackage;

namespace Kool.VsDiff.Models
{
internal class VsDiffTool : IDiffTool
{
private readonly IVsDifferenceService _diffService;

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

_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
if (Options.PreferToUsePreviewWindow)
{
using (new NewDocumentStateScope(__VSNEWDOCUMENTSTATE2.NDS_TryProvisional, VSConstants.NewDocumentStateReason.Navigation))
{
_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
}
}
else
{
_diffService.OpenComparisonWindow2(file1, file2, caption, tooltip, file1, file2, null, null, 0).Show();
}

callback?.Invoke(file1, file2);
}
Expand Down
16 changes: 9 additions & 7 deletions Kool.VsDiff.Shared/Pages/VsDiffOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ internal sealed class VsDiffOptions : UIElementDialogPage

public VsDiffOptions() => SetDefaults();

public bool DiffClipboardWithCodeEnabled { get; set; }
public bool DiffClipboardWithFileEnabled { get; set; }
public bool DiffClipboardWithDocumentEnabled { get; set; }

public bool UseCustomDiffTool { get; set; }
public string CustomDiffToolPath { get; set; }
public string CustomDiffToolArgs { get; set; }

public bool DiffClipboardWithCodeEnabled { get; set; }
public bool DiffClipboardWithFileEnabled { get; set; }
public bool DiffClipboardWithDocumentEnabled { get; set; }
public bool PreferToUsePreviewWindow { get; set; }

protected override UIElement Child => _page ??= new DiffToolOptionsPage(this);

protected override void OnActivate(CancelEventArgs e)
Expand Down Expand Up @@ -57,12 +58,13 @@ public override void ResetSettings()

private void SetDefaults()
{
DiffClipboardWithCodeEnabled = true;
DiffClipboardWithFileEnabled = true;
DiffClipboardWithDocumentEnabled = true;
UseCustomDiffTool = false;
CustomDiffToolPath = @"%ProgramFiles(x86)%\WinMerge\WinMergeU.exe";
CustomDiffToolArgs = "-e -u \"$FILE1\" \"$FILE2\"";
DiffClipboardWithCodeEnabled = true;
DiffClipboardWithFileEnabled = true;
DiffClipboardWithDocumentEnabled = true;
PreferToUsePreviewWindow = true;
}
}
}
5 changes: 5 additions & 0 deletions Kool.VsDiff.Shared/Pages/VsDiffOptionsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:kv="clr-namespace:Kool.VsDiff"
xmlns:kvm="clr-namespace:Kool.VsDiff.Models"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"
FontFamily="{DynamicResource VsFont.EnvironmentFontFamily}"
FontSize="{DynamicResource VsFont.EnvironmentFontSize}">
<UserControl.Resources>
<kvm:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</UserControl.Resources>
<StackPanel>
<GroupBox Padding="0,5">
<GroupBox.Header>
Expand Down Expand Up @@ -37,5 +41,6 @@
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithCodeEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithCode}" />
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithFileEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithFile}" />
<CheckBox Margin="0,5,0,0" IsChecked="{Binding DiffClipboardWithDocumentEnabled}" Content="{x:Static kv:VSPackage.OptionsPage_EnableDiffClipboardWithDocument}" />
<CheckBox Margin="0,5,0,0" IsChecked="{Binding PreferToUsePreviewWindow}" Content="{x:Static kv:VSPackage.OptionsPage_PreferToUsePreviewWindow}" IsEnabled="{Binding UseCustomDiffTool, Mode=OneWay, Converter={StaticResource InverseBooleanConverter}}" />
</StackPanel>
</UserControl>
12 changes: 5 additions & 7 deletions Kool.VsDiff.Shared/Pages/VsDiffOptionsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public DiffToolOptionsPage(VsDiffOptions options)

private void OnBrowseButtonClicked(object sender, RoutedEventArgs e)
{
// TODO: How to localize it?
var dialog = new OpenFileDialog();
if (!string.IsNullOrWhiteSpace(_options.CustomDiffToolPath))
{
Expand All @@ -42,12 +41,11 @@ private void OnTestButtonClicked(object sender, RoutedEventArgs e)

try
{
new CustomDiffTool(_options.CustomDiffToolPath, _options.CustomDiffToolArgs)
.Diff(file1, file2, (f1, f2) =>
{
TempFileHelper.RemoveTempFile(f1);
TempFileHelper.RemoveTempFile(f2);
});
new CustomDiffTool().Diff(file1, file2, (f1, f2) =>
{
TempFileHelper.RemoveTempFile(f1);
TempFileHelper.RemoveTempFile(f2);
});
}
catch (Exception ex)
{
Expand Down
Binary file modified Screenshots/CompareActiveDocumentWithClipboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/CompareSelectedCodeWithClipboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/CompareSelectedFileWithClipboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/CompareSelectedFiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ variables:
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'

name: 2.2.$(patch)
name: 2.3.$(patch)

pool:
vmImage: 'windows-2022'
Expand Down

0 comments on commit 9dbebd7

Please sign in to comment.