user99950
user99950

Reputation:

Disable NSToolbar customisation via window's toolbar button?

I am looking to disable the Command + Click combination on a toolbar button (located top-right) in a Cocoa window. I would still like to let the user show and hide the toolbar, but I do not want them to be able to select a different display mode (e.g. small icons, no icons, etc).

Has anyone found a way to do this? Thanks in advance.

Upvotes: 5

Views: 1721

Answers (3)

Shebuka
Shebuka

Reputation: 3228

On macOS 15+ you need to set this new value allowsDisplayModeCustomization to NO on the instance of NSToolbar of your NSWindow. The easiest way is to have an IBOutlet connected to it and then do this in awakeFromNib:

// self.ToolB_All is my NSToolbar
self.ToolB_All.allowsUserCustomization = NO;   // for completion
if (@available(macOS 15.0, *)) {
    self.ToolB_All.allowsDisplayModeCustomization = NO;
}

On older systems, you'll still need to subclass.

Upvotes: 0

sbooth
sbooth

Reputation: 16986

You don't need to subclass NSToolbar to do this. In your NSWindowController subclass, put the following in your awakeFromNib:

- (void) awakeFromNib
{
  NSToolbar *tb = [[self window] toolbar];
  [tb setAllowsUserCustomization:NO];
}

You also have the added benefit of avoiding private API use.

Upvotes: 8

smorgan
smorgan

Reputation: 21599

Have you tried using a custom NSToolbar subclass that overrides setDisplayMode: and setSizeMode: to do nothing? That won't remove the menu items of course, or the UI in the customization sheet (assuming you aren't disabling that as well with setAllowsUserCustomization:), but it might prevent them from doing anything.

Upvotes: 3

Related Questions