Skip to content

Commit

Permalink
Because we all need to find our center sometimes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyarnold committed Apr 22, 2011
1 parent ee596ae commit 65931e7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Categories/NSWindow+CenterOnScreen.h
@@ -0,0 +1,17 @@
//
// NSWindow+CenterOnScreen.h
// Hyperspaces
//
// Created by Tony Arnold on 8/01/10.
// Copyright 2010 The CocoaBots. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface NSWindow (CenterOnScreen)

- (void)centerWindowOnScreen;
- (void)centerWindowOnScreen:(NSScreen *)screen;

@end
32 changes: 32 additions & 0 deletions Categories/NSWindow+CenterOnScreen.m
@@ -0,0 +1,32 @@
//
// NSWindow+CenterOnScreen.m
// Hyperspaces
//
// Created by Tony Arnold on 8/01/10.
// Copyright 2010 The CocoaBots. All rights reserved.
//

#import "NSWindow+CenterOnScreen.h"


@implementation NSWindow (CenterOnScreen)

- (void)centerWindowOnScreen {
[self centerWindowOnScreen:[self screen]];
}

- (void)centerWindowOnScreen:(NSScreen *)screen {
if (screen == nil) {
screen = [self screen];
}

NSRect windowFrame = [self frame];
NSRect visibleFrame = [screen visibleFrame];

windowFrame.origin.x = visibleFrame.origin.x + (NSWidth(visibleFrame) - NSWidth(windowFrame)) * 0.5f;
windowFrame.origin.y = visibleFrame.origin.y + (NSHeight(visibleFrame) - NSHeight(windowFrame)) * 0.8f;

[self setFrameOrigin:windowFrame.origin];
}

@end

0 comments on commit 65931e7

Please sign in to comment.