Dragging from and to NSButton - cocoa

I am trying to write an app. that will allow the user to drag from an NSButton to another. When dropped I would like the Title to be dropped onto the button.
I cannot find a way to make NSButton draggable. Is it possible?

First a warning: this sounds like it would be confusing to a user. Buttons are intended to emulate buttons in the real world. You push them rather than drag them around.
Having said that, it's certainly possible to do in a subclass of NSButton.
Drag source
Implement the dragging source method draggingSourceOperationMaskForLocal:. Sounds like you're copying the title, so you may want to use NSDragOperationCopy.
Call dragImage:at:offset:event:pasteboard:source:slideBack: in your mouseDragged: method. Use the drag pBoard ([NSPasteboard pasteboardWithName:NSDragPboard]) and copy your button title into it. For the image you can draw your button into an NSImage and use that as the drag image, or you might use an icon or even just the title.
Drag destination
Register your custom button to be able to accept the title string using registerForDraggedTypes:.
Implement the drag destination methods draggingEntered: and performDragOperation: to return appropriate values. There are several other methods including draggingUpdated: and draggingExited: among others that you can use to provide visual feedback.
There's lots of info out there on implementing drag & drop. Read Apple's docs: Drag & Drop Programming Topics and Pasteboard Programming Guide

Related

How to display elements inside NSCollectionView with various shapes

I am a rookie Cocoa guy. I need to design and implement a view which will show collection of labels on Mac OS using Xamarin. These labels will have a text and color associated with them. When shown inside the view, label should expand till it covers whole text and it will be shown with background and foreground colors.
I have attached the picture of this user control on Windows, you can see that labels inside the StackPanel are expanding till they cover the whole text. Hope this gives better idea about my ask.
The $64,000 question is "are these labels controls?" In other words, do you expect the user to click on these to do something, or are they just for display?
If your answer is "just for display", the solution is super simple: Use an NSTextField and programmatically add attributed text (NSAttributedString) to it. Attributed text attaches display properties to runs of text within the field; properties like "background color".
If you want these to be buttons that you can click on, then things get a lot more complicated.
Since you apparently want the button layout to "flow", you might look into imbedding buttons (well, button cells) into an NSTextField using attachments. This is normally how non-text content (say, an image) can be inserted, but with some fiddling it can actually be anything a control cell can draw. See How to insert a NSButton into a NSTextView? (inline).
Warning: this is not a "rookie" topic and will involve control cells and custom event handling.
If I were doing this, I'd probably just create NSButton objects for each label (choosing an appropriate style/look like NSRecessedBezelStyle), create a custom subclass of NSView to contain them, and then override the layout method to position all of the buttons the way I want.
To be thorough, I'd also override the intrinsic size methods so the whole thing could participate in auto-layout, based on the number and size of buttons it contained.

Add an NSView around cursor?

Simple question, starting out with macOS stuff – I’d like to create a small radial menu around my cursor, top-most, above whatever application is currently active, whenever a specific mouse button is pressed.
I have the specific mouse button over all application down, but I’m wondering where I need to draw that NSView, i.e. “topmost”. I guess on iOS this would be at the UIWindow level, but would NSWindow be the wrong approach here?
this is purely an opinion-based question, but basically if you want to present any custom content on the desktop, that should use NSWindow, and you can customise the window's content for your wish.
NOTE: you can find more information about the NSWindow class in Apple's Class Reference Docs.

Cocoa Image Picker Popover

Several places in OS X (in this example, the Users & Groups pane in System Preferences) have circular image views that allow the user to either drag in an image, like in an editable NSImageView but also allow them to click to show a popover that allows various other choices of image sources.
I have checked the ImageKit framework, but the only thing I found similar is the image taking sheet.
How can I make use of this feature in my own Cocoa applications? I'd imagine it is implemented in some standard framework—but any pointers on implementing something like this would be quite appreciated.
You will have to go down the custom control root as this is not available as a stand alone control.
However you have all the prerequisites.
The circular image view
There a several ways to implement this. You could try using a standard Cocoa button and customise as needed. Although it might just be easier to build from scratch by subclassing NSView. This was you can avoid all the NSCell stuff. I would do the latter.
The popover
Roll your own master-details type view controller to be displayed as the popover's content. In the left have a NSTableView (the master), the right have a NSCollectionView (the details). Below the collection view add some buttons.

Drag and drop within a view?

I've been experimenting with the drag-and-drop support in Cocoa - draggingEntered:withInfo:, draggedImage:beganAt:, etc. It looks like OS X only triggers "drag" events when you drag something out of one view and into another.
I have a very large view which I draw stuff inside, and I'm looking for a way to drag objects within it; the objects never leave the view, so the above messages don't seem to be generated, and no drag starts. Is there a way to do "drag and drop within a view", or do I have to implement it myself?
I'm pretty sure you can't do that with drag and drop. If the things you're trying to drag are objects (like NSBezier paths) you can do a hit test on them and then use mouseDown: and mouseDragged: to implement changing the origin of your object, but it's all up to you.

Programmatically closing an NSWindow when it loses focus

I am making an image picker that will display an n by n grid of selectable button when the picker is popped up. This grid of buttons will be contained within an NSWindow but I would like for the window to be close automatically if the user clicks off the screen. Is there a flag that can be set so that when the window looses focus it will be closed automatically?
There are two notifications that you may be interested in: NSWindowDidResignKeyNotification and NSWindowDidResignMainNotification. You can simply register for the one you're interested in in awakeFromNib (or windowDidLoad if you have a custom controller) and then close or hide the window as appropriate when you receive the notifications.
I won't delve too much into whether or not this is a good idea from UI standpoint. But, it might be a better idea to have either an overlay view or a panel for the functionality you describe.
You might check out NSPanel. It's an NSWindow subclass that will hide itself when the app is in the background, and that behavior sounds very similar to what you are looking for.

Resources