Drawing a Window at Coordinates or Drawing outside of NSStatus Item - objective-c

I am trying to display something off the side of a NSStatusItem. I think I could do this in two ways:
Display a transparent window with the image I need at the coordinates of the mouse cursor.
OR
Use a custom NSStatusItem and move the controls/images in the view to the left so they are actually off the status item
The problem is, setting the NSRect frame negative (-200,0,100,50) doesn't seem to actually work. So, how can I render things outside of the bounds of the status item (think the CSS overflow property) or render a transparent window at specific coordinates?

The system will prevent you from drawing outside the status item, but using a transparent window will work.
NSRect rect; //The location of the window
NSWindow *win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[win setOpaque:NO];
[win setBackgroundColor:[NSColor clearColor]];
//or
[win setContentView:myView];
Here, myView is a custom view which will be the background of the window. In order for the window to be transparent, you either have to set the background color to clear or use a custom content view which only draws where it is not transparent. You will probably want to use a floating window so that it stays on top. Be careful not to cover up something important because your window could intercept events intended for something underneath it.

Related

Show up a custom UI somewhere on screen

I've programmed a custom UI which looks like this: http://www.youtube.com/watch?v=XLsrVVhEs94
Currently it is only working within a NSView itself but I want it to show up in every corner of my screen.
So I programmatically created an NSWindow like so [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered
defer:NO];
This works great but there is a problem: Every time I open this UI I can barley see that the NSWindow was just created. I can see a shadow that has the size of the NSWindow and after that it disappears (because of [NSColor clearColor]). I think that [NSColor clearColor] applies too slow to the just created NSWindow.
The NSWindow was set up with [window setOpaque:NO] so it is transparent.
Is there another way to display a custom UI somewhere on my screen?
- Timo
I think you want to set the defer to YES. Referring to the documentation, the defer property will either create the window immediately, or defer it until it is displayed on the screen. In this case you can then set all the window properties, add subviews, etc before showing on the screen.
NSWindow *myWin = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
... do window setup here ...
[myWin orderFront:self];
Additionally if that still flickers, you can call 'display' on the window so it draws all the subviews into it's buffer first (including your clear), then call orderFront.

Creating a custom title bar on a standard NSWindow

I've been trying to build a specific look for my menubar app.
I've been using a NSWindow with a NSBorderlessWindowMask style mask and setting [window setOpaque:NO] and [window setBackgroundColor:[NSColor clearColor]]. That gives me a blank canvas which works great for the title bar.
Now I'm having problems with the view-based NSTableView I'm using for the listing. How can I clip the NSTableCellViews to the window's rounded corners?
I started out just having a custom view wrapping the NSTableView, drawing the background with rounded corners. Using [view addClip:path] doesn't clip child views though.
I've also tried using a [view setWantsLayer:YES] with a mask. That worked great, but the table view cells would sporadically glitch out. It seems that having a NSScrollView be a child of a layer is a known problem:
My current view structure looks something like:
NSWindow
- MyTitleBarView
- MyBackgroundView
- NSScrollView
- NSTableView
I found one way to do it:
The trick is to keep the window style as the default and not set NSBorderlessWindowMask. Then you can add your custom title bar view to the window's theme frame like so:
NSView *themeFrame = [[window contentView] superview];
NSView *firstSubview = [[themeFrame subviews] objectAtIndex:0];
[titleBarView setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)];
[themeFrame addSubview:titleBarView positioned:NSWindowBelow relativeTo:firstSubview];
This basically just puts your custom title bar view on top of the standard title bar. You'll probably have to do some rejiggering to the view frames and window buttons. See INAppStoreWindow for some code examples of this.
The INAppStoreWindow project says that this method doesn't use any private APIs, and thus is able to be used on the App Store.
If you require the window to be transparent, you can just set the following on the window:
[window setOpaque:NO];
[window setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];

Resizing NSWindow to fit child NSView

I have one main NSWindow which is empty, and 5 NSViews. The NSViews have different buttons and labels etc, and the window is empty. The first view displayed is a menu, linking to the other views and back. This works fine and the views switch well.
However if the NSWindow is a certain size, and the NSView is bigger, then it spills out of the NSWindow and gets cut off.
Is there any way such that when I do:
[_window setContentView: theNewView];
to also have _window resize to fit the new view? If this is possible, can this be done with an animation?
-[NSWindow setContentSize:] does this (without animation). Give it the desired size of the content view and it will resize both content view and the window appropriately, e.g.
[_window setContentSize:theNewView.frame.size];
[_window setContentView:theNewView];
For animation, you need to compute window size manually using frameRectForContentRect: and then change window's frame with animate:YES:
[_window setContentView:theNewView];
NSRect viewScreenFrame = /*translate theNewView.frame to screen coordinates*/;
NSRect wndFrame = [_window frameRectForContentRect:viewScreenFrame];
[_window setFrame:wndFrame display:YES animate:YES];

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;
}

Get list element bounds or frame on scrolling

this question is related to Get scroll bar position of an NSScroller on the fly
But now i would like to know how to get current position of list element (green rect) on scrolling.
[self bounds] or [self frame] or [[self enclosingScrollView] ...] won't work for this kind of thing or i'm using it in wrong way.
EDIT
Inside frame of [[NSScrollView enclosingScrollView] contentView]] we see list of NSBox's.
When i click on triangle of NSBox which is list element, instance of that NSBox is stored in -activeTicketRow, i've thought that i can then get NSBox coordinates inside NSScrollView frame when [[NSScrollView enclosingScrollView] contentView]] bounds changes, but after reading "View Programming Guide" i guess not.
I've added observer in NSScrollView
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(updateConvMenu:)
name:NSViewBoundsDidChangeNotification
object:[[self enclosingScrollView] contentView]
];
​
and in -updateConvMenu i get coordinates of [[self enclosingScrollView] contentView], so that's good.
QUESTION
No i would like to store NSBox (which triangle was clicked) from [[self enclosingScrollView] contentView] inside -activeTicketRow so i can then get it's frame coordinates in -updateConvMenu when [[self enclosingScrollView] contentView] bounds changes.
I think now this question is more readable.
Views are in a hierarchy. Ask yourself "position relative to what view/window/screen?" Then use NSView's various -convertRect:... or NSWindow's -convert... methods.
You can get the position of the content view by observing the NSClipViews (the scroll view's contentView) bounds by setting - (void)setPostsBoundsChangedNotifications:YES. Then just translate the coordinates to the view you want, when the notification occurs.
For the translation have a look at the docs Josh did already post View Programming Guide
When scrolling through the list, i'm calculating whether the horizontal center of the selected item in the list does not go after a specific bounds, which overlaps on [NSScrollView frame].