Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Position, Width and Height of Mac OS X Dock? Cocoa/Carbon/C++/Qt

Tags:

c++

macos

qt

dock

I'm trying to get position and width of Mac OS X Dock in my C++/Qt app. But I can only find ways to get Available space of Desktop, it means I can get Dock height, but not width.

Is there ways to get Dock position and width using native OS API?

like image 991
IGHOR Avatar asked Mar 06 '16 11:03

IGHOR


People also ask

How do I change the position of the Dock on my Mac?

On your Mac, choose Apple menu > System Preferences, then click Dock & Menu Bar . In the Dock & Menu Bar section in the sidebar, change the options you want. For example, you can change how items appear in the Dock, adjust its size, locate it along the left or right edge of the screen, or even hide it.

How do I change the Dock size on my Mac?

Drag the slider to change the Dock size. Magnify icons when you move the pointer over them. Drag the slider to choose the magnification size.

What is the vertical line on Mac Dock?

Adding Folders to the Dock A vertical line separates the app icons on the left side of the Dock from folders, the Trash and minimized windows on the right side of the Dock.

What are the 3 sections of the Mac Dock?

Today's Dock is split into three sections, marked by dividing lines: Handoff, apps, and a third area that contains documents, folders, and the Trash.


2 Answers

This might help in a hack-free solution, NSScreen provides a method (visibleframe) which subtracts the menu and the Dock from the screen size. The frame method contains both.

[NSStatusBar system​Status​Bar].thickness will return the height of the Menu bar.

https://developer.apple.com/reference/appkit/nsscreen/1388369-visibleframe?language=objc

like image 191
MacAndor Avatar answered Oct 27 '22 05:10

MacAndor


To expand upon MacAndor's answer, you can infer the dock position by comparing the -[NSScreen visibleFrame] (which excludes the space occupied by the dock and the menu bar) with the -[NSScreen frame] which encompasses the entire screen width and height.

The example code below is dependent on the screen the window resides on. This code can be adapted to work with multiple displays by enumerating through all screens instead of using the window's screen.

// Infer the dock position (left, bottom, right)
NSScreen *screen = [self.window screen];    
NSRect visibleFrame = [screen visibleFrame];
NSRect screenFrame = screen.frame;

if (visibleFrame.origin.x > screenFrame.origin.x) {
    NSLog(@"Dock is positioned on the LEFT");
} else if (visibleFrame.origin.y > screenFrame.origin.y) {
    NSLog(@"Dock is positioned on the BOTTOM");
} else if (visibleFrame.size.width < screenFrame.size.width) {
    NSLog(@"Dock is positioned on the RIGHT");
} else {
    NSLog(@"Dock is HIDDEN");
}
like image 26
Andrew Avatar answered Oct 27 '22 04:10

Andrew