using PDF_Master.CustomControl;
using PDF_Master.Helper;
using PDF_Master.Model.PDFTool;
using PDF_Master.ViewModels.HomePanel;
using PDF_Master.ViewModels.HomePanel.PDFTools;
using PDFSettings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PDF_Master.Views.HomePanel.PDFTools
{
///
/// ToolbarPage.xaml 的交互逻辑
///
public partial class PDFToolsContent : UserControl
{
private PDFToolsContentViewModel ViewModel => DataContext as PDFToolsContentViewModel;
private ListItemQuickTool sourceListItem = null;
private ListItemQuickTool targetListItem = null;
private ListItemQuickTool quickSourceListItem = null;
private List quickTools = null;
private List allTools = null;
private List moreTools = null;
PDFToolsHelper PDFtools;
private string fileName = string.Empty;
private BitmapSource bitmapSource = null;
private bool isMoreCuts = false;
private bool isMove = false;
private Point wrpnlPoint;
private Point mousePoint;
public PDFToolsContent()
{
InitializeComponent();
PDFtools = PDFToolsHelper.GetInstance();
#region ListBox
SetListBox();
#endregion ListBox
}
//用来测试的,PDF工具还在优化中
private void Button_Click(object sender, RoutedEventArgs e)
{
var PDFtools = Helper.PDFToolsHelper.GetInstance();
PDFtools.ClearData();
SetListBox();
ListShortCutsExpend.Items.Refresh();
ListMoreCutsExpend.Items.Refresh();
ListBoxCutsWidthChanged(ListShortCutsExpend);
ListBoxCutsWidthChanged(ListMoreCutsExpend);
ListShortCuts.Items.Refresh();
ListMoreCuts.Items.Refresh();
ListBoxCutsWidthChanged(ListShortCuts);
ListBoxCutsWidthChanged(ListMoreCuts);
}
///
/// 获取与窗口相对的鼠标位置。
///
///
///
private void OnRendering(object sender, EventArgs e)
{
mousePoint = Mouse.GetPosition(this);
Console.WriteLine($"鼠标坐标:{mousePoint.X}:{mousePoint.Y}");
}
private void SetListBox()
{
quickTools = PDFtools.QuickTools;
allTools = PDFtools.AllTools;
moreTools = PDFtools.MoreTools;
ListShortCutsExpend.ItemsSource = ListShortCuts.ItemsSource = quickTools;
ListMoreCutsExpend.ItemsSource = ListMoreCuts.ItemsSource = moreTools;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
UpdateExptend();
if (ViewModel != null)
{
ViewModel.ExpendToolsHanlder -= ViewModel_ExpendToolsHanlder;
ViewModel.ExpendToolsHanlder += ViewModel_ExpendToolsHanlder;
}
}
private void ViewModel_ExpendToolsHanlder(object sender, bool e)
{
if(e)
{
ListShortCutsExpend.Items.Refresh();
ListMoreCutsExpend.Items.Refresh();
ListBoxCutsWidthChanged(ListShortCutsExpend);
ListBoxCutsWidthChanged(ListMoreCutsExpend);
}
else
{
ListShortCuts.Items.Refresh();
ListMoreCuts.Items.Refresh();
ListBoxCutsWidthChanged(ListShortCuts);
ListBoxCutsWidthChanged(ListMoreCuts);
}
}
private ListBoxEx GetListShortControl()
{
ListBoxEx ShortEx = null;
if (ListShortCuts.Visibility == Visibility.Visible)
{
ShortEx = ListShortCuts;
}
else
{
ShortEx = ListShortCutsExpend;
}
return ShortEx;
}
private ListBoxEx GetListMoreControl()
{
ListBoxEx MoreEx = null;
if (ListMoreCuts.Visibility == Visibility.Visible)
{
MoreEx = ListMoreCuts;
}
else
{
MoreEx = ListMoreCutsExpend;
}
return MoreEx;
}
private void ListBoxMoreCuts_PreviewMouseMove(object sender, MouseEventArgs e)
{
ListBoxEx MoreEx = GetListMoreControl();
if (MoreEx == null) return;
var pos = e.GetPosition(MoreEx);
if (e.LeftButton == MouseButtonState.Pressed)
{
MoreEx.AllowDrop = true;
HitTestResult result = VisualTreeHelper.HitTest(MoreEx, pos);
if (result == null)
{
sourceImage.Visibility = Visibility.Hidden;
return;
}
sourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent(result.VisualHit);
if (sourceListItem == null || sourceListItem.Content != MoreEx.SelectedItem)
{
return;
}
//获取控件相对于另一控件的坐标
wrpnlPoint = sourceListItem.TranslatePoint(new Point(), this);
System.Windows.DataObject dataObj = new System.Windows.DataObject(sourceListItem.Content as ToolItem);
if (dataObj != null)
{
//控件截图
IsMoreToolbars();
if (string.IsNullOrEmpty(fileName))
{
sourceImage.Visibility = Visibility.Hidden;
return;
}
//图片解码
ImageDecoding();
isMoreCuts = true;
DragDrop.DoDragDrop(MoreEx, dataObj, System.Windows.DragDropEffects.Move);
}
}
else
{
moveImage.Visibility = Visibility.Hidden;
sourceImage.Visibility = Visibility.Hidden;
}
}
private void ImageDecoding()
{
Stream imageStreamSource = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
bitmapSource = decoder.Frames[0];
}
private void IsMoreToolbars()
{
var source = sourceListItem.Content as ToolItem;
if (moreTools == null && moreTools.Count != 0) return;
List toolModule = moreTools.FindAll(item => item.FnName == source.FnName);
if (toolModule.Count > 0)
{
// 判断文件夹是否存在,不存在则创建
DirectoryInfo folder = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Asource"));
if (!folder.Exists)
{
folder.Create();
}
fileName = System.IO.Path.Combine(folder.FullName, $"{source.FnName}.png");
SaveToPng(sourceListItem, fileName);
}
}
private void ListBoxShortCuts_Drop(object sender, DragEventArgs e)
{
ListBoxEx ShortEx = GetListShortControl();
if(ShortEx == null) return;
var pos = e.GetPosition(ShortEx);
var result = VisualTreeHelper.HitTest(ShortEx, pos);
if (result == null)
{
WorkingWithAnimation(pos);
return;
}
//查找目标数据
targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent(result.VisualHit);
if (targetListItem == null)
{
WorkingWithAnimation(pos);
return;
}
var targetAToolModule = targetListItem.Content as ToolItem;
ToolItem target = quickTools.Find(c => c.FnName == targetAToolModule.FnName);
//查找元数据
var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
if (sourceAToolModule == null)
{
sourceImage.Visibility = Visibility.Hidden;
// target.SetValue(AToolModule.IsFilePickedProperty, false);
return;
}
if (ReferenceEquals(targetAToolModule, sourceAToolModule))
{
sourceImage.Visibility = Visibility.Hidden;
// target.SetValue(AToolModule.IsFilePickedProperty, false);
return;
}
if (targetAToolModule == null && sourceAToolModule == null)
{
sourceImage.Visibility = Visibility.Hidden;
// target.SetValue(AToolModule.IsFilePickedProperty, false);
return;
}
ModuleExchange(sourceAToolModule, targetAToolModule);
}
private void ModuleExchange(ToolItem sourceAToolModule, ToolItem targetAToolModule)
{
if (moreTools == null) return;
ToolItem source = moreTools.Find(c => c.FnName == sourceAToolModule.FnName);
ListBoxEx MoreEx = GetListMoreControl();
if (MoreEx == null) return;
ListBoxEx ShortEx = GetListShortControl();
if (ShortEx == null) return;
int targetIndex = quickTools.FindIndex(item => item.FnName == targetAToolModule.FnName);
if (targetIndex < 0) return;
if (!isMoreCuts)
{
MoreEx.AllowDrop = false;
sourceImage.Visibility = Visibility.Hidden;
source = quickTools.Find(c => c.FnName == sourceAToolModule.FnName);
if (source == null) return;
int sourceIndex = quickTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
if (sourceIndex < 0) return;
var temp = quickTools[sourceIndex];
quickTools[sourceIndex] = quickTools[targetIndex];
quickTools[targetIndex] = temp;
var sourceId = source.ToolTypeId;
var targetId = target.ToolTypeId;
source.ToolTypeId = targetId;
target.ToolTypeId = sourceId;
source.IsQuickTool = true;
target.IsQuickTool = true;
ShortEx.Items.Refresh();
ListBoxCutsWidthChanged(ShortEx);
}
else
{
if (target == null) return;
if (source == null) return;
moreTools.Remove(source);
moreTools.Insert(0, target);
quickTools.Remove(target);
quickTools.Insert(targetIndex, source);
source.IsQuickTool = true;
target.IsQuickTool = false;
var targetId = target.ToolTypeId;
source.ToolTypeId = targetId;
target.ToolTypeId = 0;//移到第一个位置
MoreEx.Items.Refresh();
ShortEx.Items.Refresh();
sourceImage.Visibility = Visibility.Hidden;
targetListItem.IsOverModular = false;
ListBoxCutsWidthChanged(ShortEx);
ListBoxCutsWidthChanged(MoreEx);
}
if (PDFtools != null)
PDFtools.SaveCacheList();
}
private void WorkingWithAnimation(Point pos)
{
sourceImage.Visibility = Visibility.Hidden;
if (!isMoreCuts || !isMove)
{
moveImage.Visibility = Visibility.Hidden;
}
else
{
if (sourceListItem == null) return;
moveImage.Visibility = Visibility.Visible;
Point point = sourceListItem.TransformToAncestor(this).Transform(new Point(0, 0));
ImageMoveTo(new Point(pos.X - 80, pos.Y), new Point(wrpnlPoint.X + 40, wrpnlPoint.Y), moveImage, 200);
}
}
///
/// 图片移动动画
///
/// 初始位置
/// 控件要移动到的位置
/// 你要移动的空间
/// 设置移动的时间片(关系到控件移动的速度)
private void ImageMoveTo(Point curPoint, Point deskPoint, Image ell, double space)
{
Storyboard storyboard = new Storyboard(); //创建Storyboard对象
double lxspeed = space, lyspeed = space; //设置X方向 / Y方向 移动时间片
//创建X轴方向动画
DoubleAnimation dox = new DoubleAnimation(
curPoint.X,
deskPoint.X,
new Duration(TimeSpan.FromMilliseconds(lxspeed))
);
//创建Y轴方向动画
DoubleAnimation doy = new DoubleAnimation(
curPoint.Y,
deskPoint.Y,
new Duration(TimeSpan.FromMilliseconds(lyspeed))
);
Storyboard.SetTarget(dox, ell);
Storyboard.SetTarget(doy, ell);
Storyboard.SetTargetProperty(dox, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
Storyboard.SetTargetProperty(doy, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
storyboard.Children.Add(dox);
storyboard.Children.Add(doy);
storyboard.Completed += Storyboard_Completed;
//动画播放
storyboard.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
moveImage.Visibility = Visibility.Hidden;
}
///
/// 控件截图
///
/// 控件
/// 文件路径 C:\Users\oyxh\AppData\Local\Temp\
private void SaveToPng(FrameworkElement visual, string fileName)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen())
{
VisualBrush brush = new VisualBrush(sourceListItem) { Stretch = Stretch.None };
context.DrawRectangle(brush, null, new Rect(0, 0, sourceListItem.ActualWidth, sourceListItem.ActualHeight));
context.Close();
}
RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)sourceListItem.ActualWidth, (int)sourceListItem.ActualHeight, 96d, 96d, PixelFormats.Default);
targetBitmap.Render(drawingVisual);
FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.On;
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
encoder.Save(stream);
stream.Close();
}
private ToolItem target;
private void ListBoxShortCuts_DragOver(object sender, DragEventArgs e)
{
ListBoxEx MoreEx = GetListMoreControl();
if (MoreEx == null) return;
ListBoxEx ShortEx = GetListShortControl();
if (ShortEx == null) return;
var pos = e.GetPosition(ShortEx);
Console.WriteLine($"pos:{pos.X} {pos.Y} this.Grid.ActualHeight1 {this.Grid.ActualHeight}");
if (pos.Y < 0)
{
isMove = false;
sourceImage.Visibility = Visibility.Hidden;
sourceListItem = null;
return;
}
if (pos.Y >= this.Grid.ActualHeight - 50)
{
isMove = false;
sourceImage.Visibility = Visibility.Hidden;
sourceListItem = null;
return;
}
if (pos.X < 10)
{
isMove = false;
sourceListItem = null;
sourceImage.Visibility = Visibility.Hidden;
return;
}
if (pos.X >= this.Grid.ActualWidth - 10)
{
isMove = false;
sourceListItem = null;
sourceImage.Visibility = Visibility.Hidden;
return;
}
if (!isMoreCuts)
{
var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
if (sourceAToolModule == null) return;
int sourceIndex = quickTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
//根据index找listbox对应的item
sourceListItem = (ListItemQuickTool)(ShortEx.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
if (sourceListItem == null)
{
return;
}
MoreEx.AllowDrop = false;
sourceImage.Visibility = Visibility.Hidden;
}
else
{
var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
if (sourceAToolModule == null) return;
int sourceIndex = moreTools.FindIndex(item => item.FnName == sourceAToolModule.FnName);
//根据index找listbox对应的item
sourceListItem = (ListItemQuickTool)(MoreEx.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
if (sourceListItem == null)
{
return;
}
isMove = true;
//图片绑定
ImageBinding(pos);
}
var result = VisualTreeHelper.HitTest(ShortEx, pos);
if (result == null)
{
return;
}
//查找目标数据
targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent(result.VisualHit);
if (targetListItem == null)
{
//sourceImage.Visibility = Visibility.Hidden;
return;
}
var targetAToolModule = targetListItem.Content as ToolItem;
if (targetAToolModule == null) return;
target = quickTools.Find(c => c.FnName == targetAToolModule.FnName);
}
///
///图片绑定
///
private void ImageBinding(System.Windows.Point pos)
{
sourceImage.Visibility = Visibility.Visible;
sourceImage.Source = bitmapSource;
sourceImage.Margin = new Thickness(pos.X - 80, pos.Y, 0, 0);
moveImage.Source = sourceImage.Source;
}
private void ListBoxShortCuts_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
ListBoxEx MoreEx = GetListMoreControl();
if (MoreEx == null) return;
ListBoxEx ShortEx = GetListShortControl();
if (ShortEx == null) return;
MoreEx.AllowDrop = false;
var pos = e.GetPosition(ShortEx);
HitTestResult result = VisualTreeHelper.HitTest(ShortEx, pos);
if (result == null)
{
return;
}
quickSourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent(result.VisualHit);
if (quickSourceListItem == null || quickSourceListItem.Content != ShortEx.SelectedItem)
{
return;
}
System.Windows.DataObject dataObj = new System.Windows.DataObject(quickSourceListItem.Content as ToolItem);
if (dataObj != null)
{
isMoreCuts = false;
DragDrop.DoDragDrop(ShortEx, dataObj, System.Windows.DragDropEffects.Move);
}
}
else
{
sourceImage.Visibility = Visibility.Hidden;
//moveImage.Visibility = Visibility.Hidden;
}
}
private void sourceImage_Drop(object sender, DragEventArgs e)
{
ListBoxShortCuts_Drop(sender, e);
}
private void sourceImage_PreviewMouseMove(object sender, MouseEventArgs e)
{
ListBoxMoreCuts_PreviewMouseMove(sender, e);
}
private void sourceImage_DragOver(object sender, DragEventArgs e)
{
ListBoxShortCuts_DragOver(sender, e);
}
private void Label_PreviewMouseMove(object sender, MouseEventArgs e)
{
ListBoxMoreCuts_PreviewMouseMove(sender, e);
}
private void ListBoxMoreCuts_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//获取当前控件元素相对鼠标点击的位置(只有点击在当前A控件,会触发事件并返回相对A控件的鼠标位置)。
Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
sourceImage.Visibility = Visibility.Hidden;
if (pp.X < 0 || pp.Y < 0)
{
moveImage.Visibility = Visibility.Hidden;
}
}
private void ListBoxShortCuts_SizeChanged(object sender, SizeChangedEventArgs e)
{
ListBoxEx ShortEx = GetListShortControl();
if (ShortEx == null) return;
ListBoxCutsWidthChanged(ShortEx);
}
private void ListBoxMoreCuts_SizeChanged(object sender, SizeChangedEventArgs e)
{
ListBoxEx MoreEx = GetListMoreControl();
if (MoreEx == null) return;
ListBoxCutsWidthChanged(MoreEx);
}
private void ListBoxCutsWidthChanged(ListBoxEx list)
{
if (list == null) return;
//3x + 4y = containerWidth;
//3x = y;
//x为间距,y为item;
double containerWidth = list.ActualWidth - 20;
int widthItem = (int)containerWidth / 5;
double margin = widthItem / 3;
int i = 0;
foreach (var item in list.Items)
{
i++;
var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
if (listBoxItem != null)
{
if (i % 4 != 0)
{
listBoxItem.Margin = new Thickness(0, 0, margin, 0);
}
else
{
listBoxItem.Margin = new Thickness(0, 0, 0, 0);
}
listBoxItem.Width = widthItem;
}
}
}
private void BtnTools_Click(object sender, RoutedEventArgs e)
{
}
private void BtnExptend_Click(object sender, RoutedEventArgs e)
{
UpdateExptend();
}
private void UpdateExptend()
{
//foreach (var item in allTools)
//{
// item.IsShowConciseContent = !(bool)BtnExptend.IsChecked;
//}
//foreach (var item in quickTools)
//{
// item.IsShowConciseContent = !(bool)BtnExptend.IsChecked;
//}
}
//自适应流式布局,以防改需求备用
//private void ListBoxMoreCutsWidthChanged()
//{
// double containerWidth = ListBoxShortCuts.ActualWidth;
// int Rowcount = (int)containerWidth / 240;
// double EndRow = containerWidth % 240.0;
// Rowcount = EndRow >= 140 ? ++Rowcount : Rowcount;
// int i = 0;
// foreach (var item in ListBoxMoreCuts.Items)
// {
// i++;
// var listBoxItem = ListBoxMoreCuts.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
// if (listBoxItem != null)
// {
// if (i % Rowcount != 0)
// {
// listBoxItem.Margin = new Thickness(0, 0, 100, 0);
// }
// else
// {
// listBoxItem.Margin = new Thickness(0, 0, 0, 0);
// }
// }
// }
//}
/////
///// 扩展收缩UI
/////
///// 是否收缩
//private void ShowToolsUI(ListBoxEx list,bool IsShowConciseContent)
//{
// foreach (var item in list.Items)
// {
// var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
// if (listBoxItem != null)
// {
// var viewItem = listBoxItem.ContentTemplate;
// var myContentPresenter = CommonHelper.FindVisualChild(listBoxItem);
// var obj = viewItem.FindName("data", myContentPresenter);
// var checkNum = obj as PDFToolItem;
// if (checkNum != null)
// {
// checkNum.IsShowConciseContent = IsShowConciseContent;
// }
// }
// }
//}
}
}