Skip to content

Commit c7c8d39

Browse files
Add SafePDFView class to prevent mouse tracking crashes in PDF rendering
🔧 New Features: - Introduced SafePDFView, a custom subclass of PDFView, to handle mouse interactions safely and prevent crashes. - Implemented methods to disable mouse tracking and interactions that could lead to issues. 📄 PDF Thumbnail and Page Representable Updates: - Updated PDFThumbnailRepresentable and PDFPageRepresentable to use SafePDFView instead of the standard PDFView. This enhancement improves the stability of PDF rendering by mitigating mouse-related crashes.
1 parent ae9cc2d commit c7c8d39

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

A6Cutter/ContentView.swift

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,12 +1193,65 @@ struct PDFPageView: View {
11931193
}
11941194
}
11951195

1196+
// Custom PDFView that prevents mouse tracking crashes
1197+
class SafePDFView: PDFView {
1198+
override func awakeFromNib() {
1199+
super.awakeFromNib()
1200+
setupSafeConfiguration()
1201+
}
1202+
1203+
override init(frame frameRect: NSRect) {
1204+
super.init(frame: frameRect)
1205+
setupSafeConfiguration()
1206+
}
1207+
1208+
required init?(coder: NSCoder) {
1209+
super.init(coder: coder)
1210+
setupSafeConfiguration()
1211+
}
1212+
1213+
private func setupSafeConfiguration() {
1214+
// Disable all mouse interactions that can cause crashes
1215+
self.allowsDragging = false
1216+
self.wantsLayer = true
1217+
1218+
// Disable mouse tracking areas
1219+
self.trackingAreas.forEach { area in
1220+
self.removeTrackingArea(area)
1221+
}
1222+
}
1223+
1224+
override func updateTrackingAreas() {
1225+
// Override to prevent automatic tracking area creation
1226+
super.updateTrackingAreas()
1227+
// Remove any tracking areas that might be added
1228+
self.trackingAreas.forEach { area in
1229+
self.removeTrackingArea(area)
1230+
}
1231+
}
1232+
1233+
override func mouseMoved(with event: NSEvent) {
1234+
// Override to prevent crashes from mouse movement
1235+
// Do nothing - this prevents the crash
1236+
}
1237+
1238+
override func mouseEntered(with event: NSEvent) {
1239+
// Override to prevent crashes from mouse events
1240+
// Do nothing - this prevents the crash
1241+
}
1242+
1243+
override func mouseExited(with event: NSEvent) {
1244+
// Override to prevent crashes from mouse events
1245+
// Do nothing - this prevents the crash
1246+
}
1247+
}
1248+
11961249
// PDF thumbnail reprezentace pro macOS
11971250
struct PDFThumbnailRepresentable: NSViewRepresentable {
11981251
let page: PDFPage
11991252

1200-
func makeNSView(context: Context) -> PDFView {
1201-
let pdfView = PDFView()
1253+
func makeNSView(context: Context) -> SafePDFView {
1254+
let pdfView = SafePDFView()
12021255

12031256
// Vytvoř nový dokument s jednou stránkou
12041257
let document = PDFDocument()
@@ -1211,13 +1264,10 @@ struct PDFThumbnailRepresentable: NSViewRepresentable {
12111264
pdfView.displayDirection = .vertical
12121265
pdfView.scaleFactor = 0.3 // Menší velikost pro thumbnaily
12131266

1214-
// Zakázat interakce pro thumbnaily
1215-
pdfView.allowsDragging = false
1216-
12171267
return pdfView
12181268
}
12191269

1220-
func updateNSView(_ nsView: PDFView, context: Context) {
1270+
func updateNSView(_ nsView: SafePDFView, context: Context) {
12211271
// Aktualizace není potřeba, stránka se nemění
12221272
// Ale ujistíme se, že je PDFView stále platný
12231273
if nsView.document == nil {
@@ -1234,17 +1284,18 @@ struct PDFThumbnailRepresentable: NSViewRepresentable {
12341284
struct PDFPageRepresentable: NSViewRepresentable {
12351285
let page: PDFPage
12361286

1237-
func makeNSView(context: Context) -> PDFView {
1238-
let pdfView = PDFView()
1287+
func makeNSView(context: Context) -> SafePDFView {
1288+
let pdfView = SafePDFView()
12391289
pdfView.document = PDFDocument()
12401290
pdfView.document?.insert(page, at: 0)
12411291
pdfView.autoScales = true
12421292
pdfView.displayMode = .singlePage
12431293
pdfView.displayDirection = .vertical
1294+
12441295
return pdfView
12451296
}
12461297

1247-
func updateNSView(_ nsView: PDFView, context: Context) {
1298+
func updateNSView(_ nsView: SafePDFView, context: Context) {
12481299
// Aktualizace není potřeba, stránka se nemění
12491300
}
12501301
}

0 commit comments

Comments
 (0)