Xcode fullscreen window without titlebar - objective-c

I would like to create a full screen application (mac) but eventhough I have the window fullscreen;
[window setFrame:[window frameRectForContentRect:[[window screen] frame]]display:YES animate:YES];
I can't get rid of the title bar? Can you change the above code to make the window without a titlebar or do you have to do it completely different? Thanks :)

CocoaWithLove has a good article about it:
http://cocoawithlove.com/2009/08/animating-window-to-fullscreen-on-mac.html
fullscreenWindow = [[FullscreenWindow alloc]
initWithContentRect:[mainWindow contentRectForFrameRect:[mainWindow frame]]
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES
screen:[mainWindow screen]];
[fullscreenWindow setLevel:NSFloatingWindowLevel];
[fullscreenWindow setContentView:[mainWindow contentView]];
[fullscreenWindow setTitle:[mainWindow title]];
[fullscreenWindow makeKeyAndOrderFront:nil];

I successfully placed the window over the titlebar using the window level NSScreenSaverWindowLevel, instead of NSFloatingWindowLevel suggested in Macmade's answer.
fullscreenWindow = [[NSWindow alloc]
initWithContentRect:[[NSScreen mainScreen] frame]
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES];
[fullscreenWindow setLevel:NSScreenSaverWindowLevel];
// Perform further configuration here, e.g. setTitle, setBackgroundColor etc.
[fullscreenWindow makeKeyAndOrderFront:nil];

Related

macOS: Problem moving child window with Parent window

I have written a sample code in objective-c to draw two windows and making the first window the parent of 2nd window, so that when first window is moved, second window also moves. I can see two windows are drawn, but the child window is not moving when I move the parent window. What is wrong with this code?
NSRect frame = NSMakeRect(0, 0, 200, 200);
NSUInteger styleMask = NSTitledWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window = [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];
NSRect frame1 = NSMakeRect(0, 0, 100, 100);
NSUInteger styleMask1 = NSTitledWindowMask;
NSRect rect1 = [NSWindow contentRectForFrameRect:frame1 styleMask:styleMask1];
NSWindow * window1 = [[NSWindow alloc] initWithContentRect:rect1 styleMask:styleMask1 backing: NSBackingStoreBuffered defer:false];
[window1 setBackgroundColor:[NSColor greenColor]];
[window1 makeKeyAndOrderFront:NSApp];
CFRunLoopRun();
[window1 setParentWindow:window];
Issue 1:
setParentWindow isn't executed. CFRunLoopRun:
Runs the current thread’s CFRunLoop object in its default mode indefinitely.
Set the parent window before CFRunLoopRun.
Issue 2:
From the documentation of parentWindow:
This property should be set from a subclass when it is overridden by a subclass’s implementation. It should not be set otherwise.
Use addChildWindow:ordered: instead.
[window addChildWindow:window1 ordered:NSWindowAbove];
As already stated the parent <-> child relationship must be present in order to let the child windows "follow" the parent.
In my case the problem was that the window was create, hidden with orderOut and then shown again. You should never call orderOut on a child window because as stated in the official doc:
Calling orderOut: on a child window causes the window to be removed
from its parent window before being removed.
So when the window is shown again the parent <-> child relationship is no more present and the child window is not moved when the parent moves.
Also note that invoking setIsVisible and passing it FALSE will internally trigger a call to orderOut.
So if you just want to hide the window without loosing the relationship with the parent you can call
[window setAlphaValue:0];
[window setViewsNeedDisplay:TRUE];

NSWindow subclass does not draw title bar on second rendering

I'm writing some glue GUI code for a scripting language implementation, and need to be able to programmatically create windows and intercept the key and mouse commands on their views. I've subclassed both NSWindow and NSView.
When I open a window for the first time, I get a window with a title bar and controls. If I close this window and open another, the controls and title don't appear in the new window. However, if I click where the controls should be, the new window (with invisible controls) still closes.
Is there something I'm doing in my window initialization that could cause this?
+ (HMSLWindow*)hmslWindowWithTitle:(NSString *)title frame:(NSRect)frame {
HMSLWindow* hmslWindow = [[HMSLWindow alloc]
initWithContentRect: frame
styleMask: NSMiniaturizableWindowMask | NSTitledWindowMask | NSClosableWindowMask
backing: NSBackingStoreBuffered
defer: YES];
hmslWindow.title = [title retain];
[hmslWindow setContentView:[[HMSLView alloc] initWithFrame:frame]];
hmslWindow.delegate = [[HMSLWindowDelegate alloc] init];
[hmslWindow cascadeTopLeftFromPoint:NSZeroPoint];
[hmslWindow makeKeyAndOrderFront:self];
[[HMSLWindow windowDictionary] setObject:hmslWindow forKey:[NSNumber numberWithInteger:hmslWindow.windowNumber]];
return hmslWindow;
}
- (void)close {
[[HMSLWindow windowDictionary]
removeObjectForKey:[NSNumber numberWithInteger:self.windowNumber]];
[self.contentView autorelease];
[self.delegate autorelease];
[self.title autorelease];
[super close];
}
First window:
Second window (contentView with white background is correct, but the titlebar is now empty):

How to override a fullscreen game with keywindow borderless window (overlay)?

I've created the app which sometimes shows up an overlay with label and textbox. It works nice, but I need it to work even with other apps are in full-screen mode and active.
For overlay, I've create custom window class and overridden canBecomeKeyWindow method to let borderless window become the key window (simply returns YES).
So it works, but when I run e.g. Minecraft, and then make it full screen, my overlay can override it. But I can't type in NSTextField in the overlay. How to fix it?
I'm creating an overlay like this:
[[NSWorkspace sharedWorkspace] hideOtherApplications];
NSRect frame = [[NSScreen mainScreen] frame];
_fadedWindow = [[CustonWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[_fadedWindow setAcceptsMouseMovedEvents:YES];
[_fadedWindow setOpaque:NO];
[_fadedWindow setLevel:CGShieldingWindowLevel()];
[_fadedWindow setBackgroundColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.8]];
NSApplicationPresentationOptions options = NSApplicationPresentationDisableProcessSwitching + NSApplicationPresentationHideDock + NSApplicationPresentationDisableForceQuit + NSApplicationPresentationDisableSessionTermination + NSApplicationPresentationDisableHideApplication;
[NSApp setPresentationOptions:options];
_fadedWindow.alphaValue = 0;
[_fadedWindow orderFrontRegardless];
[[_fadedWindow animator] setAlphaValue:1];
[_fadedWindow toggleFullScreen:self];
[_fadedWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
[_fadedWindow orderFront:self];
But still, I can't seem to populate overlay's NSTextField with keyboard input.
Try this. Create a subclass for _fadedWindow. Then put this in:
-(BOOL)canBecomeKeyWindow {
return YES;
}

Dismiss Custom Window for NSStatusItem

I've got a custom window popping up when an NSStatusItem is clicked. The code is based on MAAtachedwindow. Everything is working great but I can't figure out a way to dismiss the window when the user clicks on something else like another status bar item, or another app.
Here's my code for creating the window:
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
//setup custom status menu view
CGFloat height = [[NSStatusBar systemStatusBar] thickness];
NSRect viewFrame = NSMakeRect(0.0f, 0.0f, width, height);
statusMenuView = [[[_ISStatusMenuView alloc] initWithFrame:viewFrame] retain];
statusMenuView.offset = aOffset;
statusItem.view = statusMenuView;
//setup the window to show when clicked
NSRect contentRect = NSZeroRect;
contentRect.size = aView.frame.size;
statusMenuWindow = [[[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO] retain];
[statusMenuWindow setLevel:NSPopUpMenuWindowLevel];
[statusMenuWindow setBackgroundColor:[NSColor clearColor]];
[statusMenuWindow setMovableByWindowBackground:NO];
[statusMenuWindow setExcludedFromWindowsMenu:YES];
[statusMenuWindow setOpaque:NO];
[statusMenuWindow setHasShadow:NO];
[statusMenuWindow useOptimizedDrawing:YES];
[[statusMenuWindow contentView] addSubview:aView];
[statusMenuWindow setDelegate:self];
statusMenuView.statusMenuWindow = statusMenuWindow;
And here is how I'm showing the window:
- (void)centerView{
NSRect menuFrame = self.window.frame;
NSRect windowFrame = self.statusMenuWindow.frame;
NSPoint menuPoint = NSMakePoint(NSMidX(menuFrame), NSMinY(menuFrame));
menuPoint.x -= windowFrame.size.width*0.5f;
menuPoint.y -= windowFrame.size.height+self.offset;
[self.statusMenuWindow setFrameOrigin:menuPoint];
[self.statusMenuWindow makeKeyAndOrderFront:self];
}
I was hoping the windowDidResignKey delegate method would do the trick but it doesn't go off with this configuration. The delegate is working because windowDidMove does run.
- (void)windowDidResignKey:(NSNotification *)notification{
NSLog(#"windowDidResignKey");
[statusMenuView hideView];
}
- (void)windowDidResignMain:(NSNotification *)notification{
NSLog(#"windowDidResignMain");
}
- (void)windowDidMove:(NSNotification *)notification{
NSLog(#"windowDidMove");
}
So to recap, how can I hide my custom window when the user clicks on anything else, the way the standard status bar menus work?
Edit
After looking at the Popup example the only thing I was missing was I had to subclass NSPanel and make it so it could become the key window.
#interface Panel : NSPanel
#end
#implementation Panel
- (BOOL)canBecomeKeyWindow{
return YES;
}
#end
You need to make sure your window can become the key window, and call your window's orderOut: method when it resigns key. You should be using a custom NSWindowController, if you are not already, in which case you would just call its "close" method to dismiss your window.
Instead of posting a bunch of code, I would suggest you just look at this excellent, recently-posted example of attaching a window to a status item:
Shpakovski Popup Window Example

NSWindow Disappears

I have this code set to run on my app launch:
NSRect rect = NSMakeRect(0, 0, 200, 50); //The location of the window
NSWindow *win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[win setOpaque:NO];
[win setLevel:NSFloatingWindowLevel];
//[win setBackgroundColor:[NSColor clearColor]];
//or
NSView *myView = [[NSView alloc] initWithFrame: NSMakeRect(0, 0, 200, 50)];
NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(10, 5, 180, 40)];
[myView addSubview: myButton];
[win setHidesOnDeactivate:NO];
[win setContentView: myView];
[win orderFront: nil];
It works as expected (displays a button in the bottom left hand corner of the screen) for about a second, then it disappears. Why is it disappearing? Memory management, or something else, and how do I fix it?
First, it is extremely odd to be building user interface without simply using Interface Builder. Can be done and there are a handful of reasons to do so, but they are pretty few and far between.
Next, that code, by itself, isn't enough to say what has gone wrong. Creating a UI programmatically begs a whole series of questions; gc or not? ... how is your run loop configured? ... do you have a properly configured app wrapper?
As Abizem said, the most obvious guess would be that you have GC enabled and you haven't rooted the window in some global variable somewhere, directly or indirectly. It "just works" in a standard Cocoa application because NSWindow instances are rooted via the Cocoa application infrastructure (the Windows menu, specifically).
Are you working with Garbage Collection?
Do you have an iVar that is holding on to win? It could be that it is being garbage collected out from under you.