Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Media;
- namespace SO3WPF
- {
- public class HighlightTextBlock : ContentControl
- {
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value); }
- }
- public static readonly DependencyProperty TextProperty =
- DependencyProperty.Register("Text", typeof(string), typeof(HighlightTextBlock), new UIPropertyMetadata("", SetContentStatic));
- private static void SetContentStatic(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = d as HighlightTextBlock;
- control.SetContent();
- }
- public string HighlightText
- {
- get { return (string)GetValue(HighlightTextProperty); }
- set { SetValue(HighlightTextProperty, value); }
- }
- public static readonly DependencyProperty HighlightTextProperty =
- DependencyProperty.Register("HighlightText", typeof(string), typeof(HighlightTextBlock), new UIPropertyMetadata("", SetContentStatic));
- public Brush HighlightBrush
- {
- get { return (Brush)GetValue(ColorProperty); }
- set { SetValue(ColorProperty, value); }
- }
- public static readonly DependencyProperty ColorProperty =
- DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock), new UIPropertyMetadata(Brushes.Orange));
- private TextBlock TextContent = new TextBlock();
- public HighlightTextBlock()
- {
- Content = TextContent;
- SetContent();
- }
- int updateId = 0;
- private void SetContentOld()
- {
- var currentUpdateId = ++updateId;
- var text = Text;
- var highlightText = HighlightText;
- var parts = GetCombined(text, highlightText);//await Task.Run(() => GetCombined(text, highlightText));
- if (currentUpdateId != updateId)
- return;
- ApplyParts(parts);
- }
- Task currentWaitTask = null;
- private async void SetContent45()
- {
- var waitTask = Task.Delay(TimeSpan.FromSeconds(0.1));
- currentWaitTask = waitTask;
- await waitTask;
- if (currentWaitTask != waitTask)
- return;
- var text = Text;
- var highlightText = HighlightText;
- var parts = await Task.Run(() => GetCombined(text, highlightText));
- if (currentWaitTask != waitTask)
- return;
- currentWaitTask = null;
- ApplyParts(parts);
- }
- private void SetContent()
- {
- var mainTS = TaskScheduler.FromCurrentSynchronizationContext();
- var waitTask = Task.Delay(TimeSpan.FromSeconds(0.1));
- currentWaitTask = waitTask;
- waitTask.ContinueWith(t =>
- {
- if (currentWaitTask != waitTask)
- return;
- var text = Text;
- var highlightText = HighlightText;
- var parts = GetCombined(text, highlightText);
- currentWaitTask = null;
- ApplyParts(parts);
- }, mainTS);
- }
- private void ApplyParts(List<string> parts)
- {
- var highlightBrush = HighlightBrush;
- System.Collections.IList existingInlines = TextContent.Inlines;
- var existingCount = existingInlines.Count;
- List<Inline> inlinesToAdd = null;
- var max = Math.Max(existingCount, parts.Count);
- for (int i = 0; i < max; i++)
- {
- var text = i < parts.Count ? parts[i] : null;
- if (i < existingCount)
- {
- ((Run)existingInlines[i]).Text = text;
- }
- else // add new inline
- {
- var inline = new Run(text);
- if (i % 2 != 0) // highlight
- inline.Background = highlightBrush;
- if (inlinesToAdd == null)
- inlinesToAdd = new List<Inline>(parts.Count);
- inlinesToAdd.Add(inline);
- }
- }
- if (inlinesToAdd != null)
- TextContent.Inlines.AddRange(inlinesToAdd);
- }
- private List<string> GetCombined(string text, string highlightText)
- {
- var list = new List<string>();
- if (text == null)
- return list;
- if (highlightText == null)
- {
- list.Add(text);
- return list;
- }
- var parts = text.Split(new[] { highlightText }, StringSplitOptions.None);
- bool first = true;
- foreach (var part in parts)
- {
- if (!first)
- list.Add(highlightText);
- list.Add(part);
- first = false;
- }
- return list;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement