Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Created Nov 20, 2020
Embed
What would you like to do?
#import "MacApp.h"
@import AppKit;
@implementation MacApp
static CGFloat regularWidth = 960;
static CGFloat compressedWidth = 320;
static CGFloat height = 640;
+ (void)configureApp
{
static dispatch_once_t once;
static MacApp *windowDelegate;
dispatch_once(&once, ^{
windowDelegate = [MacApp new];
});
for (NSWindow *window in NSApplication.sharedApplication.windows) {
[self configureWindow:window withDelegate:windowDelegate];
}
}
+(void)configureWindow:(NSWindow *)window
withDelegate:(NSObject<NSWindowDelegate> *)delegate
{
// Titlebar appearance
[window setTitlebarAppearsTransparent:YES];
[window setTitleVisibility:NSWindowTitleHidden];
// Change fullscreen button to + zoom button
[window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary];
[window setShowsResizeIndicator:YES];
// Optimise resizing
CGFloat widthIncrement = regularWidth - compressedWidth;
[window setContentResizeIncrements:CGSizeMake(widthIncrement, 1)];
// Initial size
CGSize size = [MacApp sizeForExpanded:YES];
CGRect frame = CGRectMake(0, 0, size.width, size.height);
[window setFrame:frame display:YES];
[window center];
// Blur (whole) background for sidebar
NSVisualEffectView *view = [[NSVisualEffectView alloc] initWithFrame:window.screen.frame];
view.blendingMode = NSVisualEffectBlendingModeBehindWindow;
view.state = NSVisualEffectStateActive;
view.material = NSVisualEffectMaterialSidebar;
view.wantsLayer = true;
[window.contentView addSubview:view positioned:NSWindowBelow relativeTo:window.contentView.subviews[0]];
window.backgroundColor = [NSColor clearColor];
window.delegate = delegate;
}
+ (BOOL)isExandedForWidth:(CGFloat)currentWidth
{
return currentWidth > (compressedWidth + regularWidth) / 2;
}
+ (CGSize)sizeForExpanded:(BOOL)expanded
{
CGFloat width = expanded ? regularWidth : compressedWidth;
return CGSizeMake(width, height);
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
BOOL isExpanded = [MacApp isExandedForWidth:frameSize.width];
return [MacApp sizeForExpanded:isExpanded];
}
- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
{
CGPoint origin = window.frame.origin;
BOOL wasExpanded = [MacApp isExandedForWidth:CGRectGetWidth(window.frame)];
CGSize size = [MacApp sizeForExpanded:!wasExpanded];
CGRect frame = CGRectMake(origin.x, origin.y, size.width, size.height);
[window setFrame:frame display:YES animate:NO];
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment