Skip to content
Snippets Groups Projects
Commit 193ddd7a authored by George Nachman's avatar George Nachman
Browse files

Fix drag/drop for CPKColorWell

parent b563ba98
No related branches found
No related tags found
No related merge requests found
Showing with 132 additions and 186 deletions
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ColorPicker/CPKSliderView.m"
timestampString = "464294069.626344"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
landmarkName = "-setSelectedValue:"
landmarkType = "5">
<Locations>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[CPKSliderView setSelectedValue:]"
moduleName = "ColorPicker"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/georgen/iTerm2/ColorPicker/ColorPicker/CPKSliderView.m"
timestampString = "464294177.641786"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
offsetFromSymbolStart = "24">
</Location>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[CPKSliderView setSelectedValue:]"
moduleName = "ColorPicker"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/georgen/iTerm2/ColorPicker/ColorPicker/CPKSliderView.m"
timestampString = "464294177.642019"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
offsetFromSymbolStart = "115">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ColorPicker/CPKMainViewController.m"
timestampString = "466468610.039889"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "183"
endingLineNumber = "183"
landmarkName = "-colorPanelDidClose:"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ColorPicker/CPKMainViewController.m"
timestampString = "466468610.039889"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "169"
endingLineNumber = "169"
landmarkName = "-setColorPanel:"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A6184EC71BA874190088EF3C"
BuildableName = "ColorPickerDemo.app"
BlueprintName = "ColorPickerDemo"
ReferencedContainer = "container:ColorPicker.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A6184EC71BA874190088EF3C"
BuildableName = "ColorPickerDemo.app"
BlueprintName = "ColorPickerDemo"
ReferencedContainer = "container:ColorPicker.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A6184EC71BA874190088EF3C"
BuildableName = "ColorPickerDemo.app"
BlueprintName = "ColorPickerDemo"
ReferencedContainer = "container:ColorPicker.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A6184EC71BA874190088EF3C"
BuildableName = "ColorPickerDemo.app"
BlueprintName = "ColorPickerDemo"
ReferencedContainer = "container:ColorPicker.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Loading
Loading
@@ -5,9 +5,11 @@
@protocol CPKColorWellViewDelegate
@property(nonatomic, readonly) void (^willOpenPopover)();
@property(nonatomic, readonly) void (^willClosePopover)();
- (NSRect)presentationRect;
- (NSView *)presentingView;
- (BOOL)isContinuous;
- (void)colorChangedByDrag:(NSColor *)color;
@end
 
/** This really should be an NSCell. It provides the implementation of the CPKColorWell. */
Loading
Loading
@@ -26,7 +28,7 @@
 
@end
 
@interface CPKColorWellView() <NSPopoverDelegate>
@interface CPKColorWellView() <NSDraggingDestination, NSDraggingSource, NSPopoverDelegate>
@property(nonatomic) BOOL open;
@property(nonatomic) CPKPopover *popover;
@property(nonatomic, copy) void (^willClosePopover)(NSColor *);
Loading
Loading
@@ -41,6 +43,26 @@
 
@implementation CPKColorWellView
 
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
[self colorWellViewCommonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self colorWellViewCommonInit];
}
return self;
}
- (void)colorWellViewCommonInit {
[self registerForDraggedTypes:@[ NSPasteboardTypeColor ]];
}
- (void)awakeFromNib {
self.cornerRadius = 3;
self.open = NO;
Loading
Loading
@@ -48,12 +70,92 @@
 
// Use mouseDown so this will work in a NSTableView.
- (void)mouseDown:(NSEvent *)theEvent {
if (!_disabled && !self.open && theEvent.clickCount == 1) {
if (!self.delegate.isContinuous && theEvent.clickCount == 1) {
[self openPopOver];
}
}
- (void)mouseUp:(NSEvent *)theEvent {
if (self.delegate.isContinuous && theEvent.clickCount == 1) {
[self openPopOver];
}
}
- (void)openPopOver {
if (!_disabled && !self.open) {
[self openPopOverRelativeToRect:_delegate.presentationRect
ofView:_delegate.presentingView];
}
}
 
- (void)mouseDragged:(NSEvent *)theEvent {
NSColor *color = self.selectedColor ?: [NSColor clearColor];
NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:color];
dragItem.imageComponentsProvider = ^NSArray<NSDraggingImageComponent *> *(void) {
NSDraggingImageComponent *imageComponent =
[NSDraggingImageComponent draggingImageComponentWithKey:NSDraggingImageComponentIconKey];
NSImage *snapshot = [[NSImage alloc] initWithSize:self.frame.size];
[snapshot lockFocus];
[self drawRect:self.bounds];
[snapshot unlockFocus];
imageComponent.contents = snapshot;
imageComponent.frame = self.bounds;
return @[ imageComponent ];
};
[self beginDraggingSessionWithItems:@[ dragItem ]
event:theEvent
source:self];
}
#pragma mark - NSDraggingDestination
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
__block NSDragOperation operation = NSDragOperationNone;
[sender enumerateDraggingItemsWithOptions:0
forView:self
classes:@[ [NSColor class] ]
searchOptions:@{}
usingBlock:^(NSDraggingItem * _Nonnull draggingItem, NSInteger idx, BOOL * _Nonnull stop) {
NSColor *color = draggingItem.item;
if (color) {
operation = NSDragOperationGeneric;
*stop = YES;
}
}];
return operation;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
__block BOOL ok = NO;
[sender enumerateDraggingItemsWithOptions:0
forView:self
classes:@[ [NSColor class] ]
searchOptions:@{}
usingBlock:^(NSDraggingItem * _Nonnull draggingItem, NSInteger idx, BOOL * _Nonnull stop) {
NSColor *color = draggingItem.item;
if (color) {
self.popover.selectedColor = color;
self.selectedColor = color;
[self.delegate colorChangedByDrag:color];
ok = YES;
*stop = YES;
}
}];
return ok;
}
#pragma mark - NSDraggingSource
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
return NSDragOperationGeneric;
}
- (void)openPopOverRelativeToRect:(NSRect)presentationRect ofView:(NSView *)presentingView {
__weak __typeof(self) weakSelf = self;
self.selectedColor = self.color;
Loading
Loading
@@ -193,4 +295,9 @@
return self;
}
 
- (void)colorChangedByDrag:(NSColor *)color {
[self setColor:color];
[self sendAction:self.action to:self.target];
}
@end
Loading
Loading
@@ -25,4 +25,7 @@
color:(NSColor *)color
alphaAllowed:(BOOL)alphaAllowed;
 
// Changes the selected color.
- (void)selectColor:(NSColor *)color;
@end
Loading
Loading
@@ -58,10 +58,7 @@ static const CGFloat kBottomMargin = 8;
__weak __typeof(self) weakSelf = self;
self.rgbView = [[CPKRGBView alloc] initWithFrame:self.view.bounds
block: ^(NSColor *color) {
weakSelf.selectedColor = color;
weakSelf.controlsView.swatchColor = color;
_block(color);
[weakSelf.favoritesView selectColor:color];
[weakSelf selectColor:color];
}
color:_selectedColor
alphaAllowed:self.alphaAllowed];
Loading
Loading
@@ -184,4 +181,11 @@ static const CGFloat kBottomMargin = 8;
self.colorPanel = nil;
}
 
- (void)selectColor:(NSColor *)color {
self.selectedColor = color;
self.controlsView.swatchColor = color;
_block(color);
[self.favoritesView selectColor:color];
}
@end
Loading
Loading
@@ -8,8 +8,8 @@
*/
@interface CPKPopover : NSPopover
 
/** Reflects the final selected color. */
@property(nonatomic, readonly) NSColor *selectedColor;
/** Reflects the final selected color. Setter changes color in open popover. */
@property(nonatomic) NSColor *selectedColor;
 
/** Called before popover closes. */
@property(nonatomic, copy) void (^willClose)();
Loading
Loading
Loading
Loading
@@ -32,6 +32,10 @@
return self.mainViewController.selectedColor;
}
 
- (void)setSelectedColor:(NSColor *)selectedColor {
[self.mainViewController selectColor:selectedColor];
}
#pragma mark - NSPopoverDelegate
 
- (void)popoverWillClose:(NSNotification *)notification {
Loading
Loading
Loading
Loading
@@ -4,6 +4,7 @@
@interface AppDelegate ()
 
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSColorWell *nativeColorWell;
@property (nonatomic) CPKMainViewController *viewController;
@end
 
Loading
Loading
Loading
Loading
@@ -16,6 +16,7 @@
<connections>
<outlet property="colorWell" destination="MuN-WJ-BSh" id="4ny-mc-o9u"/>
<outlet property="loremIpsum" destination="q6V-rV-d7b" id="IKg-5n-F76"/>
<outlet property="nativeColorWell" destination="shh-pD-JOT" id="sEJ-eC-EbT"/>
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
</connections>
</customObject>
Loading
Loading
@@ -701,6 +702,10 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<colorWell fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="shh-pD-JOT">
<rect key="frame" x="234" y="212" width="44" height="23"/>
<color key="color" red="0.05813049898" green="0.055541899059999997" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</colorWell>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vGh-Vt-M6V" customClass="CPKColorWell">
<rect key="frame" x="397" y="313" width="53" height="27"/>
<userDefinedRuntimeAttributes>
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment