Skip to content

Commit

Permalink
added exceptions to setting magnification, first implementation of se…
Browse files Browse the repository at this point in the history
…tMagnfication through rect and point, and allowed negative values to be passed to scaleUnitSize
  • Loading branch information
jvmespark authored and CKegel committed Feb 16, 2024
1 parent 1ab8aac commit 0ae4f4c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

47 changes: 37 additions & 10 deletions AppKit/NSScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -872,19 +872,23 @@ - (void) allowsMagnification: (BOOL) value {
_allowsMagnification = value;
}

- (void) setMagnification: (CGFloat) value {
_magnification = value;
}

- (void) setMaxMagnification: (CGFloat) value {
if (value >= _minMagnification) {
_maxMagnification = value;
} else {
[NSException raise: NSInvalidArgumentException
format: @"maxMagnification (%.3f) must be greater than or equal to minMagnification (%.3f).",
value, _minMagnification];
}
}

- (void) setMinMagnification: (CGFloat) value {
if (value <= _maxMagnification) {
_minMagnification = value;
} else {
[NSException raise: NSInvalidArgumentException
format: @"minMagnification (%.3f) must be less than or equal to maxMagnification (%.3f).",
value, _maxMagnification];
}
}

Expand Down Expand Up @@ -1114,13 +1118,36 @@ - (void) resizeSubviewsWithOldSize: (NSSize) oldSize {
[self _horizontalScroll: _horizontalScroller];
}

- (void) setMagnification:(CGFloat) magnification
centeredAtPoint:(NSPoint) point {
// scale the content view centered at the given point
NSRect frame = [_clipView frame];
NSSize scaledSize = NSMakeSize(frame.size.width*magnification, frame.size.height*magnification);
NSPoint centeredOrigin = NSMakePoint(point.x - (newSize.width/2), point.y - (newSize.height/2));
[_clipView setFrame:NSMakeRect(centeredOrigin.x, centeredOrigin.y, scaledSize.width, scaledSize.height)];

// clip the resulting magnification value to min and max magnification
magnification = MAX(_minMagnification, magnification);
magnification = MIN(_maxMagnification, magnification);

self._magnification = magnification;
}

- (void) magnifyToFitRect: (NSRect) rect {
NSUnimplementedMethod();
/*
todo
Magnifies the content view proportionally such that the given rectangle fits centered in the scroll view.
The resulting magnification value is clipped to the minMagnification and maxMagnification values
*/
if (_allowsMagnification) {
// find magnification value needed to scale the content view to the given rectangle without morphing
CGFloat widthMagnification = NSWidth(_clipView.bounds) / NSWidth(rect);
CGFloat heightMagnification = NSHeight(_clipView.bounds) / NSHeight(rect);
CGFloat magnification = MIN(widthMagnification, heightMagnification);

// The resulting magnification value is clipped to the min/max magnification
magnification = MAX(_minMagnification, magnification);
magnification = MIN(_maxMagnification, magnification);

(NSPoint) point = NSMakePoint(NSMidX(rect), NSMidY(rect));

[_clipView setMagnification:magnification centeredAtPoint:point];
}
}

@end
16 changes: 2 additions & 14 deletions AppKit/NSView.m
Original file line number Diff line number Diff line change
Expand Up @@ -646,21 +646,10 @@ - (BOOL) postsBoundsChangedNotifications {
return _postsNotificationOnBoundsChange;
}

// todo after implementing this, i need to implement magnification support in nsscrollview
- (void) scaleUnitSquareToSize: (NSSize) size {
if (size.width != 1.0 || size.height != 1.0) {
if (size.width < 0) {
size.width = 0;
}
if (size.height < 0) {
size.height = 0;
}
if (_bounds == nil) {
// todo set _bounds. would _bounds ever by nil? need to look into that
// _bounds = [NSAffineTransform new]
}
// todo what if width or height is zero?

// todo need to get scaleXBy in here, located in darling-foundation
[_bounds scaleXBy: size.width yBy: size.height];

_bounds.origin.x = _bounds.origin.x / size.width;
Expand All @@ -671,8 +660,7 @@ - (void) scaleUnitSquareToSize: (NSSize) size {
isRotatedOrScaledFromBase = YES;

/* code from gnustep
todo flag should be located in appkit/include/nsview.h
todo
if (_coordinates_valid)
{
(*invalidateImp)(self, invalidateSel);
Expand Down
3 changes: 2 additions & 1 deletion AppKit/include/AppKit/NSScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ APPKIT_EXPORT NSString *const NSScrollViewDidLiveScrollNotification;
- (void) setMaxMagnification: (CGFloat) value;
- (void) setAllowsMagnification: (BOOL) value;
- (void) setMaxMagnification: (CGFloat) value;
- (void) setMagnification: (CGFloat) value;
- (void) minMagnification: (CGFloat) value;

- (void) tile;
- (void) reflectScrolledClipView: (NSClipView *) clipView;
- (void) setMagnification:(CGFloat) magnification
centeredAtPoint:(NSPoint) point;
- (void) magnifyToFitRect: (NSRect) rect;

@end

0 comments on commit 0ae4f4c

Please sign in to comment.