2021-08-04 18:36:06 -07:00
|
|
|
|
using System.Windows;
|
2021-07-28 00:02:06 -07:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Views.Controls {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 支持部分高亮的文本组件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class HighlightableTextBlock : TextBlock {
|
2021-08-04 02:09:42 -07:00
|
|
|
|
private static readonly Brush BG_EMPTY = new SolidColorBrush(Color.FromArgb(60, 0, 0, 0));
|
|
|
|
|
private static readonly Brush BG_ADDED = new SolidColorBrush(Color.FromArgb(60, 0, 255, 0));
|
|
|
|
|
private static readonly Brush BG_DELETED = new SolidColorBrush(Color.FromArgb(60, 255, 0, 0));
|
|
|
|
|
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));
|
2021-07-28 00:02:06 -07:00
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
|
|
|
|
|
"Data",
|
|
|
|
|
typeof(Models.TextChanges.Line),
|
2021-07-28 00:02:06 -07:00
|
|
|
|
typeof(HighlightableTextBlock),
|
|
|
|
|
new PropertyMetadata(null, OnContentChanged));
|
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
public Models.TextChanges.Line Data {
|
|
|
|
|
get { return (Models.TextChanges.Line)GetValue(DataProperty); }
|
|
|
|
|
set { SetValue(DataProperty, value); }
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
|
|
|
|
var txt = d as HighlightableTextBlock;
|
|
|
|
|
if (txt == null) return;
|
|
|
|
|
|
|
|
|
|
txt.Inlines.Clear();
|
|
|
|
|
txt.Text = null;
|
2021-08-04 02:09:42 -07:00
|
|
|
|
txt.Background = Brushes.Transparent;
|
|
|
|
|
txt.FontStyle = FontStyles.Normal;
|
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
if (txt.Data == null) return;
|
2021-07-28 00:02:06 -07:00
|
|
|
|
|
2021-08-04 02:09:42 -07:00
|
|
|
|
Brush highlightBrush = Brushes.Transparent;
|
2021-08-04 18:29:26 -07:00
|
|
|
|
switch (txt.Data.Mode) {
|
2021-08-04 02:09:42 -07:00
|
|
|
|
case Models.TextChanges.LineMode.None:
|
|
|
|
|
txt.Background = BG_EMPTY;
|
|
|
|
|
break;
|
|
|
|
|
case Models.TextChanges.LineMode.Indicator:
|
|
|
|
|
txt.FontStyle = FontStyles.Italic;
|
|
|
|
|
break;
|
|
|
|
|
case Models.TextChanges.LineMode.Added:
|
|
|
|
|
txt.Background = BG_ADDED;
|
|
|
|
|
highlightBrush = HL_ADDED;
|
|
|
|
|
break;
|
|
|
|
|
case Models.TextChanges.LineMode.Deleted:
|
|
|
|
|
txt.Background = BG_DELETED;
|
|
|
|
|
highlightBrush = HL_DELETED;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
txt.SetResourceReference(ForegroundProperty, txt.Data.Mode == Models.TextChanges.LineMode.Indicator ? "Brush.FG2" : "Brush.FG1");
|
2021-08-04 02:09:42 -07:00
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
if (txt.Data.Highlights == null || txt.Data.Highlights.Count == 0) {
|
|
|
|
|
txt.Text = txt.Data.Content;
|
2021-07-28 00:02:06 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var started = 0;
|
2021-08-04 18:29:26 -07:00
|
|
|
|
foreach (var highlight in txt.Data.Highlights) {
|
2021-07-28 00:02:06 -07:00
|
|
|
|
if (started < highlight.Start) {
|
2021-08-04 18:29:26 -07:00
|
|
|
|
txt.Inlines.Add(new Run(txt.Data.Content.Substring(started, highlight.Start - started)));
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
txt.Inlines.Add(new TextBlock() {
|
2021-08-04 02:09:42 -07:00
|
|
|
|
Background = highlightBrush,
|
2021-07-28 00:02:06 -07:00
|
|
|
|
LineHeight = txt.LineHeight,
|
2021-08-04 18:29:26 -07:00
|
|
|
|
Text = txt.Data.Content.Substring(highlight.Start, highlight.Count),
|
2021-07-28 00:02:06 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
started = highlight.Start + highlight.Count;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 18:29:26 -07:00
|
|
|
|
if (started < txt.Data.Content.Length) {
|
|
|
|
|
txt.Inlines.Add(new Run(txt.Data.Content.Substring(started)));
|
2021-07-28 00:02:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|