improve:PPT墨迹管理

This commit is contained in:
2026-02-20 12:29:40 +08:00
parent edac024ad9
commit d038beb07a
2 changed files with 164 additions and 356 deletions
+142 -319
View File
@@ -9,7 +9,7 @@ using System.Windows.Ink;
namespace Ink_Canvas.Helpers namespace Ink_Canvas.Helpers
{ {
/// <summary> /// <summary>
/// PPT墨迹管理器 - 负责PPT中墨迹的保存、加载和同步 /// PPT墨迹管理器 - 负责按幻灯片保存/加载墨迹、自动保存与内存管理。
/// </summary> /// </summary>
public class PPTInkManager : IDisposable public class PPTInkManager : IDisposable
{ {
@@ -21,7 +21,8 @@ namespace Ink_Canvas.Helpers
#region Private Fields #region Private Fields
private MemoryStream[] _memoryStreams; private MemoryStream[] _memoryStreams;
private int _maxSlides = 100; private const int DefaultMaxSlides = 100;
private int _maxSlides = DefaultMaxSlides;
private string _currentPresentationId = ""; private string _currentPresentationId = "";
private readonly object _lockObject = new object(); private readonly object _lockObject = new object();
private bool _disposed; private bool _disposed;
@@ -34,28 +35,30 @@ namespace Ink_Canvas.Helpers
// 添加快速切换保护机制 // 添加快速切换保护机制
private DateTime _lastSwitchTime = DateTime.MinValue; private DateTime _lastSwitchTime = DateTime.MinValue;
private int _lastSwitchSlideIndex = -1; private int _lastSwitchSlideIndex = -1;
private const int MinSwitchIntervalMs = 100; // 最小切换间隔100毫秒 private const int MinSwitchIntervalMs = 100;
// 内存管理相关字段 private long _totalMemoryUsage;
private long _totalMemoryUsage = 0; private const long MaxMemoryUsageBytes = 100 * 1024 * 1024; // 100MB
private const long MaxMemoryUsageBytes = 100 * 1024 * 1024; // 100MB限制
private DateTime _lastMemoryCleanup = DateTime.MinValue; private DateTime _lastMemoryCleanup = DateTime.MinValue;
private const int MemoryCleanupIntervalMinutes = 5; // 5分钟清理一次 private const int MemoryCleanupIntervalMinutes = 5;
private const string StrokeFileExtension = ".icstk";
#endregion #endregion
#region Constructor #region Constructor
public PPTInkManager() public PPTInkManager()
{ {
InitializeMemoryStreams(); InitializeMemoryStreams(DefaultMaxSlides + 2);
} }
private void InitializeMemoryStreams() private void InitializeMemoryStreams(int capacity)
{ {
_memoryStreams = new MemoryStream[_maxSlides + 2]; _memoryStreams = new MemoryStream[Math.Max(2, capacity)];
} }
#endregion #endregion
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// 初始化新的演示文稿 /// 初始化新的演示文稿
/// </summary> /// </summary>
@@ -67,17 +70,14 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
// 完全清理之前的墨迹状态 ClearAllStrokesInternal();
ClearAllStrokes();
// 重置墨迹锁定状态
_inkLockUntil = DateTime.MinValue; _inkLockUntil = DateTime.MinValue;
_lockedSlideIndex = -1; _lockedSlideIndex = -1;
_lastSwitchSlideIndex = -1;
_lastSwitchTime = DateTime.MinValue;
// 生成演示文稿唯一标识符
_currentPresentationId = GeneratePresentationId(presentation); _currentPresentationId = GeneratePresentationId(presentation);
// 重新初始化内存流数组
int slideCount = 0; int slideCount = 0;
try try
{ {
@@ -85,21 +85,17 @@ namespace Ink_Canvas.Helpers
} }
catch (COMException comEx) catch (COMException comEx)
{ {
var hr = (uint)comEx.HResult; uint hr = (uint)comEx.HResult;
if (hr == 0x80048010) if (hr == 0x80048010) return;
{
return;
}
throw; throw;
} }
_memoryStreams = new MemoryStream[slideCount + 2];
// 如果启用自动保存,尝试加载已保存的墨迹 int capacity = slideCount + 2;
_maxSlides = Math.Max(_maxSlides, slideCount);
_memoryStreams = new MemoryStream[capacity];
if (IsAutoSaveEnabled && !string.IsNullOrEmpty(AutoSaveLocation)) if (IsAutoSaveEnabled && !string.IsNullOrEmpty(AutoSaveLocation))
{
LoadSavedStrokes(); LoadSavedStrokes();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -119,40 +115,11 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
// 检查墨迹锁定 if (!CanWriteInk(slideIndex)) return;
if (!CanWriteInk(slideIndex)) if (slideIndex >= _memoryStreams.Length) return;
{
if (DateTime.Now < _inkLockUntil)
{
}
return;
}
if (slideIndex < _memoryStreams.Length) ReplaceSlideStream(slideIndex, strokes);
{ CheckAndPerformMemoryCleanup();
// 先释放旧的内存流,防止内存泄漏
try
{
_memoryStreams[slideIndex]?.Dispose();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放旧内存流失败: {ex}", LogHelper.LogType.Warning);
}
// 创建新的内存流
var ms = new MemoryStream();
strokes.Save(ms);
ms.Position = 0;
_memoryStreams[slideIndex] = ms;
if (ms.Length > 0)
{
}
// 检查内存使用情况
CheckAndPerformMemoryCleanup();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -162,7 +129,7 @@ namespace Ink_Canvas.Helpers
} }
/// <summary> /// <summary>
/// 强制保存指定页面的墨迹 /// 强制保存指定页墨迹到内存(不受锁定限制)。用于放映结束前保存当前画布到当前页。
/// </summary> /// </summary>
public void ForceSaveSlideStrokes(int slideIndex, StrokeCollection strokes) public void ForceSaveSlideStrokes(int slideIndex, StrokeCollection strokes)
{ {
@@ -172,26 +139,9 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
if (slideIndex < _memoryStreams.Length) if (slideIndex >= _memoryStreams.Length) return;
{ ReplaceSlideStream(slideIndex, strokes);
// 先释放旧的内存流,防止内存泄漏 LogHelper.WriteLogToFile($"已强制保存第{slideIndex}页墨迹", LogHelper.LogType.Trace);
try
{
_memoryStreams[slideIndex]?.Dispose();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放旧内存流失败: {ex}", LogHelper.LogType.Warning);
}
// 创建新的内存流
var ms = new MemoryStream();
strokes.Save(ms);
ms.Position = 0;
_memoryStreams[slideIndex] = ms;
LogHelper.WriteLogToFile($"已强制保存第{slideIndex}页墨迹,大小: {ms.Length} bytes", LogHelper.LogType.Trace);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -214,8 +164,7 @@ namespace Ink_Canvas.Helpers
if (slideIndex < _memoryStreams.Length && _memoryStreams[slideIndex] != null && _memoryStreams[slideIndex].Length > 0) if (slideIndex < _memoryStreams.Length && _memoryStreams[slideIndex] != null && _memoryStreams[slideIndex].Length > 0)
{ {
_memoryStreams[slideIndex].Position = 0; _memoryStreams[slideIndex].Position = 0;
var strokes = new StrokeCollection(_memoryStreams[slideIndex]); return new StrokeCollection(_memoryStreams[slideIndex]);
return strokes;
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -236,30 +185,17 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
// 检查快速切换保护
var now = DateTime.Now; var now = DateTime.Now;
if (now - _lastSwitchTime < TimeSpan.FromMilliseconds(MinSwitchIntervalMs) && if (now - _lastSwitchTime < TimeSpan.FromMilliseconds(MinSwitchIntervalMs) && _lastSwitchSlideIndex == slideIndex)
_lastSwitchSlideIndex == slideIndex)
{ {
LogHelper.WriteLogToFile($"快速切换保护:忽略重复的页面切换请求 {slideIndex}", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"快速切换保护:忽略重复请求 {slideIndex}", LogHelper.LogType.Trace);
return LoadSlideStrokes(slideIndex); return LoadSlideStrokes(slideIndex);
} }
// 设置墨迹锁定
LockInkForSlide(slideIndex); LockInkForSlide(slideIndex);
StrokeCollection newStrokes = LoadSlideStrokes(slideIndex);
// 加载新页面的墨迹
var newStrokes = LoadSlideStrokes(slideIndex);
// 更新切换记录
_lastSwitchTime = now; _lastSwitchTime = now;
_lastSwitchSlideIndex = slideIndex; _lastSwitchSlideIndex = slideIndex;
if (newStrokes.Count > 0)
{
}
return newStrokes; return newStrokes;
} }
catch (Exception ex) catch (Exception ex)
@@ -283,85 +219,51 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
var folderPath = GetPresentationFolderPath(); string folderPath = GetPresentationFolderPath();
if (!Directory.Exists(folderPath)) if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath); Directory.CreateDirectory(folderPath);
}
// 保存位置信息 int positionToSave = currentSlideIndex > 0 ? currentSlideIndex : (_lockedSlideIndex > 0 ? _lockedSlideIndex : _lastSwitchSlideIndex);
try if (positionToSave > 0)
{ {
// 优先使用传入的当前页码,否则使用锁定的页码 try { File.WriteAllText(Path.Combine(folderPath, "Position"), positionToSave.ToString()); }
int positionToSave = currentSlideIndex > 0 ? currentSlideIndex : _lockedSlideIndex; catch (Exception ex) { LogHelper.WriteLogToFile($"保存 Position 失败: {ex}", LogHelper.LogType.Warning); }
// 如果都没有有效值,尝试使用最后切换的页码
if (positionToSave <= 0 && _lastSwitchSlideIndex > 0)
{
positionToSave = _lastSwitchSlideIndex;
}
if (positionToSave > 0)
{
File.WriteAllText(Path.Combine(folderPath, "Position"), positionToSave.ToString());
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存位置信息失败: {ex}", LogHelper.LogType.Error);
} }
// 保存所有页面的墨迹
int savedCount = 0;
int slideCount = 0; int slideCount = 0;
try { slideCount = presentation.Slides.Count; }
try
{
slideCount = presentation.Slides.Count;
}
catch (COMException comEx) catch (COMException comEx)
{ {
var hr = (uint)comEx.HResult; if ((uint)comEx.HResult == 0x80048010) return;
if (hr == 0x80048010)
{
return;
}
throw; throw;
} }
for (int i = 1; i <= slideCount && i < _memoryStreams.Length; i++) for (int i = 1; i <= slideCount && i < _memoryStreams.Length; i++)
{ {
if (_memoryStreams[i] != null) if (_memoryStreams[i] == null) continue;
try
{ {
try if (_memoryStreams[i].Length > 8)
{ {
if (_memoryStreams[i].Length > 8) _memoryStreams[i].Position = 0;
byte[] buf = new byte[_memoryStreams[i].Length];
int read = _memoryStreams[i].Read(buf, 0, buf.Length);
if (read > 0)
{ {
var srcBuf = new byte[_memoryStreams[i].Length]; string basePath = Path.Combine(folderPath, i.ToString("0000"));
_memoryStreams[i].Position = 0; File.WriteAllBytes(basePath + StrokeFileExtension, buf);
var byteLength = _memoryStreams[i].Read(srcBuf, 0, srcBuf.Length);
var filePath = Path.Combine(folderPath, i.ToString("0000") + ".icstk");
File.WriteAllBytes(filePath, srcBuf);
savedCount++;
}
else
{
// 删除空的墨迹文件
var filePath = Path.Combine(folderPath, i.ToString("0000") + ".icstk");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
} }
} }
catch (Exception ex) else
{ {
LogHelper.WriteLogToFile($"保存第{i}页墨迹失败: {ex}", LogHelper.LogType.Error); TryDeleteStrokeFile(folderPath, i);
} }
} }
catch (Exception ex)
{
LogHelper.WriteLogToFile($"保存第{i}页墨迹到文件失败: {ex}", LogHelper.LogType.Error);
}
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -381,33 +283,35 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
var folderPath = GetPresentationFolderPath(); string folderPath = GetPresentationFolderPath();
if (!Directory.Exists(folderPath)) return; if (!Directory.Exists(folderPath)) return;
var files = new DirectoryInfo(folderPath).GetFiles("*.icstk"); var dir = new DirectoryInfo(folderPath);
int loadedCount = 0; int loadedCount = 0;
foreach (FileInfo file in dir.GetFiles("*" + StrokeFileExtension))
foreach (var file in files)
{ {
string nameWithoutExt = Path.GetFileNameWithoutExtension(file.Name);
if (!int.TryParse(nameWithoutExt, out int slideIndex) || slideIndex <= 0) continue;
if (slideIndex >= _memoryStreams.Length) continue;
try try
{ {
if (int.TryParse(Path.GetFileNameWithoutExtension(file.Name), out int slideIndex)) byte[] bytes = File.ReadAllBytes(file.FullName);
if (bytes.Length > 8)
{ {
if (slideIndex > 0 && slideIndex < _memoryStreams.Length) _memoryStreams[slideIndex] = new MemoryStream(bytes);
{ _memoryStreams[slideIndex].Position = 0;
var fileBytes = File.ReadAllBytes(file.FullName); loadedCount++;
_memoryStreams[slideIndex] = new MemoryStream(fileBytes);
_memoryStreams[slideIndex].Position = 0;
loadedCount++;
}
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.WriteLogToFile($"加载墨迹文件{file.Name}失败: {ex}", LogHelper.LogType.Error); LogHelper.WriteLogToFile($"加载墨迹文件 {file.Name} 失败: {ex}", LogHelper.LogType.Error);
} }
} }
if (loadedCount > 0)
LogHelper.WriteLogToFile($"已从磁盘加载 {loadedCount} 页墨迹", LogHelper.LogType.Trace);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -423,81 +327,24 @@ namespace Ink_Canvas.Helpers
{ {
lock (_lockObject) lock (_lockObject)
{ {
try ClearAllStrokesInternal();
{
// 安全释放所有内存流
if (_memoryStreams != null)
{
for (int i = 0; i < _memoryStreams.Length; i++)
{
try
{
_memoryStreams[i]?.Dispose();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放内存流{i}失败: {ex}", LogHelper.LogType.Warning);
}
finally
{
_memoryStreams[i] = null;
}
}
// 重新初始化数组
_memoryStreams = new MemoryStream[_maxSlides + 2];
}
CurrentStrokes?.Clear();
LogHelper.WriteLogToFile("已清除所有墨迹", LogHelper.LogType.Trace);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"清除墨迹失败: {ex}", LogHelper.LogType.Error);
}
} }
} }
/// <summary>
/// 翻页后锁定墨迹写入
/// </summary>
public void LockInkForSlide(int slideIndex) public void LockInkForSlide(int slideIndex)
{ {
_inkLockUntil = DateTime.Now.AddMilliseconds(InkLockMilliseconds); _inkLockUntil = DateTime.Now.AddMilliseconds(InkLockMilliseconds);
_lockedSlideIndex = slideIndex; _lockedSlideIndex = slideIndex;
} }
/// <summary>
/// 检查是否可以写入墨迹
/// </summary>
public bool CanWriteInk(int currentSlideIndex) public bool CanWriteInk(int currentSlideIndex)
{ {
// 如果锁定时间已过,允许写入 if (DateTime.Now >= _inkLockUntil) return true;
if (DateTime.Now >= _inkLockUntil) if (currentSlideIndex == _lockedSlideIndex) return true;
{ if (DateTime.Now - (_inkLockUntil.AddMilliseconds(-InkLockMilliseconds)) < TimeSpan.FromMilliseconds(50)) return true;
return true;
}
// 如果当前页面与锁定页面相同,允许写入(用户在当前页面绘制)
if (currentSlideIndex == _lockedSlideIndex)
{
return true;
}
// 如果当前页面不是锁定页面,但锁定时间很短(小于50ms),允许写入
// 这样可以确保旧页面的墨迹能够及时保存
if (DateTime.Now - (_inkLockUntil.AddMilliseconds(-InkLockMilliseconds)) < TimeSpan.FromMilliseconds(50))
{
return true;
}
// 只有在快速切换且页面不同时才锁定
return false; return false;
} }
/// <summary>
/// 重置墨迹锁定状态
/// </summary>
public void ResetLockState() public void ResetLockState()
{ {
lock (_lockObject) lock (_lockObject)
@@ -509,51 +356,65 @@ namespace Ink_Canvas.Helpers
} }
} }
/// <summary> #endregion
/// 检查并执行内存清理
/// </summary> #region Private Helpers
private void ClearAllStrokesInternal()
{
if (_memoryStreams != null)
{
for (int i = 0; i < _memoryStreams.Length; i++)
{
try { _memoryStreams[i]?.Dispose(); } catch (Exception ex) { LogHelper.WriteLogToFile($"释放内存流 {i} 失败: {ex}", LogHelper.LogType.Warning); }
finally { _memoryStreams[i] = null; }
}
_memoryStreams = new MemoryStream[_maxSlides + 2];
}
CurrentStrokes?.Clear();
LogHelper.WriteLogToFile("已清除所有墨迹", LogHelper.LogType.Trace);
}
private void ReplaceSlideStream(int slideIndex, StrokeCollection strokes)
{
try { _memoryStreams[slideIndex]?.Dispose(); } catch (Exception ex) { LogHelper.WriteLogToFile($"释放旧内存流失败: {ex}", LogHelper.LogType.Warning); }
var ms = new MemoryStream();
strokes.Save(ms);
ms.Position = 0;
_memoryStreams[slideIndex] = ms;
}
private void TryDeleteStrokeFile(string folderPath, int slideIndex)
{
try
{
string path = Path.Combine(folderPath, slideIndex.ToString("0000") + StrokeFileExtension);
if (File.Exists(path)) File.Delete(path);
}
catch { }
}
private void CheckAndPerformMemoryCleanup() private void CheckAndPerformMemoryCleanup()
{ {
try try
{ {
var now = DateTime.Now; var now = DateTime.Now;
if (now - _lastMemoryCleanup < TimeSpan.FromMinutes(MemoryCleanupIntervalMinutes)) return;
// 检查是否需要执行内存清理
if (now - _lastMemoryCleanup < TimeSpan.FromMinutes(MemoryCleanupIntervalMinutes))
{
return;
}
// 计算当前内存使用量
long currentMemoryUsage = 0; long currentMemoryUsage = 0;
if (_memoryStreams != null) if (_memoryStreams != null)
{ {
for (int i = 0; i < _memoryStreams.Length; i++) for (int i = 0; i < _memoryStreams.Length; i++)
{ if (_memoryStreams[i] != null) currentMemoryUsage += _memoryStreams[i].Length;
if (_memoryStreams[i] != null)
{
currentMemoryUsage += _memoryStreams[i].Length;
}
}
} }
_totalMemoryUsage = currentMemoryUsage; _totalMemoryUsage = currentMemoryUsage;
// 如果内存使用量超过限制,执行清理
if (currentMemoryUsage > MaxMemoryUsageBytes) if (currentMemoryUsage > MaxMemoryUsageBytes)
{ {
LogHelper.WriteLogToFile($"内存使用量超限 ({currentMemoryUsage / 1024 / 1024}MB)开始清理", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"墨迹内存超限 ({currentMemoryUsage / (1024 * 1024)}MB)执行清理", LogHelper.LogType.Warning);
// 清理非当前页面的墨迹
CleanupInactiveSlideStrokes(); CleanupInactiveSlideStrokes();
_lastMemoryCleanup = now;
LogHelper.WriteLogToFile($"内存清理完成,当前使用量: {_totalMemoryUsage / 1024 / 1024}MB", LogHelper.LogType.Trace);
}
else
{
_lastMemoryCleanup = now;
} }
_lastMemoryCleanup = now;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -561,90 +422,54 @@ namespace Ink_Canvas.Helpers
} }
} }
/// <summary>
/// 清理非活跃页面的墨迹
/// </summary>
private void CleanupInactiveSlideStrokes() private void CleanupInactiveSlideStrokes()
{ {
try if (_memoryStreams == null) return;
int cleaned = 0;
long freed = 0;
for (int i = 0; i < _memoryStreams.Length; i++)
{ {
if (_memoryStreams == null) return; if (i == _lockedSlideIndex || i == _lastSwitchSlideIndex) continue;
if (_memoryStreams[i] != null)
int cleanedCount = 0;
long freedMemory = 0;
for (int i = 0; i < _memoryStreams.Length; i++)
{ {
// 保留当前锁定页面和最近访问的页面 long len = _memoryStreams[i].Length;
if (i == _lockedSlideIndex || i == _lastSwitchSlideIndex) try { _memoryStreams[i].Dispose(); freed += len; cleaned++; } catch { }
{ finally { _memoryStreams[i] = null; }
continue;
}
if (_memoryStreams[i] != null)
{
long memorySize = _memoryStreams[i].Length;
try
{
_memoryStreams[i].Dispose();
freedMemory += memorySize;
cleanedCount++;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"清理页面{i}墨迹失败: {ex}", LogHelper.LogType.Warning);
}
finally
{
_memoryStreams[i] = null;
}
}
}
if (cleanedCount > 0)
{
LogHelper.WriteLogToFile($"已清理{cleanedCount}个页面的墨迹,释放内存: {freedMemory / 1024}KB", LogHelper.LogType.Trace);
} }
} }
catch (Exception ex) if (cleaned > 0)
{ LogHelper.WriteLogToFile($"已清理 {cleaned} 页墨迹,释放 {freed / 1024}KB", LogHelper.LogType.Trace);
LogHelper.WriteLogToFile($"清理非活跃页面墨迹失败: {ex}", LogHelper.LogType.Error);
}
} }
#endregion
#region Private Methods
private string GeneratePresentationId(Presentation presentation) private string GeneratePresentationId(Presentation presentation)
{ {
try try
{ {
var presentationPath = presentation.FullName; string path = presentation.FullName;
var fileHash = GetFileHash(presentationPath); string hash = GetFileHash(path);
return $"{presentation.Name}_{presentation.Slides.Count}_{fileHash}"; return $"{presentation.Name}_{presentation.Slides.Count}_{hash}";
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.WriteLogToFile($"生成演示文稿ID失败: {ex}", LogHelper.LogType.Error); LogHelper.WriteLogToFile($"生成演示文稿 ID 失败: {ex}", LogHelper.LogType.Error);
return $"unknown_{DateTime.Now.Ticks}"; return $"unknown_{DateTime.Now.Ticks}";
} }
} }
private string GetFileHash(string filePath) private static string GetFileHash(string filePath)
{ {
try try
{ {
if (string.IsNullOrEmpty(filePath)) return "unknown"; if (string.IsNullOrEmpty(filePath)) return "unknown";
using (var md5 = MD5.Create()) using (var md5 = MD5.Create())
{ {
byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(filePath)); byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(filePath));
return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 8); return BitConverter.ToString(hash).Replace("-", "").Substring(0, 8);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.WriteLogToFile($"计算文件哈希失败: {ex}", LogHelper.LogType.Error); LogHelper.WriteLogToFile($"计算文件哈希失败: {ex}", LogHelper.LogType.Error);
return "error"; return "error";
} }
} }
@@ -653,20 +478,18 @@ namespace Ink_Canvas.Helpers
{ {
return Path.Combine(AutoSaveLocation, "Auto Saved - Presentations", _currentPresentationId); return Path.Combine(AutoSaveLocation, "Auto Saved - Presentations", _currentPresentationId);
} }
#endregion #endregion
#region Dispose #region Dispose
public void Dispose() public void Dispose()
{ {
if (!_disposed) if (_disposed) return;
{ lock (_lockObject) { ClearAllStrokesInternal(); }
lock (_lockObject) _disposed = true;
{
ClearAllStrokes();
}
_disposed = true;
}
} }
#endregion #endregion
} }
} }
+22 -37
View File
@@ -100,6 +100,7 @@ namespace Ink_Canvas
// 页面切换防抖机制 // 页面切换防抖机制
private DateTime _lastSlideSwitchTime = DateTime.MinValue; private DateTime _lastSlideSwitchTime = DateTime.MinValue;
private int _pendingSlideIndex = -1; private int _pendingSlideIndex = -1;
private int _pendingPreviousSlideIndex = -1;
private System.Timers.Timer _slideSwitchDebounceTimer; private System.Timers.Timer _slideSwitchDebounceTimer;
private const int SlideSwitchDebounceMs = 150; // 防抖延迟150毫秒 private const int SlideSwitchDebounceMs = 150; // 防抖延迟150毫秒
@@ -899,11 +900,10 @@ namespace Ink_Canvas
var activePresentation = wn.Presentation; var activePresentation = wn.Presentation;
var totalSlides = activePresentation.Slides.Count; var totalSlides = activePresentation.Slides.Count;
// 更新当前播放页码 int previousSlide = _currentSlideShowPosition;
_currentSlideShowPosition = currentSlide; _currentSlideShowPosition = currentSlide;
// 使用防抖机制处理页面切换 HandleSlideSwitchWithDebounce(previousSlide, currentSlide, totalSlides);
HandleSlideSwitchWithDebounce(currentSlide, totalSlides);
}); });
} }
@@ -947,7 +947,12 @@ namespace Ink_Canvas
} }
} }
// 保存墨迹和位置信息 // 保存当前画布墨迹到当前页
await Application.Current.Dispatcher.InvokeAsync(() =>
{
if (currentPage > 0 && _singlePPTInkManager != null && inkCanvas?.Strokes != null)
_singlePPTInkManager.ForceSaveSlideStrokes(currentPage, inkCanvas.Strokes);
});
_singlePPTInkManager?.SaveAllStrokesToFile(pres, currentPage); _singlePPTInkManager?.SaveAllStrokesToFile(pres, currentPage);
await Application.Current.Dispatcher.InvokeAsync(() => await Application.Current.Dispatcher.InvokeAsync(() =>
@@ -1289,21 +1294,18 @@ namespace Ink_Canvas
/// <summary> /// <summary>
/// 使用防抖机制处理页面切换 /// 使用防抖机制处理页面切换
/// </summary> /// </summary>
private void HandleSlideSwitchWithDebounce(int currentSlide, int totalSlides) private void HandleSlideSwitchWithDebounce(int previousSlideIndex, int newSlideIndex, int totalSlides)
{ {
try try
{ {
var now = DateTime.Now; var now = DateTime.Now;
// 如果距离上次切换时间太短,使用防抖机制
if (now - _lastSlideSwitchTime < TimeSpan.FromMilliseconds(SlideSwitchDebounceMs)) if (now - _lastSlideSwitchTime < TimeSpan.FromMilliseconds(SlideSwitchDebounceMs))
{ {
_pendingSlideIndex = currentSlide; _pendingSlideIndex = newSlideIndex;
_pendingPreviousSlideIndex = previousSlideIndex;
// 停止之前的定时器
_slideSwitchDebounceTimer?.Stop(); _slideSwitchDebounceTimer?.Stop();
// 创建新的定时器
_slideSwitchDebounceTimer = new System.Timers.Timer(SlideSwitchDebounceMs); _slideSwitchDebounceTimer = new System.Timers.Timer(SlideSwitchDebounceMs);
_slideSwitchDebounceTimer.Elapsed += (sender, e) => _slideSwitchDebounceTimer.Elapsed += (sender, e) =>
{ {
@@ -1311,9 +1313,10 @@ namespace Ink_Canvas
{ {
if (_pendingSlideIndex > 0) if (_pendingSlideIndex > 0)
{ {
SwitchSlideInk(_pendingSlideIndex); SwitchSlideInk(_pendingPreviousSlideIndex, _pendingSlideIndex);
_pptUIManager?.UpdateCurrentSlideNumber(_pendingSlideIndex, totalSlides); _pptUIManager?.UpdateCurrentSlideNumber(_pendingSlideIndex, totalSlides);
_pendingSlideIndex = -1; _pendingSlideIndex = -1;
_pendingPreviousSlideIndex = -1;
} }
}); });
_slideSwitchDebounceTimer?.Stop(); _slideSwitchDebounceTimer?.Stop();
@@ -1322,9 +1325,8 @@ namespace Ink_Canvas
} }
else else
{ {
// 直接处理页面切换 SwitchSlideInk(previousSlideIndex, newSlideIndex);
SwitchSlideInk(currentSlide); _pptUIManager?.UpdateCurrentSlideNumber(newSlideIndex, totalSlides);
_pptUIManager?.UpdateCurrentSlideNumber(currentSlide, totalSlides);
} }
_lastSlideSwitchTime = now; _lastSlideSwitchTime = now;
@@ -1335,46 +1337,29 @@ namespace Ink_Canvas
} }
} }
private void SwitchSlideInk(int newSlideIndex) private void SwitchSlideInk(int previousSlideIndex, int newSlideIndex)
{ {
try try
{ {
// 检查PPT连接状态 if (_pptManager?.IsConnected != true || _pptManager?.IsInSlideShow != true) return;
if (_pptManager?.IsConnected != true || _pptManager?.IsInSlideShow != true)
{
return;
}
// 获取当前页面索引
var currentSlideIndex = _pptManager?.GetCurrentSlideNumber() ?? 0;
// 验证页面索引的有效性
if (newSlideIndex <= 0) if (newSlideIndex <= 0)
{ {
LogHelper.WriteLogToFile($"无效的新页面索引: {newSlideIndex},跳过页面切换", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"无效的新页面索引: {newSlideIndex},跳过页面切换", LogHelper.LogType.Warning);
return; return;
} }
// 如果有当前墨迹且不是第一次切换,先保存到当前页面 // 先保存上一页墨迹
if (inkCanvas.Strokes.Count > 0 && currentSlideIndex > 0 && currentSlideIndex != newSlideIndex) if (inkCanvas.Strokes.Count > 0 && previousSlideIndex > 0 && previousSlideIndex != newSlideIndex)
{ {
bool canWrite = _singlePPTInkManager?.CanWriteInk(currentSlideIndex) == true; if (_singlePPTInkManager?.CanWriteInk(previousSlideIndex) == true)
_singlePPTInkManager?.SaveCurrentSlideStrokes(previousSlideIndex, inkCanvas.Strokes);
if (canWrite)
{
_singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlideIndex, inkCanvas.Strokes);
}
} }
ClearStrokes(true); ClearStrokes(true);
timeMachine.ClearStrokeHistory(); timeMachine.ClearStrokeHistory();
StrokeCollection newStrokes = _singlePPTInkManager?.SwitchToSlide(newSlideIndex, null); StrokeCollection newStrokes = _singlePPTInkManager?.SwitchToSlide(newSlideIndex, null);
if (newStrokes != null && newStrokes.Count > 0) if (newStrokes != null && newStrokes.Count > 0)
{
inkCanvas.Strokes.Add(newStrokes); inkCanvas.Strokes.Add(newStrokes);
}
} }
catch (Exception ex) catch (Exception ex)
{ {