CPdfThumbnailUI.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. namespace Compdfkit_Tools.PDFControlUI
  8. {
  9. public partial class CPDFThumbnailUI : UserControl
  10. {
  11. /// <summary>
  12. /// Click to select the event in the thumbnail list
  13. /// </summary>
  14. public event EventHandler<int> SelectionChanged;
  15. /// <summary>
  16. /// Scroll state change event
  17. /// </summary>
  18. public event EventHandler<ScrollChangedEventArgs> ViewChanged;
  19. private bool lockGoToPage;
  20. /// <summary>
  21. /// A list of thumbnail results
  22. /// </summary>
  23. private List<ThumbnailItem> thumbResultList=new List<ThumbnailItem>();
  24. public CPDFThumbnailUI()
  25. {
  26. InitializeComponent();
  27. }
  28. /// <summary>
  29. /// Select Result Change event
  30. /// </summary>
  31. private void ThumbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  32. {
  33. if (lockGoToPage)
  34. {
  35. SelectionChanged?.Invoke(this, ThumbListBox.SelectedIndex);
  36. }
  37. lockGoToPage = false;
  38. }
  39. /// <summary>
  40. /// Set the thumbnail list
  41. /// </summary>
  42. /// <param name="thumbList"></param>
  43. public void SetThumbResult(List<ThumbnailItem> thumbList)
  44. {
  45. thumbResultList?.Clear();
  46. ThumbListBox.ItemsSource = null;
  47. if (thumbList == null || thumbList.Count == 0)
  48. {
  49. return;
  50. }
  51. thumbResultList.AddRange(thumbList);
  52. ThumbListBox.ItemsSource = thumbResultList;
  53. ThumbListBox.UpdateLayout();
  54. }
  55. /// <summary>
  56. /// Content scrolling events
  57. /// </summary>
  58. private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  59. {
  60. ViewChanged?.Invoke(this, e);
  61. }
  62. /// <summary>
  63. /// Determines whether the specified object is visible
  64. /// </summary>
  65. public bool IsItemVisible(ThumbnailItem checkItem)
  66. {
  67. if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0)
  68. {
  69. return false;
  70. }
  71. try
  72. {
  73. int index = thumbResultList.IndexOf(checkItem);
  74. if (index != -1)
  75. {
  76. ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index);
  77. if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible)
  78. {
  79. return false;
  80. }
  81. GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox);
  82. Rect visualRect= transform.TransformBounds(new Rect(
  83. 0,
  84. 0,
  85. itemContainer.ActualWidth,
  86. itemContainer.ActualHeight
  87. ));
  88. Rect containerRect=new Rect(
  89. 0,
  90. 0,
  91. ThumbListBox.ActualWidth,
  92. ThumbListBox.ActualHeight);
  93. containerRect.Intersect(visualRect);
  94. if (containerRect.Width>1 && containerRect.Height>1)
  95. {
  96. return true;
  97. }
  98. }
  99. }
  100. catch(Exception ex)
  101. {
  102. }
  103. return false;
  104. }
  105. /// <summary>
  106. /// Select an object
  107. /// </summary>
  108. public void SelectItem(int checkIndex)
  109. {
  110. if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0)
  111. {
  112. ThumbnailItem thumbItem= thumbResultList[checkIndex];
  113. if(IsItemVisible(thumbItem)==false)
  114. {
  115. ThumbListBox.ScrollIntoView(thumbItem);
  116. }
  117. ThumbListBox.SelectedIndex = checkIndex;
  118. }
  119. }
  120. /// <summary>
  121. /// Select an object
  122. /// </summary>
  123. public void SelectItemWithoutGoTo(int checkIndex)
  124. {
  125. if (ThumbListBox != null && thumbResultList != null && thumbResultList.Count > checkIndex && checkIndex >= 0)
  126. {
  127. ThumbnailItem thumbItem = thumbResultList[checkIndex];
  128. if (IsItemVisible(thumbItem) == false)
  129. {
  130. ThumbListBox.ScrollIntoView(thumbItem);
  131. }
  132. lockGoToPage = true;
  133. ThumbListBox.SelectedIndex = checkIndex;
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Thumbnail object
  139. /// </summary>
  140. public class ThumbnailItem
  141. {
  142. /// <summary>
  143. /// Image width
  144. /// </summary>
  145. public int ImageWidth { get; set; }
  146. /// <summary>
  147. /// Image height
  148. /// </summary>
  149. public int ImageHeight { get; set; }
  150. /// <summary>
  151. ///Thumbnail width
  152. /// </summary>
  153. public int ThumbnailWidth { get; set; }
  154. /// <summary>
  155. /// Thumbnail height
  156. /// </summary>
  157. public int ThumbnailHeight { get; set; }
  158. /// <summary>
  159. /// Display page numbers
  160. /// </summary>
  161. public string ShowPageText
  162. {
  163. get
  164. {
  165. if (PageIndex >= 0)
  166. {
  167. return (PageIndex + 1).ToString();
  168. }
  169. return string.Empty;
  170. }
  171. }
  172. /// <summary>
  173. /// Page index (starts with 0)
  174. /// </summary>
  175. public int PageIndex { get; set; } = -1;
  176. /// <summary>
  177. /// Thumbnails are like content
  178. /// </summary>
  179. public Image ImageData { get; set; } = new Image();
  180. }
  181. }