StickyNotePopup.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  66. InitializeComponent();
  67. AddHandler(MouseUpEvent, new MouseButtonEventHandler(StickyPopupControl_MouseUp), true);
  68. ContentText.AddHandler(MouseDownEvent, new MouseButtonEventHandler(StickyPopupControl_MouseDown), true);
  69. Loaded += StickyPopupControl_Loaded;
  70. ContentText.GotFocus += ContentText_GotFocus;
  71. ContentText.LostFocus += ContentText_LostFocus;
  72. eventAggregator.GetEvent<StickyNotePopupColorEvent>().Subscribe(SetStickyNotePopupColor, e =>
  73. e.Unicode == Unicode);
  74. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0xFF, 0x10)));
  75. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0xFF, 0x10, 0x10)));
  76. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0xFF, 0x10)));
  77. colors.Add(new ColorItem(Color.FromArgb(0xFF, 0x10, 0x70, 0xFF)));
  78. ListColor.ItemsSource = colors;
  79. }
  80. private void SetStickyNotePopupColor(StickyNoteColorUnicode stickyNoteColorUnicode)
  81. {
  82. border.BorderBrush = stickyNoteColorUnicode.brush;
  83. }
  84. private void ContentText_LostFocus(object sender, RoutedEventArgs e)
  85. {
  86. e.Handled = true;
  87. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  88. bkColor.A = saveOpacity;
  89. border.BorderBrush = new SolidColorBrush(bkColor);
  90. this.Background = new SolidColorBrush(Colors.Transparent);
  91. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  92. //为了点击某控件之后就直接关闭窗口
  93. var ui = Mouse.DirectlyOver as FrameworkElement;
  94. if (ui != null)
  95. {
  96. var colorItem = ui.DataContext as ColorItem;
  97. if (colorItem != null || ui.DataContext == null)
  98. {
  99. e.Handled = false;
  100. return;
  101. }
  102. }
  103. eventAggregator.GetEvent<StickyNoteEvent>().Publish(new StickyNoteArgs() { Unicode = Unicode, Text = (e.Source as TextBox).Text });
  104. (DataContext as StickyNotePopupViewModel).ContentTextLostFocus.Execute(sender);
  105. CloseText_MouseUp(this, null);
  106. }
  107. private void ContentText_GotFocus(object sender, RoutedEventArgs e)
  108. {
  109. Color bkColor = (border.BorderBrush as SolidColorBrush).Color;
  110. saveOpacity = bkColor.A;
  111. bkColor.A = 255;
  112. border.BorderBrush = new SolidColorBrush(bkColor);
  113. this.Background = new SolidColorBrush(Colors.Transparent);
  114. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  115. }
  116. private void StickyPopupControl_Loaded(object sender, RoutedEventArgs e)
  117. {
  118. ContentText.Focus();
  119. ContentText.CaretIndex = ContentText.Text.Length;
  120. LoadedColor();
  121. if (GetPDFViewer != null)
  122. {
  123. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  124. GetPDFViewer.PreviewMouseLeftButtonDown += GetPDFViewer_LeftButtonDown;
  125. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  126. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  127. }
  128. }
  129. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  130. {
  131. if (KeyEventsHelper.IsSingleKey(Key.Delete))
  132. {
  133. BtnDelete_Click(null, null);
  134. }
  135. if (KeyEventsHelper.IsSingleKey(Key.Escape))
  136. {
  137. CloseText_MouseUp(this, null);
  138. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  139. }
  140. }
  141. private void GetPDFViewer_LeftButtonDown(object sender, MouseButtonEventArgs e)
  142. {
  143. var ui = e.OriginalSource as FrameworkElement;
  144. if (ui != null)
  145. {
  146. if (ui.DataContext != null && ui.DataContext is ColorItem == false)
  147. {
  148. CloseText_MouseUp(this, null);
  149. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  150. }
  151. }
  152. }
  153. public void LoadedColor()
  154. {
  155. if (colors != null && colors.Count > 0)
  156. {
  157. BtnDelete.DataContext = colors[0];//为了避免点击删除按钮之后,先执行取消焦点函数后,就没法执行点击事件
  158. if (GetCurrentAnnot != null)
  159. {
  160. foreach (var item in colors)
  161. {
  162. var colorItem = (item.Color as SolidColorBrush).Color;
  163. if (colorItem.A == GetCurrentAnnot.Color.A &&
  164. colorItem.R == GetCurrentAnnot.Color.R &&
  165. colorItem.G == GetCurrentAnnot.Color.G &&
  166. colorItem.B == GetCurrentAnnot.Color.B
  167. )
  168. {
  169. ListColor.SelectedItem = item;
  170. return;
  171. }
  172. }
  173. }
  174. ListColor.SelectedItem = null;
  175. }
  176. }
  177. private void StickyPopupControl_MouseDown(object sender, MouseButtonEventArgs e)
  178. {
  179. PressPoint = new Point(0, 0);
  180. }
  181. private void StickyPopupControl_MouseUp(object sender, MouseButtonEventArgs e)
  182. {
  183. CanMove = true;
  184. GripControl.ReleaseMouseCapture();
  185. }
  186. public string StickyText
  187. {
  188. get
  189. {
  190. return ContentText.Text;
  191. }
  192. set
  193. {
  194. ContentText.Text = value;
  195. }
  196. }
  197. public string StickyAuthor
  198. {
  199. get
  200. {
  201. return AuthorText.Text;
  202. }
  203. set
  204. {
  205. AuthorText.Text = value;
  206. }
  207. }
  208. public string StickyDate
  209. {
  210. get
  211. {
  212. return DateText.Text;
  213. }
  214. set
  215. {
  216. DateText.Text = value;
  217. }
  218. }
  219. public StickyAnnotArgs GetCurrentAnnot { get; set; }
  220. public CPDFViewer GetPDFViewer { get; set; }
  221. public bool CanMove { get; set; } = true;
  222. private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
  223. {
  224. e.Handled = true;
  225. if (e.LeftButton == MouseButtonState.Pressed)
  226. {
  227. Point currentPos = e.GetPosition(ContainerElement);
  228. double newWidth = GridUi.ActualWidth + currentPos.X - PressPoint.X;
  229. double newHeight = GridUi.ActualHeight + currentPos.Y - PressPoint.Y;
  230. if (Left + newWidth >= ContainerElement.ActualWidth)
  231. {
  232. newWidth = ContainerElement.ActualWidth - Left - 5;
  233. }
  234. if (Top + newHeight >= ContainerElement.ActualHeight)
  235. {
  236. newHeight = ContainerElement.ActualHeight - Top - 5;
  237. }
  238. GridUi.Width = newWidth;
  239. GridUi.Height = newHeight;
  240. PressPoint = currentPos;
  241. CanMove = false;
  242. if (GripControl.IsMouseCaptured == false)
  243. {
  244. GripControl.CaptureMouse();
  245. }
  246. }
  247. }
  248. private void CloseText_MouseUp(object sender, MouseButtonEventArgs e)
  249. {
  250. if (e != null)
  251. {
  252. e.Handled = true;
  253. }
  254. PlaceChange = null;
  255. RemoveFromLayer();
  256. if (Closed != null)
  257. {
  258. Closed.Invoke(sender, EventArgs.Empty);
  259. }
  260. }
  261. protected override void OnMouseDown(MouseButtonEventArgs e)
  262. {
  263. e.Handled = true;
  264. PressPoint = e.GetPosition(ContainerElement);
  265. }
  266. protected override void OnMouseEnter(MouseEventArgs e)
  267. {
  268. e.Handled = true;
  269. if (PlaceChange != null)
  270. {
  271. PlaceChange.Invoke(this, new EventArgs());
  272. }
  273. }
  274. protected override void OnMouseLeave(MouseEventArgs e)
  275. {
  276. e.Handled = true;
  277. if (PlaceChange != null)
  278. {
  279. PlaceChange.Invoke(null, new EventArgs());
  280. }
  281. PressPoint = new Point(0, 0);
  282. }
  283. protected override void OnMouseUp(MouseButtonEventArgs e)
  284. {
  285. CanMove = true;
  286. if (PlaceChange != null)
  287. {
  288. PlaceChange.Invoke(this, new EventArgs());
  289. }
  290. PressPoint = new Point(0, 0);
  291. GripControl.ReleaseMouseCapture();
  292. }
  293. protected override void OnMouseMove(MouseEventArgs e)
  294. {
  295. e.Handled = true;
  296. if (e.LeftButton == MouseButtonState.Pressed && e.Source.GetType() != typeof(TextBox) && e.Source.GetType() != typeof(GripThumb))
  297. {
  298. if (CanMove == false || (PressPoint.X == 0 && PressPoint.Y == 0) || ContainerElement == null)
  299. {
  300. return;
  301. }
  302. Point currentPoint = e.GetPosition(ContainerElement);
  303. double newLeft = Left + currentPoint.X - PressPoint.X;
  304. double newTop = Top + currentPoint.Y - PressPoint.Y;
  305. if (newLeft < 0)
  306. {
  307. newLeft = 0;
  308. }
  309. if (newLeft + GridUi.ActualWidth > ContainerElement.ActualWidth)
  310. {
  311. newLeft = ContainerElement.ActualWidth - GridUi.ActualWidth;
  312. }
  313. if (newTop < 0)
  314. {
  315. newTop = 0;
  316. }
  317. if (newTop + GridUi.ActualHeight > ContainerElement.ActualHeight)
  318. {
  319. newTop = ContainerElement.ActualHeight - GridUi.ActualHeight;
  320. }
  321. Left = newLeft;
  322. Top = newTop;
  323. PressPoint = currentPoint;
  324. if (PlaceChange != null)
  325. {
  326. PlaceChange.Invoke(null, new EventArgs());
  327. }
  328. RefreshPosition();
  329. }
  330. }
  331. public override void SetAuthor(string author)
  332. {
  333. base.SetAuthor(author);
  334. StickyAuthor = author;
  335. }
  336. public override void SetDateText(string dateText)
  337. {
  338. base.SetDateText(dateText);
  339. if (Regex.IsMatch(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  340. {
  341. string dateStr = Regex.Match(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  342. dateText = dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + "/" + dateStr.Substring(0, 4) + " " + dateStr.Substring(8, 2) + ":" +
  343. dateStr.Substring(10, 2) + ":" + dateStr.Substring(12, 2);
  344. }
  345. StickyDate = dateText;
  346. }
  347. public override void SetStickyColor(Color newColor)
  348. {
  349. base.SetStickyColor(newColor);
  350. border.BorderBrush = new SolidColorBrush(newColor);
  351. this.Background = new SolidColorBrush(Colors.Transparent);
  352. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  353. }
  354. public override void SetStickyNote(string note)
  355. {
  356. base.SetStickyNote(note);
  357. StickyText = note;
  358. try
  359. {
  360. ContentText.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.AnchoredFamaily);
  361. }
  362. catch (Exception ex)
  363. {
  364. }
  365. }
  366. public override void SetReadOnly(bool readOnly)
  367. {
  368. base.SetReadOnly(readOnly);
  369. ContentText.IsReadOnly = readOnly;
  370. }
  371. public override string GetText()
  372. {
  373. return StickyText;
  374. }
  375. /// <summary>
  376. /// 鼠标滚动后,图标消失,弹框消失
  377. /// </summary>
  378. public override void Close()
  379. {
  380. //base.Close();
  381. RemoveFromLayer();
  382. }
  383. private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
  384. {
  385. var colorItem = ListColor.SelectedItem as ColorItem;
  386. if (colorItem != null)
  387. {
  388. var color = (colorItem.Color as SolidColorBrush).Color;
  389. this.SetStickyColor(color);
  390. var annot = GetCurrentAnnot as StickyAnnotArgs;
  391. if (annot != null)
  392. {
  393. annot.Color = color;
  394. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  395. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, color);
  396. AnnotEvent?.UpdateAnnot();
  397. }
  398. border.BorderBrush = new SolidColorBrush(color);
  399. this.Background = new SolidColorBrush(Colors.Transparent);
  400. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  401. eventAggregator.GetEvent<StickyNoteColorEvent>().Publish(new StickyNoteColorUnicode { Unicode = Unicode, brush = new SolidColorBrush(color) });
  402. }
  403. }
  404. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  405. {
  406. if (GetPDFViewer != null)
  407. {
  408. if (GetCurrentAnnot != null)
  409. {
  410. GetPDFViewer.RemovePageAnnot(GetCurrentAnnot.PageIndex, GetCurrentAnnot.AnnotIndex);
  411. }
  412. // Closed.Invoke(sender, EventArgs.Empty);
  413. CloseText_MouseUp(this, null);
  414. if (GetPDFViewer.ToolManager != null)
  415. {
  416. GetPDFViewer.ToolManager.DisableStickyCreate = false;
  417. }
  418. }
  419. }
  420. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  421. {
  422. var frame = e.OriginalSource as Control;
  423. if (frame != null)
  424. {
  425. var color = frame.Background as SolidColorBrush;
  426. if (color != null && color.Color == Colors.Transparent)
  427. Closed.Invoke(sender, EventArgs.Empty);
  428. }
  429. }
  430. private void StickyPopupExt_LostFocus(object sender, RoutedEventArgs e)
  431. {
  432. }
  433. }
  434. }