Skip to content

Instantly share code, notes, and snippets.

@stephancasas
Created July 1, 2024 20:59
Show Gist options
  • Save stephancasas/175f5d57b3e66b6f074d4c6ec97ecd2a to your computer and use it in GitHub Desktop.
Save stephancasas/175f5d57b3e66b6f074d4c6ec97ecd2a to your computer and use it in GitHub Desktop.
An extension on NSScreen providing menubar dimensions across various system states.
//
// NSScreen+MenuBarHeights.h
//
//
// Created by Stephan Casas on 6/26/24.
//
@import Foundation;
@import AppKit;
@interface NSScreen (__ScreenMenuBarHeights)
/// A private-API object representing the height of the menu bar in its various states for a particular display.
@property(readonly) id screenMenuBarHeights;
/// The vertical drawing area of the menu bar for this display.
@property(readonly) CGFloat menuBarLayoutHeight;
/// The vertical clearance inset maintained to prevent windows from occluding the menu bar on this display.
@property(readonly) CGFloat menuBarRestingHeight;
@end
//
// NSScreen+MenuBarHeights.m
//
//
// Created by Stephan Casas on 6/30/24.
//
@import ObjectiveC;
#import "NSScreen+MenuBarHeights.h"
@implementation NSScreen (__ScreenMenuBarHeights)
/// A private-API object representing the height of the menubar in its various states for a particular display.
- (id)screenMenuBarHeights {
NSBundle * appKit = [NSBundle bundleWithPath:@"/System/Library/Frameworks/AppKit.framework"];
[appKit load];
NSNumber * displayID = [self valueForKey:@"_displayID"];
id __NSScreenMenuBarHeights = [[appKit classNamed:@"_NSScreenMenuBarHeights"] alloc];
return ((id (*)(id, SEL, CGDirectDisplayID))objc_msgSend)(__NSScreenMenuBarHeights, NSSelectorFromString(@"_initWithDisplayID:"), [displayID intValue]);
}
/// The vertical drawing area of the menubar for this display.
- (CGFloat)menuBarLayoutHeight {
NSNumber * layoutHeight = [[self screenMenuBarHeights] valueForKey:@"layoutHeight"];
return [layoutHeight doubleValue];
}
/// The vertical clearance inset maintained to prevent windows from occluding the menubar on this display.
- (CGFloat)menuBarRestingHeight {
NSNumber * restingHeight = [[self screenMenuBarHeights] valueForKey:@"restingHeight"];
return [restingHeight doubleValue];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment