Can you detect when a Safari popover closes? - popover

We're trying to have multiple popovers in our extension and we need to know when the popover closes so we can change the popover value on the button.
Is there any listener for that?

When the popover is closed, the window object receives a blur event.
So, something like this in the popover will work:
window.onblur = function () {
// do stuff when popover is closed
};

Related

How to make NSPopover properly follow the mouse pointer and ignore mouse events?

I would like to display an informational NSPopover that tracks the user's mouse cursor.
For this, I am using an NSTrackingArea to update the popover's positioningRect whenever the mouseMoved event fires.
However, this has two drawbacks:
The popover follows the mouse with a slight delay. How can I reduce this delay to make the popover appear more "glued" to the mouse pointer?
When I move the mouse pointer in the direction of the popover, the tracking area's mouseExited method gets called, which causes the popover to "absorb" the mouse movement events, so that the tracking area's mouseMoved event no longer fires. How can I avoid the popover absorbing the mouse events, or at least keep forwarding these events?
This question is very similar to Any way around this NSTrackingArea quirk?, with the distinction that I am using NSPopover, so I have nothing to set ignoresMouseEvents on.
I took a look at your problem. I was not able to eliminate the lag, but it might reduce if you set popover.animates to false.
Wrong approach:
I was able to solve the mouseExited over popover issue by adding a new border (and shadowless) window on top of the other one. The trackingArea is added to the transparent window, the popover to the original one. Depending on the transparent windows level, it is above the popover and therefore they can't interfere with each other.
In the gif below you can see the results of my tests:
Here is some of my code:
The mouse tracking:
override func mouseMoved(with event: NSEvent) {
let location = self.view.convert(event.locationInWindow, from: nil)
popover.positioningRect.origin.x = location.x
popover.positioningRect.origin.y = location.y
}
The custom window:
transparentWindow.backgroundColor = NSColor.clear
transparentWindow.isOpaque = false
transparentWindow.styleMask = .borderless
transparentWindow.makeKeyAndOrderFront(nil)
Update 11/11/2016:
I just read the question in the link you provided. There is a window to set ignoresMouseEvents on. Even though NSPopover inherits from NSObject, you have a contentViewController, which holds an view object, which holds the popovers window. (as explained here)
So simply set
popover.contentViewController?.view.window?.ignoresMouseEvents = true
after the popover is shown.

NSPopover closes automatically

I have a xib with my view and an NSPopover with Transient behavior:
In the view controller, I have an action to control the popover like this:
#IBAction func moreClicked(sender: NSButton) {
if !moreOpen {
moreOpen = true
scriptsPopover.showRelativeToRect(sender.bounds, ofView: sender, preferredEdge: 2)
} else {
moreOpen = false
scriptsPopover.close()
}
}
When I click my button the popover appears as expected. But after 5 seconds it disappears.
I want the popover to present a number of buttons and only disappear when the user clicks one of those buttons or clicks elsewhere in the UI. Like the Autolayout Pin button in Interface Builder to mention an example.
I tried defining the behavior as Transient, Semi-transient, Application-defined. All have exactly the same result: It dismisses itself after a few seconds.
I tried implementing the popoverShouldClose delegate and returning false to let me control it. It does block the close, but when the user clicks the button to close, it just opens a new popover on top of the old. The popover also loses its arrow after I return false from popoverShouldClose, which looks weird.
Here's a recording of the annoying automatic close
See the stack trace when the popoverShouldClose method is called. You'll see the cause of this in that stack trace, and from there, try to eliminate this cause.
What I'm suspecting is that your popover is being deallocated as it isn't being strongly held by you.

How can I know NSWindow was closed using the close button ?

Is there an easy way to know when a NSWindow was closed using the close button (of the window title bar) .
Using NSWindowDelegate and the windowWillClose:(NSNotification *)notification method I get notification of each closing. But how can I know the closing request was send by the close button
Use [yourWindow standardWindowButton:NSWindowCloseButton] to find the close button, then change its target and action to a custom method.

How to detect that my window is being closed using the red window button?

I have a dialog window that can be cancelled through a custom Cancel button or using the system red window button. I need to perform some simple logic when the dialog is cancelled. How do I detect that the user has pressed the red button?
I know I can detect the window being closed using the -windowWillClose: delegate callback. But this callback is also called when I close the window programmatically after the dialog succeeds. I also know I could simply set up a BOOL flag, but is there a better solution? It would be best if I could detect the red button activation.
Define close button:
NSButton *closeButton = [self standardWindowButton:NSWindowCloseButton];
Connect close button to custom selector:
[closeButton setTarget:self.delegate];
[closeButton setAction:#selector(closeThisWindow)];
Run specific code and close window manually.
-(void)closeThisWindow {
//
// The NSWindowCloseButton has been clicked.
// Code to be run before the window closes.
//
[self close];
}

Stopping a popover window becoming the key window

I am trying to write a Mac application which runs in the menubar, which when clicked, displays an iOS-like popover. Similar to Flexibit's Fantastical, or The Iconfactory's Take Five.
I am using INPopoverController which seems to work great.
My problem is that when the user clicks on anything in the popover view, the popover becomes the key window. Is there a way to stop this from happening?
In INPopoverWindow:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
means that the popover can become a key window. You can either change it to return NO, or subclass INPopoverWindow and override that method to return NO.

Resources