Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the on-screen location of an NSStatusItem

I have a question about the NSStatusItem for cocoa in mac osx. If you look at the mac app called snippets (see the movie at http://snippetsapp.com/). you will see that once you clicked your statusbar icon that a perfectly aligned view / panel or maybe even windows appears just below the icon.

My question is ... How to calculate the position to where to place your NSWindow just like this app does?

I have tried the following:

  1. Subclass NSMenu
  2. Set the view popery for the first item of the menu (Worked but enough)
  3. Using addSubview instead of icon to NSStatusItem this worked but could not get higher then 20px
like image 414
Johnny Mast Avatar asked Aug 19 '09 18:08

Johnny Mast


5 Answers

Give the NSStatusItem a view, then get the frame of that view's window. This technically counts as UndocumentedGoodness, so don't be surprised if it breaks someday (e.g., if they start keeping the window offscreen instead).

I don't know what you mean by “could not get heigher then 20px”.

like image 114
Peter Hosey Avatar answered Nov 20 '22 14:11

Peter Hosey


To do this without the hassle of a custom view, I tried the following (that works). In the method that is set as the action for the status item i.e. the method that is called when the user clicks the status item, the frame of the status item can be retrieved by:

[[[NSApp currentEvent] window] frame]

Works a treat for me

like image 42
Steg Avatar answered Nov 20 '22 14:11

Steg


Given an NSMenuItem and an NSWindow, you can get the point that centers your window right below the menu item like this:

fileprivate var centerBelowMenuItem: CGPoint {
    guard let window = window, let barButton = statusItem.button else { return .zero }
    let rectInWindow = barButton.convert(barButton.bounds, to: nil)
    let screenRect = barButton.window?.convertToScreen(rectInWindow) ?? .zero
    // We now have the menu item rect on the screen.
    // Let's do some basic math to center our window to this point.
    let centerX = screenRect.origin.x-(window.frame.size.width-barButton.bounds.width)/2
    return CGPoint(x: centerX, y: screenRect.origin.y)
}

No need for undocumented API's.

like image 4
Oskar Avatar answered Nov 20 '22 13:11

Oskar


Maybe another solution which works for me (swift 4.1) :

   let yourStatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

   let frameOrigin = yourStatusItem.button?.window?.frame.origin
   let yourPoint = CGPoint(x: (frameOrigin?.x)!, y: (frameOrigin?.y)! - 22)


   yourWindow?.setFrameOrigin(yourPoint)
like image 4
Omddoudou Avatar answered Nov 20 '22 14:11

Omddoudou


It seems that this app uses Matt's MAAttachedWindow. There's an sample application with the same layout & position.

like image 1
Nando Vieira Avatar answered Nov 20 '22 13:11

Nando Vieira