优化代码

This commit is contained in:
2025-09-30 17:46:35 +08:00
parent 92e695ef7c
commit 92bb458345
16 changed files with 71 additions and 131 deletions
+4 -6
View File
@@ -238,9 +238,8 @@ namespace Ink_Canvas
RefreshBlackBoardSidePageListView();
AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderLeftPageListView);
await Task.Delay(1);
var leftContainer = BlackBoardLeftSidePageListView.ItemContainerGenerator.ContainerFromIndex(
CurrentWhiteboardIndex - 1) as ListViewItem;
if (leftContainer != null)
if (BlackBoardLeftSidePageListView.ItemContainerGenerator.ContainerFromIndex(
CurrentWhiteboardIndex - 1) is ListViewItem leftContainer)
{
ScrollViewToVerticalTop(leftContainer, BlackBoardLeftSidePageListScrollViewer);
}
@@ -258,9 +257,8 @@ namespace Ink_Canvas
RefreshBlackBoardSidePageListView();
AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardBorderRightPageListView);
await Task.Delay(1);
var rightContainer = BlackBoardRightSidePageListView.ItemContainerGenerator.ContainerFromIndex(
CurrentWhiteboardIndex - 1) as ListViewItem;
if (rightContainer != null)
if (BlackBoardRightSidePageListView.ItemContainerGenerator.ContainerFromIndex(
CurrentWhiteboardIndex - 1) is ListViewItem rightContainer)
{
ScrollViewToVerticalTop(rightContainer, BlackBoardRightSidePageListScrollViewer);
}
+3 -6
View File
@@ -504,8 +504,7 @@ namespace Ink_Canvas
BackgroundPalette.Child = stackPanel;
// 获取主窗口中的根网格,确保面板添加到顶层
Grid mainGrid = FindName("Main_Grid") as Grid;
if (mainGrid != null)
if (FindName("Main_Grid") is Grid mainGrid)
{
// 删除可能已存在的BackgroundPalette
foreach (UIElement element in mainGrid.Children)
@@ -526,8 +525,7 @@ namespace Ink_Canvas
mainGrid.Children.Add(BackgroundPalette);
// 设置面板位置
var clickElement = FindName("BoardChangeBackgroundColorBtn") as FrameworkElement;
if (clickElement != null)
if (FindName("BoardChangeBackgroundColorBtn") is FrameworkElement clickElement)
{
Point position = clickElement.TranslatePoint(new Point(0, 0), mainGrid);
BackgroundPalette.Margin = new Thickness(
@@ -551,10 +549,9 @@ namespace Ink_Canvas
{
if (modePanel.Children.Count > 1)
{
var whiteboardButton = modePanel.Children[0] as Border;
var blackboardButton = modePanel.Children[1] as Border;
if (whiteboardButton != null && whiteboardButton.Child is TextBlock whiteboardText)
if (modePanel.Children[0] is Border whiteboardButton && whiteboardButton.Child is TextBlock whiteboardText)
{
whiteboardButton.Background = Settings.Canvas.UsingWhiteboard ?
new SolidColorBrush(Color.FromRgb(0x25, 0x63, 0xeb)) :
+2 -3
View File
@@ -85,9 +85,8 @@ namespace Ink_Canvas
// 根据橡皮擦形状选择对应的图像资源
string resourceKey = isEraserCircleShape ? "EllipseEraserImageSource" : "RectangleEraserImageSource";
var imageSource = TryFindResource(resourceKey) as DrawingImage;
if (imageSource != null)
if (TryFindResource(resourceKey) is DrawingImage imageSource)
{
eraserFeedback.Source = imageSource;
}
@@ -53,8 +53,7 @@ namespace Ink_Canvas
foreach (var kvp in Settings.Automation.FloatingWindowInterceptor.InterceptRules)
{
var toggleName = $"ToggleSwitch{kvp.Key}";
var toggle = FindName(toggleName) as ToggleSwitch;
if (toggle != null)
if (FindName(toggleName) is ToggleSwitch toggle)
{
toggle.IsOn = kvp.Value;
}
+1 -2
View File
@@ -2895,8 +2895,7 @@ namespace Ink_Canvas
private async void UpdateChannelSelector_Checked(object sender, RoutedEventArgs e)
{
if (!isLoaded) return;
var radioButton = sender as RadioButton;
if (radioButton != null)
if (sender is RadioButton radioButton)
{
string channel = radioButton.Tag.ToString();
UpdateChannel newChannel = channel == "Beta" ? UpdateChannel.Beta : UpdateChannel.Release;
+4 -13
View File
@@ -489,20 +489,16 @@ namespace Ink_Canvas
{
SetCursorBasedOnEditingMode(inkCanvas);
}
// 修复:几何绘制模式下完全禁止触摸轨迹收集
if (drawingShapeMode != 0)
{
// 确保几何绘制模式下不切换到Ink模式,避免触摸轨迹被收集
inkCanvas.EditingMode = InkCanvasEditingMode.None;
if (isWaitUntilNextTouchDown && dec.Count > 1) return;
if (dec.Count > 1)
{
// 修复:双曲线绘制时,多指触摸不应该删除第一笔的辅助线
if ((drawingShapeMode == 24 || drawingShapeMode == 25) && drawMultiStepShapeCurrentStep == 1)
{
// 第二笔绘制双曲线时,只删除第二笔的临时笔画,保留第一笔的辅助线
try
{
inkCanvas.Strokes.Remove(lastTempStroke);
@@ -523,13 +519,10 @@ namespace Ink_Canvas
}
return;
}
// 修复:双曲线绘制时,第二笔应该基于第一笔的起点,而不是触摸实时位置
Point touchPoint = e.GetTouchPoint(inkCanvas).Position;
if ((drawingShapeMode == 24 || drawingShapeMode == 25) && drawMultiStepShapeCurrentStep == 1)
{
// 第二笔绘制双曲线时,使用触摸位置作为终点,但保持第一笔的起点
// 这里不需要特殊处理,因为MouseTouchMove函数内部已经正确处理了双曲线的绘制逻辑
MouseTouchMove(touchPoint);
}
else
@@ -538,15 +531,13 @@ namespace Ink_Canvas
MouseTouchMove(touchPoint);
}
return; // 处理完几何绘制后直接返回,不执行后面的代码
return;
}
// 其它模式下,允许橡皮、套索、批注等正常工作,不覆盖EditingMode
if (inkCanvas.EditingMode == InkCanvasEditingMode.EraseByPoint ||
inkCanvas.EditingMode == InkCanvasEditingMode.Select ||
inkCanvas.EditingMode == InkCanvasEditingMode.Ink)
{
// 允许正常橡皮、套索、批注
}
}
+5 -9
View File
@@ -211,22 +211,18 @@ namespace Ink_Canvas
// 获取全局快捷键管理器
var hotkeyManagerField = typeof(MainWindow).GetField("_globalHotkeyManager",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var hotkeyManager = hotkeyManagerField?.GetValue(mainWin) as GlobalHotkeyManager;
if (hotkeyManager != null)
if (hotkeyManagerField?.GetValue(mainWin) is GlobalHotkeyManager hotkeyManager)
{
// 禁用所有快捷键
hotkeyManager.DisableHotkeyRegistration();
// 更新菜单项文本和状态
var menuItem = sender as MenuItem;
if (menuItem != null)
if (sender is MenuItem menuItem)
{
var headerPanel = menuItem.Header as SimpleStackPanel;
if (headerPanel != null)
if (menuItem.Header is SimpleStackPanel headerPanel)
{
var textBlock = headerPanel.Children[0] as TextBlock;
if (textBlock != null)
if (headerPanel.Children[0] is TextBlock textBlock)
{
if (textBlock.Text == "禁用所有快捷键")
{