using ComPDFKit.PDFDocument;
using PDF_Office.Helper;
using PDF_Office.ViewModels.BOTA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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;
using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
namespace PDF_Office.Views.BOTA
{
///
/// BookmarkContent.xaml 的交互逻辑
///
public partial class BookmarkContent : UserControl
{
private BookmarkContentViewModel viewModel = null;
///
/// 上一个ListBoxItem,为选中状态做准备
///
private ListBoxItem histotyListBoxItem = null;
private bool isAdd = false;
public BookmarkContent()
{
InitializeComponent();
viewModel = this.DataContext as BookmarkContentViewModel;
KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
KeyEventsHelper.KeyDown += ShortCut_KeyDown;
}
private void ShortCut_KeyDown(object sender, KeyEventArgs e)
{
if (KeyEventsHelper.IsSingleKey(Key.Escape))
{
if (BookMarkListView.SelectedItems == null)
{
return;
}
BookMarkListView.SelectedItems.Clear();
}
}
///
///ListViewItem,鼠标左键点击
///
///
///
private void ListViewItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
{
object[] objects = new object[] { sender, e };
TextBlock textBlock = null;
ListViewItem listBoxItem = (sender as ListViewItem);
if (e.LeftButton == MouseButtonState.Pressed)
{
if (e.ClickCount >= 2)
{
histotyListBoxItem = listBoxItem;
if (e.OriginalSource is TextBlock)
{
textBlock = (TextBlock)e.OriginalSource;
if (textBlock != null)
{
TextBox textBox = CommonHelper.FindVisualChild(listBoxItem);
SetSelectedStatus(listBoxItem, textBox, textBlock);
}
}
}
else if (e.ClickCount == 1)
{
if (histotyListBoxItem != listBoxItem)
{
if (histotyListBoxItem != null)
{
histotyListBoxItem.IsSelected = false;
textBlock = CommonHelper.FindVisualChild(histotyListBoxItem);
textBlock.Visibility = Visibility.Visible;
textBlock.Focusable = true;
}
}
viewModel.ListViewItemMouseDownCommand.Execute(sender);
}
}
else if (e.RightButton == MouseButtonState.Pressed)
{
ContextMenu contextMenu = listBoxItem.ContextMenu;
if (contextMenu.Items.Count == 3)
{
MenuItem rename = contextMenu.Items[0] as MenuItem;
MenuItem editPageIndex = contextMenu.Items[1] as MenuItem;
MenuItem del = contextMenu.Items[2] as MenuItem;
if (isSelects)
{
rename.IsEnabled = false;
editPageIndex.IsEnabled = false;
isSelects = false;
}
else
{
CPDFBookmark bookmark = (CPDFBookmark)listBoxItem.DataContext;
if (viewModel.PDFViewer.CurrentIndex == bookmark.PageIndex)
{
editPageIndex.IsEnabled = false;
return;
}
rename.IsEnabled = true;
editPageIndex.IsEnabled = true;
editPageIndex.CommandParameter = listBoxItem.DataContext;
editPageIndex.Command = viewModel.EditPageIndexCommand;
}
}
}
}
private bool isSelects = false;
private void BookMarkListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var a1 = (sender as ListView).SelectedItems.Count;
if (a1 > 1)
{
isSelects = true;
}
}
///
/// ListViewItem双击时选中状态
///
///
///
///
private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
{
listBoxItem.IsSelected = true;
listBoxItem.Focus();
textBlock.Visibility = Visibility.Collapsed;
textBox.Dispatcher.BeginInvoke(new Action(() =>
{
textBox.Focus();
textBox.SelectAll();
//listBoxItem.IsSelected = true;
}));
}
///
/// ListViewItem失去焦点
///
///
///
private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
{
ListViewItem listItem = sender as ListViewItem;
if (listItem != null)
{
BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
viewModel.LostFocusCommand.Execute(listItem);
TextBlock textBlock = CommonHelper.FindVisualChild(listItem);
TextBox text = CommonHelper.FindVisualChild(listItem);
if (textBlock.Visibility != Visibility.Visible)
{
if (isAdd == false && isRename == false)
{
listItem.IsSelected = false;
}
else
{
listItem.IsSelected = true;
isAdd = false;
}
if (text.IsSelectionActive == true)
{
textBlock.Visibility = Visibility.Visible;
}
}
else
{
listItem.IsSelected = true;
}
//if (histotyListBoxItem != null)
//{
// histotyListBoxItem.IsSelected = false;
// TextBlock textBlock = CommonHelper.FindVisualChild(histotyListBoxItem);
// textBlock.Visibility = Visibility.Visible;
// textBlock.Focusable = true;
//}
//if (textBlock.Visibility == Visibility.Collapsed)
//{
// listItem.IsSelected = false;
// //textBlock.Visibility = Visibility.Visible;
//}
//else
//{
// listItem.IsSelected = true;
// //textBlock.Visibility = Visibility.Collapsed;
//}
}
}
private bool isRename = false;
///
/// 右键菜单-重命名
///
///
///
private void MenuItemRename_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem)
{
MenuItem menuItem = (MenuItem)sender;
CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
if (bookmark != null)
{
BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
isRename = true;
ListViewItem listBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
histotyListBoxItem = listBoxItem;
TextBox textBox = CommonHelper.FindVisualChild(listBoxItem);
TextBlock textBlock = CommonHelper.FindVisualChild(listBoxItem);
SetSelectedStatus(listBoxItem, textBox, textBlock);
}
}
}
///
/// BookMarkListView,鼠标点击
///
///
///
private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var pos = e.GetPosition(BookMarkListView);
var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
if (result != null)
{
//获取当前鼠标指针下的容器
var listBoxItem = CommonHelper.FindVisualParent(result.VisualHit);
if (listBoxItem == null)
{
if (BookMarkListView.SelectedItem != null)
{
ListViewItem item = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
item.IsSelected = false;
TextBlock box = CommonHelper.FindVisualChild(item);
box.Visibility = Visibility.Visible;
BookMarkListView.SelectedItems.Clear();
}
if (histotyListBoxItem != null)
{
var pos1 = e.GetPosition(histotyListBoxItem);
var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
if (result1 == null)
{
histotyListBoxItem.IsSelected = false;
TextBlock textBlock = CommonHelper.FindVisualChild(histotyListBoxItem);
textBlock.Visibility = Visibility.Visible;
textBlock.Focusable = true;
}
}
}
else
{
TextBox textBox = CommonHelper.FindVisualChild(listBoxItem);
TextBlock textBlock = CommonHelper.FindVisualChild(listBoxItem);
if (textBox.IsFocused == false)
{
listBoxItem.Focus();
}
}
}
//BookMarkListView.Focus();
}
}
///
/// 右键菜单-删除
///
///
///
private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
{
List pagelist = new List();
for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
{
CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
pagelist.Add(BookMarkListView.Items.IndexOf(item));
}
pagelist.Sort();
for (int i = 0; i < pagelist.Count; i++)
{
CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
if (data != null)
{
viewModel.DeleteCommand.Execute(data);
}
}
}
public void BtnAddBookmark_Click(object sender, RoutedEventArgs e)
{
isAdd = true;
viewModel.AddBookmarkCommand.Execute(BookMarkListView);
if (viewModel.IsHasBookmark == false)
{
BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
if (myListBoxItem == null)
{
BookMarkListView.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
BookMarkListView.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
}
else
{
TextBox textBox = CommonHelper.FindVisualChild(myListBoxItem);
TextBlock textBlock = CommonHelper.FindVisualChild(myListBoxItem);
SetSelectedStatus(myListBoxItem, textBox, textBlock);
}
}
}
private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
ItemContainerGenerator itemContainer = sender as ItemContainerGenerator;
if (itemContainer.Status == GeneratorStatus.ContainersGenerated)
{
BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
ListViewItem myListBoxItem = (ListViewItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(viewModel.AddCPDFBookmark));
if (myListBoxItem != null)
{
if (myListBoxItem.RenderSize.Width < 0 && myListBoxItem.RenderSize.Height < 0)
{
BookMarkListView.UpdateLayout();
myListBoxItem.UpdateLayout();
BookMarkListView.ScrollIntoView(viewModel.AddCPDFBookmark);
}
TextBox textBox = CommonHelper.FindVisualChild(myListBoxItem);
TextBlock textBlock = CommonHelper.FindVisualChild(myListBoxItem);
SetSelectedStatus(myListBoxItem, textBox, textBlock);
}
}
}
private void BookMarkListView_KeyDown(object sender, KeyEventArgs e)
{
if (BookMarkListView.SelectedItems == null)
{
return;
}
if (e.Key == Key.Escape)
{
BookMarkListView.SelectedItems.Clear();
}
//if (e.Key == Key.Delete)
//{
// if (BookMarkListView.SelectedItems.Count > 0)
// {
// //全选删除
// if (BookMarkListView.SelectedItems.Count == BookMarkListView.Items.Count)
// {
// }
// }
//}
}
}
}