Documentation
¶
Overview ¶
Package menu implements the freedesktop.org Desktop Menu Specification: it reads the applications.menu XML file and turns it into a categorized tree of installed applications — the "category menu" an application launcher shows (Accessories, Graphics, System, ...).
It deliberately does not reinvent the layers below it. The individual .desktop entries are enumerated and parsed by github.com/go-freedesktop/desktopentry (its desktopentry.ScanDirs and desktopentry.Entry), and the XDG base-directory resolution is delegated to github.com/adrg/xdg. On top of those this package adds the menu-spec gap:
- parse the <Menu> tree of an applications.menu file, including <Include>/<Exclude> match rules (<And>/<Or>/<Not>/<Category>/ <Filename>/<All>), <AppDir>/<DefaultAppDirs>, <DirectoryDir>/ <DefaultDirectoryDirs>, <MergeFile>/<MergeDir>/<DefaultMergeDirs>, <Directory>, <OnlyUnallocated>, <Deleted>, <Move> and <Layout>;
- merge referenced menu files, consolidate menus sharing a <Name>, apply <Move> rules and drop <Deleted> menus, per the spec;
- resolve the include/exclude rule expressions against the scanned desktop entries, honoring the two-phase <OnlyUnallocated> allocation;
- resolve each menu's <Directory> .directory file for its display name and icon;
- order the result with the menu's <Layout>/<DefaultLayout>.
The result is a Tree of Menu nodes, each exposing its display Name, Icon and the ordered lists of Submenus and Apps.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Menu ¶
type Menu struct {
// Name is the menu's <Name>, the identifier used by <Move>, <Layout>
// and same-name consolidation. It is never localized.
Name string
// DirectoryName is the user-visible label: the Name key of the menu's
// resolved <Directory> .directory file, or Name when there is none.
DirectoryName string
// Icon is the icon name/path from the .directory file (feeds an
// icon-theme lookup), empty when unset.
Icon string
// Comment is the localized description from the .directory file.
Comment string
Submenus []*Menu
// Apps are the application entries allocated to this menu, ordered per
// the menu's layout.
Apps []*desktopentry.Entry
}
Menu is one node of the resolved application menu.
type Tree ¶
type Tree struct {
// Root is the top-level menu (typically named "Applications").
Root *Menu
}
Tree is a fully resolved application menu.
func Load ¶
Load builds the application menu from the standard locations: the applications.menu file resolved through the XDG config directories (with the $XDG_CONFIG_HOME copy overriding the system one), the applications/ directories, the desktop-directories/ directories and the menus/applications-merged/ merge directories, all provided by github.com/adrg/xdg. Entry visibility honors $XDG_CURRENT_DESKTOP.
func LoadWithDirs ¶
func LoadWithDirs(menuFile string, appDirs, dirDirs, mergeDirs []string, current string) (*Tree, error)
LoadWithDirs is the directory-injectable form of Load, intended for tests and for embedders that manage their own search paths. menuFile is the applications.menu file to read; appDirs, dirDirs and mergeDirs supply the concrete directories that the XML's <DefaultAppDirs>, <DefaultDirectoryDirs> and <DefaultMergeDirs> elements expand to; current is the desktop environment name used for OnlyShowIn/NotShowIn filtering (empty means "show everything").
Example ¶
ExampleLoadWithDirs builds the categorized application menu from an injected set of directories (the same shape menu.Load uses with the standard XDG paths) and prints each top-level category with its resolved display name and application count.
package main
import (
"fmt"
"github.com/go-freedesktop/menu"
)
func main() {
tree, err := menu.LoadWithDirs(
"testdata/menus/applications.menu",
[]string{"testdata/applications"},
[]string{"testdata/desktop-directories"},
[]string{"testdata/menus/applications-merged"},
"", // show entries for every desktop environment
)
if err != nil {
panic(err)
}
fmt.Println(tree.Root.DirectoryName)
for _, m := range tree.Root.Submenus {
fmt.Printf(" %-12s %d app(s)\n", m.DirectoryName, len(m.Apps))
}
}
Output: All Applications Accessories 2 app(s) Games 2 app(s) Graphics 1 app(s) Network 1 app(s) Other 1 app(s) System 1 app(s)