Window resizable - kAXGrowAreaAttribute always return NULL - cocoa

In my app I want to check if windows from others apps are resizable.
I´m using the accessibility API to test if the window has the kAXGrowAreaAttribute attribute (if NULL is not resizable) as Peter Hosey answered in this question.
The problem is that the kAXGrowAreaAttribute returned value is always NULL it doesn´t matter if the window is resizable or not. Note: to retrieve the value I´m using the UIElementUtilities class from the Apple UIElementInspector example (I also have tried using AXUIElementCopyAttributeValue directly with the same result).
Any idea? I´m working in Lion, could be this the problem? Thanks in advance.
EDITED:
Playing around with the UIElementUtilities class methods I found a solution.
Just use the method
+ (BOOL)canSetAttribute:(NSString *)attributeName ofUIElement:(AXUIElementRef)element
with the kAXSizeAttribute and the focused window. It returns YES or NO depending if the window is sizable or not...

It probably is because you're in Lion. The size box was killed off; resizable windows are resizable at every edge now.
And yes, testing whether the size can be changed is probably the right way. It seems to work for me in Snow Leopard.

Swift 5 version:
func isResizable(axElement: AXUIElement) -> Bool {
var resizable: DarwinBoolean = true
let status = AXUIElementIsAttributeSettable(axElement, kAXSizeAttribute as CFString, &resizable)
if status != .success {
print("unable to determine if window is resizable")
}
return resizable.boolValue
}

Related

Backspace stops working in Flutter for desktop (Windows), if user switches input language by Win+Space

I have a strange problem:
Backspace stops working in Flutter for desktop (Windows), if user switches input language by Win+Space!
I found an issue pointing exactly same problem:
https://github.com/flutter/flutter/issues/73377
But I am looking for a workaround, since they are not resolving the issue since Jan 2021.
Any suggestion?
It seems to be resolved in the Windows version with the latest Flutter but not in MacOS.
In case someone is still stuck on this bug. It seems there is no classic way of solving it and most workarounds are volatile and unpredictable. The best solution is to adjust how we can handle the problem at hand.
problem:
The delete, backspace, and/or Arrow keys do not work as expected on Controller-controlled TextInputs and FormInputs on Flutter;
Solution:
create two widgets, a Text widget, and the input widget then switch them as required.
IMPORTANT
Do not use Controller on your input just use the initialValue property and onchanged(){} Method.
menuProductProviderCart.isReceivedAmountEdit
?TextFormField(
keyboardType:TextInputType.number,
// controller: _receivedAmountController//commented My TextEditcontroller out!!!!!,
onChanged: (amount) {
menuProductProviderCart.setReceivedAmount(double.parse(amount));
},
initialValue: 0.toString(),
enableInteractiveSelection:true,
...
When I click on bills denominations I change widget to Text; when I click on the received amount which is the text widget I change to TextInputinput:
This way you are in control of your code and it will never be unpredictable

How to detect the "Accessibility Reduce Transparency" In macOS (Objective-C)?

By default, macOS11.0 Big Sur set the "reduce transparency" to true on Accessibility. That makes the light status menu almost the same as the dark one, thus make the setting of my app's status menubar icon becomes a challenge.
On iOS, a simple UIKit function could do the trick:
if (UIAccessibilityIsReduceTransparencyEnabled()){
//use the icon for dark mode although we are in light mode now
}
But there is no way to implement it on macOS although the Apple documentation indicated the Availability is iOS 8.0+ and macOS Catalyst 13.0+
Any ideas? Thank you!
You can use the defaults read com.apple.universalaccess reduceTransparency command. It's 1 if it's enabled, and 0 if it's not. In Objective-C that looks something like this:
- (BOOL)isTransparencyEnabled {
Boolean keyExists = false;
Boolean transparencyEnabled = CFPreferencesGetAppBooleanValue( CFSTR("reduceTransparency"), CFSTR("com.apple.universalaccess"), &keyExists);
return keyExists && transparencyEnabled;
}

How to deselect the contents of a TextField in swift

I have a simple desktop app where a TextField should be focused when the window loads. I have this working, but it's a little annoying that, having loaded the users content into the TextField, the entire contents of the field become selected automatically. The user may want to start editing the content, but they will rarely/never want to replace it all at once (imagine a text editor doing this, to see what I mean).
I see there is an Action for selectAll: but what I want is the opposite Action of selectNone:
I tried passing nil to the selectText method, but that doesn't work:
textField.selectText(nil)
I found a number of answers on StackOverflow that mention a selectedTextRange, but this appears to be outdated, because Xcode 6.3 doesn't recognize this as a valid property on TextField.
Can anyone explain how I do this?
It's been a while since I've dealt with NSTextFields to this level (I work mostly in iOS these days).
After doing a little digging I found this on the net:
NSText* textEditor = [window fieldEditor:YES forObject:textField];
NSRange range = {start, length};
[textEditor setSelectedRange:range];
window is the window containing your field, textField.
This requires the field editor to be managing your field, what can be done simply by previously selecting the whole text of the field using the selectText:sender method.
Here is the final swift code that I got working based on what Duncan C posted:
if let window = NSApplication.sharedApplication().mainWindow {
let textEditor = window.fieldEditor(true, forObject: textField)!
let range = NSRange(0..<0)
textEditor.selectedRange = range
}

How do I detect "invalid drawable" in Mac OS X?

I am working on a cross platform application that is over a decade old. The UI is by Qt and backend rendering is done with OpenGL. OpenGL contexts are managed in the back end, not by Qt.
I have recently added error checking and reporting for all OpenGL code in our app. Occasionally a situation arises where the first render initiated by Qt causes an "invalid drawable" error message in the terminal and all subsequent OpenGl calls fail with an "invalid framebuffer" error reported. These invalid drawable error messages have been treated as innocuous in the past, since before the user sees it the drawable eventually becomes valid and the scene is rendered correctly. However, with the new OpenGL error check/report it's not possible since there are large numbers of errors reported.
I would like to test if the drawable is valid. If it is not, it should return before the render starts. How can I verify that the drawable is valid?
MacBook Pro, OS X Mountain Lion (10.8.3), ati graphics card
I don't know at what API level you're working. I'm not sure it's possible to detect the problem after the fact. That is, if all you have is a context (perhaps implicit as the thread's current context) that failed to connect to its drawable.
I presume that Qt is using Cocoa under the hood. I further assume it has created an NSOpenGLContext and is invoking -setView: on it. You get that "invalid drawable" error if, at the time of that call, the view's window doesn't have a window device.
One common technique is to defer setting the context's view until the view has -drawRect: called on it, since at that point you're sure that the view has a window and the window has a device. (Although that ignores the possibility of forced drawing outside of the normal window display mechanism. For example, -cacheDisplayInRect:toBitmapImageRep:.)
If you just want to know at the point of the call to -setView: whether it's safe or not, I think you can rely on checking the value of [[view window] windowNumber]. The docs for -windowNumber say:
If the window doesn’t have a window device, the value returned will be equal to or less than 0.
Another approach is to prevent the problem rather than detect it. The strategy for that is basically to make sure the window has been shown and drawn before the call to -setView:. You may be able to force that by ordering it on-screen and invoking -display on it.
Ken Thomases post gave me the basic info I needed to find a workable solution. An additional conditional was needed. Here's what worked
//----------------------------------------------------------------------------
bool vtkCocoaRenderWindow::IsDrawable()
{
// you must initialize it first
// else it always evaluates false
this->Initialize();
// first check that window is valid
NSView *theView = (NSView*)this->GetWindowId();
bool win =[[theView window] windowNumber]>0;
// then check that the drawable is valid
NSOpenGLContext *context = (NSOpenGLContext *)this->GetContextId();
bool ok = [context view] != nil;
return win && ok;
}

OSX: How to filter out the menu bar from CGWindowListCopyWindowInfo

I'm using CGWindowListCopyWindowInfo to grab the list of windows on the desktop. I'd like to filter out only the visible windows using values of the Window List keys. The one window I'm having trouble is the OSX menu bar. One solution I've thought of using is looking at the X and Y of the window bounds. No visible windows seem to have both of those equal to 0, but I'm not sure how reliable this method is.
One other way to do this would be to compare the PID number to that of the OSX Window Server, but I'm not sure how to get that. Can anyone point me towards the right API or know of a reliable way to filter out the menu bar?
Edit: I have the same code as kondy below with the following additions since the listOptions themselves aren't good enough:
CGRect windowBounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef) [windowInfo objectForKey:(id)kCGWindowBounds], &windowBounds);
if (!(windowBounds.origin.x == 0 && windowBounds.origin.y == 0))
{
// Work with windows that aren't the Menubar
}
I have found an answer to filter out the "Window Server":
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
Using these ORed options, I get a result as same as mac's "Windowed processes" in "Activity Manager"
I hope it will help you!

Resources