Getting mouse position in entire screen Mac OS X - macos

I'm writing a program that needs to get the mouse position in the screen, (not just my view). I need to continuously update variables xPos and yPos. I've heard about subclassing nsview and adding mouseDidMove, but it is never called. (Yes, I set my window view to my custom view.) How can I do this?

You need to set the NSWindow which contains the view to window.acceptMouseMovedEvents = yes. Also if you're just looking for mouse position in the screen consider NSEvent.mouseLocation

Related

How is a subview of an NSView dynamically positioned?

I am attempting to change the coordinates of an NSButton that is contained within a parent NSView and something is clearly not working, because the button position does not change. Both elements are defined in a nib file and the parent view has animation applied to it using CoreAnimation.
I have tried the following.
button.frame.origin.x = 500
and...
var frame:CGRect = button.frame
frame.origin.x = 500
button.frame = frame
Even with the animations disabled, I can not seem to dynamically position the subview. Is there some feature that prevents children views from being positioned programmatically?
Please note that I am using Swift with XCode 6.3.1.
I'm guessing you're using AutoLayout constraints, given you're using the latest tools.
If so, setting a subview's frame directly won't work the way you're expecting (if it does anything at all, it'll cause strange drawing glitches / flashing when mixed with animation). You have to create outlets for your layout constraints and modify them.
If you're not using AutoLayout, I suggest having a look at your button outlet to make sure it's actually connected (ie, you're not talking to nil). Even if the outlet is connected, make sure it's not nil at runtime - you may be trying to talk to the button before the nib is loaded and the outlet / action connections are restored.

NSScroller inside a NSScrollView does not change position when resizing the window (in Swift)

I have created a NSView with Interface Builder (Xcode 6.1). The NSView has constraints for each direction. When changing the window-size with the mouse manually, the NSView is getting a new size and the NSScroller gets a new position at the correct right border of the window, as expected.
When I set the window size manually (before it is made visible) with
myTextView.enclosingScrollView!.window!.setFrame(theRect)
or with
myTextView.window!.setFrame(theRect)
Then the window resizes well (as soon as it becomes visible), the NSTextView resizes also but the NSScroller (which is automatically part of the NSTextview and has not been created or modified seperately) does not move but stands in the middle of the ScrollView at its old position. The Text entered in the NSScrollView uses the full size and floats behind the NSScroller, which seams to stand in top of the text.
In the case, the Window is visible before setting the Window Size, then the scroll-view is truncated at the right side or bottom side.
When I change the window manually, after settzing the size and getting this behaviour, the NSScroller jumps to it's right position and the NSTextView is not longer truncated. So changing the Window Size manually does something more that I do programmatically.
What must I do, that the Scroller moves with the NSView, like changing the Size of the Window with the Mouse?
This is a typically problem when calling UI-functions from a background thread.
It can besolved, when calling from main-thread or forcing to called by the main-thread with this command:
dispatch_async(dispatch_get_main_queue())
{
// Place the UI Functions here
}
Please have a look at: Open File Dialog crashes in Swift

OS X Lion: Fullscreen NSWindow Bait & Switch

I currently have a NSWindow that allows for full screen. The window has a video player and a playlist below it. When the user goes fullscreen, I want to get rid of the playlist and just show the video.
My first thoughts for doing this is to swap out the window when I detect a fullscreen entry point. I have found that I can detect this with the following:
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
NSLog(#"My window is going fullscreen");
}
But I have been unable to figure out how to swap out the window for a new one at this point. One option I haven't yet attempted would be to modify all of the resizing flags of the video and hide the other components but I'm not certain if this would be the best solution.
Does anyone have any suggestions on a better way for doing this?
Why do you want to swap the window? Just manipulate the views in the window.
Just remove the playlist from your view when you go fullscreen with -removeFromSuperview and then resize the video view so that it fills your window.
Make sure you hold a reference to the view as an ivar somewhere, because otherwise the view will be deallocated. You can then use that reference to add the view back when the window exits full-screen mode.

How to change the height of an NSWindow titlebar?

I want to change the height of an NSWindow titlebar.
Here are some examples:
And…
I could use an NSToolbar, but the problem is that I can't place views very height (For example: I can't place the segmentedControl higher than in the picture because there is still the titlebar)
If I remove the titlebar I can't place a NSToolbar and the window isn't movable.
Have you any ideas?
This is much easier than one would think. I too went on a quest to do something similar for my app.
Real App Store app:
My App Store app look-alike:
No disrespect to INAppStoreWindow, it is a very good implementation and solid. The only draw back I saw from it though was that there was a lot of drawing code along with hardcoded settings for the TitleBar colors which Apple can adjust at anytime.
So here is how I did it:
A) Create a standard window with a Title Bar, Close, Minimize, Shadow, Resize, Full Screen - Primary Window all set.
Note: You do not need a textured window nor should you set a title
B) Next add a standard toolbar with these settings:
Icon Only
Visible at Launch - ON
Customizable - OFF
Separator - ON
Size - Regular
Remove all the Toolbar Items and add only these in the following order
NSSegmentControl (51 x 24) -- | Flexible Space | -- NSSearchField (150 x 25)
C) In your content View directly under the toolbar add a regular sized NSButton set like so:
Bordered - OFF
Transparent - OFF
Title -
Image -
Position - Text below the button
Font - System Small 11
Ok, pretty easy so far, right?!
In your Window Controller or app delegate....
setup IBOutlet(s) to your NSButton(s)
Note: Remember to hook up your IBOutlet in interface builder
Ok don't be scared we have to write a tiny bit of code now:
In awakeFromNib or windowDidLoad....
Get the content views' superview (aka NSThemeView)
Remove your button from its superView
Set the frame of your button
Add the button back to the theme view
So the code would look similar to this:
NSView *themeView = [self.contentView superview];
NSUInteger adj = 6;
[self.btnFeatured removeFromSuperview];
self.btnFeatured.frame = NSMakeRect( self.btnFeatured.frame.origin.x,
self.window.frame.size.height - self.btnFeatured.frame.size.height - adj,
self.btnFeatured.frame.size.width, self.btnFeatured.frame.size.height);
[themeView addSubview:self.btnFeatured];
That's it! You can use your outlet to enable/disable your button, setup a mask image when selected, enable/disable the toolbar or even hide everything and add a window title. All of this without worry if Apple changes their standard Window Titlebars.
P.S. No private frameworks were used in this posting whatsoever!
INAppStoreWindow is a NSWindow subclass, it tell you how to change the height of title bar.
https://github.com/indragiek/INAppStoreWindow
http://iloveco.de/adding-a-titlebar-accessory-view-to-a-window/
This example tells you how to add buttons in the title bar.
You'd have to subclass NSWindow and do a custom window frame drawing. It's not only about a titlebar. It's about whole window frame (so you can, actually, put close/minimize/zoom buttons at the bottom if you wish).
A good starter is at "Cocoa with love" website.
There are a few new solutions based on INAppStoreWindow and without warning and log message, for anyone who wants to change the height of NStitlebar, change the position of traffic light, add an item(e.g. a NSbutton) on NStitlebar and change its position, please check below.
WAYWindow:
https://github.com/weAreYeah/WAYWindow
NStitlebar_with_item:
https://github.com/ZHANGneuro/NStitlebar_with_item

Cocoa Pop-up Window Similar to iCal

I want to open an overlay window (pop up window) when a user selects a cell in my NSTableView similar to selecting an event in iCal. Selecting the event in iCal shows a Window to edit the event, but does so by smoothly animating the window open and adding an arrow pointing to the even in the underlying calendar. Does anyone know what is being used here? Is this a bunch of hidden/custom APIs or is this available for public use?
The editor pane appears to be a custom borderless, transparent window with a custom view (the view defines the shape and therefore the shadow it casts). Learn more here. You might even use MAAttachedWindow.
Regarding animation, it's as simple as asking the window's animator to animate the frame and the alpha value (grouping them together). You'll probably want to set everything up directly in its "start position" first (ie, while the window is off-screen, set its alpha to zero, and its frame to some smaller version so it "zooms in" a la iCal), then put it on screen and start the grouped animation:
[NSAnimationContext beginGrouping];
[[window animator] setFrame:someNewSlightlyLargerFrame];
[[window animator] setAlphaValue:1.0];
[NSAnimationContext endGrouping];
Once the grouping is ended, the animation will begin (asynchronously) and your code execution will continue. Something to think about is getting everything "staged" first (including making sure the subviews of your window are already updated beforehand, so they don't change in the middle of your animation ... unless you want them to).
So the two separate techniques you need to understand are a custom window and basic Cocoa animation.
If you're using OSX 10.7 and above, NSPopover will do the job you're looking for.
Are you talking about selecting even from a list at the bottom of iCal app?
Not sure what exactly you are referring to but there is an api for animating transformations within a timespan.
Looking at other Apple's applications, Apple's developers utilize the same api available to anyone else (mostly I guess). There is lots of stuff that can be customized and Apple customizes whatever is required until it looks right from design point of view ...

Resources