Custom NSCursor is being reset when subviews are added to a view in the window - macos

I'm using NSTrackingArea to define 2 areas in a NSView subclass. Then I'm using mouseEntered/mouseExited to change the cursor to a custom one.
So all works fine when the mouse enters the top tracking area and the custom cursor gets set as expected. All still good when I mouseDown and drag on the top tracking area. But I have another part of the UI that updates when the mouse is dragged and it adds subviews to a view elsewhere on the same window.
As soon as the first subview is added elsewhere, my custom cursor disappears and it reverts back to the arrow cursor. I thought I might be able to force the cursor back to the custom one using cursorUpdate for my view but for some reason it never gets called, even when set as an option in the NSTrackingArea.
Am a bit stumped with this one...

I solved it by overriding cursorUpdate in the window's custom contentView. An empty cursorUpdate method stopped the update from getting passed up the chain and the custom cursor now remains as I've set it.

Related

How to change NSCursor globally?

I am developing an application with several windows and views (NSTextView, etc.).
I would like to change the cursor globally in my application, so that even if the cursor enters the trackingRect of (for instance) an NSTextView it does not change.
Is this possible?
PS: I need also to revert back to the usual behaviour.
Yes this is possible. I had an almost similar requirement, and here is how I solved it:
When creating a window, create a transparent view with the same frame as that of the window. Then add a cursor rect to the view (that extends to the latter's bounds) for your specific cursor. Finally add it as the last subview of the window's contentView, so that it acts as an overlay. When this overlay is present, cursor rects of underlying views are not activated.
See https://stackoverflow.com/a/43886799/7908996 for detailed instructions and working code (read the window's contentView instead of WebView). It also describes how to revert back to the usual behaviour.
Hope that helps! :)

How to call NSScrollView autoscroll-method programmatically

I have simple chat application with text messages view-based NSTableView as you can see at the picture below.
Each message contains NSTextView instance having height to fit all the text.
All I need is to start NSScrollView (which NSTableView-instance is enclosed by) autoscrolling while the user selecting text dragging mouse far enough. Unfortunately, autoscrolling doesn't appear. In case of dragging somewhere outside of the text views all succeed.
I tried to call autoscroll:-method directly by simply push NSEvent-instance from NSTextView-subclass "mouse dragged"-event (like in example from this article):
- (void)mouseDragged:(NSEvent *)event
{
[self.scrollView autoscroll:event];
}
As I've overrode all the mouse events and implemented all the text selecting, this method often invokes. But the autoscrolling doesn't seem to work.
UPDATE
I figured out that before calling -autoscroll:-method there must be -mouseDown: of the same object. But it breaks my text selecting mechanism. The point even not in being first responder, there must be nothing but the mouseDown:-method.
Normally, a text view is within a scroll view of its own. Even if that's big enough to show all of the text without scrolling, it's still there. A call of -autoscroll: on anything within that scroll view (possibly including that scroll view itself?) will just try to scroll that scroll view, not the scroll view that contains the table view.
Try calling -autoscroll: on a view higher up in the hierarchy. Either self.scrollView.superview, the table cell view, or the table view.
Note, though, that the table view's scroll view will keep scrolling even after the cell view containing the text view is fully on-screen. In fact, it may keep scrolling it so far that it's off the screen in the other direction. Basically, it doesn't know that you're trying to select within the text view so it doesn't know to stop when the selection extends all the way to the edge of the text view.
Another approach might be to try to use a "bare" text view with no enclosing scroll view. I don't think IB will let you do that, so you'd have to do it programmatically. Bare text views don't play well with auto layout, though.

NSTextField dragging values

I am trying to create a text field in the Mac OSX environment that allows a user to select a number and drag horizontally to adjust that number up and down.
I know this can be done because Apple have implemented it in the inspector panel of the Sprite Kit emitter section: see the image
I have tried sub classing NSTextField to capture mouse drag events and doing the math, but they don't seem to get passed through. The mouse down event does though...
I have also tried placing a dummy view over the top and catching events as they come through. This works for mouse down, but again, the mouse dragged is never called. I know that the view can receive the mouse dragged event though, because if I place that view somewhere that a text field isn't under it, everything works fine.
What are my options here? I feel I have tried everything and the only option left is to create a new control from scratch. I don't mind that too much, but I also want the user to be able to add equations in the text field and drag individual values - that means the full functionality of NSTextField will need to be rebuilt just to add this dragging feature... Is there anything else???

How do I do something after the next NSView -layout has occurred?

In response to a user event, I want to:
add a new NSView to the window, and then
show an NSPanel positioned just below that view
I have each half of this done. I can add a new subview, and the container view's -updateConstraints identifies it and adds the correct layout constraints, so that the next time layout is performed, it's positioned correctly in the window. Also, I have a NSWindowController subclass that puts the panel on the screen.
Unfortunately, there's an ordering problem. My panel's controller just looks at the new NSView's frame property for deciding where to put it, but during this iteration of the main event loop, the -layout method hasn't been called yet, so it's still positioned at (0,0).
(If I separate these two pieces of functionality, and require two separate user events for "add view" and "create panel", then the panel is correctly positioned below the view.)
Is there a way to attach an NSPanel to an NSView, as if with a layout constraint? Or is there a way to say "do this (window controller stuff), but only after the next -layout call"?
Just call -layoutSubtreeIfNeeded on your NSView’s superview as soon as you add it and its constraints, so it will lay out immediately, then add the panel.
Or use an NSPopOver, although those draw a certain way and you might not want that.

How make button in NSWindow clickable while a sheet's on top of it

I have created a custom (themed) NSWindow, by creating a borderless window and then recreating all elements of the window border/background inside the content view. I've created the window widgets (close box, zoom box, minimize box) on top of my own fake title bar using -standardWindowButton:forStyleMask:.
Trouble is, when a sheet is presented on top of my custom window (e.g. "save changes...", those buttons do not receive the clicks.
Does anybody know how I can tell NSWindow not to intercept the clicks in my minimize box? It works with a standard NSWindow. When a sheet is up, I can still send both of them to the dock, or zoom the window out.
I thought maybe there's special code in the content view that ignores clicks in subviews while a sheet is up. But it seems as if -hitTest: is called on the content view and returns the minimize widget, but the widget's action never gets triggered.
I guess I could just replace the content view and perform the action in the content view's hitTest if it is the minimize widget ... but that seems a bit ugly.

Resources