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;
}
Related
i have an app and i try it on multi screen. I have two screens and i want to disable focus on the second screen when the both sreens are enterFullscreen, i want to force the focus on the main screen.
I tried solutions i found here but doesn't change anything.
following code shows how i enterFullScreen for my mainWindow and my second Window
[self.window.contentView enterFullScreenMode:[[NSScreen screens] firstObject] withOptions:nil];
[windowArray insertObject:self.window atIndex:0];
NSRect screenRect;
NSArray *screenArray = [NSScreen screens];
for (NSInteger index = 1; index < [screenArray count]; index++)
{
NSScreen *screen = [screenArray objectAtIndex: index];
screenRect = CGRectMake(0, 0, screen.frame.size.width , screen.frame.size.height);
NSWindow *window = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO screen:screen];
[window.contentView setWantsLayer:YES];
window.contentView.layer.backgroundColor = [NSColor blackColor].CGColor;
[window.contentView enterFullScreenMode:[[NSScreen screens] objectAtIndex:index] withOptions:nil];
[windowArray addObject:window];
}
When both screens are in fullScreen mode, when i click on the second window i have the focus on the second window (normal event) but i want to disable that and force to put the focus on my main Window. I tried to disable mouse event on the second screen but.. not working.
If someone can help me ! thanks in advance
I don't know if it's the best answer for you, but you can iterate through all the subviews of the content view, and call setEnabled: on all of them. You can see a better and complete answer here: https://stackoverflow.com/a/16336624/2923506
for (NSView *object in [self.window.contentView subviews]) {
// Check it out the object
// set enable to NO [(id)object setEnabled:NO];
}
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):
I am trying to make an overlay window that will allow drawing at the ShieldingWindowLevel, however when the window appears the cursor is still the default pointer. I would like to change it to the crosshairs. Having controller NSCursors before I am baffled why resetCursorRects is not ever called.
I manually create the window as follows (in my AppController class):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Create the window
NSRect frame = [[NSScreen mainScreen] frame];
// Provide a small area on the right to move the cursor in-and-out of the window.
frame.size.width = frame.size.width - 20;
self.window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[self.window setAcceptsMouseMovedEvents:YES];
[self.window setOpaque:NO];
[self.window setLevel:CGShieldingWindowLevel()];
[self.window setBackgroundColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.2]];
// Create the subview
ScreenOverlayView *subview = [[ScreenOverlayView alloc] initWithFrame:NSZeroRect];
[[self.window contentView] addSubview:subview];
// Add subview and show window
[self.window setContentView:subview];
[self.window makeFirstResponder:subview];
[self.window orderFrontRegardless];
}
With the following NSView subclass:
#implementation ScreenOverlayView
- (void) resetCursorRects {
[super resetCursorRects];
[self addCursorRect: [self bounds]
cursor: [NSCursor crosshairCursor]];
}
// ...
#end
I created a sample project to show this case and posted it to github, the most interesting files are ScreenOverlayView.m and AppDelegate.m.
I should point out that I have also spent a good deal of time trying to get this working with an NSTrackingArea, as you can see in the sample project. Tracking Area works if the mouse enters the view after it has appeared, but not if it is inside to start with. Using MouseEnter and MouseLeave would be fine if I had some way to set the initial cursor, but it will only change for a split second before changing back.
How can I get resetCursorRects to be invoked -OR- how can I set the cursor when I move it to the superview?
The key is that you really need to create a custom subclass of NSWindow, in order to counteract some of the default behavior that borderless windows (NSBorderlessWindowMask) have.
An updated version of your sample project is at http://www.markdouma.com/developer/full-screen-overlay.zip.
In it, I created a custom MDScreenOverlayWindow class that overrides NSWindow's canBecomeKeyWindow method like below:
// Windows created with NSBorderlessWindowMask normally can't be key,
but we want ours to be
- (BOOL)canBecomeKeyWindow {
return YES;
}
This will allow your view to become key and basically all your other stuff to work properly.
The other thing that may be of note is the drawRect: method. (It looks like you may be coming from iOS). You might want to look into NSBezierPath, as it could potentially simplify some of your drawing code. For example, I believe the drawing code you had could be consolidated into the following:
- (void)drawRect:(NSRect)rect {
// the color should probably be "pre-multiplied" by the alpha
// premultiplied version:
[[NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.8] set];
[NSBezierPath setDefaultLineWidth:2.0];
[NSBezierPath strokeLineFromPoint:currentLocation toPoint:downLocation];
}
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
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];