makeKeyAndOrderFront doesn't do either - objective-c

I've got an application that runs at startup as a menu in the OS X menu bar (i.e., as a background application). When you select an option from the menu, it transforms into a foreground application (creating a dock icon) and shows a window.
Here's the background/foreground code I'm using:
+(void) TransformToForegroundApplication {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}
+(void) TransformToBackgroundApplication {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
Pretty standard stuff, from the looks of things. And it works fine, except for one small issue.
The problem is that after I call TransformToForegroundApplication, I call makeKeyAndOrderFront: on the window to make it come to the front of the Z-order and receive focus - but it does neither. It shows up in the background, underneath everything else. I need to either click on the dock icon, or minimize all of the windows in front of it and then click on that window, in order to bring it to the front.
Any ideas?

Probably you need to activate the application, which I presume TransformProcessType() will not do automatically, and know -makeKeyAndOrderFront: will not.
Have you tried calling [NSApp activateIgnoringOtherApps:YES]?

Related

ObjectiveC Accessibility API: UnMaximize Window

I’m not sure if I am referring to this correctly, but when I use the word “UnMaximize”, I’m referring to:
When you click on the green button which is third on the top left of a
Chrome Window, it Maximizes the Window. When I use the word
“UnMaximize” above, I’m referring to the behavior that clicks that
button again so that it is no longer in full screen.
(By the way, what is the correct word for this in MacOS Terminology?)
I enjoy using the Easy Move+Resize App. While it can move Windows around, unfortunately, it has no effect on windows that are Maximized. Fortunately, the code is available on Github.
I’m curious if anyone can point me how to UnMaximize a Window using the Accessibility API
Does anyone what is the UnMaximize equivalent to kAXCloseButtonAttribute
I’m using MacOs 10.12 if that helps.
I’m grateful to #Willeke - Willeke for pointing me in the correct direction.
As mentioned in my question, I was looking at the code of the Easy Move+Resize App on GitHub. The problem with this code/app is that it does not work for Windows that are currently Maximized i.e. it tries to move these Windows, but it cannot, because they are fixed. (Note: This only has use and is relevant in a multi-monitor setup.) This app works correctly for Windows that are not Maximized.
Here, I am trying to add code that would UnMaximize a window in order to move it, and then Maximize it again after it has been moved. Obviously, the code below is in the context of this app, but I’m sure would be useful to users in other contexts.
I first added a wasMaximized property to EMRMoveResize.h
//EMRMoveResize.h
#property bool wasMaximized;
Next, I moved to EMRAppDelegate.m where the actual Event Callback code is. It should be noted that we are only concerned with moving i.e. only concerned with the Left Mouse Button. (This app uses the Right Mouse Button for resizing, which is not relavent when the Window has been maximized.) So, we are only concerned with kCGEventLeftMouseDown, kCGEventLeftMouseDragged and finally with kCGEventLeftMouseUp. In pseudo code, I have done something like:
If (LeftMouseDown) {
Find out if Window is Maximized
If (Window is Maximized) {
set the wasMaximized property
Click FullScreen Button to UnMaximize the Window in Order to Move it
}
The Window is now UnMaximized would now move as other windows in the LeftMouseDragged event, which I have not made any changes to. Finally,
If(LeftMouseUp) {
If(wasMaximized value was set) {
Click FullScreen Button again to Maximize the Window (Since it started out as Maximized)
Reset the wasMaximized property
}
}
Now for the snippets of code changes to EMRAppDelegate.m
if (type == kCGEventLeftMouseDown
|| type == kCGEventRightMouseDown) {
//..
//Skipped Unchanged Code
//..
//Find out if Window is Maximized
CFTypeRef TypeRef = nil;
if (AXUIElementCopyAttributeValue((AXUIElementRef)_clickedWindow, CFSTR("AXFullScreen"), &TypeRef)) {
if(Debug) NSLog(#"Could not get wasMaximized Value");
} else {
[moveResize setWasMaximized: CFBooleanGetValue(TypeRef)];
if(Debug) NSLog(CFBooleanGetValue(TypeRef) ? #"Updated Maximized to True" : #"Updated Maximized to False");
}
//Click FullScreen Button to UnMaximize the Window in Order to Move it
if([moveResize wasMaximized]) {
AXUIElementRef buttonRef = nil;
AXUIElementCopyAttributeValue(_clickedWindow, kAXFullScreenButtonAttribute, (CFTypeRef*)&buttonRef);
if(Debug) NSLog(#"buttonRef: %p", buttonRef);
AXUIElementPerformAction(buttonRef, kAXPressAction);
CFRelease(buttonRef);
}
//..
//Skipped Unchanged Code
//..
}
if (type == kCGEventLeftMouseUp
|| type == kCGEventRightMouseUp) {
//..
//Skipped Unchanged Code
//..
//Click FullScreen Button again to Maximize the Window (Since it started out as Maximized)
AXUIElementRef _clickedWindow = [moveResize window];
if([moveResize wasMaximized]) {
AXUIElementRef buttonRef = nil;
AXUIElementCopyAttributeValue(_clickedWindow, kAXFullScreenButtonAttribute, (CFTypeRef*)&buttonRef);
if(Debug) NSLog(#"buttonRef: %p", buttonRef);
AXUIElementPerformAction(buttonRef, kAXPressAction);
CFRelease(buttonRef);
[moveResize setWasMaximized: false];
}
//..
//Skipped Unchanged Code
//..
}
This worked for me. But I'm not an expert in Objective C or MacOS, so if you feel something can be improved, feel free to edit this post.

How to make "Browse/save file as" dialog show over NSMainMenuWindowLevel

I had the need to create a window so it covers the menubar at top and the dock at bottom. So I had to [NSWindow setLevel:NSMainMenuWindowLevel+1] (if i did just NSMainMenuWindowLevel some of the corner items in the top right of the menubar would still show over my window so I had to go +1).
So now the issue is, a user right clicks in my window (which is a canvas drawing of an image) and then they select "save as", at this point I pop open the "Save as" dialog, but it is showing behind my window. Is there anyway to like find that just opened dialog window with objc and set its level to be above NSMainMenuWindowLevel+1?
Like is there anyway to make this panel open higher then this level:
var NSSavePanel = objc_getClass('NSSavePanel');
var savePanel = sel_registerName('savePanel');
var aSavePanel = objc_msgSend(NSSavePanel, savePanel);
var runModal = sel_registerName('runModal')
Thanks
You have to defer the setting of the save panel's level until after the window is showing, which is tricky. You can do something like this (in Objective-C) before the call to -runModal:
dispatch_async(dispatch_get_main_queue(), ^{
[[NSApp modalWindow] setLevel:NSMainMenuWindowLevel+1];
});
If you can't use GCD, you can use -performSelector:withObject:afterDelay: with a 0 delay. You'll have to use the selector of a method of your own. (You can't use #selector(setLevel:) because that takes a scalar, not an object, as its parameter.)

Custom context menu XAML for WP8

I try to implement a custom ContextMenu in a LongListSelector.
I'm not using the ContextMenu from Microsoft.Phone.Controls.Toolkit, it's basically the same as in the Rowi App:
(source: hiddenpineapple.com)
Approach 1
My list item toggles a VisualState on hold and an overlay is shown with controls in it.
The problem
I can't find a way to go back to the default state when the user clicks outside of the list item (as in the default ContextMenu).
Approach 2
I've implemented a custom template for the toolkit ContextMenu which looks exactly the same. I had to move its margin top to -itemHeight, as by default it is below the item.
The problem
The problem with this solution is, that it automatically closes itself when opening and I couldn't figure out how to avoid this.
Another problem was that it didn't work well with TiltEffect.IsTiltEnabled from the Toolkit (visual problems).
I need your help
Any suggestions on how to get this working?
Answer
Thanks to Cheese, now I know how to properly close the menu when the user clicks outside.
His suggestion was to get the coordinates of a Tap event on the current page, and check if it's inside the menu. When not, close the menu.
So I added a Tap listener to the page when the menu opens, and removed it when the menu closes. From the page listener I got the event coordinates and could check if it's inside the control which holds the menu (same size and position). I received the position of the control with Point leftUpperPoint = control.TransformToVisual(page).Transform(new Point(0, 0)) and the rightLowerPoint by adding the ActualWidth and ActualHeight.
But then I realized:
Why should I even calculate if the tap is inside the menu? I always want to close the menu when the user taps anywhere on the screen. If it's outside, yes. If it's on a menu button, yes.
Another modification I made was to listen for MouseLeftButtonDown instead of Tap as it also triggers when the user swipes.
So I removed this code and came up with the following:
private void ToggleMenu(object sender, System.Windows.Input.GestureEventArgs e)
{
PhoneApplicationFrame frame = ((PhoneApplicationFrame)Application.Current.RootVisual);
VisualState state = this.States.CurrentState;
if (state == null || state.Name == "DefaultState")
{
frame.MouseLeftButtonDown += MouseDownDelegate;
this.State = "MenuState";
}
else
{
frame.MouseLeftButtonDown -= MouseDownDelegate;
this.State = "DefaultState";
}
}
private void MouseDownDelegate(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ToggleMenu(sender, null);
}
This works perfectly!
Thanks to Cheese for the hint.
Something like this by #denniscode http://dotnet.dzone.com/articles/rowi-show-tap-menu
Approach 1 problem
The best solution would be:
Get the menus coordinates, when user makes a tap - you check are tap coordinates on menu or not, if not - dissmiss - simple.
Approach 2 problem
I guess you had some button in a corner and when you tapped on it - nothing happened? And when you dissmissed the Tilt all worked. It seems that tilt works faster than a click, so, tilt changes the button coordinates, and device thiks you have missed/or dragged off
You can use what #ScottIsAFool suggested and maybe create another Dependency Property on your TapMenu control of type UIElement named CloseWhenTappedElement and automatically listen for Tap events inside your control once set. For example
<Grid x:Name="TapArea"/>
<TapMenu CloseWhenTappedElement="{Binding ElementName=TapArea"}/>

Animate opening a new window with NSWindowController

Starting with Mac OS X 10.7, new NSDocument windows as well as alert windows open with an animation. Windows opened with NSWindowController's showWindow: method, however, get no animation.
Is there a way for these windows to get the same animation?
You can define how a window opens.
In the Interface Inspector under Animation you can choose Document Window Style, and you should get the same behaviour.
Or in code:
[self.window setAnimationBehavior:NSWindowAnimationBehaviorDocumentWindow];
Here all the behaviours you can use
enum {
NSWindowAnimationBehaviorDefault = 0, // let AppKit infer animation behavior for this window
NSWindowAnimationBehaviorNone = 2, // suppress inferred animations (don't animate)
NSWindowAnimationBehaviorDocumentWindow = 3,
NSWindowAnimationBehaviorUtilityWindow = 4,
NSWindowAnimationBehaviorAlertPanel = 5
};

How can I interact with "iPad keyboard hiding button" programmatically?

There is a button at bottom right of iPad keyboard which is to hide the keypad.
How can I interact with it programmatically? (get the button then send UIControlEventTouchUpInside to it).
Does anyone know this?
[Edit]
In my case, the keyboard is shown on a modal view.
Overriding disablesAutomaticKeyboardDismissal to return NO as below allows you to dismiss the keyboard when you resignFirstResponder, even when your UITextView is on a modal view. You should put this code to your view controller, from which you initiate the keyboard:
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
Source: https://stackoverflow.com/a/6268520
In general, you would send the resignFirsResponder message to the active input view.
Something like this? I can't remember where I found this code but I used it to toggle the on-screen keyboard because it would be hidden by default if a bluetooth one was connected.
- (void) toggleKeyboard(UIKeyboardImpl * keyImpl){
if (UIKeyboardAutomaticIsOnScreen()) {
UIKeyboardOrderOutAutomatic();
} else {
UIKeyboardOrderInAutomatic();
}
Edit
I found where I got this code from. It works fine but the catch is that you need to import the private framework GraphicsServices, which would most likely get your app rejected from the App store.