NSColorPanel not showing up after the first time

57 Views Asked by At

I have a statusBarItem that I want to use to show the NSColorPanel.

enter image description here

The following code is called when the button is tapped, which activates the ColorPanel.

@IBAction func showColour( button:NSButton ){
    DispatchQueue.main.async {
        NSApplication.shared.orderFrontColorPanel(self)
        print( "panel frame:\(NSColorPanel.shared.frame), is visible:\(NSColorPanel.shared.isVisible )" )
    }
}

If the user presses the close button to close the colour panel, tapping the button to show the panel works as expected and displays the panel again. However, if the user clicks in an empty space, the colorPanel disappears, and tapping the button to reopen the panel does not work any more. However, based on the logs, it appears that the panel has a valid rect and is visible.

Any Suggestions?
Thanks
Reza

1

There are 1 best solutions below

0
reza23 On

As it was pointed out by a user on Reddit, when tapping in an empty space the app will go to the background, and is no longer the foreground application, so a utility-style window like the color panel will not be displayed. Hence as the app is not active, subsequent taps to show the colour panel, will not bring the utility window to the front.

So the solution is to make sure the app is active prior to trying to open the colour panel by calling:

NSApplication.shared.activate(ignoringOtherApps: true )

in the showColour function