Discussion:
is there a way to determine if an NSButton is toggleable ?
Guillaume Laurent
2018-08-20 10:04:12 UTC
Permalink
Hi all,


For a custom UI I’ve had to write a custom control deriving from NSButton, which highlights itself in a special way on mouse-over. In the method which does the highlighting, I check if the button’s state is either .on or .off, so I know which title or alternateTitle to display. But I realized that, no matter the button's type, the state is always toggled on click. That is, even if the button’s type is set to MomentaryPushIn, the button’s state is toggled after a click. I’d have thought that this was the case only for OnOff/Toggle/Switch… types.

Since there is no NSButton.type getter, is there a way to determine if the button’s type is toggleable or not ? And what is the reason for switching state for types like MomentaryPushIn ?


Thanks,

Guillaume.
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

Th
Jeff Nadeau
2018-08-20 23:39:06 UTC
Permalink
NSButton is interesting in that it separates out the state (e.g. `isHighlighted` and `state`) from the presentation of that state. The state manipulation behavior is totally uniform, i.e. the `state` property always toggles between Off and On when any button is clicked. Most buttons aren't configured to draw differently when On, so you don’t notice it.

Subclassers often just ignore this and implement whatever drawing behavior they want, but if you want to be sensitive to the configuration, you’ll want to check the showsStateBy mask on the cell.

• For a Toggle button, which exchanges contents based on state, the NSContentsCellMask bit will be set.
• For a On/Off button, which reflects the state in the button artwork, the NSChangeBackgroundCellMask bit will be set.

It’s legal for a button cell to have any combination of bits configured, but none of the built-in NSButtonTypes set both at once.

- Jeff
Post by Guillaume Laurent
Hi all,
For a custom UI I’ve had to write a custom control deriving from NSButton, which highlights itself in a special way on mouse-over. In the method which does the highlighting, I check if the button’s state is either .on or .off, so I know which title or alternateTitle to display. But I realized that, no matter the button's type, the state is always toggled on click. That is, even if the button’s type is set to MomentaryPushIn, the button’s state is toggled after a click. I’d have thought that this was the case only for OnOff/Toggle/Switch… types.
Since there is no NSButton.type getter, is there a way to determine if the button’s type is toggleable or not ? And what is the reason for switching state for types like MomentaryPushIn ?
Thanks,
Guillaume.
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
https://lists.apple.com/mailman/options/cocoa-dev/jnadeau%40apple.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in.narkive.ne
Guillaume Laurent
2018-08-21 09:27:55 UTC
Permalink
Post by Jeff Nadeau
NSButton is interesting in that it separates out the state (e.g. `isHighlighted` and `state`) from the presentation of that state. The state manipulation behavior is totally uniform, i.e. the `state` property always toggles between Off and On when any button is clicked. Most buttons aren't configured to draw differently when On, so you don’t notice it.
I’d s/interesting/really weird/ here :-). I wonder what decisions led to this design, certainly a lot of food for thought.

I’ve used NSButtons for quite a while and it’s only now when trying to heavily customize it that I’m aware of this issue. It really goes against the intuition you get when reading the API doc, that is you expect a MomentaryPushIn to be on only during mouse down, and off otherwise, as opposed to Toggle. Then there’s also the issue of several types looking very similar (Toggle, ClickOn/Off, etc…).
Post by Jeff Nadeau
Subclassers often just ignore this and implement whatever drawing behavior they want, but if you want to be sensitive to the configuration, you’ll want to check the showsStateBy mask on the cell.
• For a Toggle button, which exchanges contents based on state, the NSContentsCellMask bit will be set.
• For a On/Off button, which reflects the state in the button artwork, the NSChangeBackgroundCellMask bit will be set.
It’s legal for a button cell to have any combination of bits configured, but none of the built-in NSButtonTypes set both at once.
Thank you very much for your reply.
Post by Jeff Nadeau
- Jeff
Post by Guillaume Laurent
Hi all,
For a custom UI I’ve had to write a custom control deriving from NSButton, which highlights itself in a special way on mouse-over. In the method which does the highlighting, I check if the button’s state is either .on or .off, so I know which title or alternateTitle to display. But I realized that, no matter the button's type, the state is always toggled on click. That is, even if the button’s type is set to MomentaryPushIn, the button’s state is toggled after a click. I’d have thought that this was the case only for OnOff/Toggle/Switch… types.
Since there is no NSButton.type getter, is there a way to determine if the button’s type is toggleable or not ? And what is the reason for switching state for types like MomentaryPushIn ?
Thanks,
Guillaume.
_______________________________________________
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com <http://lists.apple.com/>
https://lists.apple.com/mailman/options/cocoa-dev/jnadeau%40apple.com <https://lists.apple.com/mailman/options/cocoa-dev/jnadeau%40apple.com>
_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.narkive.net

This email sent to ***@ml-in
Uli Kusterer
2018-08-23 03:03:26 UTC
Permalink
Post by Guillaume Laurent
Hi all,
For a custom UI I’ve had to write a custom control deriving from NSButton, which highlights itself in a special way on mouse-over. In the method which does the highlighting, I check if the button’s state is either .on or .off, so I know which title or alternateTitle to display. But I realized that, no matter the button's type, the state is always toggled on click. That is, even if the button’s type is set to MomentaryPushIn, the button’s state is toggled after a click. I’d have thought that this was the case only for OnOff/Toggle/Switch… types.
Since there is no NSButton.type getter, is there a way to determine if the button’s type is toggleable or not ? And what is the reason for switching state for types like MomentaryPushIn ?
You want to look at the showsStateBy bit field. See:

http://orangejuiceliberationfront.com/building-a-custom-nsbutton/

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de

_______________________________________________

Cocoa-dev mailing list (Cocoa-***@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/gegs%40ml-in.nark
Loading...