Click-Through Disabler
By default, Cocoa controls accept clicks when their window is in the back. This is called click-through. However, Mac OS X Human Interface Guidelines recommend that a control with destructive or difficult to undo action disable themselves when their window is inactive to avoid accidental clicks.
A MFClickThroughDisabler
object disables or enables a control depending on the window state and by doing so remove it’s click-through behaviour. This prevent a control from being clicked while the window is not the main window.
Files
MFClickThroughDisabler.h
MFClickThroughDisabler.m
Usage
Borrowed from the aqua human interface guidelines, here are the cases you should disable click-through:
Don’t provide click-through for items or actions that:
- are potentially harmful (for example, the Delete button in Mail)
- are difficult to recover from, such as
- actions that are difficult or impossible to cancel (the Send button in Mail)
- dismissing a dialog without knowing what action was taken (for example, it’s not easy to “unsave” a document)
- removing the user from the current context (selecting a new item in a column, for example, can change the target of the Finder window)
Also, you should not disable click-through for actions that bring a confirmation dialog.
For a control in an nib file
Before using this class, you should include the files with your program sources. Once MFClickThroughDisabler
is part of your project, open the nib file with the control you want to disable and make IB read MFClickThroughDisabler.h to add the class to the hierarchy. You can then instantiate an object of this class.
From the instantiated MFClickThroughDisabler
, ctrl-drag a line to the control you want to disable click-through. From the connection inspector, connect the control outlet. Voilà!
For a progamaticaly created control
If your control is created by your code, not from a nib file, you will need to attach the disabler in your code too. This is simple, just create a MFClickThroughDisabler
connected to your control, and make sure it is retained by your controller or somewhere else.
See the header file for details.
Known issue
If you want to disable a control that has a MFClickThroughDisabler attached, you should first detach the disabler using [disabler setControl:nil]
to prevent it from enabling the control when the window become main again.
License
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 2 of the License or any later version.
This means that you can include the source into your program, but if you make some change to it you must publish these changes. That’s all about it.