SWT Popup Context Menu width issue

38 views Asked by At

I have a org.eclipse.ui.navigator.CommonViewer with various items in my project explorer view and there are different menu contributions for various tree nodes at different levels configured to be shown when I right click on the tree nodes and these menu contributions are defined in my plugin.xml file this way -

<extension
         point="org.eclipse.ui.menus">
  <menuContribution
            locationURI="popup:org.eclipse.ui.popup.any?after=additions"> 
    <menu
      id="com.example.plugin.contextMenu"
      label="Custom Context Menu">
      <command
            categoryId="com.ui.menuCategory"
            id="com.ui.saveAllModifiedValues"
            name="Save All the Modified Values In a File">
      </command>
      <command
            categoryId="com.ui.menuCategory"
            id="com.ui.saveCurrentModifiedValues"
            name="Save Current Modified Values In a File">
      </command>
     <command
            categoryId="com.ui.menuCategory"
            id="com.ui.load"
            name="Load">
      </command>
    </menu>
  </menuContribution>
</extension>

And, I also have visibleWhen expressions defined to show/hide context menu entries based on which tree node is selected (file or a folder etc..) and as you can see the menu name label can vary in size. The issue I'm facing is that when the context menu is shown with a large label such as "Save All the Modified Values In a File" then the menu width expands to show the entire label text correctly, however the next time the menu is shown on a different tree node selection that does not include large labels then the menu width does not shrink or adjust it's size but unnecessarily is too wide even for small label entries and it looks like the menu width is cached when expanded to show large label text but the width is not recalculated to show small label text menu entries.

I tried a lot of things such as dynamically calculating the width of the widest menu item and then adjust the width of the parent menu but it looks like SWT's MenuItem and Menu classes indeed lack direct methods for setting styles, width, or bounds of a context menu and their sizes are determined by the native controls SWT calls.

Is there any other way to address the menu width issue?

0

There are 0 answers