mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-14 19:49:15 +08:00
Add highlight converter component
This commit is contained in:
parent
2aeceb7ea8
commit
8997c27819
53
Wox/Converters/HighlightTextConverter.cs
Normal file
53
Wox/Converters/HighlightTextConverter.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
|
||||||
|
namespace Wox.Converters
|
||||||
|
{
|
||||||
|
public class HighlightTextConverter : IMultiValueConverter
|
||||||
|
{
|
||||||
|
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
|
||||||
|
{
|
||||||
|
var text = value[0] as string;
|
||||||
|
var highlightData = value[1] as List<int>;
|
||||||
|
|
||||||
|
var textBlock = new Span();
|
||||||
|
|
||||||
|
if (highlightData == null || !highlightData.Any())
|
||||||
|
{
|
||||||
|
// No highlight data, just return the text
|
||||||
|
return new Run(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < text.Length; i++)
|
||||||
|
{
|
||||||
|
var currentCharacter = text.Substring(i, 1);
|
||||||
|
if (this.ShouldHighlight(highlightData, i))
|
||||||
|
{
|
||||||
|
textBlock.Inlines.Add(new Bold(new Run(currentCharacter)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
textBlock.Inlines.Add(new Run(currentCharacter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return textBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
return new[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue };
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ShouldHighlight(List<int> highlightData, int index)
|
||||||
|
{
|
||||||
|
return highlightData.Contains(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:vm="clr-namespace:Wox.ViewModel"
|
xmlns:vm="clr-namespace:Wox.ViewModel"
|
||||||
|
xmlns:converter="clr-namespace:Wox.Converters"
|
||||||
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
|
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="100"
|
||||||
d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
|
d:DataContext="{d:DesignInstance vm:ResultsViewModel}"
|
||||||
MaxHeight="{Binding MaxHeight}"
|
MaxHeight="{Binding MaxHeight}"
|
||||||
@ -30,6 +31,9 @@
|
|||||||
<Button.Content>
|
<Button.Content>
|
||||||
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"
|
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"
|
||||||
Cursor="Hand" UseLayoutRounding="False">
|
Cursor="Hand" UseLayoutRounding="False">
|
||||||
|
<Grid.Resources>
|
||||||
|
<converter:HighlightTextConverter x:Key="HighlightTextConverter"/>
|
||||||
|
</Grid.Resources>
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="32" />
|
<ColumnDefinition Width="32" />
|
||||||
<ColumnDefinition />
|
<ColumnDefinition />
|
||||||
@ -44,9 +48,23 @@
|
|||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<TextBlock Style="{DynamicResource ItemTitleStyle}" DockPanel.Dock="Left"
|
<TextBlock Style="{DynamicResource ItemTitleStyle}" DockPanel.Dock="Left"
|
||||||
VerticalAlignment="Center" ToolTip="{Binding Result.Title}" x:Name="Title"
|
VerticalAlignment="Center" ToolTip="{Binding Result.Title}" x:Name="Title"
|
||||||
Text="{Binding Result.Title}" />
|
Text="{Binding Result.Title}">
|
||||||
|
<vm:ResultsViewModel.FormattedText>
|
||||||
|
<MultiBinding Converter="{StaticResource HighlightTextConverter}">
|
||||||
|
<Binding Path="Result.Title" />
|
||||||
|
<Binding Path="Result.TitleHighlightData" />
|
||||||
|
</MultiBinding>
|
||||||
|
</vm:ResultsViewModel.FormattedText>
|
||||||
|
</TextBlock>
|
||||||
<TextBlock Style="{DynamicResource ItemSubTitleStyle}" ToolTip="{Binding Result.SubTitle}"
|
<TextBlock Style="{DynamicResource ItemSubTitleStyle}" ToolTip="{Binding Result.SubTitle}"
|
||||||
Grid.Row="1" x:Name="SubTitle" Text="{Binding Result.SubTitle}" />
|
Grid.Row="1" x:Name="SubTitle" Text="{Binding Result.SubTitle}">
|
||||||
|
<vm:ResultsViewModel.FormattedText>
|
||||||
|
<MultiBinding Converter="{StaticResource HighlightTextConverter}">
|
||||||
|
<Binding Path="Result.SubTitle" />
|
||||||
|
<Binding Path="Result.SubTitleHighlightData" />
|
||||||
|
</MultiBinding>
|
||||||
|
</vm:ResultsViewModel.FormattedText>
|
||||||
|
</TextBlock>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Button.Content>
|
</Button.Content>
|
||||||
|
@ -5,7 +5,6 @@ namespace Wox.ViewModel
|
|||||||
{
|
{
|
||||||
public class RelayCommand : ICommand
|
public class RelayCommand : ICommand
|
||||||
{
|
{
|
||||||
|
|
||||||
private Action<object> _action;
|
private Action<object> _action;
|
||||||
|
|
||||||
public RelayCommand(Action<object> action)
|
public RelayCommand(Action<object> action)
|
||||||
|
@ -3,7 +3,9 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
using Wox.Infrastructure.UserSettings;
|
using Wox.Infrastructure.UserSettings;
|
||||||
using Wox.Plugin;
|
using Wox.Plugin;
|
||||||
|
|
||||||
@ -193,8 +195,37 @@ namespace Wox.ViewModel
|
|||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region FormattedText Dependency Property
|
||||||
|
public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
|
||||||
|
"FormattedText",
|
||||||
|
typeof(Inline),
|
||||||
|
typeof(ResultsViewModel),
|
||||||
|
new PropertyMetadata(null, FormattedTextPropertyChanged));
|
||||||
|
|
||||||
|
public static void SetFormattedText(DependencyObject textBlock, IList<int> value)
|
||||||
|
{
|
||||||
|
textBlock.SetValue(FormattedTextProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Inline GetFormattedText(DependencyObject textBlock)
|
||||||
|
{
|
||||||
|
return (Inline)textBlock.GetValue(FormattedTextProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
var textBlock = d as TextBlock;
|
||||||
|
if (textBlock == null) return;
|
||||||
|
|
||||||
|
var inline = (Inline)e.NewValue;
|
||||||
|
|
||||||
|
textBlock.Inlines.Clear();
|
||||||
|
if (inline == null) return;
|
||||||
|
|
||||||
|
textBlock.Inlines.Add(inline);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public class ResultCollection : ObservableCollection<ResultViewModel>
|
public class ResultCollection : ObservableCollection<ResultViewModel>
|
||||||
|
@ -158,6 +158,7 @@
|
|||||||
<Compile Include="..\SolutionAssemblyInfo.cs">
|
<Compile Include="..\SolutionAssemblyInfo.cs">
|
||||||
<Link>Properties\SolutionAssemblyInfo.cs</Link>
|
<Link>Properties\SolutionAssemblyInfo.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Converters\HighlightTextConverter.cs" />
|
||||||
<Compile Include="Helper\SingletonWindowOpener.cs" />
|
<Compile Include="Helper\SingletonWindowOpener.cs" />
|
||||||
<Compile Include="PublicAPIInstance.cs" />
|
<Compile Include="PublicAPIInstance.cs" />
|
||||||
<Compile Include="ReportWindow.xaml.cs" />
|
<Compile Include="ReportWindow.xaml.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user