NSTrackingArea with fullscreen window/view - cocoa

I'm trying to install a NSTrackingArea into a fullscreen view in order to get mouse moved events.
However, whenever I do, I get an assertion error. I've searched the web, but have not been able to find any leads.
*** Assertion failure in -[_NSFullScreenWindow _setTrackingRect:inside:owner:userData:useTrackingNum:install:], /SourceCache/AppKit/AppKit-1038.25/AppKit.subproj/NSWindow.m:3944
Here's the code that sets up the tracking area (x=1024, y=768):
cocoaWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0.0, 0.0, x,y)
styleMask: NSTitledWindowMask
backing: NSBackingStoreBuffered
defer:NO];
glView = [[WLMacGLView alloc] initWithFrame:NSMakeRect(0.0, 0.0, x,y) pixelFormat:[WLMacGLView defaultPixelFormat]];
[glView setCocoaController:self];
//add the glView as a subview of the window's content view
[[cocoaWindow contentView] addSubview:glView];
NSRect r = [glView frame];
NSTrackingArea *track = [[NSTrackingArea alloc] initWithRect:r options: NSTrackingMouseMoved | NSTrackingActiveWhenFirstResponder | NSTrackingActiveInKeyWindow
owner:self userInfo:nil];
[glView addTrackingArea:track];
[glView enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
[glView createContext];
The assertion happens right after the call to enterFullScreenMode: withOptions:
Anyone got any ideas? Is this not the approach I should be taking to get mouse moved events in a fullscreen window?

If you want to track mouse in the whole view, I think is will be easier to implement the mouseDown:, mouseMoved: and mouseUp: methods in order to get the mouse events.

So the answer to this question turned out to be a bug in my own code.
When initializing the NSTrackingArea, I was passing in the wrong object for owner. The proper thing to pass was the NSView. With that corrected, all works as expected.

Related

How to make a MTKView inside a NSScrollView show the scrollbars

If I resize the window to be smaller than the metal view I can see the scrollbars for a second but I cannot click on them nor they stay visible. Do you know how I can change this behavior? I would expect the scrollbars to be visible and clickable as long as the window is smaller than the metal view.
nsview = gdk_quartz_window_get_nsview(window);
NSScrollView *scroll_view = [[NSScrollView alloc]initWithFrame: [nsview frame]];
[scroll_view setBorderType: NSNoBorder];
[scroll_view setHasVerticalScroller: YES];
[scroll_view setHasHorizontalScroller: YES];
[scroll_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[nsview addSubview: scroll_view];
self->clip_view = [[DvFlippedClipView alloc]initWithFrame: [nsview frame]];
[scroll_view setContentView: self->clip_view];
self->mtk_view = [[MTKView alloc]initWithFrame: [nsview frame]
device: self->device];
self->mtk_view.framebufferOnly = YES;
self->mtk_view.autoResizeDrawable = NO;
self->mtk_view.translatesAutoresizingMaskIntoConstraints = NO;
self->mtk_view_delegate = [[DvMetalViewDelegate alloc] init: self->mtk_view];
self->mtk_view.delegate = self->mtk_view_delegate;
[scroll_view setDocumentView: self->mtk_view];
From a different callback I do the following:
[self->mtk_view setBounds:NSMakeRect(0, 0, width, height)];
[self->mtk_view setFrame:NSMakeRect(0, 0, width, height)];
self->mtk_view.drawableSize = CGSizeMake(width, height);
I had a similar problem yesterday, and I suspect the strange scroller behavior and lack of scrolling altogether may be stemming from what I suspect your problem is. Hopefully it will at least enlighten your or someone else if you haven't already found an answer.
My view hierarchy looks like this:
The problem was that I had set MTKView as the view property of CenterTopViewController, rather than the Bordered Scroll View. Doing that, for all practical intents and purposes, removed MTKView from the hierarchy and set it so that its superview property pointed to the split view in which all of this resides. The scroll view did not seem to be part of the responder chain, and was never handling any scroll events (or at least not in any meaningful way).
Setting the scroll view as the view property of the view controller fixed everything.
P.S.
If you're confused as to why there is an unnecessary view above the MTKView, it's just a result of almost desperate attempts to uncover the problem before I figured it out. I haven't gotten around to fixing it yet.

How UIToolbarPosition is handled by UIToolbar?

I'm using setBackgroundImage:forToolbarPosition:metrics: method of UIToolbar.
That's my code:
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
[toolbar setBackgroundImage:[UIImage imageNamed:#"top"] forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[toolbar setBackgroundImage:[UIImage imageNamed:#"bottom"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
[self.view addSubview:toolbar];
As you can see I've put my toolbar on top. However when I run the code the toolbar image used is [UIImage imageNamed:#"bottom"].
How UIToolbarPosition is obtained?
My first idea is frame check ( < 1/2 superview frame is top, otherwise is bottom... or sort of).
It still on bottom. Any idea?
on iPhone UIToolbarPosition is always on bottom because UIToolbar is not supposed to be on top.
on iPad it works fine, UIToolbarPositionTop is used when toolbar.frame.origin.y = 0, UIToolbarPositionBottom otherwise.

Default NSRecessedBezelStyle NSButton visual bug?

I have a basic NSRecessedBezelStyle NSButton added via IB to an NSView. Why is the font messed up in its unselected state? Is this normal?
As you can see, when pushed the recessed button looks fine, but unpressed it's solid black with no shadow. Am I missing something really obvious somewhere? I tried messing around with setAttributedTitle and setAttributedAlternateTitle but that yielded odd results with the push on push off mechanic.
That is the expected behavior for NSRecessedBezelStyle with the default "Push On Push Off" Type, bezeled in On state, plain text in OFF, additionally you can change the Type so the bezel is only displayed when hovering, here is the code to make it gray.
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[attrsDictionary setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
[attrsDictionary setObject:[NSFont boldSystemFontOfSize:12.0] forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraph = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraph setAlignment:NSCenterTextAlignment];
[attrsDictionary setObject:paragraph forKey:NSParagraphStyleAttributeName];
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:#"Button" attributes:attrsDictionary] autorelease];
[button setAttributedTitle:str];

Weird font behavior on the NSTextField

I added programmatically NSTextField to my NSView:
NSTextField *projectLabel = [[NSTextField alloc] initWithFrame:frame];
[projectLabel setStringValue:#"projectName"];
[projectLabel setBezeled:NO];
[projectLabel setDrawsBackground:NO];
[projectLabel setEditable:NO];
[projectLabel setSelectable:NO];
[projectLabel setFont:[NSFont controlContentFontOfSize:13]];
projectLabel.autoresizingMask = NSViewMaxXMargin | NSViewMinYMargin;
[self addSubview:projectLabel];
[self setAutoresizesSubviews:NO];
This field was added correctly, but when I change size of view (or even move window to second display), font on field changes very weird (see attached image).
on start
after change of the size
I do not know what I did wrong
I drew this label on drawRect every time, when the size changes.
So, you're manually telling the field to display in its parent view's drawRect:?
Don't do that. It's a subview, so it'll get told to draw in its turn anyway. Just let that happen.

NSTrackingArea works weird - Entire View, or nothing... no rectangles respected

In my "InitWithFrame" method of a view I'm setting a tracking area for which I want to capture mouse enter/exit events.
My problems are two fold:
Without NSTrackingInVisibleRect the events won't be called at all.
No matter what "rect" I put it, one that covers the entire view's frame or one that occupies just a small portion of it - the mouse enter/exited events are called for the entire view, regardless of where the mouse cursor is on the view.
this is how I initialize the tracking area:
trackingArea = [[NSTrackingArea alloc] initWithRect:rect
options: (NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways )
owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
Any clues why this happens? I want the mouse enter/exit events to be called only for a small portion (the bottom part) of my view.
Mike Abdullah's answer explains point 2.
Here is a guess about why you don't receive events at all when not using the NSTrackingInVisibleRect flag:
Probably the variable rect you provide is not within the view's coordinate system. You could use the following code as the designated initializer of your NSView subclass to receive mouseEntered: and mouseExited: events for the whole area of your view:
- (id)initWithFrame:(NSRect)frame
{
if ((self = [super initWithFrame:frame]))
{
//by using [self bounds] we get our internal origin (0, 0)
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
[trackingArea release];
}
return self;
}
Apple's documentation says:
When creating a tracking-area object,
you specify a rectangle (in the view’s
coordinate system), ...
Straight from the docs for NSTrackingInVisibleRect:
The NSTrackingArea object is automatically synchronized with changes in the view’s visible area (visibleRect) and the value returned from rect is ignored.

Resources