6

Okay, here is the problem:
I have a NSTextView and I add my custom NSButton using:

[_textView addSubview:button];

Then, inside my NSButton subclass, I have (along with the NSTrackingArea stuff):

- (void)mouseEntered:(NSEvent *)event{
     [[NSCursor arrowCursor] set];
}

- (void)mouseExited:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

- (void)mouseDown:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

- (void)mouseUp:(NSEvent *)theEvent{
     [[NSCursor arrowCursor] set];
}

But when I hover it, the cursor remains the same IBeamCursor (because it's a NSTextView). Only when I press the button, the cursor gets updated. And then, when I move the mouse, still inside the button, the cursor goes back to the IBeamCursor.

Any ideas on how to do this? Thank you!

Pedro Vieira
  • 3,330
  • 3
  • 41
  • 76
  • Have you tried implementing `cursorUpdate:`, as specified in ["Managing Cursor-Update Events"](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/TrackingAreaObjects/TrackingAreaObjects.html#//apple_ref/doc/uid/10000060i-CH8-SW10)? – jscs Apr 29 '13 at 20:32
  • yes, but the outcome is the same – Pedro Vieira Apr 29 '13 at 21:14

2 Answers2

9

Adding a tracking area that only tracks enter/exit events seems to be not enough for NSTextView subviews. Somehow the textview always wins and sets it's IBeamCursor.

You can try to always enable tracking for mouse move events (NSTrackingMouseMoved) when adding the tracking area in your NSButton subclass:

#import "SSWHoverButton.h"

@interface SSWHoverButton()
{
    NSTrackingArea* trackingArea;
}

@end

@implementation SSWHoverButton

- (void)mouseMoved:(NSEvent*)theEvent
{
    [[NSCursor arrowCursor] set];
}

- (void)updateTrackingAreas
{
    if(trackingArea != nil)
    {
        [self removeTrackingArea:trackingArea];
    }
    NSTrackingAreaOptions opts = (NSTrackingMouseMoved|NSTrackingActiveAlways);
    trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)dealloc
{
    [self removeTrackingArea:trackingArea];
}

@end
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • thank you so much for your answer, but unfortunately it's not working :/ – Pedro Vieira Apr 30 '13 at 09:01
  • Hi Pedro, I just tried it in a sample Xcode project. Works like a charm here (10.8). Did you use exactly the subclass I posted above? If so, did you instantiate it instead of your own? – Thomas Zoechling Apr 30 '13 at 09:30
  • Ooops, pardon me! It's working now, and tbh, I haven't changed anything :P Thank you very much! – Pedro Vieira Apr 30 '13 at 09:36
  • Thank you for the answer. Apparently solution mentioned here https://stackoverflow.com/questions/2925580/cocoa-change-cursor-when-its-over-an-nsbutton does not work – Kaunteya Apr 25 '20 at 06:03
5

Swift 5 variant:

import Cocoa

class InsideTextButton: NSButton {

    var trackingArea: NSTrackingArea?

    override func mouseMoved(with event: NSEvent) {
        NSCursor.arrow.set()
    }

    override func updateTrackingAreas() {
        if let area = trackingArea {
            removeTrackingArea(area)
        }

        trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseMoved, .activeAlways], owner: self, userInfo: nil)

        if let area = trackingArea {
            addTrackingArea(area)
        }
    }

    deinit {
        if let area = trackingArea {
            removeTrackingArea(area)
        }
    }
}
Ely
  • 8,259
  • 1
  • 54
  • 67