From cffedb8cb74cfb8629ee2993ea01e0672dd4da22 Mon Sep 17 00:00:00 2001
From: CJKmkp <2564608840@qq.com>
Date: Sun, 5 Apr 2026 17:49:05 +0800
Subject: [PATCH] =?UTF-8?q?add:pdf=E6=8F=92=E5=85=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Ink Canvas/MainWindow_cs/MW_Colors.cs | 1 +
.../MainWindow_cs/MW_ElementsControls.cs | 150 ++++++++++++------
.../MainWindow_cs/MW_FloatingBarIcons.cs | 5 +
Ink Canvas/MainWindow_cs/MW_PPT.cs | 2 +
4 files changed, 108 insertions(+), 50 deletions(-)
diff --git a/Ink Canvas/MainWindow_cs/MW_Colors.cs b/Ink Canvas/MainWindow_cs/MW_Colors.cs
index 6eb8703e..133d23c9 100644
--- a/Ink Canvas/MainWindow_cs/MW_Colors.cs
+++ b/Ink Canvas/MainWindow_cs/MW_Colors.cs
@@ -52,6 +52,7 @@ namespace Ink_Canvas
AnimationsHelper.HideWithSlideAndFade(TwoFingerGestureBorder);
AnimationsHelper.HideWithSlideAndFade(BoardTwoFingerGestureBorder);
EnableTwoFingerGestureBorder.Visibility = Visibility.Collapsed;
+ SyncPdfPageSidebarWithCanvas();
}
BtnHideInkCanvas_Click(BtnHideInkCanvas, null);
diff --git a/Ink Canvas/MainWindow_cs/MW_ElementsControls.cs b/Ink Canvas/MainWindow_cs/MW_ElementsControls.cs
index f52e020e..169d9f9e 100644
--- a/Ink Canvas/MainWindow_cs/MW_ElementsControls.cs
+++ b/Ink Canvas/MainWindow_cs/MW_ElementsControls.cs
@@ -35,6 +35,9 @@ namespace Ink_Canvas
///
private Point dragStartPoint;
+ /// 页码侧栏当前订阅 的 PDF 视图。
+ private PdfEmbeddedView _pdfPageSidebarEventSource;
+
#region Image
///
/// 处理图片插入按钮点击事件
@@ -94,6 +97,8 @@ namespace Ink_Canvas
// 最后绑定事件处理器
BindElementEvents(element);
+ SyncPdfPageSidebarWithCanvas();
+
LogHelper.WriteLogToFile($"图片插入完成: {element.Name}");
}), DispatcherPriority.Loaded);
};
@@ -526,9 +531,6 @@ namespace Ink_Canvas
// 根据元素类型显示不同的选择工具栏
if (IsBitmapLikeCanvasElement(element))
{
- if (BorderPdfPageSidebar != null)
- BorderPdfPageSidebar.Visibility = element is PdfEmbeddedView ? Visibility.Visible : Visibility.Collapsed;
-
// 显示图片选择工具栏并设置位置
if (BorderImageSelectionControl != null)
{
@@ -540,25 +542,11 @@ namespace Ink_Canvas
// 显示图片缩放选择点
ShowImageResizeHandles(element);
- if (element is PdfEmbeddedView pdfView)
- {
- pdfView.PageNavigationStateChanged -= SelectedPdf_PageNavigationStateChanged;
- pdfView.PageNavigationStateChanged += SelectedPdf_PageNavigationStateChanged;
- UpdatePdfSidebarFromSelectedPdf();
- UpdatePdfPageSidebarPosition(pdfView);
- }
- else
- ResetPdfSidebarToIdle();
-
// 墨迹选择工具栏通过GridInkCanvasSelectionCover的可见性来控制
// 不需要直接设置BorderStrokeSelectionControl.Visibility
}
else
{
- if (BorderPdfPageSidebar != null)
- BorderPdfPageSidebar.Visibility = Visibility.Collapsed;
- ResetPdfSidebarToIdle();
-
// 隐藏图片选择工具栏
if (BorderImageSelectionControl != null)
{
@@ -586,6 +574,8 @@ namespace Ink_Canvas
// 保持选择模式,这样用户可以直接点击墨迹来选择
inkCanvas.EditingMode = InkCanvasEditingMode.Select;
}
+
+ SyncPdfPageSidebarWithCanvas();
}
///
@@ -602,15 +592,6 @@ namespace Ink_Canvas
{
// 去除选中效果
- if (element is PdfEmbeddedView pdfView)
- {
- pdfView.PageNavigationStateChanged -= SelectedPdf_PageNavigationStateChanged;
- }
-
- if (BorderPdfPageSidebar != null)
- BorderPdfPageSidebar.Visibility = Visibility.Collapsed;
- ResetPdfSidebarToIdle();
-
// 隐藏图片选择工具栏
if (BorderImageSelectionControl != null)
{
@@ -634,6 +615,8 @@ namespace Ink_Canvas
{
inkCanvas.EditingMode = InkCanvasEditingMode.Select;
}
+
+ SyncPdfPageSidebarWithCanvas();
}
///
@@ -1527,8 +1510,9 @@ namespace Ink_Canvas
// 设置工具栏位置
BorderImageSelectionControl.Margin = new Thickness(toolbarLeft, toolbarTop, 0, 0);
- if (element is PdfEmbeddedView && BorderPdfPageSidebar?.Visibility == Visibility.Visible)
- UpdatePdfPageSidebarPosition(element);
+ var pdfTarget = GetPdfSidebarTargetElement();
+ if (pdfTarget != null && BorderPdfPageSidebar != null && BorderPdfPageSidebar.Visibility == Visibility.Visible)
+ UpdatePdfPageSidebarPosition(pdfTarget);
}
catch (Exception ex)
{
@@ -1538,6 +1522,67 @@ namespace Ink_Canvas
private const double PdfPageSidebarGap = 10;
+ ///
+ /// 侧栏绑定的 PDF:若当前选中的是 PDF 则用该项;否则用画布上最后一个 PdfEmbeddedView。
+ ///
+ private PdfEmbeddedView GetPdfSidebarTargetElement()
+ {
+ if (inkCanvas == null) return null;
+ var pdfs = inkCanvas.Children.OfType().ToList();
+ if (pdfs.Count == 0) return null;
+ if (currentSelectedElement is PdfEmbeddedView sel && pdfs.Contains(sel))
+ return sel;
+ return pdfs[pdfs.Count - 1];
+ }
+
+ private void AttachPdfPageSidebarEvents(PdfEmbeddedView pdf)
+ {
+ if (pdf == null || _pdfPageSidebarEventSource == pdf) return;
+ DetachPdfPageSidebarEvents();
+ _pdfPageSidebarEventSource = pdf;
+ _pdfPageSidebarEventSource.PageNavigationStateChanged += SelectedPdf_PageNavigationStateChanged;
+ }
+
+ private void DetachPdfPageSidebarEvents()
+ {
+ if (_pdfPageSidebarEventSource != null)
+ {
+ _pdfPageSidebarEventSource.PageNavigationStateChanged -= SelectedPdf_PageNavigationStateChanged;
+ _pdfPageSidebarEventSource = null;
+ }
+ }
+
+ ///
+ /// 画布上存在 PDF 时始终显示右侧页码栏并跟随目标 PDF;无任何 PDF 时隐藏。
+ ///
+ private void SyncPdfPageSidebarWithCanvas()
+ {
+ if (BorderPdfPageSidebar == null || inkCanvas == null) return;
+
+ // 屏幕模式(已退出白板/黑板)下不显示侧栏,避免画布仍含 PDF 时栏残留在桌面上
+ if (currentMode == 0)
+ {
+ DetachPdfPageSidebarEvents();
+ BorderPdfPageSidebar.Visibility = Visibility.Collapsed;
+ ResetPdfSidebarToIdle();
+ return;
+ }
+
+ var pdf = GetPdfSidebarTargetElement();
+ if (pdf == null)
+ {
+ DetachPdfPageSidebarEvents();
+ BorderPdfPageSidebar.Visibility = Visibility.Collapsed;
+ ResetPdfSidebarToIdle();
+ return;
+ }
+
+ AttachPdfPageSidebarEvents(pdf);
+ BorderPdfPageSidebar.Visibility = Visibility.Visible;
+ UpdatePdfSidebarFromPdf(pdf);
+ UpdatePdfPageSidebarPosition(pdf);
+ }
+
///
/// 将 PDF 专用页码栏贴在当前所选 PDF 的右侧(画布坐标,与底部选中栏一致)。
///
@@ -1909,29 +1954,28 @@ namespace Ink_Canvas
}
}
- private void UpdatePdfSidebarFromSelectedPdf()
+ private void UpdatePdfSidebarFromPdf(PdfEmbeddedView pdf)
{
- if (currentSelectedElement is PdfEmbeddedView pdf)
+ if (pdf == null) return;
+
+ if (TextBlockPdfSidebarPageLabel != null)
{
- if (TextBlockPdfSidebarPageLabel != null)
- {
- TextBlockPdfSidebarPageLabel.Text = pdf.PageLabelText;
- TextBlockPdfSidebarPageLabel.Opacity = 1.0;
- }
+ TextBlockPdfSidebarPageLabel.Text = pdf.PageLabelText;
+ TextBlockPdfSidebarPageLabel.Opacity = 1.0;
+ }
- bool prevOk = pdf.CanGoPrevious;
- bool nextOk = pdf.CanGoNext;
- if (BorderPdfSidebarPagePrev != null)
- {
- BorderPdfSidebarPagePrev.Opacity = prevOk ? 1.0 : 0.35;
- BorderPdfSidebarPagePrev.IsHitTestVisible = prevOk;
- }
+ bool prevOk = pdf.CanGoPrevious;
+ bool nextOk = pdf.CanGoNext;
+ if (BorderPdfSidebarPagePrev != null)
+ {
+ BorderPdfSidebarPagePrev.Opacity = prevOk ? 1.0 : 0.35;
+ BorderPdfSidebarPagePrev.IsHitTestVisible = prevOk;
+ }
- if (BorderPdfSidebarPageNext != null)
- {
- BorderPdfSidebarPageNext.Opacity = nextOk ? 1.0 : 0.35;
- BorderPdfSidebarPageNext.IsHitTestVisible = nextOk;
- }
+ if (BorderPdfSidebarPageNext != null)
+ {
+ BorderPdfSidebarPageNext.Opacity = nextOk ? 1.0 : 0.35;
+ BorderPdfSidebarPageNext.IsHitTestVisible = nextOk;
}
}
@@ -1939,7 +1983,11 @@ namespace Ink_Canvas
{
Dispatcher.BeginInvoke(new Action(() =>
{
- UpdatePdfSidebarFromSelectedPdf();
+ if (sender is PdfEmbeddedView pdf)
+ {
+ UpdatePdfSidebarFromPdf(pdf);
+ UpdatePdfPageSidebarPosition(pdf);
+ }
if (currentSelectedElement != null && IsBitmapLikeCanvasElement(currentSelectedElement))
{
UpdateImageSelectionToolbarPosition(currentSelectedElement);
@@ -1953,7 +2001,8 @@ namespace Ink_Canvas
{
try
{
- if (currentSelectedElement is PdfEmbeddedView pdf && pdf.CanGoPrevious)
+ var pdf = GetPdfSidebarTargetElement();
+ if (pdf != null && pdf.CanGoPrevious)
await pdf.GoToPreviousPageAsync();
}
catch (Exception ex)
@@ -1966,7 +2015,8 @@ namespace Ink_Canvas
{
try
{
- if (currentSelectedElement is PdfEmbeddedView pdf && pdf.CanGoNext)
+ var pdf = GetPdfSidebarTargetElement();
+ if (pdf != null && pdf.CanGoNext)
await pdf.GoToNextPageAsync();
}
catch (Exception ex)
diff --git a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
index 310d2ebc..64ebf6ce 100644
--- a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
+++ b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs
@@ -3568,6 +3568,8 @@ namespace Ink_Canvas
break;
}
}
+
+ SyncPdfPageSidebarWithCanvas();
}
private int BoundsWidth = 5;
@@ -3832,6 +3834,7 @@ namespace Ink_Canvas
SetCurrentToolMode(InkCanvasEditingMode.Select);
UpdateCurrentToolMode("select");
HideSubPanels("select");
+ SyncPdfPageSidebarWithCanvas();
}
}
}
@@ -3898,6 +3901,7 @@ namespace Ink_Canvas
SetCurrentToolMode(InkCanvasEditingMode.Select);
UpdateCurrentToolMode("select");
HideSubPanels("select");
+ SyncPdfPageSidebarWithCanvas();
}
}
}
@@ -3964,6 +3968,7 @@ namespace Ink_Canvas
SetCurrentToolMode(InkCanvasEditingMode.Select);
UpdateCurrentToolMode("select");
HideSubPanels("select");
+ SyncPdfPageSidebarWithCanvas();
}
}
}
diff --git a/Ink Canvas/MainWindow_cs/MW_PPT.cs b/Ink Canvas/MainWindow_cs/MW_PPT.cs
index 60fb97ea..e9fd850f 100644
--- a/Ink Canvas/MainWindow_cs/MW_PPT.cs
+++ b/Ink Canvas/MainWindow_cs/MW_PPT.cs
@@ -1517,6 +1517,8 @@ namespace Ink_Canvas
currentMode = 0;
}
+ SyncPdfPageSidebarWithCanvas();
+
ClearStrokes(true);
// 清空备份历史记录,防止退出白板时恢复已结束PPT的墨迹
// 注意:这里只清空索引0的备份,不影响白板页面的墨迹(索引1及以上)