Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BFPopoverColorWell sets itself as cell for popover before showing #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BFColorPickerPopover/BFPopoverColorWell.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

@interface BFPopoverColorWell : NSColorWell <NSPopoverDelegate>

+ (void)deactivateAll;

@property (nonatomic) NSRectEdge preferredEdgeForPopover;
@property (nonatomic) BOOL useColorPanelIfAvailable;

Expand Down
104 changes: 96 additions & 8 deletions BFColorPickerPopover/BFPopoverColorWell.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "BFPopoverColorWell.h"
#import "BFColorPickerPopover.h"
#import "NSColorPanel+BFColorPickerPopover.h"
#import "NSColorWell+BFColorPickerPopover.h"

@interface BFColorPickerPopover ()
@property (nonatomic) NSColorPanel *colorPanel;
Expand All @@ -43,10 +42,43 @@ @interface BFPopoverColorWell ()
@property (nonatomic, readwrite) BOOL isActive;
@end

@interface NSColorWell (BFColorPickerPopover)

- (void)popoverDidClose:(NSNotification *)notification;
- (void)_performActivationClickWithShiftDown:(BOOL)shift;

@end

@implementation BFPopoverColorWell

static NSColorWell *hiddenWell = nil;

+ (void)deactivateAll {
[[NSColorPanel sharedColorPanel] disablePanel];
hiddenWell = [[NSColorWell alloc] init];
hiddenWell.color = [NSColor colorWithCalibratedRed:1/255.0 green:2/255.0 blue:3/255.0 alpha:1];
[hiddenWell activate:YES];
[hiddenWell deactivate];
[[NSColorPanel sharedColorPanel] enablePanel];
}

- (void)setup {
self.preferredEdgeForPopover = NSMaxXEdge;
if (@available(macOS 13.0, *)) {
// this will show a small popover in macOS 13.0
#if !defined(MAC_OS_VERSION_13_0) || MAC_OS_VERSION_13_0 > MAC_OS_X_VERSION_MAX_ALLOWED
typedef NS_ENUM(NSInteger, NSColorWellStyle) {
NSColorWellStyleDefault = 0, /// The default `colorWellStyle`. A well that accepts drag/drop of colors as well as reveals the color panel when clicked.
NSColorWellStyleMinimal, /// A minimally adorned well. By default shows a popover color picker when clicked; this interaction behavior can be customized.
NSColorWellStyleExpanded, /// An expanded well with a dedicated button for revealing the color panel. By default, clicking the well will show a popover color picker; this interaction behavior can be customized.
};
[self setValue:@(NSColorWellStyleExpanded) forKey:@"colorWellStyle"];
#else
self.colorWellStyle = NSColorWellStyleExpanded;
#endif
return;
}

self.preferredEdgeForPopover = NSMaxXEdge;
self.useColorPanelIfAvailable = YES;
}

Expand All @@ -69,14 +101,20 @@ - (id)initWithFrame:(NSRect)frame
}

- (void)activateWithPopover {
if (self.isActive) return;
if (@available(macOS 13.0, *)) {
return;
}

if (self.isActive) {
return;
}

// Setup and show the popover.
self.popover = [BFColorPickerPopover sharedPopover];
self.popover.delegate = self;
self.popover.color = self.color;
[self.popover showRelativeToRect:self.frame ofView:self.superview preferredEdge:self.preferredEdgeForPopover];
self.popover.colorWell = self;
[self.popover showRelativeToRect:self.frame ofView:self.superview preferredEdge:self.preferredEdgeForPopover];

// Disable the shared color panel, while the NSColorWell implementation is executed.
// This is done by overriding the orderFront: method of NSColorPanel in a category.
Expand All @@ -88,8 +126,15 @@ - (void)activateWithPopover {
}

- (void)activate:(BOOL)exclusive {
if (self.isActive) return;

if (@available(macOS 13.0, *)) {
[super activate:exclusive];
return;
}

if (self.isActive) {
return;
}

if (self.useColorPanelIfAvailable && [NSColorPanel sharedColorPanelExists] && [[NSColorPanel sharedColorPanel] isVisible]) {
[super activate:exclusive];
self.isActive = YES;
Expand All @@ -99,7 +144,15 @@ - (void)activate:(BOOL)exclusive {
}

- (void)deactivate {
if (!self.isActive) return;
if (@available(macOS 13.0, *)) {
[super deactivate];
return;
}

if (!self.isActive) {
return;
}

[super deactivate];
self.popover.colorWell = nil;
self.popover.delegate = nil;
Expand All @@ -109,7 +162,12 @@ - (void)deactivate {

// Force using a popover (even if useColorPanelIfAvailable = YES), when the user double clicks the well.
- (void)mouseDown:(NSEvent *)theEvent {
if([theEvent clickCount] == 2 && [NSColorPanel sharedColorPanelExists] && [[NSColorPanel sharedColorPanel] isVisible]) {
if (@available(macOS 13.0, *)) {
[super mouseDown:theEvent];
return;
}

if([theEvent clickCount] == 2 && [NSColorPanel sharedColorPanelExists] && [[NSColorPanel sharedColorPanel] isVisible]) {
[self deactivate];
[self activateWithPopover];
} else {
Expand All @@ -120,7 +178,37 @@ - (void)mouseDown:(NSEvent *)theEvent {

- (void)popoverDidClose:(NSNotification *)notification
{
if ([super respondsToSelector:@selector(popoverDidClose:)]) {
[super popoverDidClose:notification];
return;
}

[self deactivate];
}

- (void)_performActivationClickWithShiftDown:(BOOL)shift {
if (@available(macOS 13.0, *)) {
if ([super respondsToSelector:@selector(_performActivationClickWithShiftDown:)]) {
[super _performActivationClickWithShiftDown:shift];
}

return;
}

if (!self.isActive) {
BFColorPickerPopover *popover = [BFColorPickerPopover sharedPopover];
if (popover.isShown) {
BOOL animatesBackup = popover.animates;
popover.animates = NO;
[popover close];
popover.animates = animatesBackup;
}
[BFColorPickerPopover sharedPopover].target = nil;
[BFColorPickerPopover sharedPopover].action = NULL;
[self activate:!shift];
} else {
[self deactivate];
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
@interface NSColor (BFColorPickerPopover)

- (CGColorRef)copyCGColor;
+ (NSColor *)randomColor;
+ (NSColor *)bfcp_randomColor;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (CGColorRef)copyCGColor
return theColor;
}

+ (NSColor *)randomColor {
+ (NSColor *)bfcp_randomColor {
return [NSColor colorWithCalibratedRed:((CGFloat)arc4random()/UINT32_MAX) green:((CGFloat)arc4random()/UINT32_MAX) blue:((CGFloat)arc4random()/UINT32_MAX) alpha:1.0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ - (void)enablePanel {
}

- (void)orderFront:(id)sender {
if (colorPanelEnabled) {
if (@available(macOS 13.0, *)) {
[super orderFront:sender];
return;
}

if (colorPanelEnabled) {
NSColorPanel *panel = [BFColorPickerPopover sharedPopover].colorPanel;
if (panel) {
self.contentView = panel.contentView;
Expand Down
39 changes: 0 additions & 39 deletions BFColorPickerPopover/Extensions/NSColorWell+BFColorPickerPopover.h

This file was deleted.

69 changes: 0 additions & 69 deletions BFColorPickerPopover/Extensions/NSColorWell+BFColorPickerPopover.m

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

#import "BFColorPickerViewController.h"
#import "BFIconTabBar.h"
#import "NSColorWell+BFColorPickerPopover.h"
#import "BFColorPickerPopoverView.h"
#import "BFPopoverColorWell.h"


#define kColorPickerViewControllerTabbarHeight 30.0f

Expand All @@ -53,7 +54,7 @@ - (void)loadView {
// If the shared color panel is visible, close it, because we need to steal its views.
if ([NSColorPanel sharedColorPanelExists] && [[NSColorPanel sharedColorPanel] isVisible]) {
[[NSColorPanel sharedColorPanel] orderOut:self];
[NSColorWell deactivateAll];
[BFPopoverColorWell deactivateAll];
}

self.colorPanel = [NSColorPanel sharedColorPanel];
Expand Down
6 changes: 5 additions & 1 deletion BFColorPickerPopover/Helper Classes/BFIconTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ - (void)drawRect:(NSRect)dirtyRect

CGRect fromRect = CGRectMake(0.0f, 0.0f, embossedImage.size.width, embossedImage.size.height);
CGPoint position = CGPointMake(round(center.x - embossedImage.size.width / 2.0f), round(center.y - embossedImage.size.height / 2.0f));
[embossedImage drawAtPoint:position fromRect:fromRect operation:NSCompositeSourceOver fraction:1.0f];

#if !defined(MAC_OS_X_VERSION_10_12) || MAC_OS_X_VERSION_10_12 > MAC_OS_X_VERSION_MIN_REQUIRED
#define NSCompositingOperationSourceOver NSCompositeSourceOver
#endif
[embossedImage drawAtPoint:position fromRect:fromRect operation:NSCompositingOperationSourceOver fraction:1.0f];
}


Expand Down
2 changes: 1 addition & 1 deletion Demo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (void)awakeFromNib {
// [[BFColorPickerPopover sharedPopover] setAnimates:NO];

for (NSColorWell *well in @[colorWell1, colorWell2, colorWell3, colorWell4, colorWell5, colorWell6, colorWell7, colorWell8])
well.color = [NSColor randomColor];
well.color = [NSColor bfcp_randomColor];
}
- (IBAction)buttonClicked:(id)sender {

Expand Down