Not being able to edit NSTextField on NSPopover even though Editable behavior is set

前端 未结 8 1412
别跟我提以往
别跟我提以往 2020-11-27 17:19

I have an application, which open popover with NSTextField. The text field is not editable. Behavior for text field is set to Editable. I still can

8条回答
  •  悲哀的现实
    2020-11-27 17:51

    What i found when working with this solution is that when NSStatusItem becomes unresponsive, you can easily override this behavior like this

    - (BOOL)canBecomeKeyWindow {
        if([self class]==NSClassFromString(@"NSStatusBarWindow")) {
            CBStatusBarView* view = [((CBAppDelegate*)[NSApp delegate]) statusItemView];
            if(![view isActive]) return NO;
        }
        return YES;
    }
    

    You will check for the class of the window, if it matches the NSStatusBarWindow we can then check somehow if the NSStatusItem is active. If it is, that means we have to return YES, because this way the NSPopover from NSStatusItem will have all keyboard events.

    What I'm using for checking if the NSStatusItem was clicked (or is active) is that in my own custom view i have a bool value which changes when user clicks on the NSStatusItem, system automatically checks for "canBecomeKeyWindow" and when it does it will return NO and after user clicks on it (while it is returning the NO) it will change the bool value and return YES when system asks again (when NSPopover is being clicked for NSTextField editing).

    Sidenotes:

    • CBStatusBarView is my custom view for NSStatusItem
    • CBAppDelegate is my App Delegate class

提交回复
热议问题