增强打印预览功能,新增离线打印功能,新增缩放控制按钮以提升用户体验。优化打印数据准备逻辑,支持实时预览缩放,确保打印效果的一致性。同时,重构相关视图和服务以增强系统的可维护性和扩展性。

This commit is contained in:
geht
2026-05-14 11:25:17 +08:00
parent 8bcc34aee0
commit 296bc2a4b2
7 changed files with 702 additions and 109 deletions

View File

@@ -108,6 +108,9 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
public DelegateCommand ResplitCommand { get; }
public DelegateCommand SaveAndPrintCommand { get; }
public DelegateCommand RefreshPrintersCommand { get; }
public DelegateCommand ZoomOutPrintPreviewCommand { get; }
public DelegateCommand ZoomInPrintPreviewCommand { get; }
public DelegateCommand ResetPrintPreviewZoomCommand { get; }
/// <summary>PrintDot 桥接器返回的打印机列表(与打印模板页一致)。</summary>
public ObservableCollection<PrintDotPrinter> Printers { get; } = new();
@@ -169,6 +172,27 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
private set => SetProperty(ref _printPreviewStatus, value);
}
private const double PrintPreviewMinZoom = 0.5d;
private const double PrintPreviewMaxZoom = 2.0d;
private const double PrintPreviewDefaultZoom = 0.7d;
private const double PrintPreviewZoomStep = 0.1d;
private double _printPreviewZoomFactor = PrintPreviewDefaultZoom;
/// <summary>打印预览缩放倍率WebView2 ZoomFactor。</summary>
public double PrintPreviewZoomFactor
{
get => _printPreviewZoomFactor;
set
{
var clamped = Math.Round(Math.Clamp(value, PrintPreviewMinZoom, PrintPreviewMaxZoom), 2);
if (!SetProperty(ref _printPreviewZoomFactor, clamped)) return;
RaisePropertyChanged(nameof(PrintPreviewZoomText));
}
}
/// <summary>打印预览缩放显示文本(百分比)。</summary>
public string PrintPreviewZoomText => $"{Math.Round(PrintPreviewZoomFactor * 100):0}%";
/// <summary>由 View 订阅,在 UI 线程将 HTML 交给 WebView2。</summary>
public event EventHandler<string>? PrintPreviewHtmlReady;
@@ -220,6 +244,9 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
.ObservesProperty(() => Entry);
SaveAndPrintCommand = new DelegateCommand(async () => await SaveAndPrintAsync());
RefreshPrintersCommand = new DelegateCommand(async () => await RefreshPrintersAsync(verbose: true));
ZoomOutPrintPreviewCommand = new DelegateCommand(() => PrintPreviewZoomFactor -= PrintPreviewZoomStep);
ZoomInPrintPreviewCommand = new DelegateCommand(() => PrintPreviewZoomFactor += PrintPreviewZoomStep);
ResetPrintPreviewZoomCommand = new DelegateCommand(() => PrintPreviewZoomFactor = PrintPreviewDefaultZoom);
// 集合变化:批量重订阅 item.PropertyChanged 监听 HasCard/Portions并同步刷新两个 Can*。
SplitCodeDetails.CollectionChanged += OnSplitCodeDetailsCollectionChangedForCanFlags;
_ = RefreshPrintersAsync(verbose: false);
@@ -808,7 +835,8 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
string html;
try
{
html = NativePrintRenderService.RenderToHtml(_previewTemplateJson, dataObj);
// 实时预览关闭模板内部 fitPage 自适应,避免与 WebView2 外层缩放叠加后出现“越放大越小”。
html = NativePrintRenderService.RenderToHtml(_previewTemplateJson, dataObj, enableScreenAutoFit: false);
}
catch (Exception ex)
{