findClosestIcon

Find icon of the closest size. It uses icon theme cache wherever possible. The first perfect match is used. This is similar to findClosestThemedIcon, but returns file path only and allows to search for non-themed icons.

  1. string findClosestIcon(string iconName, uint desiredSize, IconThemes iconThemes, BaseDirs searchIconDirs, Exts extensions, Flag!"allowNonThemed" allowNonThemed)
    string
    findClosestIcon
    (
    alias subdirFilter = (a => true)
    IconThemes
    BaseDirs
    Exts
    )
    (
    string iconName
    ,,
    IconThemes iconThemes
    ,,,
    Flag!"allowNonThemed" allowNonThemed = Yes.allowNonThemed
    )
  2. string findClosestIcon(string iconName, uint desiredSize, IconThemes iconThemes, BaseDirs searchIconDirs, Exts extensions, Flag!"allowFallbackIcon" allowFallback)
  3. string findClosestIcon(string iconName, uint size, IconThemes iconThemes, BaseDirs searchIconDirs)

Parameters

iconName string

Name of icon to search as defined by Icon Theme Specification (i.e. without path and extension parts).

desiredSize uint

Preferred icon size to get.

iconThemes IconThemes
searchIconDirs BaseDirs

Base icon directories.

extensions Exts

Allowed file extensions.

allowNonThemed Flag!"allowNonThemed"

Allow searching for non-themed icon if could not find icon in themes (non-themed icon can be any size).

Return Value

Type: string

Icon file path or empty string if not found. Note: If icon of some size was found in the icon theme, this algorithm does not check following themes, even if they contain icons with closer size. Therefore the icon found in the more preferred theme always has presedence over icons from other themes.

Examples

auto baseDirs = ["test"];
auto iconThemes = [openIconTheme("Tango", baseDirs), openIconTheme("hicolor", baseDirs)];

string found;

//exact match
found = findClosestIcon("folder", 32, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "32x32/places", "folder.png"));

// with subdir filter
found = findClosestIcon!(subdir => subdir.context == "Places")("folder", 32, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "32x32/places", "folder.png"));

// not exact match
found = findClosestIcon!(subdir => subdir.context == "Places")("folder", 24, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "32x32/places", "folder.png"));

// no match, wrong subdir
found = findClosestIcon!(subdir => subdir.context == "MimeTypes")("folder", 32, iconThemes, baseDirs);
assert(found.empty);

//hicolor has exact match, but Tango is more preferred.
found = findClosestIcon("folder", 64, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "32x32/places", "folder.png"));

//find xpm
found = findClosestIcon("folder", 32, iconThemes, baseDirs, [".xpm"]);
assert(found == buildPath("test", "Tango", "32x32/places", "folder.xpm"));

//lookup non-themed
found = findClosestIcon("pidgin", 96, iconThemes, baseDirs);
assert(found == buildPath("test", "pidgin.png"));

//don't lookup non-themed
found = findClosestIcon("pidgin", 96, iconThemes, baseDirs, defaultIconExtensions, No.allowNonThemed);
assert(found.empty);

See Also

Meta