StickyNotePopup.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. }
  123. private void GetPDFViewer_LeftButtonDown(object sender, MouseButtonEventArgs e)
  124. {
  125. var ui = e.OriginalSource as FrameworkElement;
  126. if (ui != null)
  127. {
  128. if (ui.DataContext != null && ui.DataContext is ColorItem == false)
  129. {
  130. CloseText_MouseUp(this, null);
  131. GetPDFViewer.PreviewMouseLeftButtonDown -= GetPDFViewer_LeftButtonDown;
  132. }
  133. }
  134. }
  135. public void LoadedColor()
  136. {
  137. if (colors != null && colors.Count > 0)
  138. {
  139. BtnDelete.DataContext = colors[0];//为了避免点击删除按钮之后,先执行取消焦点函数后,就没法执行点击事件
  140. if (GetCurrentAnnot != null)
  141. {
  142. foreach (var item in colors)
  143. {
  144. var colorItem = (item.Color as SolidColorBrush).Color;
  145. if (colorItem.A == GetCurrentAnnot.Color.A &&
  146. colorItem.R == GetCurrentAnnot.Color.R &&
  147. colorItem.G == GetCurrentAnnot.Color.G &&
  148. colorItem.B == GetCurrentAnnot.Color.B
  149. )
  150. {
  151. ListColor.SelectedItem = item;
  152. return;
  153. }
  154. }
  155. }
  156. ListColor.SelectedItem = null;
  157. }
  158. }
  159. private void StickyPopupControl_MouseDown(object sender, MouseButtonEventArgs e)
  160. {
  161. PressPoint = new Point(0, 0);
  162. }
  163. private void StickyPopupControl_MouseUp(object sender, MouseButtonEventArgs e)
  164. {
  165. CanMove = true;
  166. GripControl.ReleaseMouseCapture();
  167. }
  168. public string StickyText
  169. {
  170. get
  171. {
  172. return ContentText.Text;
  173. }
  174. set
  175. {
  176. ContentText.Text = value;
  177. }
  178. }
  179. public string StickyAuthor
  180. {
  181. get
  182. {
  183. return AuthorText.Text;
  184. }
  185. set
  186. {
  187. AuthorText.Text = value;
  188. }
  189. }
  190. public string StickyDate
  191. {
  192. get
  193. {
  194. return DateText.Text;
  195. }
  196. set
  197. {
  198. DateText.Text = value;
  199. }
  200. }
  201. public StickyAnnotArgs GetCurrentAnnot { get; set; }
  202. public CPDFViewer GetPDFViewer { get; set; }
  203. public bool CanMove { get; set; } = true;
  204. private void ResizeGrip_MouseMove(object sender, MouseEventArgs e)
  205. {
  206. e.Handled = true;
  207. if (e.LeftButton == MouseButtonState.Pressed)
  208. {
  209. Point currentPos = e.GetPosition(ContainerElement);
  210. double newWidth = GridUi.ActualWidth + currentPos.X - PressPoint.X;
  211. double newHeight = GridUi.ActualHeight + currentPos.Y - PressPoint.Y;
  212. if (Left + newWidth >= ContainerElement.ActualWidth)
  213. {
  214. newWidth = ContainerElement.ActualWidth - Left - 5;
  215. }
  216. if (Top + newHeight >= ContainerElement.ActualHeight)
  217. {
  218. newHeight = ContainerElement.ActualHeight - Top - 5;
  219. }
  220. GridUi.Width = newWidth;
  221. GridUi.Height = newHeight;
  222. PressPoint = currentPos;
  223. CanMove = false;
  224. if (GripControl.IsMouseCaptured == false)
  225. {
  226. GripControl.CaptureMouse();
  227. }
  228. }
  229. }
  230. private void CloseText_MouseUp(object sender, MouseButtonEventArgs e)
  231. {
  232. if (e != null)
  233. {
  234. e.Handled = true;
  235. }
  236. PlaceChange = null;
  237. RemoveFromLayer();
  238. if (Closed != null)
  239. {
  240. Closed.Invoke(sender, EventArgs.Empty);
  241. }
  242. }
  243. protected override void OnMouseDown(MouseButtonEventArgs e)
  244. {
  245. e.Handled = true;
  246. PressPoint = e.GetPosition(ContainerElement);
  247. }
  248. protected override void OnMouseEnter(MouseEventArgs e)
  249. {
  250. e.Handled = true;
  251. if (PlaceChange != null)
  252. {
  253. PlaceChange.Invoke(this, new EventArgs());
  254. }
  255. }
  256. protected override void OnMouseLeave(MouseEventArgs e)
  257. {
  258. e.Handled = true;
  259. if (PlaceChange != null)
  260. {
  261. PlaceChange.Invoke(null, new EventArgs());
  262. }
  263. PressPoint = new Point(0, 0);
  264. }
  265. protected override void OnMouseUp(MouseButtonEventArgs e)
  266. {
  267. CanMove = true;
  268. if (PlaceChange != null)
  269. {
  270. PlaceChange.Invoke(this, new EventArgs());
  271. }
  272. PressPoint = new Point(0, 0);
  273. GripControl.ReleaseMouseCapture();
  274. }
  275. protected override void OnMouseMove(MouseEventArgs e)
  276. {
  277. e.Handled = true;
  278. if (e.LeftButton == MouseButtonState.Pressed && e.Source.GetType() != typeof(TextBox) && e.Source.GetType() != typeof(GripThumb))
  279. {
  280. if (CanMove == false || (PressPoint.X == 0 && PressPoint.Y == 0) || ContainerElement == null)
  281. {
  282. return;
  283. }
  284. Point currentPoint = e.GetPosition(ContainerElement);
  285. double newLeft = Left + currentPoint.X - PressPoint.X;
  286. double newTop = Top + currentPoint.Y - PressPoint.Y;
  287. if (newLeft < 0)
  288. {
  289. newLeft = 0;
  290. }
  291. if (newLeft + GridUi.ActualWidth > ContainerElement.ActualWidth)
  292. {
  293. newLeft = ContainerElement.ActualWidth - GridUi.ActualWidth;
  294. }
  295. if (newTop < 0)
  296. {
  297. newTop = 0;
  298. }
  299. if (newTop + GridUi.ActualHeight > ContainerElement.ActualHeight)
  300. {
  301. newTop = ContainerElement.ActualHeight - GridUi.ActualHeight;
  302. }
  303. Left = newLeft;
  304. Top = newTop;
  305. PressPoint = currentPoint;
  306. if (PlaceChange != null)
  307. {
  308. PlaceChange.Invoke(null, new EventArgs());
  309. }
  310. RefreshPosition();
  311. }
  312. }
  313. public override void SetAuthor(string author)
  314. {
  315. base.SetAuthor(author);
  316. StickyAuthor = author;
  317. }
  318. public override void SetDateText(string dateText)
  319. {
  320. base.SetDateText(dateText);
  321. if (Regex.IsMatch(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
  322. {
  323. string dateStr = Regex.Match(dateText, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
  324. dateText = dateStr.Substring(4, 2) + "/" + dateStr.Substring(6, 2) + "/" + dateStr.Substring(0, 4) + " " + dateStr.Substring(8, 2) + ":" +
  325. dateStr.Substring(10, 2) + ":" + dateStr.Substring(12, 2);
  326. }
  327. StickyDate = dateText;
  328. }
  329. public override void SetStickyColor(Color newColor)
  330. {
  331. base.SetStickyColor(newColor);
  332. border.BorderBrush = new SolidColorBrush(newColor);
  333. this.Background = new SolidColorBrush(Colors.Transparent);
  334. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  335. }
  336. public override void SetStickyNote(string note)
  337. {
  338. base.SetStickyNote(note);
  339. StickyText = note;
  340. try
  341. {
  342. ContentText.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.AnchoredFamaily);
  343. }
  344. catch (Exception ex)
  345. {
  346. }
  347. }
  348. public override void SetReadOnly(bool readOnly)
  349. {
  350. base.SetReadOnly(readOnly);
  351. ContentText.IsReadOnly = readOnly;
  352. }
  353. public override string GetText()
  354. {
  355. return StickyText;
  356. }
  357. private void ListColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
  358. {
  359. var colorItem = ListColor.SelectedItem as ColorItem;
  360. if (colorItem != null)
  361. {
  362. var color = (colorItem.Color as SolidColorBrush).Color;
  363. this.SetStickyColor(color);
  364. var annot = GetCurrentAnnot as StickyAnnotArgs;
  365. if (annot != null)
  366. {
  367. annot.Color = color;
  368. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  369. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, color);
  370. AnnotEvent?.UpdateAnnot();
  371. }
  372. border.BorderBrush = new SolidColorBrush(color);
  373. this.Background = new SolidColorBrush(Colors.Transparent);
  374. this.BorderBrush = new SolidColorBrush(Colors.Transparent);
  375. }
  376. }
  377. private void BtnDelete_Click(object sender, RoutedEventArgs e)
  378. {
  379. if (GetPDFViewer != null)
  380. {
  381. if (GetCurrentAnnot != null)
  382. GetPDFViewer.RemovePageAnnot(GetCurrentAnnot.PageIndex, GetCurrentAnnot.AnnotIndex);
  383. // Closed.Invoke(sender, EventArgs.Empty);
  384. CloseText_MouseUp(this, null);
  385. }
  386. }
  387. private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  388. {
  389. var frame = e.OriginalSource as Control;
  390. if (frame != null)
  391. {
  392. var color = frame.Background as SolidColorBrush;
  393. if (color != null && color.Color == Colors.Transparent)
  394. Closed.Invoke(sender, EventArgs.Empty);
  395. }
  396. }
  397. private void StickyPopupExt_LostFocus(object sender, RoutedEventArgs e)
  398. {
  399. }
  400. }
  401. }