ColorDropBox.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. //因为第一次计算弹窗高度时,会有偏差(加载未完成),暂时固定当作209来计算
  127. else if (offset.Y + 209 + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualHeight)
  128. {
  129. //超出窗体高度时 重新计算定位
  130. colorPop.SetValue(Canvas.TopProperty, parentWnd.ActualHeight - 260);
  131. colorPop.SetValue(Canvas.LeftProperty, offset.X + 25);
  132. }
  133. else
  134. {
  135. colorPop.SetValue(Canvas.LeftProperty, offset.X);
  136. }
  137. layerAdded = true;
  138. }
  139. }
  140. private void RemoveFromLayer()
  141. {
  142. if (popLayer != null && layerPresent != null && layerAdded)
  143. {
  144. popLayer.Remove(layerPresent);
  145. layerAdded = false;
  146. }
  147. }
  148. private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
  149. {
  150. if (popLayer == null)
  151. {
  152. Window parentWnd = Window.GetWindow(this);
  153. if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
  154. {
  155. parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
  156. {
  157. if (layerAdded)
  158. {
  159. try
  160. {
  161. Window checkWindow = Window.GetWindow(this);
  162. Point clickPoint = param.GetPosition(checkWindow);
  163. Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
  164. Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
  165. Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
  166. Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
  167. Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
  168. Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
  169. if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
  170. {
  171. return;
  172. }
  173. RemoveFromLayer();
  174. }
  175. catch
  176. {
  177. }
  178. }
  179. }), true);
  180. }
  181. }
  182. }
  183. private void ColorPop_ColorSelected(object sender, Color e)
  184. {
  185. //if (SelectedColor != e)
  186. {
  187. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  188. // TransparentRectControl.Visibility = Visibility.Collapsed;
  189. if (e != Colors.Transparent)
  190. {
  191. // NormalColorRectControl.Visibility = Visibility.Visible;
  192. // NormalColorRectControl.Fill = new SolidColorBrush(e);
  193. }
  194. else
  195. {
  196. // TransparentRectControl.Visibility = Visibility.Visible;
  197. }
  198. SelectedColor = e;
  199. SelectedColorChanged?.Invoke(this, SelectedColor);
  200. }
  201. RemoveFromLayer();
  202. }
  203. public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
  204. {
  205. Window parentWnd = Window.GetWindow(this);
  206. if (dependencyObject != null)
  207. {
  208. var frame = dependencyObject as FrameworkElement;
  209. if (frame != null)
  210. {
  211. return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  212. }
  213. }
  214. return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
  215. }
  216. public void SetThemeColors(List<Color> themeColorList)
  217. {
  218. if (colorPop != null)
  219. {
  220. colorPop.ThemeColors.Clear();
  221. foreach (Color themeColor in themeColorList)
  222. {
  223. colorPop.ThemeColors.Add(themeColor);
  224. }
  225. }
  226. }
  227. public List<Color> GetThemeColors()
  228. {
  229. if (colorPop != null)
  230. {
  231. return colorPop.ThemeColors.ToList();
  232. }
  233. return new List<Color>();
  234. }
  235. public List<Color> GetRecentlyColors()
  236. {
  237. if (colorPop != null)
  238. {
  239. return colorPop.RecentlyColors.ToList();
  240. }
  241. return new List<Color>();
  242. }
  243. public void SetSelectedColor(Color setColor)
  244. {
  245. if (SelectedColor != setColor)
  246. {
  247. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  248. // TransparentRectControl.Visibility = Visibility.Collapsed;
  249. if (setColor != Colors.Transparent)
  250. {
  251. // NormalColorRectControl.Visibility = Visibility.Visible;
  252. // NormalColorRectControl.Fill = new SolidColorBrush(setColor);
  253. }
  254. else
  255. {
  256. // TransparentRectControl.Visibility = Visibility.Visible;
  257. }
  258. SelectedColor = setColor;
  259. if (colorPop != null)
  260. {
  261. colorPop.AddColorToRecent(setColor);
  262. }
  263. }
  264. }
  265. public void ClearSelectColor()
  266. {
  267. _selectedColor = null;
  268. // NormalColorRectControl.Visibility = Visibility.Collapsed;
  269. // TransparentRectControl.Visibility = Visibility.Collapsed;
  270. }
  271. }
  272. }