StickyNotePopup.xaml.cs 19 KB

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