Floating NSWindow steals focus - objective-c

I'm trying to make an app that sort-of functions like the spotlight search that was demonstrated on WWDC.
I managed to get it to the floating level with kCGFloatingWindowLevelKey, however the window steals the focus from whatever window was previously active. I would like it to keep the focus, and still take input in the textfield from the user. Is that doable?
Answers in swift is preferred, but objective-c works as well.

Related

Native caret position macos cocoa

I want to be able to get the global caret position inside any application in Mac High Sierra using cocoa or appleScript. I already use NSEvent to get the keyboard and mouse hook but is there a way to get the caret position hook?
The caret is different from the mouse position. It moves on key event or mouse click. In windows, you can get the caret position almost anywhere. I want to know if there is the equivalent for macos.
I want to show a popup over the text caret, if i type on the keyboard or line return, it moves with the text.I tried getting the position of the key event, (locationInWindow) but it give me back the mouse position. I am not sandbox so i can even call applescripts
UPDATE : It is possible doing this by getting the bounds of the letter before the caret with the use of accessibility API.
thanks
I don't have the opportunity to try it for myself just yet, so you might beat me to the punch of confirm/reject this.
UIEvent has addGlobalMonitorForEventsMatchingMask:handler: where the mask can have a value of NSEventMaskCursorUpdate and presumably the returned NSEvent object will contain a coordinate that can be acted upon (i.e. converted to screen-space).
Caveat here is the docs explicitly say
Key-related events may only be monitored if accessibility is enabled
or if your application is trusted for accessibility access (see
AXIsProcessTrusted).
Your post seems to suggest that you do not wish to use Accessibility API ("but if not using accessibility API") so that may mean you're out of luck in the specific combination of requirements you seek to fulfill.

How do I change windows skin in api

I really wonder how winamp did it. I tried to change a drawing code to draw on title bar at ncpaint. it was run well but it was complex and it didn't draw,choosing another window.
I searched some source code or article but they used other ways... how do I do it?...
Well, Winamp just creates a borderless, decorationless window, and draws everything itself. Important is, that you still can attach a sysmenu to the window, so that a right click on the taskbar button gives the usual options.
If you want to get fancy you can process the WM_NCPAINT message to perform frame and title drawing yourself on a decorated window: http://msdn.microsoft.com/en-us/library/windows/desktop/dd145212(v=vs.85).aspx
But the easier solution actually is to just emulate the standard Windows decorations and synthesize the events the standard buttons do.

Custom NSWindows

Is is possible to make a window act like a true heads up display? If not fully can a windows do any part of the following? Any pointers on finding information about how to code a window like this would be great.
1 - Be above everything?
All other windows?
On all spaces?
Shown above the screensaver? [1]
2 - Be non interactive?
Never get focus?
Ignore all mouse clicks? (ie. if the user click where the window is, what ever is under the window receives the click)
[1] Shown above the screen saver would be a user settable preference (default to off). If the display is a big TV and the screen saver slide show is running, the user may sill want the display to be shown.
I'm not sure what you want. If you're looking for a full screen app here's Implementing the Full-Screen Experience.
Otherwise, you might be wanting to read Window Layers and Levels
Here's an exert for convenience:
There are a number of predefined window levels, specified by constants defined by the NSWindow class. The levels you typically use are: NSNormalWindowLevel, which specifies the default level; NSFloatingWindowLevel, which specifies the level for floating palettes; and NSScreenSaverWindowLevel, which specifies the level for a screen saver window. You might also use NSStatusWindowLevel for a status window, or NSModalPanelWindowLevel for a modal panel. If you need to implement your own popup menus you use NSPopUpMenuWindowLevel. The remaining two levels, NSTornOffMenuWindowLevel and NSMainMenuWindowLevel, are reserved for system use.
Oh! And I'm pretty sure you can't have anything over the screensaver.

Change to other space (MacOSX) programmatically

I am making a customised window (a NSWindow with NSBorderlessWindowMask) So far I have been able to handle dragging, resizing, cmd+click and even miniaturize with double click if allowed (see here) so my window resembles as much as possible to a normal NSWindow.
However when I drag my window to the corner of my screen the user will expect to move that window to the next space. (In case you have Spaces enabled in "SystemPreferences" > "Expose and Spaces" > "Spaces" > "Enable Spaces")
I wonder how can I change to other space programmatically and move my window there?
Unfortunately there's no public API which allows you to do this, but if you're willing to use the Private API it's possible. Take a look at CGSPrivate.h and you'll see you can make a call like this:
CGSConnection connection = _CGSDefaultConnection();
CGSMoveWorkspaceWindowList(connection, &windowNumber, 1, newSpaceNumber);
Note that using this private API will cause your app to be rejected from Apple's Mac App Store though.
Finally I found a solution.
I can't change to a certain space programmatically but I can make my window behave like other non NSBorderlessWindowMask if [window setMovableByWindowBackground:YES] is done. That was the final purpose of this question :)
The solution is written (with some detail) here because that question seems to be older (or maybe a duplicate of this question?)
Is there a way to make a custom NSWindow work with Spaces

Make an NSWindow (or something else) appear above the menubar

I want to create an NSWindow (or something else) that can appear above the mac menubar. I know this is possible because TeamViewer does it with their "mouse" image.
Example: http://i.stack.imgur.com/6iZbG.png
How do they do it? (or, how can I do it?)
You want to check out window levels, as alluded to in moritz' comment. Any level above NSMainMenuWindowLevel should appear above the menu bar.
If you really want to be above everything else, you can use a shielding window level (not technically part of the regular NSWindow window level). Shielding windows are intended for full screen apps which take over the screen, but you can use a regular window which does this. I have a magnifying glass type app that uses this to good affect.
[myWindow setWindowLevel:CGShieldingWindowLevel()];
Also as alluded to moritz' comment, doing this is generally a bad idea, so make sure you've a good reason for doing so.