fix: preserve annotations while resizing selection
This commit is contained in:
@@ -838,6 +838,24 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
MAX(0, MIN(p.y - _selection.origin.y, _selection.size.height)));
|
||||
}
|
||||
|
||||
// Annotations use selection-local coordinates. When a top or left resize
|
||||
// moves the selection origin, offset every local point in the opposite
|
||||
// direction so the annotation remains attached to the same screen pixels.
|
||||
- (void)preserveAnnotationScreenPositionsFromOrigin:(NSPoint)previousOrigin toOrigin:(NSPoint)nextOrigin {
|
||||
CGFloat dx = previousOrigin.x - nextOrigin.x;
|
||||
CGFloat dy = previousOrigin.y - nextOrigin.y;
|
||||
if (fabs(dx) <= 0.001 && fabs(dy) <= 0.001) {
|
||||
return;
|
||||
}
|
||||
for (NSDictionary *annotation in _annotations) {
|
||||
NSMutableArray *points = (NSMutableArray *)annotation[@"points"];
|
||||
for (NSUInteger i = 0; i < points.count; i++) {
|
||||
NSPoint point = [points[i] pointValue];
|
||||
points[i] = [NSValue valueWithPoint:NSMakePoint(point.x + dx, point.y + dy)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Translate a selection rectangle expressed in view-local coordinates into
|
||||
// the global top-left screen coordinate system used by
|
||||
// `/usr/sbin/screencapture -R`.
|
||||
@@ -992,7 +1010,9 @@ static id nativeOverlayKeyMonitor = nil;
|
||||
right = MAX(0, MIN(right, self.bounds.size.width));
|
||||
top = MAX(0, MIN(top, self.bounds.size.height));
|
||||
bottom = MAX(0, MIN(bottom, self.bounds.size.height));
|
||||
_selection = NSMakeRect(MIN(left, right), MIN(top, bottom), fabs(right - left), fabs(bottom - top));
|
||||
NSRect nextSelection = NSMakeRect(MIN(left, right), MIN(top, bottom), fabs(right - left), fabs(bottom - top));
|
||||
[self preserveAnnotationScreenPositionsFromOrigin:_selection.origin toOrigin:nextSelection.origin];
|
||||
_selection = nextSelection;
|
||||
} else if (_draggingTextAnnotation && _selectedTextAnnotationIndex >= 0 && _selectedTextAnnotationIndex < (NSInteger)_annotations.count) {
|
||||
NSMutableDictionary *item = (NSMutableDictionary *)_annotations[(NSUInteger)_selectedTextAnnotationIndex];
|
||||
NSMutableArray *points = (NSMutableArray *)item[@"points"];
|
||||
|
||||
Reference in New Issue
Block a user