Placing a menu item in a specific location

#1

I’ve noticed some extensions place new menu items under existing menus (like “Window”) but that these do not get added to the bottom of the menu items. I haven’t figured out if this is intentional or accidental. I’d like to add an item to the “Edit” menu, but place it about halfway down, with Hide & Unhide. Is there a means of accomplishing this?

0 Likes

#2

Only on Windows platform. There is an undocumented integer 2nd argument for the menu position in the Sketchup::Menu#add_item() instance method.

However, as it is undocumented, it might be removed at any future release (especially if misused by coders.)

0 Likes

#3

Thanks. This actually works on Mac for me (SU2018). You have to count menu dividers to get the correct positioning.

0 Likes

#4

Yes, dividers are added like menu items.

Though as you don’t have a way of specifying “below the menu item that is labeled ‘Unhide’ in the English-localization”, it depends on that the stays as it currently is in your SketchUp. If future SketchUp releases change menus or if other extensions add items above, it will not match anymore.
To be save, you could check for the method arity (menu.method(:add_item).arity >= 2) and pass the second parameter only if it supports this undocumented parameter.

0 Likes

#5

I would like to recommend to use custom menu position very sparsely. I’ve done it in a handful of extension when my added functionality directly relates to native functionality, e.g. Flip Individually Along being next to Flip Along or Open Newer Version… being next to Open… Doing this is lightly “risky” as the position will be off a bit if another extension add its menu items above it. In some situations were it would just feel awkward to have an entry at the end of the list and not grouped with similar entries I think it is justified though.

0 Likes

#6

Hmmm. I thought I remembered Mac users saying this didn’t work on Mac ?
Well, I stand corrected for SU2018 at least.

0 Likes

#7

Agreed. Here grouping new hide-related commands with other Hide menu items motivates a custom placement, but hard coding with a numerical offset is certainly brittle. I suppose you could keep it updated using Sketchup.version_number.

1 Like

#8

Disclaimer: I haven’t (yet) done any serious work with Ruby. The idea presented is just from general programming background knowledge.

Does Ruby have a way to READ the existing menus by menu position? If so, then why not (semi) future proof tools that want to add menu items in a specific location by reading the menus to find the built in item you desire to place your item under (or over), then using the menu item creation facility and the undocumented 2nd parameter.

While still relying on an undocumented facility that could disappear, at least you’d know you’re putting the new menu item where you want it - even if other extensions have inserted items - or if the menu isn’t ordered as you expect.

0 Likes

#9

On Windows we can use Microsoft API C calls using Ruby’s Fiddle library. It requires a good knowledge of C programming and the Microsoft Windows SDK (ie, exported DLL functions.)

Perhaps there is a similar means using Apple function calls, but I am unfamiliar with them.

There is also a 3rd party Win32-API Ruby library (originally by Daniel Berger) that is now in maintenance mode.

0 Likes