optimize<DiffViewer>: use Models.TextChanges.Line directly for DiffViewer and HighlightableTextBlock

This commit is contained in:
leo 2021-08-05 09:29:26 +08:00
parent da1abaee16
commit 47cb708744
3 changed files with 82 additions and 100 deletions

View file

@ -15,41 +15,15 @@ namespace SourceGit.Views.Controls {
private static readonly Brush HL_ADDED = new SolidColorBrush(Color.FromArgb(128, 0, 255, 0));
private static readonly Brush HL_DELETED = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
public class Data {
public Models.TextChanges.LineMode Mode { get; set; } = Models.TextChanges.LineMode.None;
public string Text { get; set; } = "";
public List<Models.TextChanges.HighlightRange> Highlights { get; set; } = new List<Models.TextChanges.HighlightRange>();
public bool IsContent {
get {
return Mode == Models.TextChanges.LineMode.Added
|| Mode == Models.TextChanges.LineMode.Deleted
|| Mode == Models.TextChanges.LineMode.Normal;
}
}
public bool IsDifference {
get {
return Mode == Models.TextChanges.LineMode.Added
|| Mode == Models.TextChanges.LineMode.Deleted
|| Mode == Models.TextChanges.LineMode.None;
}
}
public string FG {
get { return Mode == Models.TextChanges.LineMode.Indicator ? "Brush.FG2" : "Brush.FG1"; }
}
}
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
"Content",
typeof(Data),
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
"Data",
typeof(Models.TextChanges.Line),
typeof(HighlightableTextBlock),
new PropertyMetadata(null, OnContentChanged));
public Data Content {
get { return (Data)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
public Models.TextChanges.Line Data {
get { return (Models.TextChanges.Line)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
@ -61,10 +35,10 @@ namespace SourceGit.Views.Controls {
txt.Background = Brushes.Transparent;
txt.FontStyle = FontStyles.Normal;
if (txt.Content == null) return;
if (txt.Data == null) return;
Brush highlightBrush = Brushes.Transparent;
switch (txt.Content.Mode) {
switch (txt.Data.Mode) {
case Models.TextChanges.LineMode.None:
txt.Background = BG_EMPTY;
break;
@ -83,30 +57,30 @@ namespace SourceGit.Views.Controls {
break;
}
txt.SetResourceReference(ForegroundProperty, txt.Content.FG);
txt.SetResourceReference(ForegroundProperty, txt.Data.Mode == Models.TextChanges.LineMode.Indicator ? "Brush.FG2" : "Brush.FG1");
if (txt.Content.Highlights == null || txt.Content.Highlights.Count == 0) {
txt.Text = txt.Content.Text;
if (txt.Data.Highlights == null || txt.Data.Highlights.Count == 0) {
txt.Text = txt.Data.Content;
return;
}
var started = 0;
foreach (var highlight in txt.Content.Highlights) {
foreach (var highlight in txt.Data.Highlights) {
if (started < highlight.Start) {
txt.Inlines.Add(new Run(txt.Content.Text.Substring(started, highlight.Start - started)));
txt.Inlines.Add(new Run(txt.Data.Content.Substring(started, highlight.Start - started)));
}
txt.Inlines.Add(new TextBlock() {
Background = highlightBrush,
LineHeight = txt.LineHeight,
Text = txt.Content.Text.Substring(highlight.Start, highlight.Count),
Text = txt.Data.Content.Substring(highlight.Start, highlight.Count),
});
started = highlight.Start + highlight.Count;
}
if (started < txt.Content.Text.Length) {
txt.Inlines.Add(new Run(txt.Content.Text.Substring(started)));
if (started < txt.Data.Content.Length) {
txt.Inlines.Add(new Run(txt.Data.Content.Substring(started)));
}
}
}