ColorDropBox.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using PDF_Master.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Master.CustomControl
  19. {
  20. /// <summary>
  21. /// ColorDropBox.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class ColorDropBox : UserControl, INotifyPropertyChanged
  24. {
  25. private AdornerLayer popLayer;
  26. private AdornerPresenter layerPresent;
  27. private bool layerAdded = false;
  28. private Canvas popCanvas;
  29. private ColorDropBoxPop colorPop;
  30. public event EventHandler<Color?> SelectedColorChanged;
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. protected void OnPropertyChanged([CallerMemberName] string name = null)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  35. }
  36. private Color? _selectedColor;
  37. public Color? SelectedColor
  38. {
  39. get
  40. {
  41. return _selectedColor;
  42. }
  43. set
  44. {
  45. if (_selectedColor != value)
  46. {
  47. _selectedColor = value;
  48. OnPropertyChanged("SelectedColor");
  49. }
  50. }
  51. }
  52. public bool HasTransparentColor
  53. {
  54. get
  55. {
  56. if (colorPop != null)
  57. {
  58. return colorPop.TransparentPanel.Visibility == Visibility.Visible;
  59. }
  60. return false;
  61. }
  62. set
  63. {
  64. if (colorPop != null)
  65. {
  66. colorPop.SetTransparentPanelVisible(value);
  67. }
  68. }
  69. }
  70. public ColorDropBox()
  71. {
  72. InitializeComponent();
  73. Loaded += ColorDropBox_Loaded;
  74. Unloaded += ColorDropBox_Unloaded;
  75. MouseDown += ColorDropBox_MouseDown;
  76. colorPop = new ColorDropBoxPop();
  77. }
  78. private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
  79. {
  80. if (layerAdded)
  81. {
  82. RemoveFromLayer();
  83. return;
  84. }
  85. }
  86. private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
  87. {
  88. e.Handled = true;
  89. ShowLayer();
  90. }
  91. public void ShowLayer(DependencyObject dependencyObject = null)
  92. {
  93. if (layerAdded)
  94. {
  95. RemoveFromLayer();
  96. return;
  97. }
  98. AddToLayer(dependencyObject);
  99. }
  100. private void AddToLayer(DependencyObject dependencyObject = null)
  101. {
  102. Window parentWnd = Window.GetWindow(this);
  103. popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
  104. if (popLayer != null && colorPop != null &&!layerAdded)
  105. {
  106. if (layerPresent == null)
  107. {
  108. layerPresent = new AdornerPresenter(popLayer);
  109. }
  110. popLayer.Add(layerPresent);
  111. if (popCanvas == null)
  112. {
  113. popCanvas = new Canvas();
  114. popCanvas.Children.Add(colorPop);
  115. layerPresent.VisualContent = popCanvas;
  116. colorPop.ColorSelected += ColorPop_ColorSelected;
  117. }
  118. Point offset = GetOffsetToWindow(dependencyObject);
  119. colorPop.Visibility = Visibility.Visible;
  120. colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
  121. colorPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
  122. if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
  123. {
  124. colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
  125. }
  126. else
  127. {
  128. colorPop.SetValue(Canvas.LeftProperty, offset.X);
  129. }
  130. layerAdded = true;
  131. }
  132. }
  133. private void RemoveFromLayer()
  134. {
  135. if (popLayer != null && layerPresent != null && layerAdded)
  136. {
  137. popLayer.Remove(layerPresent);
  138. layerAdded = false;
  139. }
  140. }
  141. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  142. {
  143. if (popLayer == null)
  144. {
  145. Window parentWnd = Window.GetWindow(this);
  146. if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
  147. {
  148. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  149. {
  150. if (layerAdded)
  151. {
  152. try
  153. {
  154. Window checkWindow = Window.GetWindow(this);
  155. Point clickPoint = param.GetPosition(checkWindow);
  156. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  157. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  158. Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
  159. Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
  160. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  161. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  162. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  163. {
  164. return;
  165. }
  166. RemoveFromLayer();
  167. }
  168. catch(Exception ex)
  169. {
  170. }
  171. }
  172. }), true);
  173. }
  174. }
  175. }
  176. private void ColorPop_ColorSelected(object sender, Color e)
  177. {
  178. if (SelectedColor != e)
  179. {
  180. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  181. // TransparentRectControl.Visibility = Visibility.Collapsed;
  182. if (e != Colors.Transparent)
  183. {
  184. // NormalColorRectControl.Visibility = Visibility.Visible;
  185. // NormalColorRectControl.Fill = new SolidColorBrush(e);
  186. }
  187. else
  188. {
  189. // TransparentRectControl.Visibility = Visibility.Visible;
  190. }
  191. SelectedColor = e;
  192. SelectedColorChanged?.Invoke(this, SelectedColor);
  193. }
  194. RemoveFromLayer();
  195. }
  196. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  197. {
  198. Window parentWnd = Window.GetWindow(this);
  199. if (dependencyObject != null)
  200. {
  201. var frame = dependencyObject as FrameworkElement;
  202. if(frame != null)
  203. {
  204. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  205. }
  206. }
  207. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  208. }
  209. public void SetThemeColors(List<Color> themeColorList)
  210. {
  211. if (colorPop != null)
  212. {
  213. colorPop.ThemeColors.Clear();
  214. foreach (Color themeColor in themeColorList)
  215. {
  216. colorPop.ThemeColors.Add(themeColor);
  217. }
  218. }
  219. }
  220. public List<Color> GetThemeColors()
  221. {
  222. if (colorPop != null)
  223. {
  224. return colorPop.ThemeColors.ToList();
  225. }
  226. return new List<Color>();
  227. }
  228. public List<Color> GetRecentlyColors()
  229. {
  230. if (colorPop != null)
  231. {
  232. return colorPop.RecentlyColors.ToList();
  233. }
  234. return new List<Color>();
  235. }
  236. public void SetSelectedColor(Color setColor)
  237. {
  238. if (SelectedColor != setColor)
  239. {
  240. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  241. // TransparentRectControl.Visibility = Visibility.Collapsed;
  242. if (setColor != Colors.Transparent)
  243. {
  244. // NormalColorRectControl.Visibility = Visibility.Visible;
  245. // NormalColorRectControl.Fill = new SolidColorBrush(setColor);
  246. }
  247. else
  248. {
  249. // TransparentRectControl.Visibility = Visibility.Visible;
  250. }
  251. SelectedColor = setColor;
  252. if (colorPop != null)
  253. {
  254. colorPop.AddColorToRecent(setColor);
  255. }
  256. }
  257. }
  258. public void ClearSelectColor()
  259. {
  260. _selectedColor = null;
  261. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  262. // TransparentRectControl.Visibility = Visibility.Collapsed;
  263. }
  264. }
  265. }