optimize<Diff,Blame>: optimize layout calculation

This commit is contained in:
leo 2020-11-26 11:08:08 +08:00
parent 4d3dc4398b
commit 60254e15ad
3 changed files with 61 additions and 36 deletions

View file

@ -107,8 +107,8 @@
</Border> </Border>
<!-- Content --> <!-- Content -->
<Border Grid.Row="2" x:Name="area" BorderThickness="1" BorderBrush="{StaticResource Brush.Border2}" ClipToBounds="True" SizeChanged="ContentSizeChanged"> <Border Grid.Row="2" BorderThickness="1" BorderBrush="{StaticResource Brush.Border2}" ClipToBounds="True">
<Grid> <Grid x:Name="area" SizeChanged="OnSizeChanged">
<Grid.Resources> <Grid.Resources>
<Style x:Key="Style.DataGridText.LineNumber" TargetType="{x:Type TextBlock}"> <Style x:Key="Style.DataGridText.LineNumber" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Consolas"/> <Setter Property="FontFamily" Value="Consolas"/>
@ -116,13 +116,7 @@
<Setter Property="HorizontalAlignment" Value="Right"/> <Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Padding" Value="8,0"/> <Setter Property="Padding" Value="8,0"/>
</Style> <Setter Property="FontSize" Value="12"/>
<Style x:Key="Style.DataGridText.Block" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Consolas"/>
<Setter Property="Foreground" Value="{StaticResource Brush.FG}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Padding" Value="0"/>
</Style> </Style>
</Grid.Resources> </Grid.Resources>
@ -133,7 +127,9 @@
GridLinesVisibility="Vertical" GridLinesVisibility="Vertical"
VerticalGridLinesBrush="{StaticResource Brush.Border2}" VerticalGridLinesBrush="{StaticResource Brush.Border2}"
FrozenColumnCount="1" FrozenColumnCount="1"
RowHeight="16"
SelectionUnit="FullRow" SelectionUnit="FullRow"
FontFamily="Consolas"
SelectionMode="Single"> SelectionMode="Single">
<DataGrid.RowStyle> <DataGrid.RowStyle>
@ -146,7 +142,7 @@
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Width="Auto" IsReadOnly="True" Binding="{Binding LineNumber}" ElementStyle="{StaticResource Style.DataGridText.LineNumber}"/> <DataGridTextColumn Width="Auto" IsReadOnly="True" Binding="{Binding LineNumber}" ElementStyle="{StaticResource Style.DataGridText.LineNumber}"/>
<DataGridTemplateColumn Width="1" MinWidth="1" IsReadOnly="True"> <DataGridTemplateColumn Width="SizeToCells" MinWidth="1" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<Grid> <Grid>

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -86,10 +87,23 @@ namespace SourceGit.UI {
loading.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null); loading.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
var formatted = new FormattedText(
$"{records.Count}",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(blame.FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
12.0,
Brushes.Black);
var lineNumberWidth = formatted.Width + 16;
var minWidth = area.ActualWidth - lineNumberWidth;
if (records.Count * 16 > area.ActualHeight) minWidth -= 8;
blame.Columns[0].Width = lineNumberWidth;
blame.Columns[1].MinWidth = minWidth;
blame.ItemsSource = records; blame.ItemsSource = records;
blame.UpdateLayout(); blame.UpdateLayout();
ContentSizeChanged(null, null);
}); });
}); });
} }
@ -151,10 +165,10 @@ namespace SourceGit.UI {
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ContentSizeChanged(object sender, SizeChangedEventArgs e) { private void OnSizeChanged(object sender, SizeChangedEventArgs e) {
var total = area.ActualWidth; var total = area.ActualWidth;
var offset = blame.NonFrozenColumnsViewportHorizontalOffset; var offset = blame.NonFrozenColumnsViewportHorizontalOffset;
var minWidth = total - offset - 2; var minWidth = total - offset;
var scroller = GetVisualChild<ScrollViewer>(blame); var scroller = GetVisualChild<ScrollViewer>(blame);
if (scroller.ComputedVerticalScrollBarVisibility == Visibility.Visible) minWidth -= 8; if (scroller.ComputedVerticalScrollBarVisibility == Visibility.Visible) minWidth -= 8;

View file

@ -162,16 +162,12 @@ namespace SourceGit.UI {
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
textChangeOptions.Visibility = Visibility.Visible; textChangeOptions.Visibility = Visibility.Visible;
var formatted = new FormattedText( var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine);
lastOldLine + lastNewLine, var minWidth = editorContainer.ActualWidth - lineNumberWidth * 2;
CultureInfo.CurrentCulture, if (editorContainer.ActualHeight < lineChanges.Count * 16) minWidth -= 8;
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
12.0,
fgCommon);
var minWidth = editorContainer.ActualWidth - formatted.Width - 16 - 8;
var editor = CreateTextEditor(new string[] { "OldLine", "NewLine" }); var editor = CreateTextEditor(new string[] { "OldLine", "NewLine" });
editor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[1].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
editor.Columns[2].MinWidth = minWidth; editor.Columns[2].MinWidth = minWidth;
editor.ItemsSource = blocks; editor.ItemsSource = blocks;
editor.SetValue(Grid.ColumnSpanProperty, 2); editor.SetValue(Grid.ColumnSpanProperty, 2);
@ -233,29 +229,22 @@ namespace SourceGit.UI {
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
textChangeOptions.Visibility = Visibility.Visible; textChangeOptions.Visibility = Visibility.Visible;
var number = lastOldLine; var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine);
if (lastOldLine.Length > lastNewLine.Length) number = lastNewLine; var minWidth = editorContainer.ActualWidth / 2 - lineNumberWidth;
if (editorContainer.ActualHeight < lineChanges.Count * 16) minWidth -= 8;
var formatted = new FormattedText(
number,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
12.0,
fgCommon);
var minWidth = editorContainer.ActualWidth / 2 - formatted.Width - 16 - 8;
var oldEditor = CreateTextEditor(new string[] { "OldLine" }); var oldEditor = CreateTextEditor(new string[] { "OldLine" });
oldEditor.SetValue(Grid.ColumnProperty, 0); oldEditor.SetValue(Grid.ColumnProperty, 0);
oldEditor.ContextMenuOpening += OnTextChangeContextMenuOpening; oldEditor.ContextMenuOpening += OnTextChangeContextMenuOpening;
oldEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll)); oldEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll));
oldEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
oldEditor.Columns[1].MinWidth = minWidth; oldEditor.Columns[1].MinWidth = minWidth;
oldEditor.ItemsSource = oldSideBlocks; oldEditor.ItemsSource = oldSideBlocks;
var newEditor = CreateTextEditor(new string[] { "NewLine" }); var newEditor = CreateTextEditor(new string[] { "NewLine" });
newEditor.SetValue(Grid.ColumnProperty, 1); newEditor.SetValue(Grid.ColumnProperty, 1);
newEditor.ContextMenuOpening += OnTextChangeContextMenuOpening; newEditor.ContextMenuOpening += OnTextChangeContextMenuOpening;
newEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll)); newEditor.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(OnTwoSidesScroll));
newEditor.Columns[0].Width = new DataGridLength(lineNumberWidth, DataGridLengthUnitType.Pixel);
newEditor.Columns[1].MinWidth = minWidth; newEditor.Columns[1].MinWidth = minWidth;
newEditor.ItemsSource = newSideBlocks; newEditor.ItemsSource = newSideBlocks;
@ -354,9 +343,15 @@ namespace SourceGit.UI {
return child; return child;
} }
/// <summary>
/// Create text editor.
/// </summary>
/// <param name="lineNumbers"></param>
/// <returns></returns>
private DataGrid CreateTextEditor(string[] lineNumbers) { private DataGrid CreateTextEditor(string[] lineNumbers) {
var grid = new DataGrid(); var grid = new DataGrid();
grid.SetValue(Grid.RowProperty, 1); grid.SetValue(Grid.RowProperty, 1);
grid.RowHeight = 16.0;
grid.GridLinesVisibility = DataGridGridLinesVisibility.Vertical; grid.GridLinesVisibility = DataGridGridLinesVisibility.Vertical;
grid.VerticalGridLinesBrush = FindResource("Brush.Border2") as Brush; grid.VerticalGridLinesBrush = FindResource("Brush.Border2") as Brush;
grid.FrozenColumnCount = lineNumbers.Length; grid.FrozenColumnCount = lineNumbers.Length;
@ -365,7 +360,6 @@ namespace SourceGit.UI {
foreach (var number in lineNumbers) { foreach (var number in lineNumbers) {
var colLineNumber = new DataGridTextColumn(); var colLineNumber = new DataGridTextColumn();
colLineNumber.Width = DataGridLength.Auto;
colLineNumber.IsReadOnly = true; colLineNumber.IsReadOnly = true;
colLineNumber.Binding = new Binding(number); colLineNumber.Binding = new Binding(number);
colLineNumber.ElementStyle = FindResource("Style.DataGridText.LineNumber") as Style; colLineNumber.ElementStyle = FindResource("Style.DataGridText.LineNumber") as Style;
@ -396,6 +390,27 @@ namespace SourceGit.UI {
return grid; return grid;
} }
/// <summary>
/// Calculate max width for line number column.
/// </summary>
/// <param name="oldLine"></param>
/// <param name="newLine"></param>
/// <returns></returns>
private double CalcLineNumberColWidth(string oldLine, string newLine) {
var number = oldLine;
if (newLine.Length > oldLine.Length) number = newLine;
var formatted = new FormattedText(
number,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
12.0,
Brushes.Black);
return formatted.Width + 16;
}
#endregion #endregion
#region EVENTS #region EVENTS