StickyNotePopup.xaml.cs 16 KB

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