StickyNotePopup.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.CustomControl.CompositeControl;
  5. using PDF_Master.EventAggregators;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Properties;
  8. using PDF_Master.ViewModels.BOTA;
  9. using PDF_Master.ViewModels;
  10. using PDF_Master.ViewModels.PropertyPanel.AnnotPanel;
  11. using PDF_Master.Views.BOTA;
  12. using Prism.Events;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Controls.Primitives;
  23. using System.Windows.Data;
  24. using System.Windows.Documents;
  25. using System.Windows.Input;
  26. using System.Windows.Markup;
  27. using System.Windows.Media;
  28. using System.Windows.Media.Imaging;
  29. using System.Windows.Navigation;
  30. using System.Windows.Shapes;
  31. using PDF_Master.ViewModels.Tools;
  32. namespace PDF_Master.Views.PropertyPanel.AnnotPanel
  33. {
  34. /// <summary>
  35. /// StickyNotePopup.xaml 的交互逻辑
  36. /// </summary>
  37. public class GripThumb : Thumb
  38. {
  39. private Pen DrawPen;
  40. private Brush FillBrush;
  41. public GripThumb()
  42. {
  43. DefaultStyleKey = typeof(GripThumb);
  44. DrawPen = new Pen(new SolidColorBrush(Colors.Blue), 2);
  45. FillBrush = new SolidColorBrush(Color.FromArgb(0x0A, 0xFF, 0xFF, 0xFF));
  46. }
  47. protected override void OnRender(DrawingContext DrawContext)
  48. {
  49. DrawContext.DrawRectangle(FillBrush, null, new Rect(0, 0, Width, Height));
  50. DrawContext.DrawLine(DrawPen, new Point(Width, 0), new Point(0, Height));
  51. DrawContext.DrawLine(DrawPen, new Point(Width, Height / 3), new Point(Width / 3, Height));
  52. DrawContext.DrawLine(DrawPen, new Point(Width, Height * 2 / 3), new Point(Width * 2 / 3, Height));
  53. }
  54. }
  55. /// <summary>
  56. /// StickyNotePopup.xaml 的交互逻辑
  57. /// </summary>
  58. public partial class StickyNotePopup : StickyPopupExt
  59. {
  60. private ObservableCollection<ColorItem> colors = new ObservableCollection<ColorItem>();
  61. private Point PressPoint;
  62. public Point OffsetParent;
  63. public IEventAggregator eventAggregator;
  64. private byte saveOpacity = 1;
  65. private string Unicode = "";
  66. public StickyNotePopup(IEventAggregator eventAggregator)
  67. {
  68. this.eventAggregator = eventAggregator;
  69. InitializeComponent();
  70. AddHandler(MouseUpEvent, new MouseButtonEventHandler(StickyPopupControl_MouseUp), true);
  71. ContentText.AddHandler(MouseDownEvent, new MouseButtonEventHandler(StickyPopupControl_MouseDown), true);
  72. Loaded += StickyPopupControl_Loaded;
  73. ContentText.GotFocus += ContentText_GotFocus;
  74. ContentText.LostFocus += ContentText_LostFocus;
  75. eventAggregator.GetEvent<StickyNotePopupColorEvent>().Subscribe(SetStickyNotePopupColor, e =>
  76. e.Unicode == Unicode);
  77. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x10)));
  78. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0x10, 0x10)));
  79. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0xFF, 0x10)));
  80. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0x70, 0xFF)));
  81. ListColor.ItemsSource = colors;
  82. }
  83. private void SetStickyNotePopupColor(StickyNoteColorUnicode stickyNoteColorUnicode)
  84. {
  85. border.BorderBrush = stickyNoteColorUnicode.brush;
  86. //if (ListColor.SelectedItem is ColorItem colorItem)
  87. //{
  88. // var color = (colorItem.Color as SolidColorBrush).Color;
  89. // if (color.A != GetCurrentAnnot.Color.A ||
  90. // color.R != GetCurrentAnnot.Color.R ||
  91. // color.G != GetCurrentAnnot.Color.G ||
  92. // color.B != GetCurrentAnnot.Color.B
  93. // )
  94. // {
  95. // ListColor.SelectedIndex = -1;
  96. // }
  97. //}
  98. foreach (var item in colors)
  99. {
  100. var colorItem = (item.Color as SolidColorBrush).Color;
  101. if (colorItem.A == GetCurrentAnnot.Color.A &&
  102. colorItem.R == GetCurrentAnnot.Color.R &&
  103. colorItem.G == GetCurrentAnnot.Color.G &&
  104. colorItem.B == GetCurrentAnnot.Color.B
  105. )
  106. {
  107. ListColor.SelectedItem = item;
  108. return;
  109. }
  110. else
  111. {
  112. ListColor.SelectedIndex = -1;
  113. }
  114. }
  115. }
  116. private void ContentText_LostFocus(object sender, RoutedEventArgs e)
  117. {
  118. e.Handled = true;
  119. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  120. bkColor.A = saveOpacity;
  121. border.BorderBrush = new SolidColorBrush(bkColor);
  122. this.Background = new SolidColorBrush(Colors.Transparent);
  123. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  124. //为了点击某控件之后就直接关闭窗口
  125. var ui = Mouse.DirectlyOver as FrameworkElement;
  126. if (ui != null)
  127. {
  128. var colorItem = ui.DataContext as ColorItem;
  129. if (colorItem != null || ui.DataContext == null)
  130. {
  131. e.Handled = false;
  132. return;
  133. }
  134. }
  135. eventAggregator.GetEvent<StickyNoteEvent>().Publish(new StickyNoteArgs() { Unicode = Unicode, Text = (e.Source as TextBox).Text });
  136. (DataContext as StickyNotePopupViewModel).ContentTextLostFocus.Execute(sender);
  137. CloseText_MouseUp(this, null);
  138. }
  139. private void ContentText_GotFocus(object sender, RoutedEventArgs e)
  140. {
  141. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  142. saveOpacity = bkColor.A;
  143. bkColor.A = 255;
  144. border.BorderBrush = new SolidColorBrush(bkColor);
  145. this.Background = new SolidColorBrush(Colors.Transparent);
  146. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  147. }
  148. private void StickyPopupControl_Loaded(object sender, RoutedEventArgs e)
  149. {
  150. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  151. ContentText.Focus();
  152. ContentText.CaretIndex = ContentText.Text.Length;
  153. LoadedColor();
  154. if (GetPDFViewer != null)
  155. {
  156. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  157. GetPDFViewer.PreviewMouseLeftButtonDown += GetPDFViewer_LeftButtonDown;
  158. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  159. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  160. }
  161. }
  162. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  163. {
  164. if (KeyEventsHelper.IsSingleKey(Key.Delete))
  165. {
  166. BtnDelete_Click(null, null);
  167. }
  168. if (KeyEventsHelper.IsSingleKey(Key.Escape))
  169. {
  170. CloseText_MouseUp(this, null);
  171. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  172. }
  173. }
  174. private void GetPDFViewer_LeftButtonDown(object sender, MouseButtonEventArgs e)
  175. {
  176. var ui = e.OriginalSource as FrameworkElement;
  177. if (ui != null)
  178. {
  179. if (ui.DataContext != null && ui.DataContext is ColorItem == false)
  180. {
  181. CloseText_MouseUp(this, null);
  182. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  183. }
  184. }
  185. }
  186. public void LoadedColor()
  187. {
  188. if (colors != null && colors.Count > 0)
  189. {
  190. BtnDelete.DataContext = colors[0];//为了避免点击删除按钮之后,先执行取消焦点函数后,就没法执行点击事件
  191. if (GetCurrentAnnot != null)
  192. {
  193. foreach (var item in colors)
  194. {
  195. var colorItem = (item.Color as SolidColorBrush).Color;
  196. if (colorItem.A == GetCurrentAnnot.Color.A &&
  197. colorItem.R == GetCurrentAnnot.Color.R &&
  198. colorItem.G == GetCurrentAnnot.Color.G &&
  199. colorItem.B == GetCurrentAnnot.Color.B
  200. )
  201. {
  202. ListColor.SelectedItem = item;
  203. return;
  204. }
  205. }
  206. }
  207. ListColor.SelectedItem = null;
  208. }
  209. }
  210. private void StickyPopupControl_MouseDown(object sender, MouseButtonEventArgs e)
  211. {
  212. PressPoint = new Point(0, 0);
  213. }
  214. private void StickyPopupControl_MouseUp(object sender, MouseButtonEventArgs e)
  215. {
  216. CanMove = true;
  217. GripControl.ReleaseMouseCapture();
  218. }
  219. public string StickyText
  220. {
  221. get
  222. {
  223. return ContentText.Text;
  224. }
  225. set
  226. {
  227. ContentText.Text = value;
  228. }
  229. }
  230. public string StickyAuthor
  231. {
  232. get
  233. {
  234. return AuthorText.Text;
  235. }
  236. set
  237. {
  238. AuthorText.Text = value;
  239. }
  240. }
  241. public string StickyDate
  242. {
  243. get
  244. {
  245. return TxtDate.Text;
  246. }
  247. set
  248. {
  249. TxtDate.Text = value;
  250. }
  251. }
  252. public StickyAnnotArgs GetCurrentAnnot { get; set; }
  253. public CPDFViewer GetPDFViewer { get; set; }
  254. public AnnotToolContentViewModel AnnotToolContentVM { get; set; }
  255. public bool CanMove { get; set; } = true;
  256. private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
  257. {
  258. e.Handled = true;
  259. if (e.LeftButton == MouseButtonState.Pressed)
  260. {
  261. Point currentPos = e.GetPosition(ContainerElement);
  262. double newWidth = GridUi.ActualWidth + currentPos.X - PressPoint.X;
  263. double newHeight = GridUi.ActualHeight + currentPos.Y - PressPoint.Y;
  264. if (Left + newWidth >= ContainerElement.ActualWidth)
  265. {
  266. newWidth = ContainerElement.ActualWidth - Left - 5;
  267. }
  268. if (Top + newHeight >= ContainerElement.ActualHeight)
  269. {
  270. newHeight = ContainerElement.ActualHeight - Top - 5;
  271. }
  272. GridUi.Width = newWidth;
  273. GridUi.Height = newHeight;
  274. PressPoint = currentPos;
  275. CanMove = false;
  276. if (GripControl.IsMouseCaptured == false)
  277. {
  278. GripControl.CaptureMouse();
  279. }
  280. }
  281. }
  282. private void CloseText_MouseUp(object sender, MouseButtonEventArgs e)
  283. {
  284. if (e != null)
  285. {
  286. e.Handled = true;
  287. }
  288. PlaceChange = null;
  289. RemoveFromLayer();
  290. if (Closed != null)
  291. {
  292. Closed.Invoke(sender, EventArgs.Empty);
  293. }
  294. }
  295. protected override void OnMouseDown(MouseButtonEventArgs e)
  296. {
  297. e.Handled = true;
  298. PressPoint = e.GetPosition(ContainerElement);
  299. }
  300. protected override void OnMouseEnter(MouseEventArgs e)
  301. {
  302. e.Handled = true;
  303. if (PlaceChange != null)
  304. {
  305. PlaceChange.Invoke(this, new EventArgs());
  306. }
  307. }
  308. protected override void OnMouseLeave(MouseEventArgs e)
  309. {
  310. e.Handled = true;
  311. if (PlaceChange != null)
  312. {
  313. PlaceChange.Invoke(null, new EventArgs());
  314. }
  315. PressPoint = new Point(0, 0);
  316. }
  317. protected override void OnMouseUp(MouseButtonEventArgs e)
  318. {
  319. CanMove = true;
  320. if (PlaceChange != null)
  321. {
  322. PlaceChange.Invoke(this, new EventArgs());
  323. }
  324. PressPoint = new Point(0, 0);
  325. GripControl.ReleaseMouseCapture();
  326. }
  327. protected override void OnMouseMove(MouseEventArgs e)
  328. {
  329. e.Handled = true;
  330. if (e.LeftButton == MouseButtonState.Pressed && e.Source.GetType() != typeof(TextBox) && e.Source.GetType() != typeof(GripThumb))
  331. {
  332. if (CanMove == false || (PressPoint.X == 0 && PressPoint.Y == 0) || ContainerElement == null)
  333. {
  334. return;
  335. }
  336. Point currentPoint = e.GetPosition(ContainerElement);
  337. double newLeft = Left + currentPoint.X - PressPoint.X;
  338. double newTop = Top + currentPoint.Y - PressPoint.Y;
  339. if (newLeft < 0)
  340. {
  341. newLeft = 0;
  342. }
  343. if (newLeft + GridUi.ActualWidth > ContainerElement.ActualWidth)
  344. {
  345. newLeft = ContainerElement.ActualWidth - GridUi.ActualWidth;
  346. }
  347. if (newTop < 0)
  348. {
  349. newTop = 0;
  350. }
  351. if (newTop + GridUi.ActualHeight > ContainerElement.ActualHeight)
  352. {
  353. newTop = ContainerElement.ActualHeight - GridUi.ActualHeight;
  354. }
  355. Left = newLeft;
  356. Top = newTop;
  357. PressPoint = currentPoint;
  358. if (PlaceChange != null)
  359. {
  360. PlaceChange.Invoke(null, new EventArgs());
  361. }
  362. RefreshPosition();
  363. }
  364. }
  365. public override void SetAuthor(string author)
  366. {
  367. base.SetAuthor(author);
  368. StickyAuthor = author;
  369. }
  370. public override void SetDateText(string dateText)
  371. {
  372. base.SetDateText(dateText);
  373. if (Regex.IsMatch(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  374. {
  375. string dateStr = Regex.Match(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  376. dateText = dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + "/" + dateStr.Substring(0, 4) + " " + dateStr.Substring(8, 2) + ":" +
  377. dateStr.Substring(10, 2) + ":" + dateStr.Substring(12, 2);
  378. }
  379. StickyDate = dateText;
  380. }
  381. public override void SetStickyColor(Color newColor)
  382. {
  383. base.SetStickyColor(newColor);
  384. border.BorderBrush = new SolidColorBrush(newColor);
  385. this.Background = new SolidColorBrush(Colors.Transparent);
  386. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  387. }
  388. public override void SetStickyNote(string note)
  389. {
  390. base.SetStickyNote(note);
  391. StickyText = note;
  392. try
  393. {
  394. ContentText.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.AnchoredFamaily);
  395. }
  396. catch
  397. {
  398. }
  399. }
  400. public override void SetReadOnly(bool readOnly)
  401. {
  402. base.SetReadOnly(readOnly);
  403. ContentText.IsReadOnly = readOnly;
  404. }
  405. public override string GetText()
  406. {
  407. return StickyText;
  408. }
  409. /// <summary>
  410. /// 鼠标滚动后,图标消失,弹框消失
  411. /// </summary>
  412. public override void Close()
  413. {
  414. //base.Close();
  415. RemoveFromLayer();
  416. }
  417. private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
  418. {
  419. var colorItem = ListColor.SelectedItem as ColorItem;
  420. if (colorItem != null)
  421. {
  422. var color = (colorItem.Color as SolidColorBrush).Color;
  423. this.SetStickyColor(color);
  424. var annot = GetCurrentAnnot as StickyAnnotArgs;
  425. if (annot != null)
  426. {
  427. annot.Color = color;
  428. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  429. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, color);
  430. AnnotEvent?.UpdateAnnot();
  431. }
  432. border.BorderBrush = new SolidColorBrush(color);
  433. this.Background = new SolidColorBrush(Colors.Transparent);
  434. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  435. eventAggregator.GetEvent<StickyNoteColorEvent>().Publish(new StickyNoteColorUnicode { Unicode = Unicode, brush = new SolidColorBrush(color) });
  436. }
  437. }
  438. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  439. {
  440. if (GetPDFViewer != null)
  441. {
  442. //猜测:此方法对GetCurrentAnnot进行了修改,所以删除要在修改后删除,而非修改前
  443. CloseText_MouseUp(this, null);
  444. if (GetCurrentAnnot != null)
  445. {
  446. bool result = GetPDFViewer.RemovePageAnnot(GetCurrentAnnot.PageIndex, GetCurrentAnnot.AnnotIndex);
  447. if (result && AnnotToolContentVM != null)
  448. {
  449. if (string.IsNullOrEmpty(AnnotToolContentVM.StrAnnotToolChecked))
  450. {
  451. AnnotToolContentVM.ViewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  452. }
  453. bool isTabItemAnnotation = AnnotToolContentVM.IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  454. if (isTabItemAnnotation)
  455. {
  456. AnnotationContentViewModel viewModel = AnnotToolContentVM.GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  457. if (viewModel != null)
  458. {
  459. viewModel.DeleteModifiedAnnot(GetCurrentAnnot.PageIndex, GetCurrentAnnot.AnnotIndex);
  460. }
  461. }
  462. }
  463. }
  464. // Closed.Invoke(sender, EventArgs.Empty);
  465. //CloseText_MouseUp(this, null);
  466. if (GetPDFViewer.ToolManager != null)
  467. {
  468. GetPDFViewer.ToolManager.DisableStickyCreate = false;
  469. }
  470. }
  471. else
  472. {
  473. CloseText_MouseUp(this, null);
  474. }
  475. }
  476. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  477. {
  478. var frame = e.OriginalSource as Control;
  479. if (frame != null)
  480. {
  481. var color = frame.Background as SolidColorBrush;
  482. if (color != null && color.Color == Colors.Transparent)
  483. Closed.Invoke(sender, EventArgs.Empty);
  484. }
  485. }
  486. private void StickyPopupExt_LostFocus(object sender, RoutedEventArgs e)
  487. {
  488. }
  489. private void StickyPopupExt_PreviewKeyDown(object sender, KeyEventArgs e)
  490. {
  491. if (e.Key == Key.Delete)
  492. {
  493. BtnDelete_Click(null, null);
  494. }
  495. if (e.Key == Key.Enter)
  496. {
  497. CloseText_MouseUp(this, null);
  498. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  499. }
  500. }
  501. }
  502. }