optimize<Blame>: using ItemsControl instead of TextBox to show line numbers

This commit is contained in:
leo 2020-11-20 15:46:06 +08:00
parent b97d5e608d
commit cad64f6352
2 changed files with 48 additions and 25 deletions

View file

@ -110,30 +110,49 @@
<Border Grid.Row="2" BorderThickness="1" BorderBrush="{StaticResource Brush.Border2}" ClipToBounds="True"> <Border Grid.Row="2" BorderThickness="1" BorderBrush="{StaticResource Brush.Border2}" ClipToBounds="True">
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="1"/> <ColumnDefinition Width="1"/>
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBox <ItemsControl
Grid.Column="1"
x:Name="lineNumber" x:Name="lineNumber"
Grid.Column="0" Padding="0"
AcceptsReturn="True" Margin="0"
AcceptsTab="True"
BorderThickness="0" BorderThickness="0"
Background="Transparent" Background="Transparent"
IsReadOnly="True" VirtualizingPanel.ScrollUnit="Item"
Margin="4,0,4,0" VirtualizingPanel.IsVirtualizing="True"
FontSize="13" VirtualizingPanel.VirtualizationMode="Recycling">
HorizontalContentAlignment="Right" <ItemsControl.ItemsPanel>
VerticalAlignment="Stretch" <ItemsPanelTemplate>
FontFamily="Consolas"/> <VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<Rectangle Grid.Column="1" Width="1" Fill="{StaticResource Brush.Border2}"/> <ItemsControl.Template>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
<ItemsPresenter/>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding .}" Padding="0" Margin="0" FontFamily="Consolas" FontSize="13" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="{StaticResource Brush.FG}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Rectangle Grid.Column="3" Width="1" Fill="{StaticResource Brush.Border2}"/>
<RichTextBox <RichTextBox
x:Name="content" x:Name="content"
Grid.Column="2" Grid.Column="4"
AcceptsReturn="True" AcceptsReturn="True"
AcceptsTab="True" AcceptsTab="True"
IsReadOnly="True" IsReadOnly="True"

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -56,7 +55,7 @@ namespace SourceGit.UI {
content.Document.Blocks.Clear(); content.Document.Blocks.Clear();
if (blame.IsBinary) { if (blame.IsBinary) {
lineNumber.Text = ""; lineNumber.ItemsSource = null;
Paragraph p = new Paragraph(new Run("BINARY FILE BLAME NOT SUPPORTED!!!")); Paragraph p = new Paragraph(new Run("BINARY FILE BLAME NOT SUPPORTED!!!"));
p.Margin = new Thickness(0); p.Margin = new Thickness(0);
@ -68,9 +67,14 @@ namespace SourceGit.UI {
content.Document.Blocks.Add(p); content.Document.Blocks.Add(p);
} else { } else {
var numbers = new StringBuilder(); List<string> numbers = new List<string>();
for (int i = 0; i < blame.LineCount; i++) numbers.AppendLine(i.ToString()); for (int i = 0; i < blame.LineCount; i++) numbers.Add(i.ToString());
lineNumber.Text = numbers.ToString(); lineNumber.ItemsSource = numbers;
var fg = FindResource("Brush.FG") as SolidColorBrush;
var tf = new Typeface(content.FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
var ns = new NumberSubstitution();
var mp = new Thickness(0);
for (int i = 0; i < blame.Blocks.Count; i++) { for (int i = 0; i < blame.Blocks.Count; i++) {
var frag = blame.Blocks[i]; var frag = blame.Blocks[i];
@ -78,11 +82,11 @@ namespace SourceGit.UI {
Paragraph p = new Paragraph(new Run(frag.Content)); Paragraph p = new Paragraph(new Run(frag.Content));
p.DataContext = frag; p.DataContext = frag;
p.Margin = new Thickness(0); p.Margin = mp;
p.Padding = new Thickness(0); p.Padding = mp;
p.LineHeight = 1; p.LineHeight = 1;
p.Background = BG[i % 2]; p.Background = BG[i % 2];
p.Foreground = FindResource("Brush.FG") as SolidColorBrush; p.Foreground = fg;
p.FontStyle = FontStyles.Normal; p.FontStyle = FontStyles.Normal;
p.ContextMenuOpening += (sender, ev) => { p.ContextMenuOpening += (sender, ev) => {
if (!content.Selection.IsEmpty) return; if (!content.Selection.IsEmpty) return;
@ -114,10 +118,10 @@ namespace SourceGit.UI {
frag.Content, frag.Content,
CultureInfo.CurrentUICulture, CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight, FlowDirection.LeftToRight,
new Typeface(content.FontFamily, p.FontStyle, p.FontWeight, p.FontStretch), tf,
content.FontSize, content.FontSize,
Brushes.Black, Brushes.Black,
new NumberSubstitution(), ns,
TextFormattingMode.Ideal); TextFormattingMode.Ideal);
if (minWidth < formatter.Width) { if (minWidth < formatter.Width) {
content.Document.PageWidth = formatter.Width + 16; content.Document.PageWidth = formatter.Width + 16;