ColorDropBox.xaml.cs 11 KB

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