ソースを参照

compdfkit(win) - 优化搜索性能,进度条

weixiangjie 11 ヶ月 前
コミット
2db5235331

+ 2 - 2
Demo/Examples/Compdfkit_Tools/Common/BaseControl/CustomProgressBarControl.xaml

@@ -10,7 +10,7 @@
         <local:CommonResourceConverter x:Key="CommonResourceConverter"></local:CommonResourceConverter>
     </UserControl.Resources>
 
-    <Grid x:Name="Grid">
+    <Grid x:Name="Grid" Margin="5,0,5,0">
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="Auto"></ColumnDefinition>
             <ColumnDefinition Width="*"></ColumnDefinition>
@@ -19,7 +19,7 @@
 
         <TextBlock Text="{Binding Converter={StaticResource CommonResourceConverter},ConverterParameter=Property_Progress}" FontFamily="Microsoft YaHei" FontSize="14" VerticalAlignment="Center"></TextBlock>
         
-        <ProgressBar Grid.Column="1" Height="5" Foreground="#0078D7" Margin="5,0,5,0" Value="{Binding ProgressValue}" Maximum="{Binding ProgressMaxValue}"></ProgressBar>
+        <ProgressBar Grid.Column="1" Height="{Binding ProgressBarHeight}" Foreground="#0078D7" Margin="5,0,5,0" Value="{Binding ProgressValue}" Maximum="{Binding ProgressMaxValue}"></ProgressBar>
         
         <StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
             <TextBlock Text="{Binding ProgressValue}" FontFamily="Microsoft YaHei" FontSize="14"></TextBlock>

+ 8 - 0
Demo/Examples/Compdfkit_Tools/Common/BaseControl/CustomProgressBarControl.xaml.cs

@@ -24,6 +24,14 @@ namespace Compdfkit_Tools.Common
             set => SetValue(ProgressMaxValueProperty, value);
         }
         
+        public static readonly DependencyProperty ProgressBarHeightProperty =
+            DependencyProperty.Register(nameof(ProgressBarHeight), typeof(double), typeof(CustomProgressBarControl), new PropertyMetadata(5.0));
+        public double ProgressBarHeight
+        {
+            get => (double)GetValue(ProgressBarHeightProperty);
+            set => SetValue(ProgressBarHeightProperty, value);
+        }
+        
         public CustomProgressBarControl()
         {
             InitializeComponent();

+ 35 - 4
Demo/Examples/Compdfkit_Tools/PDFView/PDFSearch/PDFSearchControl/CPDFSearchControl.xaml

@@ -7,15 +7,16 @@
              xmlns:ui="clr-namespace:Compdfkit_Tools.PDFControlUI"
              xmlns:common="clr-namespace:Compdfkit_Tools.Common"
              mc:Ignorable="d" 
-             d:DesignHeight="450" d:DesignWidth="800">
+             d:DesignHeight="450" d:DesignWidth="800" Background="White">
     <UserControl.Resources>
         <common:BotaResourceConverter x:Key="BotaResourceConverter" />
     </UserControl.Resources>
     <Grid>
         <Grid.RowDefinitions>
-            <RowDefinition Height="auto"></RowDefinition>
-            <RowDefinition Height="auto"></RowDefinition>
+            <RowDefinition Height="Auto"></RowDefinition>
+            <RowDefinition Height="Auto"></RowDefinition>
             <RowDefinition Height="*"></RowDefinition>
+            <RowDefinition Height="Auto"></RowDefinition>
         </Grid.RowDefinitions>
         <ui:CPDFSearchInputUI x:Name="SearchInput" InputTextBoxHeight="25"></ui:CPDFSearchInputUI>
         
@@ -25,11 +26,41 @@
                     <ColumnDefinition Width="*"></ColumnDefinition>
                     <ColumnDefinition Width="auto"></ColumnDefinition>
                 </Grid.ColumnDefinitions>
-                <TextBlock Name="ResultText" Foreground="#666666" VerticalAlignment="Center" Margin="16,0,0,0"></TextBlock>
+                <StackPanel Orientation="Horizontal" Margin="16,0,0,0">
+                    <TextBlock Name="ResultNum" Foreground="#666666" VerticalAlignment="Center"></TextBlock>
+                    <TextBlock Name="ResultText" Foreground="#666666" VerticalAlignment="Center" Margin="3,0,0,0"></TextBlock>
+                </StackPanel>
                 <TextBlock Grid.Column="1" VerticalAlignment="Center" Margin="0,0,16,0" Foreground="#666666"
                            Text="{Binding Converter={StaticResource BotaResourceConverter},ConverterParameter=Text_Page}"></TextBlock>
             </Grid>
         </Border>
         <ui:CPDFSearchResultUI x:Name="SearchResult" Grid.Row="2"></ui:CPDFSearchResultUI>
+        <Border Grid.Row="3" x:Name="ProgressBorder" Visibility="Hidden">
+            <Grid>
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition></ColumnDefinition>
+                    <ColumnDefinition Width="auto"></ColumnDefinition>
+                </Grid.ColumnDefinitions>
+                <Rectangle Height="1" VerticalAlignment="Top" Fill="#50000000" Grid.ColumnSpan="2"></Rectangle>
+                <common:CustomProgressBarControl x:Name="ProgressBar" Height="30" ProgressBarHeight="10" ></common:CustomProgressBarControl>
+                <Button Grid.Column="1" HorizontalAlignment="Right" Width="20" Height="20" Background="#01000000" BorderThickness="0" Margin="10,0,5,0" Click="SearchCancel_Click">
+                    <Path Fill="Black" IsHitTestVisible="False">
+                        <Path.RenderTransform>
+                            <TransformGroup>
+                                <TranslateTransform X="8" Y="-8"/>
+                                <RotateTransform Angle="45"/>
+                                <TranslateTransform X="-4" Y="-5"/>
+                            </TransformGroup>
+                        </Path.RenderTransform>
+                        <Path.Data>
+                            M9.48528137,2.98528137 L9.48428137,8.48428137 L14.9852814,8.48528137 L14.9852814,9.48528137 L9.48428137,9.48428137 L9.48528137,14.9852814 L8.48528137,14.9852814 
+                            L8.48428137,9.48428137 L2.98528137,9.48528137 L2.98528137,8.48528137 L8.48428137,8.48428137 L8.48528137,2.98528137 L9.48528137,2.98528137 Z
+                        </Path.Data>
+                    </Path>
+                </Button>
+            </Grid>
+        </Border>
+        
+        
     </Grid>
 </UserControl>

+ 88 - 35
Demo/Examples/Compdfkit_Tools/PDFView/PDFSearch/PDFSearchControl/CPDFSearchControl.xaml.cs

@@ -3,6 +3,8 @@ using Compdfkit_Tools.PDFControlUI;
 using ComPDFKitViewer.PdfViewer;
 using System;
 using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Media;
@@ -21,10 +23,12 @@ namespace Compdfkit_Tools.PDFControl
         private PDFTextSearch textSearch;
 
         private string keyWord;
+        
+        private bool isClearResult = false;
+        
 
         private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
-
-        private int ResultCount = 0;
+        
         public CPDFSearchControl()
         {
             InitializeComponent();
@@ -43,15 +47,75 @@ namespace Compdfkit_Tools.PDFControl
         private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
         {
             textSearch = new PDFTextSearch();
+            
+            SearchInput.SearchEvent -= SearchInput_SearchEvent;
+            SearchInput.ClearEvent -= SearchInput_ClearEvent;
+            textSearch.SearchCompletedHandler -= TextSearch_SearchCompletedHandler;
+            SearchResult.SelectionChanged -= SearchResult_SelectionChanged;
+            textSearch.SearchPercentHandler -= TextSearch_SearchPercentHandler;
+            textSearch.SearchCancelHandler -= TextSearch_SearchCancelHandler;
+            
             SearchInput.SearchEvent += SearchInput_SearchEvent;
             SearchInput.ClearEvent += SearchInput_ClearEvent;
             textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
             SearchResult.SelectionChanged += SearchResult_SelectionChanged;
+            textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
+            textSearch.SearchCancelHandler += TextSearch_SearchCancelHandler;
+        }
+
+        private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
+        {
+            Dispatcher.Invoke(() =>
+            {
+                if(!e.Items.ContainsKey(e.CurrentPage))
+                {
+                    return;
+                }
+                ProgressBar.ProgressValue = e.CurrentPage + 1;
+                foreach (var item in e.Items[e.CurrentPage])
+                {
+                    if (item == null)
+                    {
+                        continue;
+                    }
+                    
+                    var result = new BindSearchResult()
+                    {
+                        PageIndex = item.PageIndex,
+                        TextContent = item.TextContent,
+                        TextRect = item.TextRect,
+                        SearchWord = keyWord,
+                        HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
+                        PageRotate = item.PageRotate
+                    };
+                    SearchResult.AddSearchResult(result);
+                    ResultNum.Text = SearchResult.ResultListControl.Items.Count.ToString();
+                }
+                
+            });
         }
 
         private void SearchInput_ClearEvent(object sender, EventArgs e)
         {
-            ClearSearchResult();
+            if (textSearch.CanDoSearch)
+            {
+                Dispatcher.Invoke(ClearSearchResult);
+            }
+            else
+            {
+                isClearResult = true;
+                textSearch.CancleSearch();
+            }
+            ProgressBorder.Visibility = Visibility.Hidden;
+        }
+        
+        private void TextSearch_SearchCancelHandler(object sender, TextSearchResult e)
+        {
+            if (isClearResult)
+            {
+                Dispatcher.Invoke(ClearSearchResult);
+                isClearResult = false;
+            }
         }
 
         private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
@@ -76,45 +140,22 @@ namespace Compdfkit_Tools.PDFControl
 
         private void SearchResult_SelectionChanged(object sender, int e)
         {
+            if (e < 0)
+            {
+                return;
+            }
             currentHighLightIndex = e;
             BindSearchResult result = SearchResult.GetSelectItem();
             HighLightSelectResult(result);
-            ResultText.Text = LanguageHelper.BotaManager.GetString("Text_Result")+ (e+1) + "/" + ResultCount;
+            ResultNum.Text = "";
+            ResultText.Text = LanguageHelper.BotaManager.GetString("Text_Result")+ (e+1) + "/" + SearchResult.ResultListControl.Items.Count;
         }
 
         private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
         {
             Dispatcher.Invoke(() =>
             {
-                List<BindSearchResult> resultList = new List<BindSearchResult>();
-
-                foreach (int pageIndex in e.Items.Keys)
-                {
-                    List<TextSearchItem> textSearchItems = e.Items[pageIndex];
-                    foreach (TextSearchItem item in textSearchItems)
-                    {
-                        resultList.Add(new BindSearchResult()
-                        {
-                            PageIndex = item.PageIndex,
-                            TextContent = item.TextContent,
-                            TextRect = item.TextRect,
-                            SearchWord = keyWord,
-                            HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
-                            PageRotate = item.PageRotate
-                        });
-                    }
-                }
-                SearchResult.SetSearchResult(resultList);
-                ResultCount=resultList.Count;
-
-                if (resultList==null ||  resultList.Count==0)
-                {
-                    ResultText.Text= string.Empty;
-                }
-                else
-                {
-                    ResultText.Text = ResultCount + " " + LanguageHelper.BotaManager.GetString("Tip_Result");
-                }
+                ProgressBorder.Visibility = Visibility.Hidden;
             });
         }
 
@@ -132,9 +173,15 @@ namespace Compdfkit_Tools.PDFControl
 
             if (textSearch != null && textSearch.CanDoSearch)
             {
+                ResultNum.Text = "0";
+                ResultText.Text = LanguageHelper.BotaManager.GetString("Tip_Result");
+                ProgressBar.ProgressMaxValue = pdfView.Document.PageCount;
+                ProgressBorder.Visibility = Visibility.Visible;
+                
                 keyWord = e;
                 textSearch.TextSearchDocument = pdfView.Document;
-                textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
+                SearchResult.ClearSearchResult();
+                textSearch.SearchText(keyWord,C_Search_Options.Search_Case_Insensitive);
             }
         }
 
@@ -159,9 +206,15 @@ namespace Compdfkit_Tools.PDFControl
 
         private void ClearSearchResult()
         {
-            SearchResult?.SetSearchResult(null);
+            SearchResult?.ClearSearchResult();
+            ResultNum.Text = string.Empty;
             ResultText.Text = string.Empty;
             SearchInput.SearchKeyWord=string.Empty;
         }
+
+        private void SearchCancel_Click(object sender, RoutedEventArgs e)
+        {
+            textSearch.CancleSearch();
+        }
     }
 }

+ 12 - 11
Demo/Examples/Compdfkit_Tools/PDFView/PDFSearch/PDFSearchUI/CPDFSearchResultUI.xaml.cs

@@ -40,23 +40,24 @@ namespace Compdfkit_Tools.PDFControlUI
             InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
         }
 
-        public void SetSearchResult(List<BindSearchResult> results)
+        public void ClearSearchResult()
         {
             searchResults.Clear();
-            if (results == null || results.Count == 0)
+            ResultListControl.ItemsSource = null;
+            ResultListControl.Visibility = Visibility.Collapsed;
+            NoResultText.Visibility = Visibility.Visible;
+        }
+        
+        public void AddSearchResult(BindSearchResult result)
+        {
+            if (result == null)
             {
-                ResultListControl.ItemsSource = null;
-                NoResultText.Visibility = Visibility.Visible;
                 return;
             }
-
-            foreach (BindSearchResult item in results)
+            searchResults.Add(new TextBindData()
             {
-                searchResults.Add(new TextBindData()
-                {
-                    BindProperty = item
-                });
-            }
+                BindProperty = result
+            });
             ResultListControl.ItemsSource = searchResults;
             ResultListControl.Visibility = Visibility.Visible;
             NoResultText.Visibility = Visibility.Collapsed;