Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make NSWindow look disabled and not respond to user input?

Tags:

macos

cocoa

I'm not sure that there's some system-level feature like that. Anyway, is there a way to make NSWindow looks disabled and does not respond to user input?

like image 769
eonil Avatar asked Oct 06 '22 19:10

eonil


1 Answers

I don't think mdominick's answer is correct. Since I only wanted to disable the NSTextFields and NSButtons, I came up with this:

for (NSView *item in [self.window.contentView subviews])
{
    if ([item isKindOfClass:[NSTextField class]] || [item isKindOfClass:[NSButton class]])
    {
        [(id)item setEnabled:NO];
    }
}

EDIT

Whilst working on a new app, this method in a subclass of NSWindow was really convenient

- (void)setEnabled:(BOOL)enabled
{
    [self setEnabled:enabled view:self.window.contentView];
}

- (void)setEnabled:(BOOL)enabled view:(id)view
{
    if ([view isHidden]) return;

    for (NSView *item in [view subviews])
    {
        if ([item isKindOfClass:[NSProgressIndicator class]])
        {
            NSProgressIndicator *pI = (NSProgressIndicator *)item;
            [pI setHidden:enabled];
            if (!enabled) [pI startAnimation:nil];
            if (enabled) [pI stopAnimation:nil];
        }
        if ([item isKindOfClass:[NSTextField class]] || [item isKindOfClass:[NSButton class]] || [item isKindOfClass:[NSTextView class]])
        {
            [(id)item setEnabled:enabled];
            continue;
        }
        if (item.subviews.count)
        {
            [self setEnabled:enabled view:item];
        }
    }
}
like image 109
mmackh Avatar answered Oct 20 '22 14:10

mmackh