Losing focus of NSView after setFrame of NSWindow - objective-c

I am switching my NSWindow from normal mode to fullscreen, by setting its frame (I know there is a method for the view to go fullscreen, but it needs to be this way)
In my NSOpenGLView I am tracking the onMouseMove event...
After switching to fullscreen (or back), I have to click the view (inside window), to receive the mouseMove event. It looks like it is going out of focus, but I don't understand why (I am just using setFrame) and how to make it focused again, without the user needing to click the window.
Code in my NSOpenGLView (NSView):
if (!fullscreenOn) {
//!switch to fullscreen mode
NSRect mainDisplayRect = [[[self window] screen] frame];
[[self window] setStyleMask:NSBorderlessWindowMask];
[[self window] setFrame:[[self window] frameRectForContentRect:mainDisplayRect] display:YES animate:NO];
[[NSApplication sharedApplication] setPresentationOptions: NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock];
fullscreenOn = YES;
} else {
[[self window] setStyleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSTexturedBackgroundWindowMask];
[[NSApplication sharedApplication] setPresentationOptions: NSApplicationPresentationDefault];
... some code for the right size ...
[[self window] setFrame:frame display: YES animate: YES];
fullscreenOn = NO;
}

the problem was with setting [[self window] setStyleMask:NSBorderlessWindowMask]; which cause this behaviour... making the window borderless by default fixed the problem

Related

Non modal dialog has to appear as modal window

I can show my custom NSWindowController as modal window by code:
TSAppDelegate* appDelegate = (TSAppDelegate*) [[NSApplication sharedApplication] delegate];
NSWindow* mainWindow = appDelegate.window;
[NSApp beginSheet: [self window]
modalForWindow: mainWindow
modalDelegate: NULL
didEndSelector: NULL
contextInfo: NULL];
int acceptedModal = (int)[NSApp runModalForWindow: [self window]];
[NSApp endSheet: [self window]];
[[self window] close];
It works. But I need non modal window. It has to appear as modal (see pic) and be NON MODAL.
I tried
TSAppDelegate* appDelegate = (TSAppDelegate*) [[NSApplication sharedApplication] delegate];
NSWindow* mainWindow = appDelegate.window;
[[self window] setParentWindow: mainWindow];
or
[mainWindow addChildWindow: [self window] ordered: NSWindowAbove];
It works as non modal but appears as normal popup window.
Is it possible?
It seems that you just want a window to come down over your view but not be modal.
Rather than use a sheet, you could just use a separate view that you animate into and out of position.
You'll have to do some work yourself: setting the correct position, animating the view, responding to events, etc.

Custom Window Style in Cocoa

OK, this is what I'm trying to do :
I have a custom NSPanel subclass
I want the NSPanel to be borderless (NO title - I'm drawing a titlebar myself) AND resizeable
The thing is that :
once I set the styleMask to NSResizableWindowMask, the default title bar appears as well.
once I set the styleMask to NSBorderlessWindowMask, the default title bar disappears (that's good), but the window loses its resizing ability.
This is my Code :
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect styleMask:NSTitledWindowMask backing:bufferingType defer:deferCreation])) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setMovableByWindowBackground:YES];
[self setLevel:NSFloatingWindowLevel];
//[self setStyleMask:[self styleMask]&~NSTitledWindowMask];
}
return self;
}
As you may see from the commented-out code, I've tried using any possible combination of bit operations with the mask so that I combine what I need.
Any ideas??
Just do them at a time like this
styleMask:NSTitledWindowMask | NSResizableWindowMask

NSWindow windowDidResignKey not getting invoked after redisplaying window

I have a custom NSWindow subclass that the user can toggle the display of with the click of a button. I'd also like the window to disappear when the window resigns key status (e.g. by the user clicking outside the window).
I have a delegate that implements windowDidResignKey: but I find that this delegate method is only invoked the first time the window resigns key.
Here's how I toggle the display of the window (via user action or windowDidResignKey):
- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
attachedWindow.delegate = self;
[attachedWindow setLevel:NSMainMenuWindowLevel+1]; // show window in front of all other apps on desktop
[attachedWindow makeKeyAndOrderFront:self];
}
else
{
attachedWindow.delegate = nil;
[attachedWindow orderOut:self];
[attachedWindow release];
attachedWindow = nil;
}
}
Here's my implementation of windowDidResignKey:
- (void) windowDidResignKey:(NSNotification *)note
{
[self toggleWindowAtPoint:NSMakePoint(0, 0)];
}
I'm finding that the first time the custom window is displayed, windowDidResignKey: gets called. Every time the custom window is re-displayed after that, windowDidResignKey: is not getting invoked.
The issue was that in some cases, the custom window was not actually becoming the key window after calling [attachedWindow makeKeyAndOrderFront:self].
I fixed this by adding the following line before re-creating the window:
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
In the context of the code snippet above:
- (void) toggleWindowAtPoint:(NSPoint)point
{
// Attach/detach window.
if (!attachedWindow)
{
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
attachedWindow = [[CustomWindow alloc] attachedToPoint:point];
....
Have you tried calling [attachedWindow makeFirstResponder:attachedWindow] in your toggle method?
If you want to activate a window without using activateIgnoringOtherApps: you should use a NSPanel with a NSNonactivatingPanelMask:
[[CustomPanel alloc]
initWithContentRect: NSZeroRect
styleMask: NSNonactivatingPanelMask
backing: NSBackingStoreBuffered
defer: NO];

Window Fade in and Out

how do I fade a window in when it's opened and out when closed?
This should probably be done in Objective C. This has to be part of an AppleScript-Objective-C project. I'm using a property linked to the window, and doing makeKeyAndOrderFront on it...
Any help would be appreciated!
Subclass (or add a category to) NSWindow and add:
- (void)fadeInAndMakeKeyAndOrderFront:(BOOL)orderFront {
[self setAlphaValue:0.0];
if (orderFront) {
[self makeKeyAndOrderFront:nil];
}
[[self animator] setAlphaValue:1.0];
}
- (void)fadeOutAndOrderOut:(BOOL)orderOut {
if (orderOut) {
NSTimeInterval delay = [[NSAnimationContext currentContext] duration] + 0.1;
[self performSelector:#selector(orderOut:) withObject:nil afterDelay:delay];
}
[[self animator] setAlphaValue:0.0];
}
This allows you to fade in/out windows programmatically.
To have a window fade out when its close button gets pushed, add this to the window's delegate:
- (BOOL)windowShouldClose:(id)sender {
[window fadeOutAndOrderOut:YES];
return NO;
}
To show a window with fade-in call [window fadeInAndMakeKeyAndOrderFront:YES]; instead of what you'd call otherwise.

Cocoa Fullscreen problem with keyDown and keyUp

I make fullscreen in this way:
NSRect frame = [[NSScreen mainScreen] frame];
// Instantiate new borderless window
fullscreenWindow = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer: NO];
startingWindow = [self window];
[startingWindow setAcceptsMouseMovedEvents:NO];
[startingWindow orderOut:nil];
if(fullscreenWindow != nil)
{
// Set the options for our new fullscreen window
[fullscreenWindow setReleasedWhenClosed: YES];
[fullscreenWindow setAcceptsMouseMovedEvents:YES];
[fullscreenWindow setContentView: self];
[fullscreenWindow makeKeyAndOrderFront:self ];
[fullscreenWindow setLevel: NSPopUpMenuWindowLevel-1];
[fullscreenWindow makeFirstResponder:self];
}
After switching to fullscreen not working keyDown and keyUp.
What to do to make it working?
Thanks in advance.
Best regards Chudziutki
You need to override the NSWindow class with your own class that inherits from NSWindow. Then you override the keyDown and keyUp event messages in order to capture them. You then pass these events to whoever needs them.
Why are you doing all this work yourself? Just send your view an -enterFullScreenMode:withOptions: message.