using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Master.CustomControl;
using PDF_Master.Helper;
using PDF_Master.Properties;
using PDF_Master.ViewModels.HomePanel.RecentFiles;
using PDFSettings;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Navigation;
using winform = System.Windows.Forms;
namespace PDF_Master.Views.HomePanel.RecentFiles
{
public partial class RecentFilesContent : UserControl
{
private RecentFilesContentViewModel ViewModel => DataContext as RecentFilesContentViewModel;
public RecentFilesContent()
{
InitializeComponent();
}
#region 初始化
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
InitVariables();
InitEvents();
}
private void InitVariables()
{
if (RecentFilesList.SelectedItems != null)
RecentFilesList.SelectedItems.Clear();
if (GridRecentFilesList.SelectedItems != null)
GridRecentFilesList.SelectedItems.Clear();
}
private void InitEvents()
{
ViewModel.RecentFilesSelectionHandler -= ViewModel_RecentFilesSelection;
ViewModel.RecentFilesSelectionHandler += ViewModel_RecentFilesSelection;
}
#endregion
#region UI操作
private void ViewModel_RecentFilesSelection(object sender, bool e)
{
//if (e)
//{
// RecentFilesList.SelectedItems.Clear();
// if (GridRecentFilesList.SelectedItems.Count > 0)
// {
// for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
// {
// RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
// }
// }
//}
//else
//{
// GridRecentFilesList.SelectedItems.Clear();
// if (RecentFilesList.SelectedItems.Count > 0)
// {
// for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
// {
// GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
// }
// }
//}
}
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
try
{
ListView listView;
if (BtnListMode.IsChecked == true)
listView = RecentFilesList;
else
listView = GridRecentFilesList;
var contexMenu = sender as ContextMenu;
if (contexMenu != null)
{
foreach (var item in contexMenu.Items)
{
var menuitem = item as MenuItem;
menuitem.Visibility = Visibility.Visible;
}
}
//临时绑定文案
(contexMenu.Items[0] as MenuItem).Header = ViewModel.T_MenuShow;
(contexMenu.Items[1] as MenuItem).Header = ViewModel.T_MenuOpenFile;
(contexMenu.Items[2] as MenuItem).Header = ViewModel.T_MenuRemove;
if (listView.SelectedItems.Count >= 2)
{
foreach (var item in contexMenu.Items)
{
var menuitem = item as MenuItem;
if (menuitem.Name == "FilePathMenuItem")
menuitem.Visibility = Visibility.Collapsed;
if(menuitem.Name == "MenuOpenFile")
{
menuitem.Visibility = Visibility.Visible;
}
}
}
else
{
foreach (var item in contexMenu.Items)
{
var menuitem = item as MenuItem;
if (menuitem.Name == "MenuOpenFile")
{
menuitem.Visibility = Visibility.Collapsed;
}
}
}
}
catch { }
}
///
/// 禁用触发鼠标右键,列表项被选中
///
private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
Trace.WriteLine("Preview MouseRightButtonDown");
e.Handled = true;
}
#endregion
#region Events
///
/// 右键菜单:删除文件记录
///
private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
{
ListView listView;
if (BtnListMode.IsChecked == true)
listView = RecentFilesList;
else
listView = GridRecentFilesList;
if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
{
var item = (sender as MenuItem).DataContext as OpenFileInfo;
if (item != null)
{
ViewModel?.RemoveFileItemCommand.Execute(item);
}
}
else
{
ViewModel?.RemoveFilesFromContainerCommand.Execute(listView.SelectedItems);
}
}
///
/// 右键菜单:打开文件路径
///
///
///
private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
{
if ((sender as MenuItem) != null)
{
var menuItem = sender as MenuItem;
var file = menuItem.DataContext as OpenFileInfo;
ViewModel?.ExplorerFileCommand.Execute(file);
}
}
///
/// 双击打开最近列表
///
private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && (item.DataContext as OpenFileInfo) != null)
{
var fileInfo = item.DataContext as OpenFileInfo;
if(fileInfo != null)
{
ViewModel?.OpenRecentFilesCommand.Execute(fileInfo);
}
}
}
///
/// 文件列表项:打开文件
///
private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
{
var item = (sender as MenuItem).DataContext as OpenFileInfo;
if (item != null)
{
ViewModel?.OpenRecentFilesCommand.Execute(item);
}
}
///
/// 文件列表项:删除文件记录
///
private void DocItemListViewControl_DeleteItemHandler(object sender, RoutedEventArgs e)
{
var item = (sender as MenuItem).DataContext as OpenFileInfo;
if (item != null)
{
ViewModel?.RemoveFileItemCommand.Execute(item);
}
}
#endregion
private void UserControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
//Windows:最近文件列表补充点击空白处取消选中;移除Esc取消选中逻辑
//点击ESC取消选中
//if (GridRecentFilesList.Visibility == Visibility.Visible)
//{
// GridRecentFilesList.SelectedIndex = -1;
//}
//else
//{
// RecentFilesList.SelectedIndex = -1;
//}
}
}
private void MenuOpenFile_Click(object sender, RoutedEventArgs e)
{
ListView listView;
if (BtnListMode.IsChecked == true)
listView = RecentFilesList;
else
listView = GridRecentFilesList;
if (listView.SelectedItems.Count >=2)
{
List files = new List();
foreach (OpenFileInfo item in listView.SelectedItems)
{
var fileInfo = item;
if (!File.Exists(fileInfo.FilePath))
{
SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
}
else
{
files.Add(fileInfo.FilePath);
}
}
ViewModel?.LoadPdfViewer(files.ToArray());
}
}
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
//已经加载后,切换页签时,确认是否要刷新数据源
if(IsLoaded&&(bool)e.NewValue)
{
ViewModel.UpdateItemSoucre();
}
}
}
}