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

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -86,10 +87,23 @@ namespace SourceGit.UI {
loading.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
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.UpdateLayout();
ContentSizeChanged(null, null);
});
});
}
@ -151,10 +165,10 @@ namespace SourceGit.UI {
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ContentSizeChanged(object sender, SizeChangedEventArgs e) {
private void OnSizeChanged(object sender, SizeChangedEventArgs e) {
var total = area.ActualWidth;
var offset = blame.NonFrozenColumnsViewportHorizontalOffset;
var minWidth = total - offset - 2;
var minWidth = total - offset;
var scroller = GetVisualChild<ScrollViewer>(blame);
if (scroller.ComputedVerticalScrollBarVisibility == Visibility.Visible) minWidth -= 8;

View file

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