From 60254e15ad4e9bc0ea2eea28cd0f2114742d35b4 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 26 Nov 2020 11:08:08 +0800 Subject: [PATCH] optimize: optimize layout calculation --- src/UI/Blame.xaml | 16 ++++------- src/UI/Blame.xaml.cs | 22 ++++++++++++--- src/UI/DiffViewer.xaml.cs | 59 ++++++++++++++++++++++++--------------- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/UI/Blame.xaml b/src/UI/Blame.xaml index 443b314c..49c4a3ef 100644 --- a/src/UI/Blame.xaml +++ b/src/UI/Blame.xaml @@ -107,8 +107,8 @@ - - + + - - @@ -133,7 +127,9 @@ GridLinesVisibility="Vertical" VerticalGridLinesBrush="{StaticResource Brush.Border2}" FrozenColumnCount="1" + RowHeight="16" SelectionUnit="FullRow" + FontFamily="Consolas" SelectionMode="Single"> @@ -146,7 +142,7 @@ - + diff --git a/src/UI/Blame.xaml.cs b/src/UI/Blame.xaml.cs index 77736953..d9df77aa 100644 --- a/src/UI/Blame.xaml.cs +++ b/src/UI/Blame.xaml.cs @@ -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 { /// /// /// - 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(blame); if (scroller.ComputedVerticalScrollBarVisibility == Visibility.Visible) minWidth -= 8; diff --git a/src/UI/DiffViewer.xaml.cs b/src/UI/DiffViewer.xaml.cs index 9c44ac1b..cc8ae45e 100644 --- a/src/UI/DiffViewer.xaml.cs +++ b/src/UI/DiffViewer.xaml.cs @@ -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; } + /// + /// Create text editor. + /// + /// + /// 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; } + + /// + /// Calculate max width for line number column. + /// + /// + /// + /// + 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