I want to show a new window when an user taps on a button in the main window, but when I called the separate window using makeKeyAndOrderFront, the window popped up far away from the main window.
Here's what I wrote, in a subclass of NSViewController:
myWindow = NSWindow()
myWindow.setFrame(myView.frame, display: true)
myWindow.contentView = myView
myWindow.makeKeyAndOrderFront(self)
However, this opens a new window outside of the main window. I want to display it within the main window, possibly in the center of the main window (I set the .frame value of myView to be positioned on the center).
So how can I call the new window within the main window? In other words, can I specify where the new window shows up?
You'll need to convert the coordinates and or rect to screen space.
NSView and NSWindow provide methods that begin with "convert" take a look at those.
Sometimes you'll need to chain to convert from view space to window then window to screen.
Keep in mind that a window has an NSScreen screen method and that a window could appear on many different size screens and potentially span multiple screens.
Related
The user clicks a button and I present a new viewController as show.
Inside that VC there is a AVPlayerView, the same size as the VC.
The app itself can change size if the user drags any of its corners but the VC displaying the movie cannot.
Why things like that that should work by default, don't work as expected? What can be done to make this work?
Example:
This is the main app.
I can drag its corners and make it bigger or small. I can click on the green semaphore and make it full screen and it works fine.
When I press the Help icon, this "window" appears. It is a viewController that was added the story board.
This cannot be scaled. It "appears" to be a window, because it contains a semaphore. I cannot scale this but I can make it full screen by the green semaphore.
Note: I have added the view to a WindowController and now when the user presses the Help button this window controller is launched and the video appears. No change. Video cannot be scaled.
Found the problem.
I had to add a window controller controlling that View Controller.
View controller was not resizing even after #1.
Then I found that I could not have
self.preferredContentSize = CGSize(width: 940,height: 504)
inside awakeFromNib.
Now it works perfectly.
"The app itself can change size" –– what you're saying is that the window can be resized by the user.
"but the VC displaying the movie cannot" –– Again in this case, you're not trying to resize a view controller, you're trying to resize a window. The window contains your view controller.
The reason the window can't be resized is probably due to the layout constaints. I imagine there must be constraints somewhere in the view hierarchy which specifies that the video player (or some other view) has a specific width and height. Because it's a constant value, the window can't resize.
I want to create a borderless window floating above the main window. The main window should be the key window (I want it to handle the keyboard and mouse events). Also, the floating window should close as the user clicks outside of it. Basically, I'm creating a very custom context menu, just like NSMenu. It should behave likewise.
So I created my window this way:
NSWindow *menuWindow = [NSWindow windowWithContentViewController:menuViewController];
menuWindow.styleMask = NSBorderlessWindowMask;
menuWindow.level = NSFloatingWindowLevel;
[menuWindow makeKeyAndOrderFront:self];
That works perfectly, but how do I handle clicks outside to close it? The window doesn't invoke delegate's windowDidResignKey, because it's not a key window. If it does overload canBecomeKeyWindow (so that it returns YES), then the floating window grabs the user input, which is not what I want.
So, is there any way to automatically close a borderless window? Can NSPanel help? I tried using it, but with no success (the becomesKeyOnlyIfNeeded selector doesn't do what I need).
To detect clicks outside the window but within your app, you could install an event monitor using +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]. If you also want to close the window when the user clicks in a different app, you could observe the NSApplicationDidResignActiveNotification notification.
NSWindow has a property hidesOnDeactivate that should do this for you.
From the Apple Docs:
The value of this property is true if the window is removed from the
screen when its application is deactivated; false if it remains
onscreen. The default value for NSWindow is false; the default value
for NSPanel is true.
I have a semi-transparent NSView subclass that acts as a kind of tooltip. When the tooltip is drawn on the screen it draws on top of a form and so it covers a couple of NSTextField's.
When I hover the mouse over the tooltip at the spots where a NSTextField exists, the cursor changes to the input style cursor.
How can I prevent the cursor from changing?
Overlapping views are not really supported very well in Cocoa and the behaviour is undefined.
Instead, your tooltip view should be in its own borderless window, large enough to contain your view. The tooltip window should be a child window of the main window it's attached to. To make a window a child of another window, you use the addChildWindow:ordered: method of NSWindow.
Child windows are attached to the parent window and will move with their parent window when the parent window moves. If you just open a new window without making it a child window, it will be "left behind" if the other window is moved.
To make a borderless window, pass NSBorderlessWindowMask as the styleMask to the initWithContentRect:styleMask:backing:defer: method of NSWindow.
I have noticed when an apps window contains an Outline view (such as XCode) it changes color when that window is in focus. With XCode for example, if the window is current then the outline view has a blueish background, if it looses focus it goes grey,
Can anyone help me to replicate this? I presume its something to do with drawRect: but can only manage to get the color to change when the window loads.
Maybe its a built in function and I'm just missing something?
All you have to do in your -drawRect: is check whether the window has main status and draw accordingly:
- (void)drawRect:(NSRect)rect
{
if ([[self window] isMainWindow]) {
// draw active appearance
} else {
// draw inactive appearance
}
}
A window's delegate gets messages whenever a window gets or resigns main or key window status. You can implement the appropriate methods (like -windowDidBecomeMain: and -windowDidResignMain:) in your window delegate to update the window and its subviews as necessary.
How can I write an own resize corner/handle for an NSWindow? I'm using a subclass of an NSWindow without the default resize corner, but I need it for my window.
The basic outline:
Create a view.
Put some "drag me" indicator appropriate to your window in the view.
Have the view's drag action method respond by resizing the window so that the corner in question is now located at the current mouse position.