BookmarkContent.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using ComPDFKit.PDFDocument;
  2. using Microsoft.Office.Interop.Word;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.ViewModels.BOTA;
  6. using Prism.Events;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Animation;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  25. namespace PDF_Master.Views.BOTA
  26. {
  27. /// <summary>
  28. /// BookmarkContent.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class BookmarkContent : UserControl
  31. {
  32. private BookmarkContentViewModel viewModel = null;
  33. /// <summary>
  34. /// 上一个ListBoxItem,为选中状态做准备
  35. /// </summary>
  36. public ListBoxItem histotyListBoxItem = null;
  37. private bool isAdd = false;
  38. private string unicode = "";
  39. private bool isCleanSelectAll = false;
  40. public BookmarkContent(IEventAggregator eventAggregator)
  41. {
  42. InitializeComponent();
  43. viewModel = this.DataContext as BookmarkContentViewModel;
  44. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  45. //Windows:BOTA列表,移除点击阅读页取消选中逻辑
  46. //eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  47. }
  48. private void CleanSelectAll(CleanSelectAllArgs obj)
  49. {
  50. isCleanSelectAll = true;
  51. BookMarkListView.SelectedIndex = -1;
  52. foreach (var item in BookMarkListView.Items)
  53. {
  54. ListBoxItem listItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(item));
  55. if (listItem != null)
  56. {
  57. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  58. //TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  59. if (textBlock.Visibility == Visibility.Collapsed)
  60. {
  61. textBlock.Visibility = Visibility.Visible;
  62. }
  63. }
  64. }
  65. //BookMarkListView.SelectedItems.Clear();
  66. }
  67. /// <summary>
  68. ///ListViewItem,鼠标左键点击
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
  73. {
  74. object[] objects = new object[] { sender, e };
  75. TextBlock textBlock = null;
  76. ListBoxItem listBoxItem = (sender as ListBoxItem);
  77. if (e.LeftButton == MouseButtonState.Pressed)
  78. {
  79. if (e.ClickCount >= 2)
  80. {
  81. histotyListBoxItem = listBoxItem;
  82. if (e.OriginalSource is TextBlock)
  83. {
  84. textBlock = (TextBlock)e.OriginalSource;
  85. if (textBlock != null)
  86. {
  87. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  88. SetSelectedStatus(listBoxItem, textBox, textBlock);
  89. }
  90. }
  91. }
  92. else if (e.ClickCount == 1)
  93. {
  94. isRename = false;
  95. CleanHistotyListBoxItem(listBoxItem, textBlock);
  96. viewModel.ListViewItemMouseDownCommand.Execute(sender);
  97. }
  98. }
  99. else if (e.RightButton == MouseButtonState.Pressed)
  100. {
  101. CleanHistotyListBoxItem(listBoxItem, textBlock);
  102. }
  103. }
  104. private void CleanHistotyListBoxItem(ListBoxItem listBoxItem, TextBlock textBlock)
  105. {
  106. if (histotyListBoxItem != listBoxItem)
  107. {
  108. if (histotyListBoxItem != null)
  109. {
  110. histotyListBoxItem.IsSelected = false;
  111. textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  112. textBlock.Visibility = Visibility.Visible;
  113. textBlock.Focusable = true;
  114. }
  115. }
  116. }
  117. private bool isSelects = false;
  118. private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  119. {
  120. MenuItem rename = ContextMenuBook.Items[0] as MenuItem;
  121. MenuItem editPageIndex = ContextMenuBook.Items[1] as MenuItem;
  122. MenuItem del = ContextMenuBook.Items[2] as MenuItem;
  123. var a1 = (sender as ListBox).SelectedItems.Count;
  124. if (a1 >= 1)
  125. {
  126. isSelects = true;
  127. ContextMenuBook.Visibility = Visibility.Visible;
  128. if (a1 > 1)
  129. {
  130. rename.IsEnabled = false;
  131. editPageIndex.IsEnabled = false;
  132. }
  133. if (a1 == 1)
  134. {
  135. isSelects = false;
  136. CPDFBookmark bookmark = null;
  137. if (BookMarkListView.SelectedItem != null)
  138. {
  139. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  140. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  141. {
  142. editPageIndex.IsEnabled = false;
  143. return;
  144. }
  145. rename.IsEnabled = true;
  146. editPageIndex.IsEnabled = true;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. ContextMenuBook.Visibility = Visibility.Collapsed;
  153. isSelects = false;
  154. }
  155. }
  156. /// <summary>
  157. /// ListViewItem双击时选中状态
  158. /// </summary>
  159. /// <param name="listBoxItem"></param>
  160. /// <param name="textBox"></param>
  161. /// <param name="textBlock"></param>
  162. private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
  163. {
  164. if (listBoxItem == null || textBox == null || textBlock == null)
  165. {
  166. return;
  167. }
  168. listBoxItem.IsSelected = true;
  169. listBoxItem.Focus();
  170. textBlock.Visibility = Visibility.Collapsed;
  171. isRename = true;
  172. textBox.Dispatcher.BeginInvoke(new Action(() =>
  173. {
  174. textBox.Focus();
  175. textBox.SelectAll();
  176. //listBoxItem.IsSelected = true;
  177. }));
  178. }
  179. /// <summary>
  180. /// ListViewItem失去焦点
  181. /// </summary>
  182. /// <param name="sender"></param>
  183. /// <param name="e"></param>
  184. private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
  185. {
  186. ListBoxItem listItem = sender as ListBoxItem;
  187. if (listItem != null)
  188. {
  189. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  190. viewModel.LostFocusCommand.Execute(listItem);
  191. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listItem);
  192. TextBox text = CommonHelper.FindVisualChild<TextBox>(listItem);
  193. if (textBlock.Visibility != Visibility.Visible)
  194. {
  195. if (isAdd == false || isRename == false)
  196. {
  197. listItem.IsSelected = false;
  198. if (isRename)
  199. {
  200. isRename = false;
  201. listItem.IsSelected = true;
  202. }
  203. if (listItem.IsSelected == false)
  204. {
  205. textBlock.Visibility = Visibility.Visible;
  206. }
  207. }
  208. else
  209. {
  210. if (isCleanSelectAll == false)
  211. {
  212. listItem.IsSelected = true;
  213. isAdd = false;
  214. }
  215. }
  216. }
  217. else
  218. {
  219. if (isCleanSelectAll == false)
  220. {
  221. //点击空白
  222. //textBlock.Text = text.Text;
  223. listItem.IsSelected = true;
  224. }
  225. }
  226. }
  227. }
  228. private bool isRename = false;
  229. /// <summary>
  230. /// 右键菜单-重命名
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void MenuItemRename_Click(object sender, RoutedEventArgs e)
  235. {
  236. if (sender is MenuItem)
  237. {
  238. MenuItem menuItem = (MenuItem)sender;
  239. CPDFBookmark bookmark = BookMarkListView.SelectedItem as CPDFBookmark;
  240. if (bookmark != null)
  241. {
  242. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  243. isRename = true;
  244. ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
  245. histotyListBoxItem = listBoxItem;
  246. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  247. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  248. SetSelectedStatus(listBoxItem, textBox, textBlock);
  249. }
  250. }
  251. }
  252. /// <summary>
  253. /// BookMarkListView,鼠标点击
  254. /// </summary>
  255. /// <param name="sender"></param>
  256. /// <param name="e"></param>
  257. private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  258. {
  259. if (e.LeftButton == MouseButtonState.Pressed)
  260. {
  261. var pos = e.GetPosition(BookMarkListView);
  262. var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
  263. if (result != null)
  264. {
  265. //获取当前鼠标指针下的容器
  266. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  267. if (listBoxItem == null)
  268. {
  269. if (BookMarkListView.SelectedItem != null)
  270. {
  271. ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
  272. item.IsSelected = false;
  273. TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
  274. box.Visibility = Visibility.Visible;
  275. BookMarkListView.SelectedItems.Clear();
  276. }
  277. if (histotyListBoxItem != null)
  278. {
  279. var pos1 = e.GetPosition(histotyListBoxItem);
  280. var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
  281. if (result1 == null)
  282. {
  283. histotyListBoxItem.IsSelected = false;
  284. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
  285. textBlock.Visibility = Visibility.Visible;
  286. textBlock.Focusable = true;
  287. }
  288. }
  289. }
  290. else
  291. {
  292. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
  293. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
  294. if (textBox.IsFocused == false)
  295. {
  296. listBoxItem.Focus();
  297. }
  298. }
  299. }
  300. //BookMarkListView.Focus();
  301. }
  302. }
  303. /// <summary>
  304. /// 右键菜单-删除
  305. /// </summary>
  306. /// <param name="sender"></param>
  307. /// <param name="e"></param>
  308. private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
  309. {
  310. List<int> pagelist = new List<int>();
  311. for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
  312. {
  313. CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
  314. pagelist.Add(BookMarkListView.Items.IndexOf(item));
  315. }
  316. pagelist.Sort();
  317. for (int i = 0; i < pagelist.Count; i++)
  318. {
  319. CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
  320. if (data != null)
  321. {
  322. viewModel.DeleteCommand.Execute(data);
  323. }
  324. }
  325. }
  326. public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
  327. {
  328. isAdd = true;
  329. isRename = false;
  330. viewModel.AddBookmarkCommand.Execute(BookMarkListView);
  331. if (viewModel.IsHasBookmark == false)
  332. {
  333. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  334. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  335. if (myListBoxItem == null)
  336. {
  337. BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
  338. BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
  339. }
  340. else
  341. {
  342. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  343. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  344. histotyListBoxItem = myListBoxItem;
  345. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  346. }
  347. }
  348. }
  349. private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  350. {
  351. ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
  352. if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
  353. {
  354. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  355. ListBoxItem myListBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
  356. if (myListBoxItem != null)
  357. {
  358. if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
  359. {
  360. BookMarkListView.UpdateLayout();
  361. myListBoxItem.UpdateLayout();
  362. BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
  363. }
  364. TextBox textBox = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  365. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  366. SetSelectedStatus(myListBoxItem, textBox, textBlock);
  367. }
  368. }
  369. }
  370. private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
  371. {
  372. if (BookMarkListView.SelectedItems == null)
  373. {
  374. return;
  375. }
  376. if (e.Key == Key.Escape)
  377. {
  378. //Windows:BOTA列表,移除Esc取消选中逻辑
  379. //BookMarkListView.SelectedItems.Clear();
  380. }
  381. //if (e.Key == Key.Delete)
  382. //{
  383. // if (BookMarkListView.SelectedItems.Count > 0)
  384. // {
  385. // //全选删除
  386. // if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
  387. // {
  388. // }
  389. // }
  390. //}
  391. }
  392. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  393. {
  394. //ContextMenu contextMenu = sender as ContextMenu;
  395. if (sender is ContextMenu contextMenu)
  396. {
  397. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  398. MenuItem rename = contextMenu.Items[0] as MenuItem;
  399. MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
  400. MenuItem del = contextMenu.Items[2] as MenuItem;
  401. rename.IsEnabled = true;
  402. editPageIndex.IsEnabled = true;
  403. if (BookMarkListView.SelectedItems.Count > 1)
  404. {
  405. rename.IsEnabled = false;
  406. editPageIndex.IsEnabled = false;
  407. //isSelects = false;
  408. }
  409. else
  410. {
  411. Trace.WriteLine($"{BookMarkListView.SelectedItems.Count}");
  412. CPDFBookmark bookmark = null;
  413. if (BookMarkListView.SelectedItem != null)
  414. {
  415. bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  416. if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
  417. {
  418. editPageIndex.IsEnabled = false;
  419. return;
  420. }
  421. rename.IsEnabled = true;
  422. editPageIndex.IsEnabled = true;
  423. }
  424. }
  425. }
  426. }
  427. private void MenuChangeItem_Click(object sender, RoutedEventArgs e)
  428. {
  429. if (isSelects == false)
  430. {
  431. if (BookMarkListView.SelectedItem != null)
  432. {
  433. CPDFBookmark bookmark = (CPDFBookmark)BookMarkListView.SelectedItem;
  434. if (bookmark != null && sender is MenuItem editPageIndex)
  435. {
  436. editPageIndex.CommandParameter = bookmark;
  437. editPageIndex.Command = viewModel.EditPageIndexCommand;
  438. }
  439. }
  440. }
  441. }
  442. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  443. {
  444. if (e.Key == Key.Delete)
  445. {
  446. if (BookMarkListView.SelectedItems.Count != 0)
  447. {
  448. MenuItemDeleteCommand_Click(sender, e);
  449. TxtTitle.Dispatcher.BeginInvoke(new Action(() =>
  450. {
  451. TxtTitle.Focus();
  452. }));
  453. }
  454. }
  455. }
  456. }
  457. }