eonil
eonil

Reputation: 85955

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

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?

Upvotes: 5

Views: 3336

Answers (4)

Debasis Das
Debasis Das

Reputation: 102

As a different option, one can have a hidden window with NSMakeRect(0,0,10,0) height 0 and write a disableWindow method in the windowController or window subclass that throws a panel with the above hidden window. On opening the panel the screen will be disabled. enableWindow would alternatively orderOut the above stated hiddenWindow re-enabling the main window.

Upvotes: 0

mmackh
mmackh

Reputation: 3630

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];
        }
    }
}

Upvotes: 4

Git.Coach
Git.Coach

Reputation: 3092

Since the first answer would still allow keyboard inputs, I would make it clean and walk trough each UI element in a loop and set it to disabled in addition to make the window half visible. (Edit: mmackh's answer has the code to do this.)

Or you you use the easy way and simply add an invisible NSView on top of your window that is firstResponder and "catches" all inputs.

Upvotes: 0

mdominick
mdominick

Reputation: 1319

You could try something like:

[someWindow setAlpha:0.5]; // you can change this number to you visual taste.
[someWindow setIgnoresMouseEvents:YES];

Upvotes: 0

Related Questions