| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Windows; |
| | | 4 | | using System.Windows.Controls; |
| | | 5 | | using System.Windows.Documents; |
| | | 6 | | |
| | | 7 | | namespace SwitchBlade.Core |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a text segment with an indication of whether it should be bold. |
| | | 11 | | /// This is a pure data type with no WPF dependency, enabling unit testing. |
| | | 12 | | /// </summary> |
| | | 13 | | public readonly struct HighlightSegment |
| | | 14 | | { |
| | | 15 | | public string Text { get; } |
| | | 16 | | public bool IsBold { get; } |
| | | 17 | | |
| | | 18 | | public HighlightSegment(string text, bool isBold) |
| | | 19 | | { |
| | | 20 | | Text = text; |
| | | 21 | | IsBold = isBold; |
| | | 22 | | } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Attached behavior that highlights matching search characters in a TextBlock |
| | | 27 | | /// by rendering them with bold font weight. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <remarks> |
| | | 30 | | /// Uses four attached DPs (Title, SearchText, IsEnabled, UseFuzzy). |
| | | 31 | | /// When any of them changes, the TextBlock.Inlines collection is rebuilt |
| | | 32 | | /// with bold <see cref="Run"/> segments for matched characters. |
| | | 33 | | /// </remarks> |
| | | 34 | | public static class SearchHighlightBehavior |
| | | 35 | | { |
| | | 36 | | // --- Title --- |
| | 1 | 37 | | public static readonly DependencyProperty TitleProperty = |
| | 1 | 38 | | DependencyProperty.RegisterAttached( |
| | 1 | 39 | | "Title", typeof(string), typeof(SearchHighlightBehavior), |
| | 1 | 40 | | new PropertyMetadata(string.Empty, OnPropertyChanged)); |
| | | 41 | | |
| | 17 | 42 | | public static string GetTitle(DependencyObject obj) => (string)obj.GetValue(TitleProperty); |
| | 5 | 43 | | public static void SetTitle(DependencyObject obj, string value) => obj.SetValue(TitleProperty, value); |
| | | 44 | | |
| | | 45 | | // --- SearchText --- |
| | 1 | 46 | | public static readonly DependencyProperty SearchTextProperty = |
| | 1 | 47 | | DependencyProperty.RegisterAttached( |
| | 1 | 48 | | "SearchText", typeof(string), typeof(SearchHighlightBehavior), |
| | 1 | 49 | | new PropertyMetadata(string.Empty, OnPropertyChanged)); |
| | | 50 | | |
| | 16 | 51 | | public static string GetSearchText(DependencyObject obj) => (string)obj.GetValue(SearchTextProperty); |
| | 4 | 52 | | public static void SetSearchText(DependencyObject obj, string value) => obj.SetValue(SearchTextProperty, value); |
| | | 53 | | |
| | | 54 | | // --- IsEnabled --- |
| | 1 | 55 | | public static readonly DependencyProperty IsEnabledProperty = |
| | 1 | 56 | | DependencyProperty.RegisterAttached( |
| | 1 | 57 | | "IsEnabled", typeof(bool), typeof(SearchHighlightBehavior), |
| | 1 | 58 | | new PropertyMetadata(true, OnPropertyChanged)); |
| | | 59 | | |
| | 16 | 60 | | public static bool GetIsEnabled(DependencyObject obj) => (bool)obj.GetValue(IsEnabledProperty); |
| | 4 | 61 | | public static void SetIsEnabled(DependencyObject obj, bool value) => obj.SetValue(IsEnabledProperty, value); |
| | | 62 | | |
| | | 63 | | // --- UseFuzzy --- |
| | 1 | 64 | | public static readonly DependencyProperty UseFuzzyProperty = |
| | 1 | 65 | | DependencyProperty.RegisterAttached( |
| | 1 | 66 | | "UseFuzzy", typeof(bool), typeof(SearchHighlightBehavior), |
| | 1 | 67 | | new PropertyMetadata(true, OnPropertyChanged)); |
| | | 68 | | |
| | 17 | 69 | | public static bool GetUseFuzzy(DependencyObject obj) => (bool)obj.GetValue(UseFuzzyProperty); |
| | 2 | 70 | | public static void SetUseFuzzy(DependencyObject obj, bool value) => obj.SetValue(UseFuzzyProperty, value); |
| | | 71 | | |
| | | 72 | | // --- HighlightColor --- |
| | 1 | 73 | | public static readonly DependencyProperty HighlightColorProperty = |
| | 1 | 74 | | DependencyProperty.RegisterAttached( |
| | 1 | 75 | | "HighlightColor", typeof(string), typeof(SearchHighlightBehavior), |
| | 1 | 76 | | new PropertyMetadata("#FF0078D4", OnPropertyChanged)); |
| | | 77 | | |
| | 16 | 78 | | public static string GetHighlightColor(DependencyObject obj) => (string)obj.GetValue(HighlightColorProperty); |
| | 4 | 79 | | public static void SetHighlightColor(DependencyObject obj, string value) => obj.SetValue(HighlightColorProperty, |
| | | 80 | | |
| | | 81 | | // --- Core logic --- |
| | | 82 | | private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| | 16 | 83 | | { |
| | 17 | 84 | | if (d is not TextBlock textBlock) return; |
| | | 85 | | |
| | 15 | 86 | | string title = GetTitle(textBlock); |
| | 15 | 87 | | string searchText = GetSearchText(textBlock); |
| | 15 | 88 | | bool isEnabled = GetIsEnabled(textBlock); |
| | 15 | 89 | | bool useFuzzy = GetUseFuzzy(textBlock); |
| | 15 | 90 | | string highlightColor = GetHighlightColor(textBlock); |
| | | 91 | | |
| | 15 | 92 | | var segments = BuildSegments(title, searchText, isEnabled, useFuzzy); |
| | | 93 | | |
| | 15 | 94 | | System.Windows.Media.Brush? highlightBrush = null; |
| | | 95 | | try |
| | 15 | 96 | | { |
| | 15 | 97 | | if (!string.IsNullOrEmpty(highlightColor)) |
| | 14 | 98 | | { |
| | 14 | 99 | | var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(highli |
| | 13 | 100 | | highlightBrush = new System.Windows.Media.SolidColorBrush(color); |
| | 13 | 101 | | } |
| | 14 | 102 | | } |
| | 3 | 103 | | catch { /* Fallback to default/none */ } |
| | | 104 | | |
| | 15 | 105 | | textBlock.Inlines.Clear(); |
| | 99 | 106 | | foreach (var seg in segments) |
| | 27 | 107 | | { |
| | 27 | 108 | | var run = new Run(seg.Text); |
| | 27 | 109 | | if (seg.IsBold) |
| | 6 | 110 | | { |
| | 6 | 111 | | run.FontWeight = FontWeights.Bold; |
| | 6 | 112 | | if (highlightBrush != null) |
| | 4 | 113 | | run.Foreground = highlightBrush; |
| | 6 | 114 | | } |
| | 27 | 115 | | textBlock.Inlines.Add(run); |
| | 27 | 116 | | } |
| | 16 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Builds a list of <see cref="HighlightSegment"/> for the given title, |
| | | 121 | | /// marking matched characters as bold when highlighting is enabled. |
| | | 122 | | /// This method is pure (no WPF dependency) and safe to call from any thread. |
| | | 123 | | /// </summary> |
| | | 124 | | internal static List<HighlightSegment> BuildSegments(string title, string searchText, bool isEnabled, bool useFu |
| | 25 | 125 | | { |
| | 25 | 126 | | var segments = new List<HighlightSegment>(); |
| | | 127 | | |
| | 25 | 128 | | if (string.IsNullOrEmpty(title)) |
| | 2 | 129 | | { |
| | 2 | 130 | | segments.Add(new HighlightSegment(string.Empty, false)); |
| | 2 | 131 | | return segments; |
| | | 132 | | } |
| | | 133 | | |
| | 23 | 134 | | if (!isEnabled || string.IsNullOrEmpty(searchText)) |
| | 10 | 135 | | { |
| | 10 | 136 | | segments.Add(new HighlightSegment(title, false)); |
| | 10 | 137 | | return segments; |
| | | 138 | | } |
| | | 139 | | |
| | 13 | 140 | | var matchedIndices = FuzzyMatcher.GetMatchedIndices(title, searchText, useFuzzy); |
| | | 141 | | |
| | 13 | 142 | | if (matchedIndices.Length == 0) |
| | 2 | 143 | | { |
| | 2 | 144 | | segments.Add(new HighlightSegment(title, false)); |
| | 2 | 145 | | return segments; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // Build a set for O(1) lookup |
| | 11 | 149 | | var matchSet = new HashSet<int>(matchedIndices); |
| | | 150 | | |
| | | 151 | | // Group consecutive characters with the same bold/normal state into segments |
| | 11 | 152 | | int segmentStart = 0; |
| | 11 | 153 | | bool currentBold = matchSet.Contains(0); |
| | | 154 | | |
| | 162 | 155 | | for (int i = 1; i <= title.Length; i++) |
| | 70 | 156 | | { |
| | 70 | 157 | | bool isBold = i < title.Length && matchSet.Contains(i); |
| | 70 | 158 | | if (i == title.Length || isBold != currentBold) |
| | 30 | 159 | | { |
| | 30 | 160 | | var text = title.Substring(segmentStart, i - segmentStart); |
| | 30 | 161 | | segments.Add(new HighlightSegment(text, currentBold)); |
| | | 162 | | |
| | 30 | 163 | | segmentStart = i; |
| | 30 | 164 | | currentBold = isBold; |
| | 30 | 165 | | } |
| | 70 | 166 | | } |
| | | 167 | | |
| | 11 | 168 | | return segments; |
| | 25 | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |