Skip to content

Commit

Permalink
Make the diff margin popup a text box, use fonts and colors from Visu…
Browse files Browse the repository at this point in the history
…al Studio settings (Fixes yysun#50)
  • Loading branch information
sharwell committed Feb 9, 2013
1 parent 0dcd62a commit b308da7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 18 deletions.
16 changes: 13 additions & 3 deletions Diff/DiffMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class DiffMargin : IWpfTextViewMargin
private const double MarginWidth = 10.0;

private readonly IWpfTextView _textView;
private readonly IClassificationFormatMap _classificationFormatMap;
private readonly IEditorFormatMap _editorFormatMap;
private readonly DiffMarginViewModel _viewModel;
private readonly DiffMarginControl _gitDiffBarControl;
Expand All @@ -33,10 +34,11 @@ public class DiffMargin : IWpfTextViewMargin
/// Creates a <see cref="GitDiffMargin" /> for a given <see cref="IWpfTextView" /> .
/// </summary>
/// <param name="textView"> The <see cref="IWpfTextView" /> to attach the margin to. </param>
public DiffMargin(IWpfTextView textView, ITextDocumentFactoryService textDocumentFactoryService, IEditorFormatMapService editorFormatMapService)
internal DiffMargin(IWpfTextView textView, DiffMarginFactory factory)
{
_textView = textView;
_editorFormatMap = editorFormatMapService.GetEditorFormatMap(textView);
_classificationFormatMap = factory.ClassificationFormatMapService.GetClassificationFormatMap(textView);
_editorFormatMap = factory.EditorFormatMapService.GetEditorFormatMap(textView);

_editorFormatMap.FormatMappingChanged += HandleFormatMappingChanged;
_textView.Closed += (sender, e) => _editorFormatMap.FormatMappingChanged -= HandleFormatMappingChanged;
Expand All @@ -45,7 +47,7 @@ public DiffMargin(IWpfTextView textView, ITextDocumentFactoryService textDocumen
_textView.Options.OptionChanged += HandleOptionChanged;

_gitDiffBarControl = new DiffMarginControl();
_viewModel = new DiffMarginViewModel(this, _textView, textDocumentFactoryService, new GitCommands());
_viewModel = new DiffMarginViewModel(this, _textView, factory.TextDocumentFactoryService, new GitCommands());
_gitDiffBarControl.DataContext = _viewModel;
_gitDiffBarControl.Width = MarginWidth;
UpdateVisibility();
Expand All @@ -65,6 +67,14 @@ public System.Windows.FrameworkElement VisualElement
}
}

public IClassificationFormatMap ClassificationFormatMap
{
get
{
return _classificationFormatMap;
}
}

public IWpfTextView TextView
{
get
Expand Down
17 changes: 12 additions & 5 deletions Diff/DiffMarginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,29 @@
internal sealed class DiffMarginFactory : IWpfTextViewMarginProvider
{
[Import]
private ITextDocumentFactoryService TextDocumentFactoryService
internal ITextDocumentFactoryService TextDocumentFactoryService
{
get;
set;
private set;
}

[Import]
private IEditorFormatMapService EditorFormatMapService
internal IClassificationFormatMapService ClassificationFormatMapService
{
get;
set;
private set;
}

[Import]
internal IEditorFormatMapService EditorFormatMapService
{
get;
private set;
}

public IWpfTextViewMargin CreateMargin(IWpfTextViewHost textViewHost, IWpfTextViewMargin containerMargin)
{
return new DiffMargin(textViewHost.TextView, TextDocumentFactoryService, EditorFormatMapService);
return new DiffMargin(textViewHost.TextView, this);
}
}
}
25 changes: 15 additions & 10 deletions Diff/View/DiffMarginControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@
</Button>
<Popup AllowsTransparency="True"
HorizontalOffset="8"
IsOpen="{Binding ShowPopup,
Mode=TwoWay}"
IsOpen="{Binding ShowPopup, Mode=TwoWay}"
Placement="MousePoint"
PlacementTarget="{Binding ElementName=diffControl}"
StaysOpen="False">
Expand Down Expand Up @@ -178,14 +177,20 @@
Margin="0,-1,0,0"
Visibility="{Binding IsDiffTextVisible,
Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Margin="5"
FontFamily="Consolas"
FontSize="12"
Text="{Binding DiffText, Mode=OneWay}"
MaxWidth="{Binding MaxWidth, Mode=OneWay}"
MaxHeight="{Binding MaxHeight, Mode=OneWay}"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"/>
<TextBox Margin="5"
IsReadOnly="True"
FontFamily="{Binding FontFamily, Mode=OneWay}"
FontStretch="{Binding FontStretch, Mode=OneWay}"
FontStyle="{Binding FontStyle, Mode=OneWay}"
FontWeight="{Binding FontWeight, Mode=OneWay}"
FontSize="{Binding FontSize, Mode=OneWay}"
Text="{Binding DiffText, Mode=OneWay}"
MaxWidth="{Binding MaxWidth, Mode=OneWay}"
MaxHeight="{Binding MaxHeight, Mode=OneWay}"
Background="{Binding Background, Mode=OneWay}"
Foreground="{Binding Foreground, Mode=OneWay}"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"/>
</Border>
</StackPanel>
</Border>
Expand Down
78 changes: 78 additions & 0 deletions Diff/ViewModel/DiffViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Media;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
Expand Down Expand Up @@ -62,6 +63,61 @@ public Brush DiffBrush
}
}

public FontFamily FontFamily
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.TypefaceEmpty)
return new FontFamily("Consolas");

return _margin.ClassificationFormatMap.DefaultTextProperties.Typeface.FontFamily;
}
}

public FontStretch FontStretch
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.TypefaceEmpty)
return FontStretches.Normal;

return _margin.ClassificationFormatMap.DefaultTextProperties.Typeface.Stretch;
}
}

public FontStyle FontStyle
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.TypefaceEmpty)
return FontStyles.Normal;

return _margin.ClassificationFormatMap.DefaultTextProperties.Typeface.Style;
}
}

public FontWeight FontWeight
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.TypefaceEmpty)
return FontWeights.Normal;

return _margin.ClassificationFormatMap.DefaultTextProperties.Typeface.Weight;
}
}

public double FontSize
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.FontRenderingEmSizeEmpty)
return 12.0;

return _margin.ClassificationFormatMap.DefaultTextProperties.FontRenderingEmSize;
}
}

public double MaxWidth
{
get
Expand All @@ -78,6 +134,28 @@ public double MaxHeight
}
}

public Brush Background
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.BackgroundBrushEmpty)
return _margin.TextView.Background;

return _margin.ClassificationFormatMap.DefaultTextProperties.BackgroundBrush;
}
}

public Brush Foreground
{
get
{
if (_margin.ClassificationFormatMap.DefaultTextProperties.ForegroundBrushEmpty)
return (Brush)Application.Current.Resources[VsBrushes.ToolWindowTextKey];

return _margin.ClassificationFormatMap.DefaultTextProperties.ForegroundBrush;
}
}

public int LineNumber { get { return _hunkRangeInfo.NewHunkRange.StartingLineNumber; } }

private void HandleBrushesChanged(object sender, EventArgs e)
Expand Down

0 comments on commit b308da7

Please sign in to comment.