1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 13:28:33 +00:00

Data model picker - Implemented search

This commit is contained in:
Robert 2024-05-05 16:27:08 +02:00
parent 10ad1d6a58
commit db6fb33c96
10 changed files with 215 additions and 42 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
@ -69,12 +70,19 @@ public class DataModelPicker : TemplatedControl
public static readonly StyledProperty<bool> IsEventPickerProperty =
AvaloniaProperty.Register<DataModelPicker, bool>(nameof(IsEventPicker));
private readonly ObservableCollection<DataModelVisualizationViewModel> _searchResults = [];
private TextBlock? _currentPathDescription;
private TextBlock? _currentPathDisplay;
private MaterialIcon? _currentPathIcon;
private TreeView? _dataModelTreeView;
private ListBox? _searchListBox;
private TextBox? _searchBox;
private Panel? _searchContainer;
private StackPanel? _searchEmpty;
private DispatcherTimer? _updateTimer;
private string? _lastSearch;
private bool _firstUpdate;
static DataModelPicker()
{
@ -200,7 +208,14 @@ public class DataModelPicker : TemplatedControl
if (DataModelUIService == null)
return;
DataModelViewModel?.Update(DataModelUIService, new DataModelUpdateConfiguration(!IsEventPicker));
DataModelViewModel?.Update(DataModelUIService, new DataModelUpdateConfiguration(!IsEventPicker, !string.IsNullOrEmpty(_searchBox?.Text)));
ApplySearch();
if (_firstUpdate)
{
_firstUpdate = false;
UpdateCurrentPath(true);
}
}
private void GetDataModel()
@ -229,14 +244,15 @@ public class DataModelPicker : TemplatedControl
if (DataModelUIService == null || DataModelViewModel == null)
return;
DataModelViewModel.Update(DataModelUIService, new DataModelUpdateConfiguration(IsEventPicker));
DataModelViewModel.Update(DataModelUIService, new DataModelUpdateConfiguration(!IsEventPicker, !string.IsNullOrEmpty(_searchBox?.Text)));
DataModelViewModel.ApplyTypeFilter(true, FilterTypes?.ToArray() ?? Type.EmptyTypes);
ApplySearch();
}
private void DataModelTreeViewOnSelectionChanged(object? sender, SelectionChangedEventArgs e)
private void TreeViewOnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
// Multi-select isn't a think so grab the first one
object? selected = _dataModelTreeView?.SelectedItems[0];
// Multi-select isn't a thing so grab the first one
object? selected = e.AddedItems[0];
if (selected == null)
return;
@ -255,12 +271,48 @@ public class DataModelPicker : TemplatedControl
treeViewItem.IsExpanded = !treeViewItem.IsExpanded;
}
private void ApplySearch()
{
if (DataModelViewModel == null || string.IsNullOrWhiteSpace(_searchBox?.Text))
{
if (_dataModelTreeView != null)
_dataModelTreeView.IsVisible = true;
if (_searchContainer != null)
_searchContainer.IsVisible = false;
return;
}
if (_dataModelTreeView != null)
_dataModelTreeView.IsVisible = false;
if (_searchContainer != null)
_searchContainer.IsVisible = true;
if (_searchBox.Text != _lastSearch)
{
_searchResults.Clear();
foreach (DataModelVisualizationViewModel searchResult in DataModelViewModel.GetSearchResults(_searchBox.Text).DistinctBy(r => r.Path).Take(20))
_searchResults.Add(searchResult);
_lastSearch = _searchBox.Text;
}
if (_searchListBox != null)
{
_searchListBox.IsVisible = _searchResults.Count > 0;
_searchListBox.SelectedItem = _searchResults.FirstOrDefault(r => r.DataModelPath?.Path == DataModelPath?.Path);
}
if (_searchEmpty != null)
_searchEmpty.IsVisible = _searchResults.Count == 0;
}
private void UpdateCurrentPath(bool selectCurrentPath)
{
if (DataModelPath == null)
if (DataModelPath == null || _dataModelTreeView == null)
return;
if (_dataModelTreeView != null && selectCurrentPath)
if (selectCurrentPath)
{
// Expand the path
DataModel? start = DataModelPath.Target;
@ -286,7 +338,7 @@ public class DataModelPicker : TemplatedControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
if (_dataModelTreeView != null)
_dataModelTreeView.SelectionChanged -= DataModelTreeViewOnSelectionChanged;
_dataModelTreeView.SelectionChanged -= TreeViewOnSelectionChanged;
if (_dataModelTreeView != null)
_dataModelTreeView.DoubleTapped -= DataModelTreeViewOnDoubleTapped;
@ -294,11 +346,22 @@ public class DataModelPicker : TemplatedControl
_currentPathDisplay = e.NameScope.Find<TextBlock>("CurrentPathDisplay");
_currentPathDescription = e.NameScope.Find<TextBlock>("CurrentPathDescription");
_dataModelTreeView = e.NameScope.Find<TreeView>("DataModelTreeView");
_searchBox = e.NameScope.Find<TextBox>("SearchBox");
_searchContainer = e.NameScope.Find<Panel>("SearchContainer");
_searchListBox = e.NameScope.Find<ListBox>("SearchListBox");
_searchEmpty = e.NameScope.Find<StackPanel>("SearchEmpty");
if (_dataModelTreeView != null)
_dataModelTreeView.SelectionChanged += DataModelTreeViewOnSelectionChanged;
if (_dataModelTreeView != null)
{
_dataModelTreeView.SelectionChanged += TreeViewOnSelectionChanged;
_dataModelTreeView.DoubleTapped += DataModelTreeViewOnDoubleTapped;
}
if (_searchListBox != null)
{
_searchListBox.ItemsSource = _searchResults;
_searchListBox.SelectionChanged += TreeViewOnSelectionChanged;
}
}
#region Overrides of Visual
@ -307,17 +370,23 @@ public class DataModelPicker : TemplatedControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
GetDataModel();
UpdateCurrentPath(true);
_updateTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(200), DispatcherPriority.Background, Update);
_updateTimer.Start();
_firstUpdate = true;
}
/// <inheritdoc />
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
DataModelViewModel?.Dispose();
_updateTimer?.Stop();
_updateTimer = null;
_lastSearch = null;
if (_searchBox != null)
_searchBox.Text = null;
}
#endregion

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core;
using Artemis.Core.Modules;
@ -41,13 +42,21 @@ public class DataModelEventViewModel : DataModelVisualizationViewModel
}
// Only update children if the parent is expanded
if (Parent != null && !Parent.IsRootViewModel && !Parent.IsVisualizationExpanded)
if (Parent != null && !Parent.IsRootViewModel && !Parent.IsVisualizationExpanded && (configuration == null || !configuration.UpdateAllChildren))
return;
foreach (DataModelVisualizationViewModel dataModelVisualizationViewModel in Children)
dataModelVisualizationViewModel.Update(dataModelUIService, configuration);
}
/// <inheritdoc />
public override IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search)
{
if (PropertyDescription?.Name != null && PropertyDescription.Name.Contains(search, StringComparison.OrdinalIgnoreCase))
return [this];
return [];
}
/// <summary>
/// Always returns <see langword="null" /> for data model events
/// </summary>

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Artemis.UI.Shared.Services;
using ReactiveUI;
@ -62,6 +63,12 @@ public class DataModelListItemViewModel : DataModelVisualizationViewModel
internal set => this.RaiseAndSetIfChanged(ref _displayValue, value);
}
/// <inheritdoc />
public override IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search)
{
return [];
}
/// <inheritdoc />
public override object? GetCurrentValue()
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Artemis.Core;
using Artemis.Core.Modules;
@ -119,6 +120,17 @@ public class DataModelListViewModel : DataModelVisualizationViewModel
CountDisplay = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
}
/// <inheritdoc />
public override IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search)
{
if (PropertyDescription?.Name != null && PropertyDescription.Name.Contains(search, StringComparison.OrdinalIgnoreCase))
return [this];
if (PropertyDescription?.Description != null && PropertyDescription.Description.Contains(search, StringComparison.OrdinalIgnoreCase))
return [this];
return [];
}
/// <inheritdoc />
public override string ToString()
{

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core;
using Artemis.Core.Modules;
using Artemis.UI.Shared.Services;
@ -52,14 +54,20 @@ public class DataModelPropertiesViewModel : DataModelVisualizationViewModel
// Always populate properties
PopulateProperties(dataModelUIService, configuration);
// Only update children if the parent is expanded
if (Parent != null && !Parent.IsRootViewModel && !Parent.IsVisualizationExpanded)
// Only update children if the parent is expanded or when searching
if (Parent != null && !Parent.IsRootViewModel && !Parent.IsVisualizationExpanded && (configuration == null || !configuration.UpdateAllChildren))
return;
foreach (DataModelVisualizationViewModel dataModelVisualizationViewModel in Children)
dataModelVisualizationViewModel.Update(dataModelUIService, configuration);
}
/// <inheritdoc />
public override IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search)
{
return Children.SelectMany(c => c.GetSearchResults(search));
}
/// <inheritdoc />
public override object? GetCurrentValue()
{

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Artemis.Core;
using Artemis.Core.Modules;
using Artemis.UI.Shared.Services;
@ -71,6 +72,17 @@ public class DataModelPropertyViewModel : DataModelVisualizationViewModel
DisplayViewModel?.UpdateValue(DisplayValue);
}
/// <inheritdoc />
public override IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search)
{
if (PropertyDescription?.Name != null && PropertyDescription.Name.Contains(search, StringComparison.OrdinalIgnoreCase))
return [this];
if (DisplayValue != null && DisplayValue.ToString()?.Contains(search, StringComparison.OrdinalIgnoreCase) == true)
return [this];
return [];
}
/// <inheritdoc />
public override string ToString()
{

View File

@ -8,14 +8,21 @@ public class DataModelUpdateConfiguration
/// <summary>
/// Creates a new instance of the <see cref="DataModelUpdateConfiguration" /> class
/// </summary>
/// <param name="createEventChildren">A boolean indicating whether or not event children should be created</param>
public DataModelUpdateConfiguration(bool createEventChildren)
/// <param name="createEventChildren">A boolean indicating whether event children should be created</param>
/// <param name="updateAllChildren">A boolean indicating whether all children should be updated</param>
public DataModelUpdateConfiguration(bool createEventChildren, bool updateAllChildren)
{
CreateEventChildren = createEventChildren;
UpdateAllChildren = updateAllChildren;
}
/// <summary>
/// Gets a boolean indicating whether or not event children should be created
/// Gets a boolean indicating whether event children should be created
/// </summary>
public bool CreateEventChildren { get; }
/// <summary>
/// Gets a boolean indicating whether all children should be updated
/// </summary>
public bool UpdateAllChildren { get; }
}

View File

@ -144,6 +144,13 @@ public abstract class DataModelVisualizationViewModel : ReactiveObject, IDisposa
/// <param name="configuration">The configuration to apply while updating</param>
public abstract void Update(IDataModelUIService dataModelUIService, DataModelUpdateConfiguration? configuration);
/// <summary>
/// Gets the search results for the provided search string
/// </summary>
/// <param name="search">The search string</param>
/// <returns>The search results</returns>
public abstract IEnumerable<DataModelVisualizationViewModel> GetSearchResults(string search);
/// <summary>
/// Gets the current value of the property being visualized
/// </summary>
@ -281,7 +288,7 @@ public abstract class DataModelVisualizationViewModel : ReactiveObject, IDisposa
foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(t => t.MetadataToken))
{
string childPath = AppendToPath(propertyInfo.Name);
if (!ShouldIncludePath(childPath, propertyInfo))
continue;

View File

@ -11,38 +11,43 @@
<Style Selector="dataModelPicker|DataModelPicker">
<Setter Property="Template">
<ControlTemplate>
<Grid RowDefinitions="Auto,Auto,*" Width="600" Height="400" Margin="10">
<TextBox Grid.Row="0" Watermark="Search - not yet implemented 😱" Name="SearchBox" IsEnabled="False" />
<Grid RowDefinitions="Auto,Auto,*" MinWidth="600" MaxHeight="1200" Height="400" Margin="10">
<TextBox Grid.Row="0" Watermark="Search" Name="SearchBox" Classes="search-box" />
<Border Grid.Row="1" Classes="card card-condensed" Margin="0 10">
<Panel>
<Grid ColumnDefinitions="Auto,*"
RowDefinitions="*"
MinHeight="38"
IsVisible="{CompiledBinding DataModelPath, RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNotNull}}">
IsVisible="{CompiledBinding ElementName=CurrentPathDisplay, Path=Text, Converter={x:Static StringConverters.IsNotNullOrEmpty}}">
<avalonia:MaterialIcon Grid.Column="0"
Grid.Row="0"
Name="CurrentPathIcon"
Kind="QuestionMarkCircle"
Grid.Row="0"
Name="CurrentPathIcon"
Kind="QuestionMarkCircle"
Height="22"
Width="22"
Margin="5 0 15 0"
IsVisible="{CompiledBinding !IsEventPicker, RelativeSource={RelativeSource TemplatedParent}}"/>
Margin="5 0 15 0"
IsVisible="{CompiledBinding !IsEventPicker, RelativeSource={RelativeSource TemplatedParent}}" />
<avalonia:MaterialIcon Grid.Column="0"
Grid.Row="0"
Grid.Row="0"
Name="EventIcon"
Kind="LightningBolt"
Height="22"
Width="22"
Width="22"
Margin="5 0 15 0"
IsVisible="{CompiledBinding IsEventPicker, RelativeSource={RelativeSource TemplatedParent}}"/>
IsVisible="{CompiledBinding IsEventPicker, RelativeSource={RelativeSource TemplatedParent}}" />
<StackPanel Grid.Column="1" Grid.Row="0" VerticalAlignment="Center">
<TextBlock Name="CurrentPathDisplay" Classes="BodyStrongTextBlockStyle" MaxHeight="50" />
<TextBlock Name="CurrentPathDescription" Classes="BodyTextBlockStyle" Foreground="{DynamicResource TextFillColorSecondary}" MaxHeight="50" />
<TextBlock Name="CurrentPathDescription"
IsVisible="{CompiledBinding ElementName=CurrentPathDescription, Path=Text, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
Classes="BodyTextBlockStyle"
Foreground="{DynamicResource TextFillColorSecondary}"
MaxHeight="50" />
</StackPanel>
</Grid>
<Grid MinHeight="38"
IsVisible="{CompiledBinding DataModelPath, RelativeSource={RelativeSource TemplatedParent}, Converter={x:Static ObjectConverters.IsNull}}" ColumnDefinitions="*,Auto"
IsVisible="{CompiledBinding ElementName=CurrentPathDisplay, Path=Text, Converter={x:Static StringConverters.IsNullOrEmpty}}"
ColumnDefinitions="*,Auto"
RowDefinitions="*,*">
<TextBlock Grid.Column="0" Grid.Row="0" Classes="BodyStrongTextBlockStyle">Welcome to the data model picker</TextBlock>
<TextBlock Grid.Column="0" Grid.Row="1" Foreground="{DynamicResource TextFillColorSecondary}">Select a value from the data model below</TextBlock>
@ -53,7 +58,8 @@
<TreeView Grid.Row="2"
Name="DataModelTreeView"
ItemsSource="{CompiledBinding DataModelViewModel.Children, RelativeSource={RelativeSource TemplatedParent}}">
ItemsSource="{CompiledBinding DataModelViewModel.Children, RelativeSource={RelativeSource TemplatedParent}}"
IsVisible="False">
<TreeView.Styles>
<Style Selector="TreeViewItem">
<Setter Property="IsExpanded" Value="{CompiledBinding IsVisualizationExpanded, Mode=TwoWay, DataType=dataModel:DataModelVisualizationViewModel}" />
@ -69,7 +75,7 @@
FontFamily="{StaticResource RobotoMono}"
FontSize="13"
HorizontalAlignment="Right"
Margin="0 0 10 0" />
Margin="10 0"/>
</Grid>
</TreeDataTemplate>
@ -79,10 +85,10 @@
<TextBlock Text="{CompiledBinding PropertyDescription.Name}" ToolTip.Tip="{CompiledBinding PropertyDescription.Description}" />
<TextBlock Text=" changed"
ToolTip.Tip="{CompiledBinding PropertyDescription.Description}"
IsVisible="{CompiledBinding IsEventPicker, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dataModelPicker:DataModelPicker}}}"/>
IsVisible="{CompiledBinding IsEventPicker, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dataModelPicker:DataModelPicker}}}" />
</StackPanel>
<ContentControl Grid.Column="1" Content="{CompiledBinding DisplayViewModel}" FontFamily="{StaticResource RobotoMono}" FontSize="13" Margin="0 0 10 0" />
<ContentControl Grid.Column="1" Content="{CompiledBinding DisplayViewModel}" FontFamily="{StaticResource RobotoMono}" FontSize="13" Margin="10 0" />
</Grid>
</TreeDataTemplate>
@ -94,7 +100,7 @@
FontFamily="{StaticResource RobotoMono}"
FontSize="13"
HorizontalAlignment="Right"
Margin="0 0 10 0" />
Margin="10 0" />
</Grid>
</TreeDataTemplate>
@ -103,10 +109,46 @@
</TreeDataTemplate>
</TreeView.DataTemplates>
</TreeView>
<StackPanel Grid.Row="2" VerticalAlignment="Center" Spacing="20" IsVisible="False">
<avalonia:MaterialIcon Kind="CloseCircle" Width="64" Height="64"></avalonia:MaterialIcon>
<TextBlock Classes="h4" TextAlignment="Center">No parts of the data model match your search</TextBlock>
</StackPanel>
<Panel Name="SearchContainer" Grid.Row="2">
<ListBox Name="SearchListBox">
<ListBox.DataTemplates>
<DataTemplate DataType="{x:Type dataModel:DataModelPropertyViewModel}">
<Grid ColumnDefinitions="Auto,*">
<StackPanel Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="{CompiledBinding DisplayPath}" ToolTip.Tip="{CompiledBinding PropertyDescription.Description}" />
<TextBlock Text=" changed"
ToolTip.Tip="{CompiledBinding PropertyDescription.Description}"
IsVisible="{CompiledBinding IsEventPicker, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dataModelPicker:DataModelPicker}}}" />
</StackPanel>
<ContentControl Grid.Column="1" Content="{CompiledBinding DisplayViewModel}" FontFamily="{StaticResource RobotoMono}" FontSize="13" Margin="10 0"/>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type dataModel:DataModelListViewModel}">
<Grid ColumnDefinitions="Auto,*">
<TextBlock Grid.Column="0" Text="{CompiledBinding DisplayPath}" ToolTip.Tip="{CompiledBinding PropertyDescription.Description}" />
<TextBlock Grid.Column="1"
Text="{CompiledBinding CountDisplay, Mode=OneWay}"
FontFamily="{StaticResource RobotoMono}"
FontSize="13"
HorizontalAlignment="Right"
Margin="10 0"/>
</Grid>
</DataTemplate>
<TreeDataTemplate DataType="{x:Type dataModel:DataModelEventViewModel}" ItemsSource="{CompiledBinding Children}">
<TextBlock Text="{CompiledBinding DisplayPath}" ToolTip.Tip="{CompiledBinding PropertyDescription.Description}" />
</TreeDataTemplate>
</ListBox.DataTemplates>
</ListBox>
<StackPanel Name="SearchEmpty" VerticalAlignment="Center" Spacing="20" IsVisible="False">
<avalonia:MaterialIcon Kind="CloseCircle" Width="64" Height="64"></avalonia:MaterialIcon>
<TextBlock Classes="h4" TextAlignment="Center">No parts of the data model match your search</TextBlock>
</StackPanel>
</Panel>
</Grid>
</ControlTemplate>

View File

@ -100,7 +100,7 @@ public partial class DataModelDebugViewModel : ActivatableViewModelBase
lock (MainDataModel)
{
MainDataModel.Update(_dataModelUIService, new DataModelUpdateConfiguration(true));
MainDataModel.Update(_dataModelUIService, new DataModelUpdateConfiguration(true, false));
}
}