Invisible Fullscreen View with Cocoa - objective-c

I'm trying to create an application which change the mouse cursor, but to do that the cursor must be inside a NSView. To do this all the time, I would need to have an intangible and invisible fullscreen view.
I've created a subclass of NSView which implement the following code to became full screen in initWithFrame:
[self enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
And using that code, I can change the cursor inside the view:
- (void)resetCursorRects{
[self addCursorRect:[self bounds] cursor:appCursor];
}
The cursor changed successfully, but here comes the first problem: the view is in fullscreen, but it's visible, like a gray background. I've tried this method:
[[self window] setOpaque:NO];
And this one:
[[self window] setBackgroundColor:[NSColor colorWithWhite:1.0 alpha:0.0]];
But both of them just change the view color to black instead of gray. How can I fix that?

Related

Transparent NSWindow, but not subviews?

Is it possible to have a transparent NSWindow, but not for it's subviews (NSTextField and NSButton). Right now the text is also showing wat's below, I'd like that to be 0% transparent.
Here's what I do in the NSWindow init:
[self setAlphaValue:0.9];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
You need to put a dark backgound image having the default colour of window behind each of textfield and button.
For this purpose you can subclass all required objects and add the above.

Grey border around view when using NSBorderlessWindowMask

I'm having the exact same problem as in this question:
Gray border when using NSBorderlessWindowMask
However, the accepted answer (as in the comments) of removing the window shadow doesn't seem to work, at least on Lion.
I've subclassed NSWindow, and created a borderless window in this manner:
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect
styleMask:(NSBorderlessWindowMask | NSResizableWindowMask)
backing:bufferingType
defer:flag];
[self setMovableByWindowBackground:YES];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setHasShadow:YES];
[self setLevel:NSMainMenuWindowLevel];
return self;
}
Please note that this app will only be run on Lion (so NSResizableWindowMask doesn't change the appearance). I tried disabling the shadow, and toggling numerous settings for my window but I can't seem to remove this grey border:
Nowhere in my code do I add a border. I simply have a NSSplitView added in Interface Builder in a window. During runtime I add the colored view as a subview to the left pane of the split view, completely filling the bounds of the left split view.
Edit: This happens even using a simple NSView, not even a split view.
TL;DR: Why does my NSView have a grey border around it?
ok got it. to remove the shadow simply add this to your NSWindow subclass:
- (BOOL)hasShadow {
return NO;
}
and to remove the border you need to know that this border is coming from the view - not the window (just like you said it in your edit). So you have to disable the border for the view with this code:
[myview setBorderType:NSNoBorder];

Is there Any way to imitate Lion's Launchpad?

I have been struggling to imitate Launchpad.
At the beginning I thought about making NSWindow bgcolor transparent:
//make NSWindow's bgcolor transparent
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
But now I realized it's way more ideal to
capture wallpaper
blur it and make it bg-image for NSWindow or a view
Rather than hiding all the opened windows and menubar, which was the first idea I had have come with (Still not sure with better, if you had any better idea...).
Capture & blur wallpaper used by a user
Make it background image for nswindow or a view
Fade-in to fullscreen view
Click somewhere blank or press ESC to fade-out
Are those possible to achieve without using private APIs?
Sorry if it's not clear my poor English.
Simply I'm trying to imitate Launchpad-styled full screen.
Thanks for any advice.
To get an image of the desktop background, use:
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageuRLForScreen:[NSScreen mainScreen]
NSImage *theDekstopImage = [[NSImage alloc] initWithContentsOfURL:imageURL];
You can blur the image using CIFilter. Here's a Apple doc describing how to apply filters.
And then you can load that image into a color and set that as the background color for the window. Additionally, set the window to have no style mask (no close buttons, title frame, etc.), cover the screen, and be in front of everything except the dock:
[yourWindow setBackgroundColor:[NSColor colorWithPatternImage:theDesktopImage]];
[yourWindow setStyleMask:NSBorderlessWindowMask];
[yourWindow setLevel:kCGDockWindowLevel - 1];
[yourWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
To have the window fade in, you can use NSWindow's animator proxy. (Replace 1.0 with 0.0 to make it fade out.)
[[yourWindow animator] setAlphaValue:1.0];
Of course you could customize that a bit more with things like CoreAnimation, but this should work just fine.
To handle background clicking, I suggest making a subclass of NSView where you -orderOut: your window on -mouseDown:. Then put an instance of that subclass that spans the entire frame of your window.
Also, NSViews sometimes don't respond to key events, so you can add an event listener to detect any time the escape key is pressed while your app is active:
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:(NSEvent *ev)^ {
if([ev keyCode] == 0x53) {
[yourWindow orderOut:self];
}
return ev;
}

NSTextView fails when used as contentView

I am programmatically creating a NSTextView to be the contentView for a window. When I create a WebView using the following code, it displays scrollbars, and the resizing thumb, and resizing the window works flawlessly.
When I comment out the WebView and attempt to use a NSTextView as the contentView, it does not "work" when the window is resized: Resizing the window using the thumb causes the content of the text view to not repaint correctly, it also paints over the title of the window, and the resizing thumb is also not repainted.
-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
NSTextView* view = [[NSTextView alloc] initWithFrame:[window frame]];
// WebView* view = [[WebView alloc] initWithFrame:[window frame]];
[window setContentView:view];
[window makeFirstResponder:view];
[window makeKeyAndOrderFront:view];
}
Edit: Working code. Creates a NSScrollView to be the windows new contentView, and adds an NSTextView as its document.
-(void)applicationDidFinishLaunching:(NSNotification*)aNotification{
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[window frame]];
NSTextView* view = [[NSTextView alloc] initWithFrame:[scrollView bounds]];
[window setContentView:scrollView];
[scrollView setDocumentView:view];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
[window makeFirstResponder:view];
[window makeKeyAndOrderFront:view];
}
A web view makes and manages its own scrollers and is a special case rather than the norm. An NSTextView does not. It's just the text view. This is the norm - scrollable views come pre-wrapped in an NSScrollView only in the convenience of Interface Builder. In code, you must create an NSScrollView as well, then wrap the view in it. It's the scroll view that would be your top-level view in that case.

Custom NSView Fill Paints Over Bottom Bar

I have a window which has a custom NSView and has a bottom bar with controls on it, one of which is an NSColorWheel.
For simplicity sake the Window is 332px high, with the custom NSView being 300px high and the bottom bar being 32px high.
The bottom bar is created as part of my awakeFromNib when the app loads the window using the following code:
[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];
In my custom NSView class I fill the rectangle with color. Everything works fine when the app loads using the following in my NSView class:
- (void)drawRect:(NSRect)dirtyRect
{
dirtyRect = [self bounds];
NSColor * mNewColor = [NSColor blackColor];
[mNewColor set];
[NSBezierPath fillRect:dirtyRect];
}
However, if I subsequently call a method that changes the color of the custom NSView when a color wheel in the bottom bar is changed, the bottom bar gets overwritten with the color. The following code illustrates this method (this code is in the custom NSView class:
- (void)changeBackgroundColor:(NSNotification *)notification
{
NSLog(#"Changed background color");
NSRect mRect = [self bounds];
NSColor * mNewColor = [theColorWell color];
[mNewColor set];
[NSBezierPath fillRect:mRect];
[self setNeedsDisplay:YES];
}
Resizing the window instantly corrects the problem, but obviously I don't want the user to have to resize the window for an obvious bug!
What I don't understand is why my bounds appear to be mapping to the parent window and not the custom NSView when I call setNeedsDisplay and yet the bound correctly adjust when I resize the window using the mouse (even if just by 1 pixel).
Do I somehow need to account for the bottom bar on the redraw?
Any and all help much appreciated.
You should do all your drawing in the drawRect: method of your custom NSView. Cocoa automatically sets up the graphics context for you when it calls this method - things may not draw correctly if you perform drawing operations in other methods.
Your code in drawRect: could set the colour to the the current background colour as specified by your NSColorWell and fill the dirtyRect rectangle with this.
Then in the other method just call [self setNeedsDisplay:YES]; and then drawRect: will automatically be called to redraw the view.
See here for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4 (in particular the Drawing View Content section)