210 lines
8.7 KiB
C#
210 lines
8.7 KiB
C#
using Ink_Canvas.Helpers;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Ink;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace Ink_Canvas
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
private class PageListViewItem
|
|
{
|
|
public int Index { get; set; }
|
|
public StrokeCollection Strokes { get; set; }
|
|
}
|
|
|
|
ObservableCollection<PageListViewItem> blackBoardSidePageListViewObservableCollection = new ObservableCollection<PageListViewItem>();
|
|
|
|
/// <summary>
|
|
/// <para>刷新白板的缩略图页面列表。</para>
|
|
/// </summary>
|
|
private void RefreshBlackBoardSidePageListView()
|
|
{
|
|
if (blackBoardSidePageListViewObservableCollection.Count == WhiteboardTotalCount)
|
|
{
|
|
foreach (int index in Enumerable.Range(1, WhiteboardTotalCount))
|
|
{
|
|
var st = ApplyHistoriesToNewStrokeCollection(TimeMachineHistories[index]);
|
|
st.Clip(new Rect(0, 0, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight));
|
|
var pitem = new PageListViewItem
|
|
{
|
|
Index = index,
|
|
Strokes = st,
|
|
};
|
|
blackBoardSidePageListViewObservableCollection[index - 1] = pitem;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
blackBoardSidePageListViewObservableCollection.Clear();
|
|
foreach (int index in Enumerable.Range(1, WhiteboardTotalCount))
|
|
{
|
|
var st = ApplyHistoriesToNewStrokeCollection(TimeMachineHistories[index]);
|
|
st.Clip(new Rect(0, 0, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight));
|
|
var pitem = new PageListViewItem
|
|
{
|
|
Index = index,
|
|
Strokes = st,
|
|
};
|
|
blackBoardSidePageListViewObservableCollection.Add(pitem);
|
|
}
|
|
}
|
|
|
|
var _st = inkCanvas.Strokes.Clone();
|
|
_st.Clip(new Rect(0, 0, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight));
|
|
var _pitem = new PageListViewItem
|
|
{
|
|
Index = CurrentWhiteboardIndex,
|
|
Strokes = _st,
|
|
};
|
|
blackBoardSidePageListViewObservableCollection[CurrentWhiteboardIndex - 1] = _pitem;
|
|
|
|
BlackBoardLeftSidePageListView.SelectedIndex = CurrentWhiteboardIndex - 1;
|
|
BlackBoardRightSidePageListView.SelectedIndex = CurrentWhiteboardIndex - 1;
|
|
}
|
|
|
|
private void TrySwitchWhiteboardPageByTouchPoint(ListView listView, ScrollViewer scrollViewer, Point pointInScrollViewer)
|
|
{
|
|
if (listView == null || scrollViewer == null) return;
|
|
try
|
|
{
|
|
var transform = scrollViewer.TransformToVisual(listView);
|
|
if (transform == null) return;
|
|
var pointInListView = transform.Transform(pointInScrollViewer);
|
|
var hit = VisualTreeHelper.HitTest(listView, pointInListView);
|
|
if (hit?.VisualHit == null) return;
|
|
var container = FindAncestorOfType<ListViewItem>(hit.VisualHit);
|
|
if (container == null) return;
|
|
int index = listView.ItemContainerGenerator.IndexFromContainer(container);
|
|
if (index < 0 || index >= blackBoardSidePageListViewObservableCollection.Count) return;
|
|
var item = blackBoardSidePageListViewObservableCollection[index];
|
|
if (item == null) return;
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
|
|
if (index + 1 != CurrentWhiteboardIndex)
|
|
{
|
|
if (currentSelectedElement != null)
|
|
{
|
|
var previousEditingMode = inkCanvas.EditingMode;
|
|
UnselectElement(currentSelectedElement);
|
|
inkCanvas.EditingMode = previousEditingMode;
|
|
currentSelectedElement = null;
|
|
}
|
|
SaveStrokes();
|
|
ClearStrokes(true);
|
|
CurrentWhiteboardIndex = index + 1;
|
|
RestoreStrokes();
|
|
UpdateIndexInfoDisplay();
|
|
}
|
|
BlackBoardLeftSidePageListView.SelectedIndex = index;
|
|
BlackBoardRightSidePageListView.SelectedIndex = index;
|
|
}
|
|
catch
|
|
{
|
|
// 忽略命中测试或切换过程中的异常
|
|
}
|
|
}
|
|
|
|
private static T FindAncestorOfType<T>(DependencyObject current) where T : DependencyObject
|
|
{
|
|
while (current != null)
|
|
{
|
|
if (current is T found) return found;
|
|
current = VisualTreeHelper.GetParent(current);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static void ScrollViewToVerticalTop(FrameworkElement element, ScrollViewer scrollViewer)
|
|
{
|
|
if (element == null || scrollViewer == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var scrollViewerOffset = scrollViewer.VerticalOffset;
|
|
var point = new Point(0, scrollViewerOffset);
|
|
var transform = element.TransformToVisual(scrollViewer);
|
|
if (transform == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var tarPos = transform.Transform(point);
|
|
scrollViewer.ScrollToVerticalOffset(tarPos.Y);
|
|
}
|
|
|
|
|
|
private void BlackBoardLeftSidePageListView_OnMouseUp(object sender, MouseButtonEventArgs e)
|
|
{
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
|
|
var item = BlackBoardLeftSidePageListView.SelectedItem;
|
|
var index = BlackBoardLeftSidePageListView.SelectedIndex;
|
|
if (item != null)
|
|
{
|
|
// 只有当选择的页面与当前页面不同时才进行切换
|
|
if (index + 1 != CurrentWhiteboardIndex)
|
|
{
|
|
// 隐藏图片选择工具栏
|
|
if (currentSelectedElement != null)
|
|
{
|
|
// 保存当前编辑模式
|
|
var previousEditingMode = inkCanvas.EditingMode;
|
|
UnselectElement(currentSelectedElement);
|
|
// 恢复编辑模式
|
|
inkCanvas.EditingMode = previousEditingMode;
|
|
currentSelectedElement = null;
|
|
}
|
|
|
|
SaveStrokes();
|
|
ClearStrokes(true);
|
|
CurrentWhiteboardIndex = index + 1;
|
|
RestoreStrokes();
|
|
UpdateIndexInfoDisplay();
|
|
}
|
|
// 无论是否切换页面,都更新选择索引
|
|
BlackBoardLeftSidePageListView.SelectedIndex = index;
|
|
}
|
|
}
|
|
|
|
private void BlackBoardRightSidePageListView_OnMouseUp(object sender, MouseButtonEventArgs e)
|
|
{
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
|
|
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
|
|
var item = BlackBoardRightSidePageListView.SelectedItem;
|
|
var index = BlackBoardRightSidePageListView.SelectedIndex;
|
|
if (item != null)
|
|
{
|
|
// 只有当选择的页面与当前页面不同时才进行切换
|
|
if (index + 1 != CurrentWhiteboardIndex)
|
|
{
|
|
// 隐藏图片选择工具栏
|
|
if (currentSelectedElement != null)
|
|
{
|
|
// 保存当前编辑模式
|
|
var previousEditingMode = inkCanvas.EditingMode;
|
|
UnselectElement(currentSelectedElement);
|
|
// 恢复编辑模式
|
|
inkCanvas.EditingMode = previousEditingMode;
|
|
currentSelectedElement = null;
|
|
}
|
|
|
|
SaveStrokes();
|
|
ClearStrokes(true);
|
|
CurrentWhiteboardIndex = index + 1;
|
|
RestoreStrokes();
|
|
UpdateIndexInfoDisplay();
|
|
}
|
|
// 无论是否切换页面,都更新选择索引
|
|
BlackBoardRightSidePageListView.SelectedIndex = index;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|