CPdfThumbnailUI.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. else
  38. {
  39. lockGoToPage = false;
  40. }
  41. }
  42. /// <summary>
  43. /// Set the thumbnail list
  44. /// </summary>
  45. /// <param name="thumbList"></param>
  46. public void SetThumbResult(List<ThumbnailItem> thumbList)
  47. {
  48. thumbResultList?.Clear();
  49. ThumbListBox.ItemsSource = null;
  50. if (thumbList == null || thumbList.Count == 0)
  51. {
  52. return;
  53. }
  54. thumbResultList.AddRange(thumbList);
  55. ThumbListBox.ItemsSource = thumbResultList;
  56. ThumbListBox.UpdateLayout();
  57. }
  58. /// <summary>
  59. /// Content scrolling events
  60. /// </summary>
  61. private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  62. {
  63. ViewChanged?.Invoke(this, e);
  64. }
  65. /// <summary>
  66. /// Determines whether the specified object is visible
  67. /// </summary>
  68. public bool IsItemVisible(ThumbnailItem checkItem)
  69. {
  70. if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0)
  71. {
  72. return false;
  73. }
  74. try
  75. {
  76. int index = thumbResultList.IndexOf(checkItem);
  77. if (index != -1)
  78. {
  79. ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index);
  80. if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible)
  81. {
  82. return false;
  83. }
  84. GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox);
  85. Rect visualRect= transform.TransformBounds(new Rect(
  86. 0,
  87. 0,
  88. itemContainer.ActualWidth,
  89. itemContainer.ActualHeight
  90. ));
  91. Rect containerRect=new Rect(
  92. 0,
  93. 0,
  94. ThumbListBox.ActualWidth,
  95. ThumbListBox.ActualHeight);
  96. containerRect.Intersect(visualRect);
  97. if (containerRect.Width>1 && containerRect.Height>1)
  98. {
  99. return true;
  100. }
  101. }
  102. }
  103. catch(Exception ex)
  104. {
  105. }
  106. return false;
  107. }
  108. /// <summary>
  109. /// Select an object
  110. /// </summary>
  111. public void SelectItem(int checkIndex)
  112. {
  113. if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0)
  114. {
  115. ThumbnailItem thumbItem= thumbResultList[checkIndex];
  116. if(IsItemVisible(thumbItem)==false)
  117. {
  118. ThumbListBox.ScrollIntoView(thumbItem);
  119. }
  120. ThumbListBox.SelectedIndex = checkIndex;
  121. }
  122. }
  123. /// <summary>
  124. /// Select an object
  125. /// </summary>
  126. public void SelectItemWithoutGoTo(int checkIndex)
  127. {
  128. if (ThumbListBox != null && thumbResultList != null && thumbResultList.Count > checkIndex && checkIndex >= 0)
  129. {
  130. ThumbnailItem thumbItem = thumbResultList[checkIndex];
  131. if (IsItemVisible(thumbItem) == false)
  132. {
  133. ThumbListBox.ScrollIntoView(thumbItem);
  134. }
  135. lockGoToPage = true;
  136. ThumbListBox.SelectedIndex = checkIndex;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Thumbnail object
  142. /// </summary>
  143. public class ThumbnailItem
  144. {
  145. /// <summary>
  146. /// Image width
  147. /// </summary>
  148. public int ImageWidth { get; set; }
  149. /// <summary>
  150. /// Image height
  151. /// </summary>
  152. public int ImageHeight { get; set; }
  153. /// <summary>
  154. ///Thumbnail width
  155. /// </summary>
  156. public int ThumbnailWidth { get; set; }
  157. /// <summary>
  158. /// Thumbnail height
  159. /// </summary>
  160. public int ThumbnailHeight { get; set; }
  161. /// <summary>
  162. /// Display page numbers
  163. /// </summary>
  164. public string ShowPageText
  165. {
  166. get
  167. {
  168. if (PageIndex >= 0)
  169. {
  170. return (PageIndex + 1).ToString();
  171. }
  172. return string.Empty;
  173. }
  174. }
  175. /// <summary>
  176. /// Page index (starts with 0)
  177. /// </summary>
  178. public int PageIndex { get; set; } = -1;
  179. /// <summary>
  180. /// Thumbnails are like content
  181. /// </summary>
  182. public Image ImageData { get; set; } = new Image();
  183. }
  184. }