Permalink
Browse files

better screen detection. Improved preview quality

  • Loading branch information...
1 parent b6e799d commit eed8d0a713282a2405957e700726d1aeaa4ad791 @oscardelben committed Sep 15, 2011
@@ -12,7 +12,6 @@
555BB72D141DE18A00AE82A9 /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 555BB72C141DE18A00AE82A9 /* Sparkle.framework */; };
555BB730141DE1B700AE82A9 /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 555BB72C141DE18A00AE82A9 /* Sparkle.framework */; };
555BB733141DE5E500AE82A9 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 555BB732141DE5E500AE82A9 /* dsa_pub.pem */; };
- 55A8833714151D5D0076BB4A /* NSScreen+PointConversion.m in Sources */ = {isa = PBXBuildFile; fileRef = 55A8833614151D5D0076BB4A /* NSScreen+PointConversion.m */; };
55A8833A1415357E0076BB4A /* ColorPicker_menubar.png in Resources */ = {isa = PBXBuildFile; fileRef = 55A883391415357E0076BB4A /* ColorPicker_menubar.png */; };
55A8833C141539E10076BB4A /* APPL.icns in Resources */ = {isa = PBXBuildFile; fileRef = 55A8833B141539E10076BB4A /* APPL.icns */; };
55A8833E14153BCA0076BB4A /* close.png in Resources */ = {isa = PBXBuildFile; fileRef = 55A8833D14153BCA0076BB4A /* close.png */; };
@@ -67,8 +66,6 @@
555BB72C141DE18A00AE82A9 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = "<group>"; };
555BB732141DE5E500AE82A9 /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
558D25E61417BB69004031C3 /* Color Picker Pro.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = "Color Picker Pro.entitlements"; sourceTree = "<group>"; };
- 55A8833514151D5D0076BB4A /* NSScreen+PointConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSScreen+PointConversion.h"; sourceTree = "<group>"; };
- 55A8833614151D5D0076BB4A /* NSScreen+PointConversion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSScreen+PointConversion.m"; sourceTree = "<group>"; };
55A883391415357E0076BB4A /* ColorPicker_menubar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ColorPicker_menubar.png; sourceTree = "<group>"; };
55A8833B141539E10076BB4A /* APPL.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = APPL.icns; sourceTree = "<group>"; };
55A8833D14153BCA0076BB4A /* close.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = close.png; sourceTree = "<group>"; };
@@ -244,8 +241,6 @@
55C9F189140236EA00321C9E /* RSLoginItems.m */,
550A63CD1406567B00864125 /* NSColorFormatter.h */,
550A63CE1406567B00864125 /* NSColorFormatter.m */,
- 55A8833514151D5D0076BB4A /* NSScreen+PointConversion.h */,
- 55A8833614151D5D0076BB4A /* NSScreen+PointConversion.m */,
55A8833F1415F8EE0076BB4A /* PreferencesController.h */,
55A883401415F8EE0076BB4A /* PreferencesController.m */,
55A8834E14162EF80076BB4A /* HelpController.h */,
@@ -370,7 +365,6 @@
55C9F1A81402E00E00321C9E /* ColorHistoryView.m in Sources */,
55C9F1B61402F29800321C9E /* DDHotKeyCenter.m in Sources */,
550A63CF1406567B00864125 /* NSColorFormatter.m in Sources */,
- 55A8833714151D5D0076BB4A /* NSScreen+PointConversion.m in Sources */,
55A883421415F8EF0076BB4A /* PreferencesController.m in Sources */,
55A8835014162EF80076BB4A /* HelpController.m in Sources */,
5510B8FE141E4EF3005FD2CA /* EventsResponderView.m in Sources */,
@@ -3,7 +3,6 @@
#import "RSLoginItems.h"
#import "DDHotKeyCenter.h"
#import "NSColorFormatter.h"
-#import "NSScreen+PointConversion.h"
#import "PreferencesController.h"
#import "HelpController.h"
#import "EventsResponderView.h"
@@ -104,12 +103,11 @@ - (void)toggleShowWindow
- (void)updateViews
{
NSPoint mouseLocation = [NSEvent mouseLocation];
-
- NSScreen *screen = [NSScreen currentScreenForMouseLocation];
- NSPoint normalizedPoint = [screen flipPoint:[screen convertPointToScreenCoordinates:mouseLocation]];
-
- statusItemView.mouseLocation = normalizedPoint;
- viewController.mouseLocation = normalizedPoint;
+ NSScreen *principalScreen = [[NSScreen screens] objectAtIndex:0];
+ mouseLocation = NSMakePoint(mouseLocation.x, principalScreen.frame.size.height - mouseLocation.y);
+
+ statusItemView.mouseLocation = mouseLocation;
+ viewController.mouseLocation = mouseLocation;
[statusItemView setNeedsDisplay:YES];
[viewController updateView];
View
@@ -7,50 +7,19 @@
//
#import "ColorPicker.h"
-#import "NSScreen+PointConversion.h"
#define kWidth 28
#define kHeight 28
@implementation ColorPicker
-#pragma mark Utils
-
-+ (CGImageRef)getImageRefForScreen:(NSScreen *)aScreen forRect:(CGRect)aRect
-{
- NSNumber *screenNumber = [[aScreen deviceDescription] objectForKey:@"NSScreenNumber"];
- CGDirectDisplayID displayID = (CGDirectDisplayID) [screenNumber pointerValue];
-
- return CGDisplayCreateImageForRect(displayID, aRect);
-}
-
-+(CGImageRef)getImageRefForRect:(CGRect)aRect
-{
- CGImageRef imageRef = [self getImageRefForScreen:[NSScreen currentScreenForMouseLocation] forRect:aRect];
-
- if (imageRef == NULL) {
- NSLog(@"Warning: imageRef returned NULL. DisplayID was probably invalid. Using the default screen as fallback.");
-
- imageRef = [self getImageRefForScreen:[NSScreen mainScreen] forRect:aRect];
-
- // Check if NULL again
- if (imageRef == NULL) {
- NSLog(@"Warning: imageRef NULL after using the default monitor. Returning");
- return nil;
- }
- }
-
- return imageRef;
-}
-
-
#pragma mark -
-+ (NSImage *)imageForLocation:(NSPoint)mouseLocation
++ (NSImage *)imageForLocation:(NSPoint)mouseLocation;
{
- CGRect imageRect = CGRectMake(fabs(mouseLocation.x) - kWidth / 2, fabs(mouseLocation.y) - kHeight / 2, kWidth, kHeight);
+ CGRect imageRect = CGRectMake(mouseLocation.x - kWidth / 2, mouseLocation.y - kHeight / 2, kWidth, kHeight);
- CGImageRef imageRef = [self getImageRefForRect:imageRect];
+ CGImageRef imageRef = CGWindowListCreateImage(imageRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageShouldBeOpaque);
NSImage *image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(kWidth, kHeight)];
@@ -62,9 +31,9 @@ + (NSImage *)imageForLocation:(NSPoint)mouseLocation
+ (NSColor *)colorAtLocation:(NSPoint)mouseLocation
{
- CGRect imageRect = CGRectMake(fabs(mouseLocation.x), fabs(mouseLocation.y), 1, 1);
+ CGRect imageRect = CGRectMake(mouseLocation.x, mouseLocation.y, 1, 1);
- CGImageRef imageRef = [self getImageRefForRect:imageRect];
+ CGImageRef imageRef = CGWindowListCreateImage(imageRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
@@ -1,28 +0,0 @@
-//
-// NSScreen+PointConversion.h
-// ColorPicker
-//
-// Created by Oscar Del Ben on 9/5/11.
-// Copyright 2011 DibiStore. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-
-@interface NSScreen (PointConversion)
-
-/*
- Returns the screen where the mouse resides
-*/
-+ (NSScreen *)currentScreenForMouseLocation;
-
-/*
- Allows you to convert a point from global coordinates to the current screen coordinates.
-*/
-- (NSPoint)convertPointToScreenCoordinates:(NSPoint)aPoint;
-
-/*
- Allows to flip the point coordinates, so y is 0 at the top instead of the bottom. x remains the same
-*/
-- (NSPoint)flipPoint:(NSPoint)aPoint;
-
-@end
@@ -1,38 +0,0 @@
-//
-// NSScreen+PointConversion.m
-// ColorPicker
-//
-// Created by Oscar Del Ben on 9/5/11.
-// Copyright 2011 DibiStore. All rights reserved.
-//
-
-#import "NSScreen+PointConversion.h"
-
-@implementation NSScreen (PointConversion)
-
-+ (NSScreen *)currentScreenForMouseLocation
-{
- NSPoint mouseLocation = [NSEvent mouseLocation];
-
- NSEnumerator *screenEnumerator = [[NSScreen screens] objectEnumerator];
- NSScreen *screen;
- while ((screen = [screenEnumerator nextObject]) && !NSMouseInRect(mouseLocation, screen.frame, NO))
- ;
-
- return screen;
-}
-
-- (NSPoint)convertPointToScreenCoordinates:(NSPoint)aPoint
-{
- float normalizedX = fabs(fabs(self.frame.origin.x) - fabs(aPoint.x));
- float normalizedY = aPoint.y - self.frame.origin.y;
-
- return NSMakePoint(normalizedX, normalizedY);
-}
-
-- (NSPoint)flipPoint:(NSPoint)aPoint
-{
- return NSMakePoint(aPoint.x, self.frame.size.height - aPoint.y);
-}
-
-@end

0 comments on commit eed8d0a

Please sign in to comment.