bug: remove eraser stroke adding into graphic context

This commit is contained in:
dscyrescotti
2024-06-09 17:40:58 +07:00
parent 99abf94351
commit 90de6310e1
4 changed files with 8 additions and 13 deletions
+5 -8
View File
@@ -159,7 +159,8 @@ extension GraphicContext {
createdAt: .now, createdAt: .now,
thickness: pen.thickness thickness: pen.thickness
) )
withPersistence(\.backgroundContext) { [graphicContext = object, _stroke = eraserStroke] context in eraserStroke.graphicContext = self
withPersistence(\.backgroundContext) { [_stroke = eraserStroke] context in
let stroke = EraserObject(\.backgroundContext) let stroke = EraserObject(\.backgroundContext)
stroke.bounds = _stroke.bounds stroke.bounds = _stroke.bounds
stroke.color = _stroke.color stroke.color = _stroke.color
@@ -168,11 +169,9 @@ extension GraphicContext {
stroke.createdAt = _stroke.createdAt stroke.createdAt = _stroke.createdAt
stroke.quads = [] stroke.quads = []
stroke.strokes = .init() stroke.strokes = .init()
graphicContext?.strokes.add(stroke)
_stroke.object = stroke _stroke.object = stroke
try context.saveIfNeeded() try context.saveIfNeeded()
} }
eraserStroke.graphicContext = self
stroke = eraserStroke stroke = eraserStroke
} }
currentStroke = stroke currentStroke = stroke
@@ -219,17 +218,16 @@ extension GraphicContext {
if let stroke = currentStroke { if let stroke = currentStroke {
switch stroke.style { switch stroke.style {
case .marker: case .marker:
guard !tree.isEmpty else { return } guard let _stroke = stroke.stroke(as: PenStroke.self) else { break }
let _stroke = tree.remove(stroke.anyStroke, in: stroke.strokeBox)
withPersistence(\.backgroundContext) { [graphicContext = object, _stroke] context in withPersistence(\.backgroundContext) { [graphicContext = object, _stroke] context in
if let stroke = _stroke?.stroke(as: PenStroke.self)?.object { if let stroke = _stroke.object {
graphicContext?.strokes.remove(stroke) graphicContext?.strokes.remove(stroke)
context.delete(stroke) context.delete(stroke)
} }
try context.saveIfNeeded() try context.saveIfNeeded()
} }
case .eraser: case .eraser:
guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { return } guard let eraserStroke = stroke.stroke(as: EraserStroke.self) else { break }
eraserStrokes.remove(eraserStroke) eraserStrokes.remove(eraserStroke)
withPersistence(\.backgroundContext) { [eraserStroke] context in withPersistence(\.backgroundContext) { [eraserStroke] context in
if let stroke = eraserStroke.object { if let stroke = eraserStroke.object {
@@ -238,7 +236,6 @@ extension GraphicContext {
try context.saveIfNeeded() try context.saveIfNeeded()
} }
} }
} }
currentStroke = nil currentStroke = nil
currentPoint = nil currentPoint = nil
+1 -1
View File
@@ -59,7 +59,7 @@ extension Canvas {
let graphicContext = canvas.graphicContext let graphicContext = canvas.graphicContext
self?.graphicContext.object = graphicContext self?.graphicContext.object = graphicContext
self?.graphicContext.loadStrokes(bounds) self?.graphicContext.loadStrokes(bounds)
context.refresh(canvas, mergeChanges: false) context.refreshAllObjects()
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
self?.state = .loaded self?.state = .loaded
} }
@@ -194,7 +194,6 @@ struct PenDock: View {
.stroke(Color.gray, lineWidth: 0.4) .stroke(Color.gray, lineWidth: 0.4)
} }
.padding(0.2) .padding(0.2)
.drawingGroup()
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.hoverEffect(.lift) .hoverEffect(.lift)
@@ -302,7 +301,6 @@ struct PenDock: View {
.resizable() .resizable()
.renderingMode(.template) .renderingMode(.template)
} }
.drawingGroup()
.foregroundStyle(.black.opacity(0.2)) .foregroundStyle(.black.opacity(0.2))
.blur(radius: 3) .blur(radius: 3)
if let tip = pen.style.icon.tip { if let tip = pen.style.icon.tip {
+2 -2
View File
@@ -68,8 +68,8 @@ struct MemosView: View {
memoObject.updatedAt = .now memoObject.updatedAt = .now
let canvasObject = CanvasObject(context: managedObjectContext) let canvasObject = CanvasObject(context: managedObjectContext)
canvasObject.width = 4_000 canvasObject.width = 8_000
canvasObject.height = 4_000 canvasObject.height = 8_000
let toolObject = ToolObject(\.viewContext) let toolObject = ToolObject(\.viewContext)
toolObject.pens = [] toolObject.pens = []