|
@@ -0,0 +1,360 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+using System.Windows.Controls;
|
|
|
+using System.Windows.Controls.Primitives;
|
|
|
+using System.Windows.Data;
|
|
|
+using System.Windows.Documents;
|
|
|
+using System.Windows.Input;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+using System.Windows.Navigation;
|
|
|
+using System.Windows.Shapes;
|
|
|
+
|
|
|
+namespace compdfkit_tools.PDFControlUI
|
|
|
+{
|
|
|
+ public partial class CPDFBookmarkResultUI : UserControl
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 书签列表点击选中更改事件
|
|
|
+ /// </summary>
|
|
|
+ public event EventHandler<int> SelectionChanged;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 搜索结果列表点击选中事件
|
|
|
+ /// </summary>
|
|
|
+ public event EventHandler<BookmarkChangeData> BookmarkChanged;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 书签删除点击事件
|
|
|
+ /// </summary>
|
|
|
+ public event EventHandler<BookmarkChangeData> BookmarkDelete;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 点击书签事件
|
|
|
+ /// </summary>
|
|
|
+ public event EventHandler<int> BookmarkClicked;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 绑定书签结果集合
|
|
|
+ /// </summary>
|
|
|
+ private ObservableCollection<BookmarkBindData> bookmarkResults;
|
|
|
+
|
|
|
+ public CPDFBookmarkResultUI()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+ bookmarkResults = new ObservableCollection<BookmarkBindData>();
|
|
|
+ ICollectionView groupView = CollectionViewSource.GetDefaultView(bookmarkResults);
|
|
|
+ groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(BookmarkBindData.ShowPageIndex)));
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 鼠标移入事件 用以控制展示右侧编辑删除面板
|
|
|
+ /// </summary>
|
|
|
+ private void Grid_MouseEnter(object sender, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ Grid sourceGrid=sender as Grid;
|
|
|
+ if(sourceGrid!=null && sourceGrid.Children.Count==2)
|
|
|
+ {
|
|
|
+ Border sourceBorder = sourceGrid.Children[1] as Border;
|
|
|
+ if(sourceBorder!=null)
|
|
|
+ {
|
|
|
+ sourceBorder.Visibility = Visibility.Visible;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 鼠标移出事件 用以控制隐藏右侧编辑 删除面板
|
|
|
+ /// </summary>
|
|
|
+ private void Grid_MouseLeave(object sender, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ Grid sourceGrid = sender as Grid;
|
|
|
+ if (sourceGrid != null && sourceGrid.Children.Count == 2)
|
|
|
+ {
|
|
|
+ Border sourceBorder = sourceGrid.Children[1] as Border;
|
|
|
+ if (sourceBorder != null)
|
|
|
+ {
|
|
|
+ sourceBorder.Visibility = Visibility.Collapsed;
|
|
|
+ }
|
|
|
+ TextBox sourceTextBox = sourceGrid.Children[0] as TextBox;
|
|
|
+ if (sourceTextBox != null)
|
|
|
+ {
|
|
|
+ sourceTextBox.IsReadOnly = true;
|
|
|
+ sourceTextBox.BorderThickness = new Thickness(0);
|
|
|
+ sourceTextBox.IsHitTestVisible = false;
|
|
|
+
|
|
|
+ BookmarkBindData bindData=sourceGrid.Tag as BookmarkBindData;
|
|
|
+ if (bindData!=null && IsBookmarkChanged(bindData,sourceTextBox.Text))
|
|
|
+ {
|
|
|
+ BookmarkChanged?.Invoke(this, new BookmarkChangeData()
|
|
|
+ {
|
|
|
+ PageIndex=bindData.BindProperty.PageIndex,
|
|
|
+ BookmarkTitle=bindData.BindProperty.BookmarkTitle,
|
|
|
+ NewTitle=sourceTextBox.Text
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 编辑按钮点击事件 启用书签文本编辑
|
|
|
+ /// </summary>
|
|
|
+ private void EditBorder_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ Border sourceBtn =sender as Border;
|
|
|
+ if(sourceBtn != null)
|
|
|
+ {
|
|
|
+ DependencyObject findElement = null;
|
|
|
+ if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
|
|
|
+ {
|
|
|
+ Grid sourceGrid = findElement as Grid;
|
|
|
+ if (sourceGrid != null && sourceGrid.Children.Count == 2)
|
|
|
+ {
|
|
|
+ TextBox sourceTextBox = sourceGrid.Children[0] as TextBox;
|
|
|
+ if (sourceTextBox != null)
|
|
|
+ {
|
|
|
+ sourceTextBox.IsReadOnly=false;
|
|
|
+ sourceTextBox.IsHitTestVisible = true;
|
|
|
+ sourceTextBox.BorderThickness=new Thickness(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除按钮点击事件 触发删除事件通知
|
|
|
+ /// </summary>
|
|
|
+ private void DelBorder_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ Border sourceBtn = sender as Border;
|
|
|
+ if (sourceBtn != null)
|
|
|
+ {
|
|
|
+ DependencyObject findElement = null;
|
|
|
+ if (FindParent<Grid>(sourceBtn, out findElement) && findElement != null)
|
|
|
+ {
|
|
|
+ Grid sourceGrid = findElement as Grid;
|
|
|
+ BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
|
|
|
+ if (bindData != null)
|
|
|
+ {
|
|
|
+ BookmarkDelete?.Invoke(this, new BookmarkChangeData()
|
|
|
+ {
|
|
|
+ PageIndex = bindData.BindProperty.PageIndex,
|
|
|
+ BookmarkTitle = bindData.BindProperty.BookmarkTitle
|
|
|
+ });
|
|
|
+ bookmarkResults?.Remove(bindData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 递归查询指定类型的父节点元素
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="T">父元素节点类型</typeparam>
|
|
|
+ /// <param name="checkElement">要查找的元素</param>
|
|
|
+ /// <param name="parent">查找到的父节点元素</param>
|
|
|
+ /// <returns>查找到对应类型父节点元素则为true</returns>
|
|
|
+ private bool FindParent<T>(DependencyObject checkElement,out DependencyObject parent)
|
|
|
+ {
|
|
|
+ parent = null;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (checkElement != null)
|
|
|
+ {
|
|
|
+ DependencyObject parentElement= VisualTreeHelper.GetParent(checkElement);
|
|
|
+ if(parentElement!=null && parentElement is T)
|
|
|
+ {
|
|
|
+ parent =parentElement;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return FindParent<T>(parentElement,out parent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 判断书签文本是否更改
|
|
|
+ /// </summary>
|
|
|
+ private bool IsBookmarkChanged(BookmarkBindData bindData,string newTitle)
|
|
|
+ {
|
|
|
+ if(bindData!=null &&bindData.BindProperty!=null)
|
|
|
+ {
|
|
|
+ return bindData.BindProperty.BookmarkTitle != newTitle;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 将书签列表绑定到UI控件
|
|
|
+ /// </summary>
|
|
|
+ public void SetBookmarkResult(List<BindBookmarkResult> results)
|
|
|
+ {
|
|
|
+ bookmarkResults?.Clear();
|
|
|
+ if (results == null || results.Count == 0)
|
|
|
+ {
|
|
|
+ ResultListControl.ItemsSource = null;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (BindBookmarkResult item in results)
|
|
|
+ {
|
|
|
+ bookmarkResults.Add(new BookmarkBindData()
|
|
|
+ {
|
|
|
+ BindProperty = item
|
|
|
+ });
|
|
|
+ }
|
|
|
+ ResultListControl.ItemsSource = bookmarkResults;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取选中书签结果
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>搜索结果</returns>
|
|
|
+ public BindBookmarkResult GetSelectItem()
|
|
|
+ {
|
|
|
+ BookmarkBindData bindData = ResultListControl.SelectedItem as BookmarkBindData;
|
|
|
+ if (bindData != null)
|
|
|
+ {
|
|
|
+ return bindData.BindProperty;
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定索引书签对象
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="index">指定索引</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public BindBookmarkResult GetItem(int index)
|
|
|
+ {
|
|
|
+ if (index < 0)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (ResultListControl.HasItems && ResultListControl.Items.Count > index)
|
|
|
+ {
|
|
|
+ BookmarkBindData bindData = ResultListControl.Items[index] as BookmarkBindData;
|
|
|
+ if (bindData != null)
|
|
|
+ {
|
|
|
+ return bindData.BindProperty;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 清除选中结果
|
|
|
+ /// </summary>
|
|
|
+ public void ClearSelection()
|
|
|
+ {
|
|
|
+ int oldSelectionIndex = ResultListControl.SelectedIndex;
|
|
|
+ ResultListControl.UnselectAll();
|
|
|
+ if (oldSelectionIndex != ResultListControl.SelectedIndex)
|
|
|
+ {
|
|
|
+ SelectionChanged?.Invoke(this,ResultListControl.SelectedIndex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置选中结果
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="selectIndex">选中索引</param>
|
|
|
+ public void SelectItem(int selectIndex)
|
|
|
+ {
|
|
|
+ if (ResultListControl.SelectedIndex != selectIndex)
|
|
|
+ {
|
|
|
+ ResultListControl.SelectedIndex = selectIndex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 书签列表选中改变事件
|
|
|
+ /// </summary>
|
|
|
+ private void ResultListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
+ {
|
|
|
+ SelectionChanged?.Invoke(this, ResultListControl.SelectedIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 书签列表点击书签事件
|
|
|
+ /// </summary>
|
|
|
+ private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
+ {
|
|
|
+ Grid sourceGrid = sender as Grid;
|
|
|
+ if (sourceGrid != null)
|
|
|
+ {
|
|
|
+ BookmarkBindData bindData = sourceGrid.Tag as BookmarkBindData;
|
|
|
+ if (bindData != null)
|
|
|
+ {
|
|
|
+ BookmarkClicked?.Invoke(this, bindData.BindProperty.PageIndex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 点击空白清除选中项
|
|
|
+ /// </summary>
|
|
|
+ private void ResultListControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
+ {
|
|
|
+ ResultListControl?.UnselectAll();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class BookmarkChangeData
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 页面索引
|
|
|
+ /// </summary>
|
|
|
+ public int PageIndex { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 原书签标题
|
|
|
+ /// </summary>
|
|
|
+ public string BookmarkTitle { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 修改后的标题
|
|
|
+ /// </summary>
|
|
|
+ public string NewTitle { get;set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class BindBookmarkResult
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 页面索引
|
|
|
+ /// </summary>
|
|
|
+ public int PageIndex { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 书签标题
|
|
|
+ /// </summary>
|
|
|
+ public string BookmarkTitle { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ internal class BookmarkBindData
|
|
|
+ {
|
|
|
+ public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
|
|
|
+ public BindBookmarkResult BindProperty { get; set; }
|
|
|
+ public BookmarkBindData()
|
|
|
+ {
|
|
|
+ BindProperty = new BindBookmarkResult();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|