代码清理

This commit is contained in:
PrefacedCorg
2025-08-31 11:43:52 +08:00
parent a2b711da05
commit ff086e497c
62 changed files with 826 additions and 825 deletions
@@ -128,7 +128,7 @@ namespace Ink_Canvas.Helpers
if (points.Length < 4) return points;
var result = new List<StylusPoint>();
// 添加第一个点
result.Add(points[0]);
@@ -142,9 +142,9 @@ namespace Ink_Canvas.Helpers
// 计算改进的控制点
var controlPoints = CalculateImprovedControlPoints(p0, p1, p2, p3);
// 限制插值步数,避免点数爆炸
int steps = Math.Min(UseAdaptiveInterpolation ?
int steps = Math.Min(UseAdaptiveInterpolation ?
CalculateAdaptiveSteps(p0, p1, p2, p3) : InterpolationSteps, 16);
// 生成贝塞尔曲线点,但跳过第一个点避免重复
@@ -179,7 +179,7 @@ namespace Ink_Canvas.Helpers
// 计算控制点距离(基于点间距离)
double dist1 = Math.Sqrt((p1.X - p0.X) * (p1.X - p0.X) + (p1.Y - p0.Y) * (p1.Y - p0.Y));
double dist2 = Math.Sqrt((p3.X - p2.X) * (p3.X - p2.X) + (p3.Y - p2.Y) * (p3.Y - p2.Y));
double controlDist1 = dist1 * CurveTension;
double controlDist2 = dist2 * CurveTension;
@@ -214,7 +214,7 @@ namespace Ink_Canvas.Helpers
// 基于长度和曲率计算步数
int baseSteps = Math.Max(8, Math.Min(20, (int)(totalLength / 10)));
int curvatureSteps = (int)(curvature * 10);
return Math.Max(InterpolationSteps, Math.Min(24, baseSteps + curvatureSteps));
}
+39 -39
View File
@@ -1,3 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -15,8 +17,6 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Ink_Canvas.Helpers
{
@@ -1238,7 +1238,7 @@ namespace Ink_Canvas.Helpers
// 查找解压后的主程序文件
string newAppPath = null;
string[] possibleExeNames = { "InkCanvasForClass.exe", "Ink Canvas.exe", "InkCanvas.exe" };
foreach (string exeName in possibleExeNames)
{
string testPath = Path.Combine(extractPath, exeName);
@@ -1260,12 +1260,12 @@ namespace Ink_Canvas.Helpers
try
{
LogHelper.WriteLogToFile($"AutoUpdate | 准备启动新版本进程: {newAppPath}");
// 启动新版本进程(以更新模式)
string arguments = $"--update-mode --old-process-id={currentProcessId} --extract-path=\"{extractPath}\" --target-path=\"{currentAppDir}\" --is-silence={isInSilence}";
LogHelper.WriteLogToFile($"AutoUpdate | 启动新进程的命令行: {newAppPath} {arguments}");
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = newAppPath,
@@ -1276,10 +1276,10 @@ namespace Ink_Canvas.Helpers
Process.Start(startInfo);
LogHelper.WriteLogToFile("AutoUpdate | 新版本进程启动命令已执行");
// 等待一小段时间确保新进程启动
Thread.Sleep(2000);
// 关闭当前旧软件进程
LogHelper.WriteLogToFile("AutoUpdate | 关闭当前旧软件进程");
App.IsAppExitByUser = true;
@@ -1312,7 +1312,7 @@ namespace Ink_Canvas.Helpers
if (args.Contains("--update-mode"))
{
LogHelper.WriteLogToFile("AutoUpdate | 检测到更新模式启动");
// 解析命令行参数
int oldProcessId = -1;
string extractPath = null;
@@ -1326,7 +1326,7 @@ namespace Ink_Canvas.Helpers
{
string arg = args[i];
LogHelper.WriteLogToFile($"AutoUpdate | 处理参数 {i}: {arg}");
if (arg.StartsWith("--old-process-id="))
{
string processIdStr = arg.Substring("--old-process-id=".Length);
@@ -1365,7 +1365,7 @@ namespace Ink_Canvas.Helpers
// 启动更新任务
Task.Run(async () => await PerformUpdate(oldProcessId, extractPath, targetPath, isSilence));
return true; // 返回true表示是更新模式
}
}
LogHelper.WriteLogToFile($"AutoUpdate | 参数验证失败 - 老进程ID: {oldProcessId}, 解压路径: {extractPath}, 目标路径: {targetPath}", LogHelper.LogType.Error);
return false;
@@ -1437,7 +1437,7 @@ namespace Ink_Canvas.Helpers
// 复制文件到目标目录
LogHelper.WriteLogToFile($"AutoUpdate | 开始复制文件从 {extractPath} 到 {targetPath}");
try
{
// 使用递归复制方法,支持重试机制
@@ -1445,11 +1445,11 @@ namespace Ink_Canvas.Helpers
if (copySuccess)
{
LogHelper.WriteLogToFile("AutoUpdate | 文件复制完成");
}
else
{
}
else
{
LogHelper.WriteLogToFile("AutoUpdate | 文件复制失败,部分文件可能无法覆盖", LogHelper.LogType.Error);
if (!isSilence)
{
MessageBox.Show("更新失败:部分文件无法覆盖,可能是文件正在使用中。\n请关闭所有相关程序后重试。", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -1460,7 +1460,7 @@ namespace Ink_Canvas.Helpers
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 文件复制失败: {ex.Message}", LogHelper.LogType.Error);
if (!isSilence)
{
MessageBox.Show($"更新失败:文件复制时出错\n{ex.Message}", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -1472,7 +1472,7 @@ namespace Ink_Canvas.Helpers
try
{
LogHelper.WriteLogToFile("AutoUpdate | 清理临时文件");
// 删除解压目录
if (Directory.Exists(extractPath))
{
@@ -1503,23 +1503,23 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile("AutoUpdate | 更新操作完成");
// 启动更新后的应用程序
// 启动更新后的应用程序
string newAppPath = Path.Combine(targetPath, "InkCanvasForClass.exe");
if (File.Exists(newAppPath))
{
try
{
LogHelper.WriteLogToFile($"AutoUpdate | 准备启动更新后的应用程序: {newAppPath}");
// 获取当前更新进程ID
int currentUpdateProcessId = Process.GetCurrentProcess().Id;
LogHelper.WriteLogToFile($"AutoUpdate | 当前更新进程ID: {currentUpdateProcessId}");
// 创建一个临时标记文件,用于新进程检测更新状态
string updateMarkerFile = Path.Combine(targetPath, "update_in_progress.tmp");
File.WriteAllText(updateMarkerFile, currentUpdateProcessId.ToString());
LogHelper.WriteLogToFile($"AutoUpdate | 创建更新标记文件: {updateMarkerFile}");
// 启动更新后的应用程序(标记为最终应用,不受相同进程影响)
ProcessStartInfo startInfo = new ProcessStartInfo
{
@@ -1528,24 +1528,24 @@ namespace Ink_Canvas.Helpers
WorkingDirectory = targetPath,
UseShellExecute = false
};
Process newProcess = Process.Start(startInfo);
LogHelper.WriteLogToFile($"AutoUpdate | 最终应用程序启动成功,PID: {newProcess?.Id},已标记为最终应用");
// 等待一小段时间确保最终应用程序启动
Thread.Sleep(2000);
// 结束当前更新进程
LogHelper.WriteLogToFile("AutoUpdate | 更新流程完成,结束更新进程");
// 强制结束当前更新进程
try
{
LogHelper.WriteLogToFile("AutoUpdate | 强制结束更新进程");
// 标记为应用主动退出,避免看门狗重启
App.IsAppExitByUser = true;
// 写入退出信号文件,确保看门狗不会重启
try
{
@@ -1557,7 +1557,7 @@ namespace Ink_Canvas.Helpers
{
LogHelper.WriteLogToFile($"AutoUpdate | 写入看门狗退出信号文件失败: {ex.Message}", LogHelper.LogType.Warning);
}
Environment.Exit(0);
}
catch (Exception ex)
@@ -1569,7 +1569,7 @@ namespace Ink_Canvas.Helpers
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 启动更新后的应用程序失败: {ex.Message}", LogHelper.LogType.Error);
if (!isSilence)
{
MessageBox.Show($"更新完成,但启动应用程序失败:{ex.Message}\n请手动启动应用程序。", "启动失败", MessageBoxButton.OK, MessageBoxImage.Warning);
@@ -1579,7 +1579,7 @@ namespace Ink_Canvas.Helpers
else
{
LogHelper.WriteLogToFile($"AutoUpdate | 更新后的应用程序文件不存在: {newAppPath}", LogHelper.LogType.Error);
if (!isSilence)
{
MessageBox.Show($"更新完成,但未找到应用程序文件:{newAppPath}\n请检查更新是否成功。", "文件缺失", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -1589,7 +1589,7 @@ namespace Ink_Canvas.Helpers
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 执行更新操作时出错: {ex.Message}", LogHelper.LogType.Error);
if (!isSilence)
{
MessageBox.Show($"更新失败:{ex.Message}", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -1615,7 +1615,7 @@ namespace Ink_Canvas.Helpers
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
bool fileCopied = false;
// 重试机制,最多重试3次
for (int retry = 0; retry < 3; retry++)
{
@@ -1638,7 +1638,7 @@ namespace Ink_Canvas.Helpers
}
}
}
await Task.Run(() => file.CopyTo(targetFilePath));
fileCopied = true;
break;
@@ -1646,14 +1646,14 @@ namespace Ink_Canvas.Helpers
catch (Exception ex)
{
LogHelper.WriteLogToFile($"AutoUpdate | 复制文件失败 (重试 {retry + 1}/3) {file.FullName} -> {targetFilePath}: {ex.Message}", LogHelper.LogType.Warning);
if (retry < 2)
{
Thread.Sleep(1000); // 等待1秒后重试
}
}
}
if (!fileCopied)
{
allFilesCopied = false;
@@ -1698,7 +1698,7 @@ namespace Ink_Canvas.Helpers
{
File.Delete(targetFilePath);
}
await Task.Run(() => file.CopyTo(targetFilePath));
}
catch (Exception ex)
@@ -1866,7 +1866,7 @@ namespace Ink_Canvas.Helpers
}
// 执行安装,静默模式
InstallNewVersionApp(remoteVersion, true);
InstallNewVersionApp(remoteVersion, true);
App.IsAppExitByUser = true;
Application.Current.Dispatcher.Invoke(() =>
{
@@ -1996,7 +1996,7 @@ namespace Ink_Canvas.Helpers
return false;
}
LogHelper.WriteLogToFile($"AutoUpdate | 手动安装版本: {version}");
InstallNewVersionApp(version, true);
InstallNewVersionApp(version, true);
App.IsAppExitByUser = true;
Application.Current.Dispatcher.Invoke(() =>
{
+33 -33
View File
@@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
@@ -5,7 +6,6 @@ using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
namespace Ink_Canvas.Helpers
{
@@ -758,26 +758,26 @@ namespace Ink_Canvas.Helpers
// 如果所有文件都不存在或损坏,返回新的统计对象
var newStats = new UsageStats
{
DeviceId = DeviceId,
LastLaunchTime = DateTime.Now,
LaunchCount = 0,
TotalUsageSeconds = 0,
AverageSessionSeconds = 0,
LastUpdateCheck = DateTime.MinValue,
UpdatePriority = UpdatePriority.Medium,
UsageFrequency = UsageFrequency.Medium
};
var newStats = new UsageStats
{
DeviceId = DeviceId,
LastLaunchTime = DateTime.Now,
LaunchCount = 0,
TotalUsageSeconds = 0,
AverageSessionSeconds = 0,
LastUpdateCheck = DateTime.MinValue,
UpdatePriority = UpdatePriority.Medium,
UsageFrequency = UsageFrequency.Medium
};
// 保存新统计到文件
SaveUsageStatsToFile(UsageStatsFilePath, newStats);
return newStats;
return newStats;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"DeviceIdentifier | 加载使用统计失败: {ex.Message}", LogHelper.LogType.Error);
// 返回默认统计对象
return new UsageStats
{
@@ -800,7 +800,7 @@ namespace Ink_Canvas.Helpers
{
// 保存到主文件
SaveUsageStatsToFile(UsageStatsFilePath, stats);
// 保存到备份文件
SaveUsageStatsToFile(UsageStatsBackupPath, stats);
}
@@ -822,7 +822,7 @@ namespace Ink_Canvas.Helpers
{
return stats;
}
}
}
@@ -843,31 +843,31 @@ namespace Ink_Canvas.Helpers
if (File.Exists(filePath))
{
byte[] encryptedData = File.ReadAllBytes(filePath);
if (encryptedData.Length < 32) // SHA256校验和长度为32字节
{
LogHelper.WriteLogToFile($"DeviceIdentifier | 加密文件格式错误: {filePath}", LogHelper.LogType.Error);
return null;
}
// 提取校验和和加密数据
byte[] checksum = new byte[32];
byte[] data = new byte[encryptedData.Length - 32];
Array.Copy(encryptedData, 0, checksum, 0, 32);
Array.Copy(encryptedData, 32, data, 0, data.Length);
// 使用SHA256生成解密密钥
using (var sha256 = SHA256.Create())
{
byte[] keyBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(DeviceId + "ICC_Usage_Stats_Salt"));
// XOR解密
byte[] decryptedData = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
decryptedData[i] = (byte)(data[i] ^ keyBytes[i % keyBytes.Length]);
}
// 验证校验和
byte[] computedChecksum = sha256.ComputeHash(decryptedData);
if (!checksum.SequenceEqual(computedChecksum))
@@ -875,7 +875,7 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"DeviceIdentifier | 加密文件校验和验证失败: {filePath}", LogHelper.LogType.Error);
return null;
}
string json = Encoding.UTF8.GetString(decryptedData);
var stats = JsonConvert.DeserializeObject<UsageStats>(json);
if (stats != null && !string.IsNullOrEmpty(stats.DeviceId))
@@ -909,27 +909,27 @@ namespace Ink_Canvas.Helpers
string json = JsonConvert.SerializeObject(stats, Formatting.Indented);
byte[] data = Encoding.UTF8.GetBytes(json);
// 使用SHA256生成加密密钥(基于设备ID)
using (var sha256 = SHA256.Create())
{
byte[] keyBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(DeviceId + "ICC_Usage_Stats_Salt"));
// 简单的XOR加密
byte[] encryptedData = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
encryptedData[i] = (byte)(data[i] ^ keyBytes[i % keyBytes.Length]);
}
// 添加SHA256校验和
byte[] checksum = sha256.ComputeHash(data);
byte[] finalData = new byte[checksum.Length + encryptedData.Length];
checksum.CopyTo(finalData, 0);
encryptedData.CopyTo(finalData, checksum.Length);
File.WriteAllBytes(filePath, finalData);
LogHelper.WriteLogToFile($"DeviceIdentifier | 加密使用统计已保存到: {filePath}");
}
}
@@ -958,7 +958,7 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"DeviceIdentifier | 记录更新检查失败: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 从备份文件恢复使用统计数据
@@ -997,7 +997,7 @@ namespace Ink_Canvas.Helpers
try
{
var status = new List<string>();
// 检查主文件
if (File.Exists(UsageStatsFilePath))
{
@@ -1339,7 +1339,7 @@ namespace Ink_Canvas.Helpers
return descriptions.Count > 0 ? string.Join(", ", descriptions) : "普通用户";
}
/// <summary>
/// 关机时保存使用时间数据
/// </summary>
@@ -1360,7 +1360,7 @@ namespace Ink_Canvas.Helpers
// 2. 计算本次会话时长(防止异常值)
TimeSpan sessionDuration = DateTime.Now - App.appStartTime;
long sessionSeconds = Math.Max(0, (long)sessionDuration.TotalSeconds);
// 防止异常大的会话时长(超过24小时)
if (sessionSeconds > 86400)
{
@@ -1373,10 +1373,10 @@ namespace Ink_Canvas.Helpers
stats.LaunchCount++;
stats.AverageSessionSeconds = stats.TotalUsageSeconds / (double)Math.Max(1, stats.LaunchCount);
stats.LastLaunchTime = DateTime.Now;
// 4. 保存数据
SaveUsageStats(stats);
LogHelper.WriteLogToFile("DeviceIdentifier | 关机保存完成");
}
catch (Exception ex)
+37 -37
View File
@@ -1,11 +1,11 @@
using Newtonsoft.Json;
using NHotkey.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Input;
using Newtonsoft.Json;
using NHotkey.Wpf;
namespace Ink_Canvas.Helpers
{
@@ -19,7 +19,7 @@ namespace Ink_Canvas.Helpers
private readonly MainWindow _mainWindow;
private bool _isDisposed;
private bool _hotkeysShouldBeRegistered = true; // 启动时注册热键
// 配置文件路径
private static readonly string HotkeyConfigFile = Path.Combine(App.RootPath, "HotkeyConfig.json");
#endregion
@@ -227,7 +227,7 @@ namespace Ink_Canvas.Helpers
try
{
// 开始注册默认快捷键集合
// 基本操作快捷键
RegisterHotkey("Undo", Key.Z, ModifierKeys.Control, () => _mainWindow.SymbolIconUndo_MouseUp(null, null));
RegisterHotkey("Redo", Key.Y, ModifierKeys.Control, () => _mainWindow.SymbolIconRedo_MouseUp(null, null));
@@ -272,14 +272,14 @@ namespace Ink_Canvas.Helpers
try
{
// 开始从配置文件加载快捷键设置
// 检查是否应该注册快捷键
if (!_hotkeysShouldBeRegistered)
{
// 当前状态不允许注册快捷键,跳过加载
return;
}
// 尝试从配置文件加载
if (LoadHotkeysFromConfigFile())
{
@@ -319,7 +319,7 @@ namespace Ink_Canvas.Helpers
try
{
LogHelper.WriteLogToFile("开始保存快捷键配置到配置文件", LogHelper.LogType.Event);
if (SaveHotkeysToConfigFile())
{
LogHelper.WriteLogToFile("快捷键配置已成功保存到配置文件", LogHelper.LogType.Event);
@@ -347,7 +347,7 @@ namespace Ink_Canvas.Helpers
{
_hotkeysShouldBeRegistered = true;
LogHelper.WriteLogToFile("启用快捷键注册功能");
// 立即加载快捷键设置
LoadHotkeysFromSettings();
}
@@ -376,7 +376,7 @@ namespace Ink_Canvas.Helpers
{
_hotkeysShouldBeRegistered = false;
LogHelper.WriteLogToFile("禁用快捷键注册功能");
// 注销所有快捷键
UnregisterAllHotkeys();
}
@@ -438,20 +438,20 @@ namespace Ink_Canvas.Helpers
// 获取原有的动作
var originalAction = _registeredHotkeys[hotkeyName].Action;
// 注销原有快捷键
UnregisterHotkey(hotkeyName);
// 注册新的快捷键
var success = RegisterHotkey(hotkeyName, key, modifiers, originalAction);
if (success)
{
LogHelper.WriteLogToFile($"成功更新快捷键 {hotkeyName}: {modifiers}+{key}", LogHelper.LogType.Event);
// 自动保存配置
SaveHotkeysToSettings();
}
return success;
}
catch (Exception ex)
@@ -472,17 +472,17 @@ namespace Ink_Canvas.Helpers
try
{
// 通过反射访问主窗口的penType字段
var penTypeField = _mainWindow.GetType().GetField("penType",
var penTypeField = _mainWindow.GetType().GetField("penType",
BindingFlags.NonPublic | BindingFlags.Instance);
if (penTypeField != null)
{
penTypeField.SetValue(_mainWindow, penTypeIndex);
// 调用CheckPenTypeUIState方法更新UI状态
var checkPenTypeMethod = _mainWindow.GetType().GetMethod("CheckPenTypeUIState",
var checkPenTypeMethod = _mainWindow.GetType().GetMethod("CheckPenTypeUIState",
BindingFlags.NonPublic | BindingFlags.Instance);
if (checkPenTypeMethod != null)
{
checkPenTypeMethod.Invoke(_mainWindow, null);
@@ -604,7 +604,7 @@ namespace Ink_Canvas.Helpers
{
Formatting = Formatting.Indented
};
string jsonContent = JsonConvert.SerializeObject(config, settings);
// 直接写入原文件,覆盖原有内容
@@ -688,9 +688,9 @@ namespace Ink_Canvas.Helpers
try
{
// 通过反射访问主窗口的FloatingbarSelectionBG字段
var floatingbarSelectionBGField = _mainWindow.GetType().GetField("FloatingbarSelectionBG",
var floatingbarSelectionBGField = _mainWindow.GetType().GetField("FloatingbarSelectionBG",
BindingFlags.NonPublic | BindingFlags.Instance);
if (floatingbarSelectionBGField != null)
{
var floatingbarSelectionBG = floatingbarSelectionBGField.GetValue(_mainWindow);
@@ -707,7 +707,7 @@ namespace Ink_Canvas.Helpers
return true; // 返回true表示应该注销快捷键
}
}
// 通过反射访问Canvas.GetLeft方法来获取高光位置
var canvasType = Type.GetType("System.Windows.Controls.Canvas, PresentationFramework");
if (canvasType != null)
@@ -719,7 +719,7 @@ namespace Ink_Canvas.Helpers
if (leftPosition != null)
{
var position = Convert.ToDouble(leftPosition);
// 根据高光位置判断当前选中的工具
// 位置计算基于SetFloatingBarHighlightPosition方法中的逻辑
bool isMouseMode;
@@ -739,20 +739,20 @@ namespace Ink_Canvas.Helpers
{
isMouseMode = false;
}
return isMouseMode;
}
}
}
}
}
// 如果无法获取高光状态,则回退到inkCanvas.EditingMode判断
// 通过反射访问主窗口的inkCanvas字段
var inkCanvasField = _mainWindow.GetType().GetField("inkCanvas",
var inkCanvasField = _mainWindow.GetType().GetField("inkCanvas",
BindingFlags.NonPublic | BindingFlags.Instance);
if (inkCanvasField != null)
{
var inkCanvas = inkCanvasField.GetValue(_mainWindow);
@@ -768,23 +768,23 @@ namespace Ink_Canvas.Helpers
// 检查是否为批注模式
var isInkMode = editingMode.ToString().Contains("Ink");
var isSelectMode = editingMode.ToString().Contains("Select");
// 如果是批注模式或选择模式,则应该注册快捷键(返回false)
// 如果是橡皮擦模式或其他模式,则不应该注册快捷键(返回true)
var shouldNotRegisterHotkeys = !isInkMode && !isSelectMode;
return shouldNotRegisterHotkeys;
}
}
}
}
// 如果无法获取任何状态信息,则回退到原来的判断逻辑
// 通过反射访问主窗口的currentMode字段(作为最后的备用方案)
var currentModeField = _mainWindow.GetType().GetField("currentMode",
var currentModeField = _mainWindow.GetType().GetField("currentMode",
BindingFlags.NonPublic | BindingFlags.Instance);
if (currentModeField != null)
{
var currentMode = currentModeField.GetValue(_mainWindow);
@@ -798,7 +798,7 @@ namespace Ink_Canvas.Helpers
return isSelectMode;
}
}
return false; // 默认允许快捷键
}
catch (Exception ex)
@@ -816,7 +816,7 @@ namespace Ink_Canvas.Helpers
{
if (!_isDisposed)
{
_isDisposed = true;
}
}
@@ -861,4 +861,4 @@ namespace Ink_Canvas.Helpers
}
#endregion
}
}
}
@@ -28,13 +28,13 @@ namespace Ink_Canvas.Helpers
return originalStroke;
var originalPoints = originalStroke.StylusPoints.ToArray();
// 预处理:去除噪声点
var cleanedPoints = RemoveNoisePoints(originalPoints);
// 使用改进的贝塞尔曲线拟合
var smoothedPoints = ApplyCubicBezierSmoothing(cleanedPoints);
// 后处理:重采样和优化
var finalPoints = PostProcessPoints(smoothedPoints);
@@ -61,7 +61,7 @@ namespace Ink_Canvas.Helpers
var next = points[i + 1];
// 计算到前一个点的距离
double distToPrev = Math.Sqrt((curr.X - prev.X) * (curr.X - prev.X) +
double distToPrev = Math.Sqrt((curr.X - prev.X) * (curr.X - prev.X) +
(curr.Y - prev.Y) * (curr.Y - prev.Y));
// 如果距离太近,跳过这个点
@@ -148,7 +148,7 @@ namespace Ink_Canvas.Helpers
// 计算控制点距离
double dist1 = CalculateDistance(p0, p1);
double dist2 = CalculateDistance(p2, p3);
double controlDist1 = dist1 * _config.CurveTension;
double controlDist2 = dist2 * _config.CurveTension;
@@ -322,4 +322,4 @@ namespace Ink_Canvas.Helpers
return result.ToArray();
}
}
}
}
+37 -37
View File
@@ -63,7 +63,7 @@ namespace Ink_Canvas.Helpers
/// <param name="endPoint">抬笔点</param>
public void AddFadingStroke(Stroke stroke, Point startPoint, Point endPoint)
{
if (!IsEnabled || stroke == null)
if (!IsEnabled || stroke == null)
{
return;
}
@@ -238,12 +238,12 @@ namespace Ink_Canvas.Helpers
public void UpdateFadeTime(int fadeTime)
{
FadeTime = fadeTime;
foreach (var kvp in _fadeTimers)
{
var stroke = kvp.Key;
var timer = kvp.Value;
timer.Stop();
timer.Interval = TimeSpan.FromMilliseconds(FadeTime);
timer.Start();
@@ -283,14 +283,14 @@ namespace Ink_Canvas.Helpers
{
// 创建路径几何,使用墨迹的实际位置
var geometry = stroke.GetGeometry();
if (geometry == null)
if (geometry == null)
{
return null;
}
// 获取绘画属性
var drawingAttribs = stroke.DrawingAttributes;
// 创建路径元素,确保使用正确的绘画属性
var path = new Path
{
@@ -302,7 +302,7 @@ namespace Ink_Canvas.Helpers
StrokeLineJoin = PenLineJoin.Round,
Fill = drawingAttribs.IsHighlighter ? new SolidColorBrush(drawingAttribs.Color) : null, // 高亮笔需要填充
Opacity = 0.95, // 初始透明度更高,显得更自然
// 优化渲染质量
UseLayoutRounding = false,
SnapsToDevicePixels = false
@@ -312,19 +312,19 @@ namespace Ink_Canvas.Helpers
if (drawingAttribs.IsHighlighter)
{
path.Opacity = 0.4; // 高亮笔初始透明度更低,更符合荧光笔特性
// 为高亮笔添加特殊的混合效果
// 使用更柔和的笔触样式
path.StrokeStartLineCap = PenLineCap.Flat;
path.StrokeEndLineCap = PenLineCap.Flat;
path.StrokeLineJoin = PenLineJoin.Miter;
// 高亮笔通常需要更宽的笔触来覆盖下面的内容
if (drawingAttribs.Width < 20)
{
path.StrokeThickness = Math.Max(drawingAttribs.Width * 1.5, 20);
}
}
// 高亮笔通常需要更宽的笔触来覆盖下面的内容
if (drawingAttribs.Width < 20)
{
path.StrokeThickness = Math.Max(drawingAttribs.Width * 1.5, 20);
}
}
// 不设置任何变换,保持墨迹原有粗细
var bounds = geometry.Bounds;
@@ -356,7 +356,7 @@ namespace Ink_Canvas.Helpers
// 获取当前透明度和判断是否为高亮笔
var currentOpacity = visual.Opacity;
var isHighlighter = stroke.DrawingAttributes.IsHighlighter;
// 根据墨迹类型选择不同的动画效果
if (isHighlighter)
{
@@ -439,19 +439,19 @@ namespace Ink_Canvas.Helpers
{
var stylusPoints = stroke.StylusPoints;
var totalPoints = stylusPoints.Count;
// 分段算法 - 确保所有墨迹都有足够的动画效果
var strokeLength = CalculateStrokeLength(stylusPoints);
var segmentCount = CalculateOptimalSegmentCount(totalPoints, strokeLength);
// 强制最小分段数量,确保短墨迹也有动画效果
segmentCount = Math.Max(segmentCount, 4);
var pointsPerSegment = Math.Max(1, totalPoints / segmentCount);
// 隐藏原始视觉元素
originalVisual.Visibility = Visibility.Hidden;
var segments = new List<UIElement>();
var parent = _mainWindow.inkCanvas?.Parent as Panel;
if (parent == null)
@@ -465,7 +465,7 @@ namespace Ink_Canvas.Helpers
{
var startIndex = i * pointsPerSegment;
var endIndex = (i == segmentCount - 1) ? totalPoints - 1 : (i + 1) * pointsPerSegment;
// 确保有足够的点来创建分段,对于短墨迹特殊处理
if (endIndex <= startIndex && totalPoints > 1)
{
@@ -473,12 +473,12 @@ namespace Ink_Canvas.Helpers
startIndex = i;
endIndex = Math.Min(i + 1, totalPoints - 1);
}
// 为每个分段添加重叠,确保连接处平滑
var overlap = Math.Max(1, pointsPerSegment / 6); // 15%的重叠,平衡平滑与速度
var actualStartIndex = Math.Max(0, startIndex - overlap);
var actualEndIndex = Math.Min(totalPoints - 1, endIndex + overlap);
var segment = CreateStrokeSegment(stroke, actualStartIndex, actualEndIndex, opacity);
if (segment != null)
{
@@ -576,10 +576,10 @@ namespace Ink_Canvas.Helpers
for (int i = 0; i < segments.Count; i++)
{
var segment = segments[i];
// 使用预计算的动画曲线获取延迟时间
var delay = animationCurve[i];
// 使用定时器延迟启动每个分段的动画
var timer = new DispatcherTimer
{
@@ -595,7 +595,7 @@ namespace Ink_Canvas.Helpers
lock (completedSegments)
{
completedSegments.Add(segment);
// 检查是否所有分段都完成了
if (completedSegments.Count >= totalSegments)
{
@@ -676,7 +676,7 @@ namespace Ink_Canvas.Helpers
{
// 移除所有分段
var parent = _mainWindow.inkCanvas?.Parent as Panel;
foreach (var segment in segments)
{
if (parent != null && parent.Children.Contains(segment))
@@ -729,7 +729,7 @@ namespace Ink_Canvas.Helpers
private double CalculateStrokeLength(StylusPointCollection points)
{
if (points.Count < 2) return 0;
double totalLength = 0;
for (int i = 1; i < points.Count; i++)
{
@@ -749,22 +749,22 @@ namespace Ink_Canvas.Helpers
const double PIXELS_PER_SEGMENT = 12.0; // 每段适中长度,平衡效果与速度
const int MIN_SEGMENTS = 5; // 适当的最小分段数,确保动画效果
const int MAX_SEGMENTS = 100; // 适中的最大分段数,平衡性能与效果
// 根据长度计算基础分段数
var lengthBasedSegments = Math.Max(MIN_SEGMENTS, (int)(strokeLength / PIXELS_PER_SEGMENT));
// 根据点密度调整,平衡效果与速度
var density = pointCount > 0 ? strokeLength / pointCount : 1;
var densityFactor = Math.Max(0.4, Math.Min(2.5, density / 1.8));
var finalSegments = (int)(lengthBasedSegments * densityFactor);
// 对于短墨迹,确保至少有4个分段
if (pointCount <= 5)
{
finalSegments = Math.Max(finalSegments, 4);
}
// 限制在合理范围内
return Math.Min(MAX_SEGMENTS, Math.Max(MIN_SEGMENTS, finalSegments));
}
@@ -778,7 +778,7 @@ namespace Ink_Canvas.Helpers
var baseDuration = totalDuration / Math.Max(segmentCount, 1);
var minDuration = 150; // 每段最少150ms,确保动画完整显示
var maxDuration = 500; // 每段最多500ms,平衡速度与完整性
return Math.Max(minDuration, Math.Min(maxDuration, baseDuration));
}
@@ -788,17 +788,17 @@ namespace Ink_Canvas.Helpers
private int[] CreateAppleStyleAnimationCurve(int segmentCount, int totalDuration)
{
var curve = new int[segmentCount];
// 平衡速度与完整性,确保动画有足够时间播放
var availableTime = totalDuration * 0.6; // 使用60%的总时间,给动画留足够缓冲
var delayBetweenSegments = Math.Max(60, availableTime / Math.Max(segmentCount, 1));
for (int i = 0; i < segmentCount; i++)
{
// 线性延迟,确保每个分段都有足够时间
curve[i] = (int)(i * delayBetweenSegments);
}
return curve;
}
@@ -829,4 +829,4 @@ namespace Ink_Canvas.Helpers
}
#endregion
}
}
}
+10 -10
View File
@@ -12,22 +12,22 @@ namespace Ink_Canvas.Helpers
public double SmoothingStrength { get; set; } = 0.4;
public double ResampleInterval { get; set; } = 2.5;
public int InterpolationSteps { get; set; } = 12;
// 贝塞尔曲线参数
public bool UseAdaptiveInterpolation { get; set; } = true;
public double CurveTension { get; set; } = 0.3;
public double MinCurvatureThreshold { get; set; } = 0.1;
public double MaxCurvatureThreshold { get; set; } = 0.8;
// 性能参数
public bool UseHardwareAcceleration { get; set; } = true;
public bool UseAsyncProcessing { get; set; } = true;
public int MaxConcurrentTasks { get; set; } = Environment.ProcessorCount;
public int MaxPointsPerStroke { get; set; } = 10000;
// 质量设置
public SmoothingQuality Quality { get; set; } = SmoothingQuality.Balanced;
public enum SmoothingQuality
{
Performance, // 性能优先
@@ -49,7 +49,7 @@ namespace Ink_Canvas.Helpers
public static InkSmoothingConfig FromSettings()
{
var config = new InkSmoothingConfig();
try
{
// 尝试从MainWindow.Settings加载配置(兼容性)
@@ -66,7 +66,7 @@ namespace Ink_Canvas.Helpers
{
Debug.WriteLine($"加载平滑配置失败: {ex.Message}");
}
return config;
}
@@ -85,7 +85,7 @@ namespace Ink_Canvas.Helpers
CurveTension = 0.2;
MaxConcurrentTasks = Math.Max(1, Environment.ProcessorCount / 2);
break;
case SmoothingQuality.Balanced:
SmoothingStrength = 0.4;
ResampleInterval = 2.5;
@@ -94,7 +94,7 @@ namespace Ink_Canvas.Helpers
CurveTension = 0.3;
MaxConcurrentTasks = Environment.ProcessorCount;
break;
case SmoothingQuality.Quality:
SmoothingStrength = 0.6;
ResampleInterval = 1.5;
@@ -120,7 +120,7 @@ namespace Ink_Canvas.Helpers
MainWindow.Settings.Canvas.UseHardwareAcceleration = UseHardwareAcceleration;
MainWindow.Settings.Canvas.UseAsyncInkSmoothing = UseAsyncProcessing;
MainWindow.Settings.Canvas.MaxConcurrentSmoothingTasks = MaxConcurrentTasks;
}
}
catch (Exception ex)
@@ -152,4 +152,4 @@ namespace Ink_Canvas.Helpers
$"张力: {CurveTension:F2}, 硬件加速: {UseHardwareAcceleration}";
}
}
}
}
+6 -6
View File
@@ -1,9 +1,9 @@
using System;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Ink;
using Microsoft.Office.Interop.PowerPoint;
namespace Ink_Canvas.Helpers
{
@@ -57,11 +57,11 @@ namespace Ink_Canvas.Helpers
{
// 完全清理之前的墨迹状态
ClearAllStrokes();
// 重置墨迹锁定状态
_inkLockUntil = DateTime.MinValue;
_lockedSlideIndex = -1;
// 生成演示文稿唯一标识符
_currentPresentationId = GeneratePresentationId(presentation);
@@ -164,7 +164,7 @@ namespace Ink_Canvas.Helpers
{
// 确定要保存的页面索引
int saveToSlideIndex = _lockedSlideIndex > 0 ? _lockedSlideIndex : slideIndex;
// 确保页面索引有效
if (saveToSlideIndex > 0 && saveToSlideIndex < _memoryStreams.Length)
{
@@ -179,7 +179,7 @@ namespace Ink_Canvas.Helpers
// 加载新页面的墨迹
var newStrokes = LoadSlideStrokes(slideIndex);
LogHelper.WriteLogToFile($"已切换到第{slideIndex}页,加载墨迹数量: {newStrokes.Count}", LogHelper.LogType.Trace);
return newStrokes;
}
catch (Exception ex)
+3 -3
View File
@@ -1,4 +1,5 @@
using System;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@@ -7,7 +8,6 @@ using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Threading;
using Microsoft.Office.Interop.PowerPoint;
using Application = System.Windows.Application;
using Timer = System.Timers.Timer;
@@ -219,7 +219,7 @@ namespace Ink_Canvas.Helpers
{
_lastSlideShowState = currentSlideShowState;
SlideShowStateChanged?.Invoke(currentSlideShowState);
if (!currentSlideShowState)
{
LogHelper.WriteLogToFile("检测到PPT放映已结束", LogHelper.LogType.Trace);
+1 -1
View File
@@ -157,7 +157,7 @@ namespace Ink_Canvas.Helpers
{
// 检查是否应该显示PPT按钮
// 不仅要检查按钮设置,还要确保确实在PPT放映模式下
bool shouldShowButtons = ShowPPTButton &&
bool shouldShowButtons = ShowPPTButton &&
_mainWindow.BtnPPTSlideShowEnd.Visibility == Visibility.Visible &&
_mainWindow.PPTManager?.IsInSlideShow == true;
@@ -1,9 +1,9 @@
using iNKORE.UI.WPF.Modern.Controls;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using iNKORE.UI.WPF.Modern.Controls;
namespace Ink_Canvas.Helpers.Plugins.BuiltIn.SuperLauncher
{
@@ -1,3 +1,5 @@
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -8,8 +10,6 @@ using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using Newtonsoft.Json;
namespace Ink_Canvas.Helpers.Plugins.BuiltIn.SuperLauncher
{
@@ -1,10 +1,10 @@
using Ink_Canvas.Windows;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Ink_Canvas.Windows;
using Microsoft.Win32;
namespace Ink_Canvas.Helpers.Plugins.BuiltIn.SuperLauncher
{
@@ -1,3 +1,5 @@
using Ink_Canvas.Helpers.Plugins.BuiltIn.SuperLauncher;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -5,8 +7,6 @@ using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Ink_Canvas.Helpers.Plugins.BuiltIn.SuperLauncher;
using Newtonsoft.Json;
namespace Ink_Canvas.Helpers.Plugins.BuiltIn
{
@@ -89,4 +89,4 @@ namespace Ink_Canvas.Helpers.Plugins
base.Cleanup();
}
}
}
}
@@ -45,4 +45,4 @@ namespace Ink_Canvas.Helpers.Plugins
/// </summary>
void OnConfigurationChanged();
}
}
}
+1 -1
View File
@@ -554,4 +554,4 @@ namespace Ink_Canvas.Helpers.Plugins
/// </summary>
Error
}
}
}
@@ -1,8 +1,8 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Ink_Canvas.Helpers.Plugins
{
@@ -270,4 +270,4 @@ namespace Ink_Canvas.Helpers.Plugins
}
}
}
}
}
+2 -2
View File
@@ -1,3 +1,5 @@
using Ink_Canvas.Windows;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -8,8 +10,6 @@ using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Ink_Canvas.Windows;
using Newtonsoft.Json;
using Timer = System.Timers.Timer;
namespace Ink_Canvas.Helpers.Plugins
@@ -452,4 +452,4 @@ namespace Ink_Canvas.Helpers.Plugins
#endregion
}
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
using System;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Ink_Canvas.Helpers
{