CPDFBOTABarControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using ComPDFKit.PDFDocument;
  2. using compdfkit_tools.PDFControlUI;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace compdfkit_tools.PDFControl
  20. {
  21. public enum BOTATools
  22. {
  23. Bookmark = 1 << 0,
  24. Outline = 1 << 1,
  25. Thumbnail = 1 << 2,
  26. Annotation = 1 << 3,
  27. Search = 1 << 4,
  28. }
  29. /// <summary>
  30. /// BOTABarControl.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class CPDFBOTABarControl : UserControl
  33. {
  34. private CPDFViewer pdfViewer;
  35. private ToggleButton bookmarkButton;
  36. private ToggleButton outlineButton;
  37. private ToggleButton thumbnailButton;
  38. private ToggleButton annotButton;
  39. private ToggleButton searchButton;
  40. public CPDFBOTABarControl(BOTATools botaTools)
  41. {
  42. InitializeComponent();
  43. BOTABarTitleGrid.ColumnDefinitions.Clear();
  44. InitBOTABar(botaTools);
  45. AddBOTAContent(botaTools);
  46. }
  47. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  48. {
  49. this.pdfViewer = pdfViewer;
  50. }
  51. public void InitBOTABar(BOTATools botaTools)
  52. {
  53. foreach (BOTATools item in Enum.GetValues(typeof(BOTATools)))
  54. {
  55. if ((item & botaTools) > 0)
  56. {
  57. BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition());
  58. }
  59. else if ((item & botaTools) > 0)
  60. {
  61. BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition());
  62. }
  63. else if ((item & botaTools) > 0)
  64. {
  65. BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition());
  66. }
  67. else if ((item & botaTools) > 0)
  68. {
  69. BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition());
  70. }
  71. else if ((item & botaTools) > 0)
  72. {
  73. BOTABarTitleGrid.ColumnDefinitions.Add(new ColumnDefinition());
  74. }
  75. }
  76. }
  77. public void AddBOTAContent(BOTATools botaTools)
  78. {
  79. int botaCounter = 0;
  80. var brush = (Brush)FindResource("btn.bg.bota");
  81. while (botaTools > 0)
  82. {
  83. if ((botaTools & BOTATools.Bookmark) > 0)
  84. {
  85. bookmarkButton = new ToggleButton();
  86. bookmarkButton.Background = brush;
  87. Geometry bookmarkGeometry = Geometry.Parse("M17.5,1.25 L2.5,1.25 L2.5,19.1421541 L10,15.3425 L17.5,19.1421541 L17.5,1.25 Z M16.25,2.5 L16.25,17.1075 L10,13.9422615 L3.75,17.10625 L3.75,2.5 L16.25,2.5 Z");
  88. Path path = new Path
  89. {
  90. Width = 20,
  91. Height = 20,
  92. Data = bookmarkGeometry,
  93. Fill = (Brush)FindResource("btn.logo.bota")
  94. };
  95. bookmarkButton.Content = path;
  96. Grid.SetColumn(bookmarkButton, botaCounter++);
  97. BOTABarTitleGrid.Children.Add(bookmarkButton);
  98. bookmarkButton.Click += BookmarkButton_Click;
  99. botaTools -= BOTATools.Bookmark;
  100. }
  101. else if ((botaTools & BOTATools.Outline) > 0)
  102. {
  103. outlineButton = new ToggleButton();
  104. outlineButton.Background = brush;
  105. Geometry outlineGeometry = Geometry.Parse(" M5,14.375 L5,18.125 L1.25,18.125 L1.25,14.375 L5,14.375 Z M18.75,15.625 L18.75,16.875 L7.5,16.875 L7.5,15.625 L18.75,15.625 Z M5,8.125 L5,11.875 L1.25,11.875 L1.25,8.125 L5,8.125 Z M18.75,9.375 L18.75,10.625 L7.5,10.625 L7.5,9.375 L18.75,9.375 Z M5,1.875 L5,5.625 L1.25,5.625 L1.25,1.875 L5,1.875 Z M18.75,3.125 L18.75,4.375 L7.5,4.375 L7.5,3.125 L18.75,3.125 Z");
  106. Path path = new Path
  107. {
  108. Width = 20,
  109. Height = 20,
  110. Data = outlineGeometry,
  111. Fill = (Brush)FindResource("btn.logo.bota"),
  112. };
  113. outlineButton.Content = path;
  114. Grid.SetColumn(outlineButton, botaCounter++);
  115. BOTABarTitleGrid.Children.Add(outlineButton);
  116. outlineButton.Click += OutlineButton_Click; ;
  117. botaTools -= BOTATools.Outline;
  118. }
  119. else if ((botaTools & BOTATools.Thumbnail) > 0)
  120. {
  121. thumbnailButton = new ToggleButton();
  122. thumbnailButton.Background = brush;
  123. Geometry thumbnailGeometry = Geometry.Parse("M15 9V17H5L5 3L9 3V7C9 8.10457 9.89543 9 11 9H15ZM14.1716 7H11V3.82843L12.5858 5.41421L14.1716 7ZM17 8V17C17 18.1046 16.1046 19 15 19H5C3.89543 19 3 18.1046 3 17V3C3 1.89543 3.89543 1 5 1H10H10.1716C10.702 1 11.2107 1.21071 11.5858 1.58579L14 4L16.4142 6.41421C16.7893 6.78929 17 7.29799 17 7.82843V8Z");
  124. Path path = new Path
  125. {
  126. Width = 20,
  127. Height = 20,
  128. Data = thumbnailGeometry,
  129. Fill = (Brush)FindResource("btn.logo.bota"),
  130. };
  131. thumbnailButton.Content = path;
  132. Grid.SetColumn(thumbnailButton, botaCounter++);
  133. BOTABarTitleGrid.Children.Add(thumbnailButton);
  134. thumbnailButton.Click += ThumbnailButton_Click;
  135. botaTools -= BOTATools.Thumbnail;
  136. }
  137. else if ((botaTools & BOTATools.Annotation) > 0)
  138. {
  139. annotButton = new ToggleButton();
  140. annotButton.Background = brush;
  141. Geometry thumbnailGeometry = Geometry.Parse("M20,1.25 L18,18.75 L0,18.75 L2,1.25 L20,1.25 Z M10.4360672,3.51788083 L9.2754713,3.51788083 L4.624,15.1438808 L3.125,15.1442308 L3.125,16.3942308 L6.97115385,16.3942308 L6.97115385,15.1442308 L5.971,15.1438808 L6.913,12.7878808 L12.798,12.7878808 L13.74,15.1438808 L12.7403846,15.1442308 L12.7403846,16.3942308 L16.5865385,16.3942308 L16.5865385,15.1442308 L15.086,15.1438808 L10.4360672,3.51788083 Z M9.855,5.43125 L12.298,11.5378808 L7.413,11.5378808 L9.855,5.43125 Z");
  142. Path path = new Path
  143. {
  144. Width = 20,
  145. Height = 20,
  146. Data = thumbnailGeometry,
  147. Fill = (Brush)FindResource("btn.logo.bota"),
  148. };
  149. annotButton.Content = path;
  150. Grid.SetColumn(annotButton, botaCounter++);
  151. BOTABarTitleGrid.Children.Add(annotButton);
  152. annotButton.Click += AnnotButton_Click;
  153. botaTools -= BOTATools.Annotation;
  154. }
  155. else if ((botaTools & BOTATools.Search) > 0)
  156. {
  157. searchButton = new ToggleButton();
  158. searchButton.Background = brush;
  159. Geometry thumbnailGeometry = Geometry.Parse(" M14 8.5C14 11.5376 11.5376 14 8.5 14C5.46243 14 3 11.5376 3 8.5C3 5.46243 5.46243 3 8.5 3C11.5376 3 14 5.46243 14 8.5ZM13.0492 14.4633C11.7874 15.4274 10.2106 16 8.5 16C4.35786 16 1 12.6421 1 8.5C1 4.35786 4.35786 1 8.5 1C12.6421 1 16 4.35786 16 8.5C16 10.2105 15.4274 11.7873 14.4634 13.0491L18.4572 17.0429L17.043 18.4571L13.0492 14.4633Z");
  160. Path path = new Path
  161. {
  162. Width = 20,
  163. Height = 20,
  164. Data = thumbnailGeometry,
  165. Fill = (Brush)FindResource("btn.logo.bota"),
  166. };
  167. searchButton.Content = path;
  168. Grid.SetColumn(searchButton, botaCounter++);
  169. BOTABarTitleGrid.Children.Add(searchButton);
  170. searchButton.Click += SearchButton_Click;
  171. botaTools -= BOTATools.Search;
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 获取Bota工具
  177. /// </summary>
  178. /// <returns></returns>
  179. private UIElement GetBotaTool()
  180. {
  181. return BotaToolContainer.Child;
  182. }
  183. private void SetBotaTool(UIElement newChild)
  184. {
  185. BotaToolContainer.Child = newChild;
  186. }
  187. /// <summary>
  188. /// 展开Bota工具
  189. /// </summary>
  190. /// <param name="isExpand"></param>
  191. private void ExpandTool(bool isExpand)
  192. {
  193. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  194. }
  195. /// <summary>
  196. /// 清除工具栏状态
  197. /// </summary>
  198. private void ClearToolState(UIElement ignoreTool)
  199. {
  200. foreach (UIElement child in BOTABarTitleGrid.Children)
  201. {
  202. if (child != ignoreTool && child is ToggleButton buttonTool)
  203. {
  204. buttonTool.IsChecked = false;
  205. }
  206. }
  207. }
  208. private void SearchButton_Click(object sender, RoutedEventArgs e)
  209. {
  210. UIElement botaTool = GetBotaTool();
  211. if (botaTool == null || !(botaTool is CPDFSearchControl))
  212. {
  213. CPDFSearchControl searchControl = new CPDFSearchControl();
  214. if (pdfViewer != null && pdfViewer.Document != null)
  215. {
  216. searchControl.InitWithPDFViewer(pdfViewer);
  217. }
  218. SetBotaTool(searchControl);
  219. }
  220. ExpandTool(searchButton.IsChecked == true);
  221. ClearToolState(searchButton);
  222. }
  223. private void AnnotButton_Click(object sender, RoutedEventArgs e)
  224. {
  225. }
  226. private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
  227. {
  228. UIElement botaTool = GetBotaTool();
  229. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  230. {
  231. CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
  232. if (pdfViewer != null && pdfViewer.Document != null)
  233. {
  234. thumbnailControl.InitWithPDFViewer(pdfViewer);
  235. thumbnailControl.LoadThumb();
  236. }
  237. SetBotaTool(thumbnailControl);
  238. }
  239. ExpandTool(thumbnailButton.IsChecked == true);
  240. ClearToolState(thumbnailButton);
  241. }
  242. private void OutlineButton_Click(object sender, RoutedEventArgs e)
  243. {
  244. UIElement botaTool = GetBotaTool();
  245. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  246. {
  247. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  248. if (pdfViewer != null && pdfViewer.Document != null)
  249. {
  250. outlineControl.InitWithPDFViewer(pdfViewer);
  251. }
  252. SetBotaTool(outlineControl);
  253. }
  254. ExpandTool(outlineButton.IsChecked == true);
  255. ClearToolState(outlineButton);
  256. }
  257. private void BookmarkButton_Click(object sender, RoutedEventArgs e)
  258. {
  259. UIElement botaTool = GetBotaTool();
  260. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  261. {
  262. CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
  263. if (pdfViewer != null && pdfViewer.Document != null)
  264. {
  265. pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
  266. }
  267. SetBotaTool(pdfBookmarkControl);
  268. }
  269. ExpandTool(bookmarkButton.IsChecked == true);
  270. ClearToolState(bookmarkButton);
  271. }
  272. }
  273. }