代码清理

This commit is contained in:
PrefacedCorg
2025-08-03 16:46:33 +08:00
parent 745b798d89
commit 11a5a7fdbe
73 changed files with 5733 additions and 3857 deletions
+94 -50
View File
@@ -1,19 +1,17 @@
using Ink_Canvas.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
namespace Ink_Canvas {
public partial class MainWindow : Window {
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
private StrokeCollection[] strokeCollections = new StrokeCollection[101];
private bool[] whiteboadLastModeIsRedo = new bool[101];
private StrokeCollection lastTouchDownStrokeCollection = new StrokeCollection();
@@ -23,17 +21,21 @@ namespace Ink_Canvas {
private TimeMachineHistory[][] TimeMachineHistories = new TimeMachineHistory[101][]; //最多99页,0用来存储非白板时的墨迹以便还原
// 保存每页白板图片信息
private void SaveStrokes(bool isBackupMain = false) {
private void SaveStrokes(bool isBackupMain = false)
{
// 确保画布上的所有UI元素都被保存到时间机器历史记录中
var currentHistory = timeMachine.ExportTimeMachineHistory();
var elementsInHistory = new HashSet<UIElement>();
// 收集已经在历史记录中的元素
if (currentHistory != null) {
foreach (var h in currentHistory) {
if (currentHistory != null)
{
foreach (var h in currentHistory)
{
if (h.CommitType == TimeMachineHistoryType.ElementInsert &&
h.InsertedElement != null &&
!h.StrokeHasBeenCleared) {
!h.StrokeHasBeenCleared)
{
elementsInHistory.Add(h.InsertedElement);
}
}
@@ -41,26 +43,34 @@ namespace Ink_Canvas {
// 检查画布上的所有UI元素,确保它们都在历史记录中
var missingElements = 0;
foreach (UIElement child in inkCanvas.Children) {
if (child is Image || child is MediaElement) {
if (!elementsInHistory.Contains(child)) {
foreach (UIElement child in inkCanvas.Children)
{
if (child is Image || child is MediaElement)
{
if (!elementsInHistory.Contains(child))
{
timeMachine.CommitElementInsertHistory(child);
missingElements++;
}
}
}
// 确保画布上的所有墨迹都被保存
if (inkCanvas.Strokes.Count > 0) {
if (inkCanvas.Strokes.Count > 0)
{
// 检查是否有墨迹没有在时间机器历史记录中
var strokesInHistory = new HashSet<Stroke>();
if (currentHistory != null) {
foreach (var h in currentHistory) {
if (currentHistory != null)
{
foreach (var h in currentHistory)
{
if (h.CommitType == TimeMachineHistoryType.UserInput &&
h.CurrentStroke != null &&
!h.StrokeHasBeenCleared) {
foreach (Stroke stroke in h.CurrentStroke) {
!h.StrokeHasBeenCleared)
{
foreach (Stroke stroke in h.CurrentStroke)
{
strokesInHistory.Add(stroke);
}
}
@@ -69,24 +79,30 @@ namespace Ink_Canvas {
// 收集没有在历史记录中的墨迹
var missingStrokes = new StrokeCollection();
foreach (Stroke stroke in inkCanvas.Strokes) {
if (!strokesInHistory.Contains(stroke)) {
foreach (Stroke stroke in inkCanvas.Strokes)
{
if (!strokesInHistory.Contains(stroke))
{
missingStrokes.Add(stroke);
}
}
if (missingStrokes.Count > 0) {
if (missingStrokes.Count > 0)
{
timeMachine.CommitStrokeUserInputHistory(missingStrokes);
}
}
if (isBackupMain) {
if (isBackupMain)
{
var timeMachineHistory = timeMachine.ExportTimeMachineHistory();
TimeMachineHistories[0] = timeMachineHistory;
timeMachine.ClearStrokeHistory();
} else {
}
else
{
var timeMachineHistory = timeMachine.ExportTimeMachineHistory();
TimeMachineHistories[CurrentWhiteboardIndex] = timeMachineHistory;
timeMachine.ClearStrokeHistory();
@@ -95,7 +111,8 @@ namespace Ink_Canvas {
}
}
private void ClearStrokes(bool isErasedByCode) {
private void ClearStrokes(bool isErasedByCode)
{
_currentCommitType = CommitReason.ClearingCanvas;
if (isErasedByCode) _currentCommitType = CommitReason.CodeInput;
@@ -110,8 +127,10 @@ namespace Ink_Canvas {
}
// 恢复每页白板图片信息
private void RestoreStrokes(bool isBackupMain = false) {
try {
private void RestoreStrokes(bool isBackupMain = false)
{
try
{
var targetIndex = isBackupMain ? 0 : CurrentWhiteboardIndex;
// 先清空当前画布的墨迹
@@ -122,35 +141,46 @@ namespace Ink_Canvas {
inkCanvas.Children.Clear();
// 如果历史记录为空,直接返回(新页面或空页面)
if (TimeMachineHistories[targetIndex] == null) {
if (TimeMachineHistories[targetIndex] == null)
{
timeMachine.ClearStrokeHistory();
return;
}
if (isBackupMain) {
if (isBackupMain)
{
timeMachine.ImportTimeMachineHistory(TimeMachineHistories[0]);
foreach (var item in TimeMachineHistories[0]) ApplyHistoryToCanvas(item);
} else {
}
else
{
timeMachine.ImportTimeMachineHistory(TimeMachineHistories[CurrentWhiteboardIndex]);
// 通过时间机器历史恢复所有内容(墨迹和图片)
foreach (var item in TimeMachineHistories[CurrentWhiteboardIndex]) ApplyHistoryToCanvas(item);
}
// 确保选中状态被清除,因为我们切换了页面
if (selectedUIElement != null) {
if (selectedUIElement != null)
{
DeselectUIElement();
}
}
catch {
catch
{
// ignored
}
}
private async void BtnWhiteBoardPageIndex_Click(object sender, EventArgs e) {
if (sender == BtnLeftPageListWB) {
if (BoardBorderLeftPageListView.Visibility == Visibility.Visible) {
private async void BtnWhiteBoardPageIndex_Click(object sender, EventArgs e)
{
if (sender == BtnLeftPageListWB)
{
if (BoardBorderLeftPageListView.Visibility == Visibility.Visible)
{
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
} else {
}
else
{
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
RefreshBlackBoardSidePageListView();
AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderLeftPageListView);
@@ -159,11 +189,15 @@ namespace Ink_Canvas {
(ListViewItem)BlackBoardLeftSidePageListView.ItemContainerGenerator.ContainerFromIndex(
CurrentWhiteboardIndex - 1), BlackBoardLeftSidePageListScrollViewer);
}
} else if (sender == BtnRightPageListWB)
}
else if (sender == BtnRightPageListWB)
{
if (BoardBorderRightPageListView.Visibility == Visibility.Visible) {
if (BoardBorderRightPageListView.Visibility == Visibility.Visible)
{
AnimationsHelper.HideWithSlideAndFade(BoardBorderRightPageListView);
} else {
}
else
{
AnimationsHelper.HideWithSlideAndFade(BoardBorderLeftPageListView);
RefreshBlackBoardSidePageListView();
AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderRightPageListView);
@@ -176,7 +210,8 @@ namespace Ink_Canvas {
}
private void BtnWhiteBoardSwitchPrevious_Click(object sender, EventArgs e) {
private void BtnWhiteBoardSwitchPrevious_Click(object sender, EventArgs e)
{
if (CurrentWhiteboardIndex <= 1) return;
// 取消任何UI元素的选择
@@ -192,12 +227,14 @@ namespace Ink_Canvas {
UpdateIndexInfoDisplay();
}
private void BtnWhiteBoardSwitchNext_Click(object sender, EventArgs e) {
private void BtnWhiteBoardSwitchNext_Click(object sender, EventArgs e)
{
Trace.WriteLine("113223234");
if (Settings.Automation.IsAutoSaveStrokesAtClear &&
inkCanvas.Strokes.Count > Settings.Automation.MinimumAutomationStrokeNumber) SaveScreenShot(true);
if (CurrentWhiteboardIndex >= WhiteboardTotalCount) {
if (CurrentWhiteboardIndex >= WhiteboardTotalCount)
{
// 在最后一页时,点击“新页面”按钮直接新增一页
BtnWhiteBoardAdd_Click(sender, e);
return;
@@ -216,7 +253,8 @@ namespace Ink_Canvas {
UpdateIndexInfoDisplay();
}
private void BtnWhiteBoardAdd_Click(object sender, EventArgs e) {
private void BtnWhiteBoardAdd_Click(object sender, EventArgs e)
{
if (WhiteboardTotalCount >= 99) return;
if (Settings.Automation.IsAutoSaveStrokesAtClear &&
inkCanvas.Strokes.Count > Settings.Automation.MinimumAutomationStrokeNumber) SaveScreenShot(true);
@@ -244,12 +282,14 @@ namespace Ink_Canvas {
if (WhiteboardTotalCount >= 99) BtnWhiteBoardAdd.IsEnabled = false;
if (BlackBoardLeftSidePageListView.Visibility == Visibility.Visible) {
if (BlackBoardLeftSidePageListView.Visibility == Visibility.Visible)
{
RefreshBlackBoardSidePageListView();
}
}
private void BtnWhiteBoardDelete_Click(object sender, RoutedEventArgs e) {
private void BtnWhiteBoardDelete_Click(object sender, RoutedEventArgs e)
{
ClearStrokes(true);
if (CurrentWhiteboardIndex != WhiteboardTotalCount)
@@ -267,7 +307,8 @@ namespace Ink_Canvas {
if (WhiteboardTotalCount < 99) BtnWhiteBoardAdd.IsEnabled = true;
}
private void UpdateIndexInfoDisplay() {
private void UpdateIndexInfoDisplay()
{
TextBlockWhiteBoardIndexInfo.Text =
$"{CurrentWhiteboardIndex}/{WhiteboardTotalCount}";
@@ -289,13 +330,16 @@ namespace Ink_Canvas {
BtnWhiteBoardSwitchPrevious.IsEnabled = true;
if (CurrentWhiteboardIndex == 1) {
if (CurrentWhiteboardIndex == 1)
{
BtnWhiteBoardSwitchPrevious.IsEnabled = false;
BtnLeftWhiteBoardSwitchPreviousGeometry.Brush = new SolidColorBrush(Color.FromArgb(127, 24, 24, 27));
BtnLeftWhiteBoardSwitchPreviousLabel.Opacity = 0.5;
BtnRightWhiteBoardSwitchPreviousGeometry.Brush = new SolidColorBrush(Color.FromArgb(127, 24, 24, 27));
BtnRightWhiteBoardSwitchPreviousLabel.Opacity = 0.5;
} else {
}
else
{
BtnLeftWhiteBoardSwitchPreviousGeometry.Brush = new SolidColorBrush(Color.FromArgb(255, 24, 24, 27));
BtnLeftWhiteBoardSwitchPreviousLabel.Opacity = 1;
BtnRightWhiteBoardSwitchPreviousGeometry.Brush = new SolidColorBrush(Color.FromArgb(255, 24, 24, 27));