From d0bc0b686498c28103631d7228b74ae3c72e52a7 Mon Sep 17 00:00:00 2001 From: azakhi Date: Fri, 24 Aug 2018 18:03:38 +0300 Subject: [PATCH] Add section search feature --- .../ToolPanes/CommentBlockBrowserViewModel.cs | 57 ++++++++++++++++++- .../AvalonDock/AvalonDockWorkspaceView.xaml | 2 +- .../ToolPanes/CommentBlockBrowserView.xaml | 7 ++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/Filtration/ViewModels/ToolPanes/CommentBlockBrowserViewModel.cs b/Filtration/ViewModels/ToolPanes/CommentBlockBrowserViewModel.cs index e2c9880..9dc542a 100644 --- a/Filtration/ViewModels/ToolPanes/CommentBlockBrowserViewModel.cs +++ b/Filtration/ViewModels/ToolPanes/CommentBlockBrowserViewModel.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Text.RegularExpressions; +using System.Windows.Data; using System.Windows.Media.Imaging; using GalaSoft.MvvmLight.Messaging; @@ -15,6 +17,8 @@ namespace Filtration.ViewModels.ToolPanes { private IEnumerable _itemFilterCommentBlockViewModels; private IItemFilterCommentBlockViewModel _selectedItemFilterCommentBlockViewModel; + private CollectionView _commentBlocksView; + private string _searchText; public CommentBlockBrowserViewModel() : base("Section Browser") { @@ -24,7 +28,9 @@ namespace Filtration.ViewModels.ToolPanes icon.UriSource = new Uri("pack://application:,,,/Filtration;component/Resources/Icons/add_section_icon.png"); icon.EndInit(); IconSource = icon; - + _searchText = ""; + _commentBlocksView = (CollectionView)CollectionViewSource.GetDefaultView(new List()); + Messenger.Default.Register(this, message => { switch (message.Notification) @@ -46,7 +52,23 @@ namespace Filtration.ViewModels.ToolPanes private set { _itemFilterCommentBlockViewModels = value; - RaisePropertyChanged(); + + _commentBlocksView = _itemFilterCommentBlockViewModels != null ? + (CollectionView)CollectionViewSource.GetDefaultView(_itemFilterCommentBlockViewModels) + : (CollectionView)CollectionViewSource.GetDefaultView(new List()); + + _commentBlocksView.Filter = SearchFilter; + _commentBlocksView.Refresh(); + RaisePropertyChanged("CommentBlocksView"); + } + } + + public CollectionView CommentBlocksView + { + get => _commentBlocksView; + private set + { + _commentBlocksView = value; } } @@ -64,6 +86,18 @@ namespace Filtration.ViewModels.ToolPanes } } + public string SearchText + { + get => _searchText; + set + { + _searchText = value; + _commentBlocksView.Refresh(); + RaisePropertyChanged(); + RaisePropertyChanged("CommentBlocksView"); + } + } + protected override void OnActiveDocumentChanged(object sender, EventArgs e) { if (AvalonDockWorkspaceViewModel.ActiveScriptViewModel != null && AvalonDockWorkspaceViewModel.ActiveDocument.IsScript) @@ -81,5 +115,24 @@ namespace Filtration.ViewModels.ToolPanes ItemFilterCommentBlockViewModels = null; SelectedItemFilterCommentBlockViewModel = null; } + + private bool SearchFilter(object obj) + { + if (string.IsNullOrEmpty(_searchText)) + return true; + + var block = obj as IItemFilterCommentBlockViewModel; + var searchWords = Regex.Split(_searchText, @"\s+"); + foreach(var word in searchWords) + { + if (string.IsNullOrEmpty(word)) + continue; + + if (block.Comment.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0) + return true; + } + + return false; + } } } diff --git a/Filtration/Views/AvalonDock/AvalonDockWorkspaceView.xaml b/Filtration/Views/AvalonDock/AvalonDockWorkspaceView.xaml index 2ce3949..fdd92e3 100644 --- a/Filtration/Views/AvalonDock/AvalonDockWorkspaceView.xaml +++ b/Filtration/Views/AvalonDock/AvalonDockWorkspaceView.xaml @@ -94,7 +94,7 @@ - + diff --git a/Filtration/Views/ToolPanes/CommentBlockBrowserView.xaml b/Filtration/Views/ToolPanes/CommentBlockBrowserView.xaml index 1098bcd..6ddecca 100644 --- a/Filtration/Views/ToolPanes/CommentBlockBrowserView.xaml +++ b/Filtration/Views/ToolPanes/CommentBlockBrowserView.xaml @@ -13,7 +13,12 @@ - + + + + +