Adding LXDE start menu sections (or “sub-menus”)

This is a follow-up to my previous post about adding LXDE start menu entries.

So you are now an expert in the .desktop file format and you know how to create one and how to “link” it to one of the existing LXDE start menu sections (Accessories, Graphics, Internet, Office, Other, Sound & Video, System Tools).

But what if you want to create a new section ? Or what if you want to add a sub-section to an existing section ? Well: easy as pie !

OK, I’m lying: you will need to create and edit some XML files, but isn’t this even more funny than clicking on a ¬†“create section” button in a UI ? ūüėČ

How the LXDE main menu is configured

The default LXDE start menu layout is configured here: /etc/xdg/menus/lxde-applications.menu

It’an XML file that contains a bunch of <Menu> tags describing the menu layout. On my Fedora box, the “Accessories” section is described like this:

<Menu>
   <Name>Accessories</Name>
   <Directory>lxde-utility.directory</Directory>
   <Include>
      <And>
         <Category>Utility</Category>
         <Not><Category>System</Category></Not>
      </And>
   </Include>
</Menu>

This translates to “the ‘Accessories’ section is described in the lxde-utility.directory file and a .desktop file will appear in this section if its category is ‘Utility’“. Said differently: “if you want your program shortcut it to be displayed in the ‘Accessories’ section, then create a .desktop file containing “Categories=Utility”“.

If you are looking for the directory that contains the .directory files, it’s here: usr/share/desktop-directories.

How to customize the main menu

To customize your start menu, you will NOT modify the global  /etc/xdg/menus/lxde-applications.menu file, but you will instead create a file that will contain your own extensions. This means that each user will be able to get its own customized menu:

  • Custom main menu extensions must be configured in¬†~/.config/menus/lxde-applications.menu
  • Custom .directory files must be created in ~/.local/share/desktop-directories

Exercise #1: creating a new section in the main menu

First, create this file: ~/.config/menus/lxde-applications.menu file:

<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" "http://www.freedesktop.org/standards/menu-spec/1.0/menu.dtd">
<Menu>
   <Name>My Menu Extension</Name>
   <MergeFile type="parent">/etc/xdg/menus/lxde-applications.menu</MergeFile>
   <Menu>
      <Name>CustomMenu</Name>
      <Directory>custom_menu.directory</Directory>
      <Include>
         <And><Category>CustomMenuCategory</Category></And>
      </Include>
   </Menu>
</Menu>
  • The <MergeFile> tag says that we are going to extend the existing¬†/etc/xdg/menus/lxde-applications.menu configuration file.
  • Our new custom section is configured in the¬†~/.local/share/desktop-directories/custom_menu.directory file.
  • Each .desktop file containing¬†“Categories=CustomMenuCategory” will belong to this new section.

Next, create ~/.local/share/desktop-directories/custom_menu.directory:

[laurent@localhost desktop-directories]$ cat ~/.local/share/desktop-directories/custom_menu.directory
[Desktop Entry]
Encoding=UTF-8
Name=The Custom Menu Name

Then, create ~/.local/share/applications/custom_menu_entry.desktop (because the new section will not appear if there is no item inside):

[laurent@localhost applications]$ cat ~/.local/share/applications/custom_menu_entry.desktop
[Desktop Entry]
Name=custom menu entry
Exec=/usr/bin/foo
Comment=
Icon=
NoDisplay=false
Categories=CustomMenuCategory;
Type=Application

Finally, reload the menu by executing “lxpanelctl restart” and there you are:

Exercise #2: creating a sub-section in an existing main menu section

OK, we are now going to add a sub-section in the “Accessories” section.

First, the updated ~/.config/menus/lxde-applications.menu

<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" "http://www.freedesktop.org/standards/menu-spec/1.0/menu.dtd">
<Menu>
   <Name>My Menu Extension</Name>
   <MergeFile type="parent">/etc/xdg/menus/lxde-applications.menu</MergeFile>
   <Menu>
      <!-- parent section name: -->
      <Name>Accessories</Name>
      <Menu>
        !-- our new sub-section name: -->
        <Name>SubMenu</Name>
        <Directory>sub_menu.directory</Directory>
        <Include>
           <And><Category>SubMenuCategory</Category></And>
        </Include>
     </Menu>
   </Menu>
</Menu>

So what’s happening here ? Well, we are nesting our new SubMenu under the existing Accessories menu.
In order for this to work, your must make sure that the parent section name (“Accessories” in this example), is spelled exactly as in /etc/xdg/menus/lxde-applications.menu

Then, ~/.local/share/desktop-directories/sub_menu.directory:

[Desktop Entry]
Encoding=UTF-8
Name=The Sub Menu Name

Then, ~/.local/share/applications/sub_menu_entry.desktop:

[Desktop Entry]
Name=sub menu entry
Exec=hellotheworld
Comment=
Icon=
NoDisplay=false
Categories=SubMenuCategory;
Type=Application

Finally, “lxpanelctl restart” and there you are:

Exercise #3: conditional fun

If you look at your existing /etc/xdg/menus/lxde-applications.menu file, you will see that you can use conditional expressions to define how your .desktop files are going to be linked to your menu sections.

For example, this section definition:

<Menu>
   <Name>Administration</Name>
   <Directory>lxde-settings-system.directory</Directory>
   <Include>
      <And>
         <Category>Settings</Category>
         <Category>System</Category>
      </And>
   </Include>
</Menu>

Means that a .desktop file will be linked to the “Administration” section if its categories are “Settings” and “System” (Categories=Settings;System)

And this section definition:

<Menu>
   <Name>Accessories</Name>
   <Directory>lxde-utility.directory</Directory>
   <Include>
      <And>
         <Category>Utility</Category>
         <Not><Category>System</Category></Not>
      </And>
   </Include>
</Menu>

Means that a .desktop file will be linked to the “Accessories” section if its categories are “Utility” and not “System”. So “Categories=Utility;Foo” will link the .desktop file to the Accessories section, and¬†“Categories=Utility;System” will link it to the “System Tools” section.

You can also use the <Xor> tag, but I guess that you can easily understand how it works.

Laurent KUBASKI

Advertisements