Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to display popover without giving focus to this window #10

Merged
merged 4 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SFBPopover.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef NS_ENUM(NSUInteger, SFBPopoverPosition) {
// Show the popover- prefer these to showWindow:
- (void) displayPopoverInWindow:(nonnull NSWindow *)window atPoint:(NSPoint)point;
- (void) displayPopoverInWindow:(nonnull NSWindow *)window atPoint:(NSPoint)point chooseBestLocation:(BOOL)chooseBestLocation;
- (void) displayPopoverInWindow:(NSWindow *)window atPoint:(NSPoint)point chooseBestLocation:(BOOL)chooseBestLocation makeKey:(BOOL)makeKey;

// ========================================
// Move the popover to a new attachment point (should be currently displayed)
Expand Down
65 changes: 36 additions & 29 deletions SFBPopover.m
Original file line number Diff line number Diff line change
Expand Up @@ -226,39 +226,46 @@ - (SFBPopoverPosition) bestPositionInWindow:(NSWindow *)window atPoint:(NSPoint)

- (void) displayPopoverInWindow:(NSWindow *)window atPoint:(NSPoint)point
{
[self displayPopoverInWindow:window atPoint:point chooseBestLocation:NO];
[self displayPopoverInWindow:window atPoint:point chooseBestLocation:NO makeKey:YES];
}

- (void) displayPopoverInWindow:(NSWindow *)window atPoint:(NSPoint)point chooseBestLocation:(BOOL)chooseBestLocation
{
if([_popoverWindow isVisible])
return;

if(chooseBestLocation)
[_popoverWindow setPopoverPosition:[self bestPositionInWindow:window atPoint:point]];

NSPoint attachmentPoint = [[_popoverWindow popoverWindowFrame] attachmentPoint];
NSPoint pointOnScreen = point;
if(window) {
NSRect rect = { .origin = point, .size = NSZeroSize };
NSRect rectOnScreen = [window convertRectToScreen:rect];
pointOnScreen = rectOnScreen.origin;
}

pointOnScreen.x -= attachmentPoint.x;
pointOnScreen.y -= attachmentPoint.y;

[_popoverWindow setFrameOrigin:pointOnScreen];

if(self.animates)
[_popoverWindow setAlphaValue:0];

[window addChildWindow:_popoverWindow ordered:NSWindowAbove];

[_popoverWindow makeKeyAndOrderFront:nil];

if(self.animates)
[[_popoverWindow animator] setAlphaValue:1];
[self displayPopoverInWindow:window atPoint:point chooseBestLocation:chooseBestLocation makeKey:YES];
}

- (void) displayPopoverInWindow:(NSWindow *)window atPoint:(NSPoint)point chooseBestLocation:(BOOL)chooseBestLocation makeKey:(BOOL)makeKey
{
if([_popoverWindow isVisible])
return;

if(chooseBestLocation)
[_popoverWindow setPopoverPosition:[self bestPositionInWindow:window atPoint:point]];

NSPoint attachmentPoint = [[_popoverWindow popoverWindowFrame] attachmentPoint];
NSPoint pointOnScreen = point;
if(window) {
NSRect rect = { .origin = point, .size = NSZeroSize };
NSRect rectOnScreen = [window convertRectToScreen:rect];
pointOnScreen = rectOnScreen.origin;
}

pointOnScreen.x -= attachmentPoint.x;
pointOnScreen.y -= attachmentPoint.y;

[_popoverWindow setFrameOrigin:pointOnScreen];

if(self.animates)
[_popoverWindow setAlphaValue:0];

[window addChildWindow:_popoverWindow ordered:NSWindowAbove];

if (makeKey) {
[_popoverWindow makeKeyAndOrderFront:nil];
}

if(self.animates)
[[_popoverWindow animator] setAlphaValue:1];
}

- (void) movePopoverToPoint:(NSPoint)point
Expand Down