using PDF_Master.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PDF_Master.CustomControl
{
///
/// ColorDropBox.xaml 的交互逻辑
///
public partial class ColorDropBox : UserControl, INotifyPropertyChanged
{
private AdornerLayer popLayer;
private AdornerPresenter layerPresent;
private bool layerAdded = false;
private Canvas popCanvas;
private ColorDropBoxPop colorPop;
public event EventHandler SelectedColorChanged;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private Color? _selectedColor;
public Color? SelectedColor
{
get
{
return _selectedColor;
}
set
{
if (_selectedColor != value)
{
_selectedColor = value;
OnPropertyChanged("SelectedColor");
}
}
}
public bool HasTransparentColor
{
get
{
if (colorPop != null)
{
return colorPop.TransparentPanel.Visibility == Visibility.Visible;
}
return false;
}
set
{
if (colorPop != null)
{
colorPop.SetTransparentPanelVisible(value);
}
}
}
public ColorDropBox()
{
InitializeComponent();
Loaded += ColorDropBox_Loaded;
Unloaded += ColorDropBox_Unloaded;
MouseDown += ColorDropBox_MouseDown;
colorPop = new ColorDropBoxPop();
}
private void ColorDropBox_Unloaded(object sender, RoutedEventArgs e)
{
if (layerAdded)
{
RemoveFromLayer();
return;
}
}
private void ColorDropBox_MouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
ShowLayer();
}
public void ShowLayer(DependencyObject dependencyObject = null)
{
if (layerAdded)
{
RemoveFromLayer();
return;
}
AddToLayer(dependencyObject);
}
private void AddToLayer(DependencyObject dependencyObject = null)
{
Window parentWnd = Window.GetWindow(this);
popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
if (popLayer != null && colorPop != null &&!layerAdded)
{
if (layerPresent == null)
{
layerPresent = new AdornerPresenter(popLayer);
}
popLayer.Add(layerPresent);
if (popCanvas == null)
{
popCanvas = new Canvas();
popCanvas.Children.Add(colorPop);
layerPresent.VisualContent = popCanvas;
colorPop.ColorSelected += ColorPop_ColorSelected;
}
Point offset = GetOffsetToWindow(dependencyObject);
colorPop.Visibility = Visibility.Visible;
colorPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
colorPop.SetValue(Canvas.TopProperty, offset.Y + this.ActualHeight + 2);
if (offset.X + colorPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
{
colorPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - colorPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
}
else
{
colorPop.SetValue(Canvas.LeftProperty, offset.X);
}
layerAdded = true;
}
}
private void RemoveFromLayer()
{
if (popLayer != null && layerPresent != null && layerAdded)
{
popLayer.Remove(layerPresent);
layerAdded = false;
}
}
private void ColorDropBox_Loaded(object sender, RoutedEventArgs e)
{
if (popLayer == null)
{
Window parentWnd = Window.GetWindow(this);
if (parentWnd != null && parentWnd.Content is UIElement && colorPop != null)
{
parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
{
if (layerAdded)
{
try
{
Window checkWindow = Window.GetWindow(this);
Point clickPoint = param.GetPosition(checkWindow);
Point leftTop = TransformToVisual(checkWindow).Transform(new Point(0, 0));
Point rightBottom = TransformToVisual(checkWindow).Transform(new Point(ActualWidth, ActualHeight));
Point popLeftTop = new Point(Canvas.GetLeft(colorPop), Canvas.GetTop(colorPop));
Point popRightBottom = new Point(colorPop.ActualWidth, colorPop.ActualHeight);
Rect dropboxRect = new Rect(leftTop.X, leftTop.Y, rightBottom.X - leftTop.X, rightBottom.Y - leftTop.Y);
Rect popboxRect = new Rect(popLeftTop.X, popLeftTop.Y, popRightBottom.X, popRightBottom.Y);
if (dropboxRect.Contains(clickPoint) || popboxRect.Contains(clickPoint))
{
return;
}
RemoveFromLayer();
}
catch(Exception ex)
{
}
}
}), true);
}
}
}
private void ColorPop_ColorSelected(object sender, Color e)
{
if (SelectedColor != e)
{
// NormalColorRectControl.Visibility = Visibility.Collapsed;
// TransparentRectControl.Visibility = Visibility.Collapsed;
if (e != Colors.Transparent)
{
// NormalColorRectControl.Visibility = Visibility.Visible;
// NormalColorRectControl.Fill = new SolidColorBrush(e);
}
else
{
// TransparentRectControl.Visibility = Visibility.Visible;
}
SelectedColor = e;
SelectedColorChanged?.Invoke(this, SelectedColor);
}
RemoveFromLayer();
}
public Point GetOffsetToWindow(DependencyObject dependencyObject = null)
{
Window parentWnd = Window.GetWindow(this);
if (dependencyObject != null)
{
var frame = dependencyObject as FrameworkElement;
if(frame != null)
{
return frame.TransformToAncestor(parentWnd).Transform(new Point(0, 0));
}
}
return TransformToAncestor(parentWnd).Transform(new Point(0, 0));
}
public void SetThemeColors(List themeColorList)
{
if (colorPop != null)
{
colorPop.ThemeColors.Clear();
foreach (Color themeColor in themeColorList)
{
colorPop.ThemeColors.Add(themeColor);
}
}
}
public List GetThemeColors()
{
if (colorPop != null)
{
return colorPop.ThemeColors.ToList();
}
return new List();
}
public List GetRecentlyColors()
{
if (colorPop != null)
{
return colorPop.RecentlyColors.ToList();
}
return new List();
}
public void SetSelectedColor(Color setColor)
{
if (SelectedColor != setColor)
{
// NormalColorRectControl.Visibility = Visibility.Collapsed;
// TransparentRectControl.Visibility = Visibility.Collapsed;
if (setColor != Colors.Transparent)
{
// NormalColorRectControl.Visibility = Visibility.Visible;
// NormalColorRectControl.Fill = new SolidColorBrush(setColor);
}
else
{
// TransparentRectControl.Visibility = Visibility.Visible;
}
SelectedColor = setColor;
if (colorPop != null)
{
colorPop.AddColorToRecent(setColor);
}
}
}
public void ClearSelectColor()
{
_selectedColor = null;
// NormalColorRectControl.Visibility = Visibility.Collapsed;
// TransparentRectControl.Visibility = Visibility.Collapsed;
}
}
}