findClosestIcon

Find icon closest of the size. It uses icon theme cache wherever possible. The first perfect match is used.

  1. string findClosestIcon(string iconName, uint size, IconThemes iconThemes, BaseDirs searchIconDirs, Exts extensions, Flag!"allowFallbackIcon" allowFallback)
    string
    findClosestIcon
    (
    alias subdirFilter = (a => true)
    IconThemes
    BaseDirs
    Exts
    )
    (
    string iconName
    ,
    uint size
    ,
    IconThemes iconThemes
    ,,,
    Flag!"allowFallbackIcon" allowFallback = Yes.allowFallbackIcon
    )
  2. 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).

size uint

Preferred icon size to get.

iconThemes IconThemes
searchIconDirs BaseDirs

Base icon directories.

extensions Exts

Allowed file extensions.

allowFallback Flag!"allowFallbackIcon"

Allow searching for non-themed fallback 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"));

found = findClosestIcon("folder", 24, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "24x24/devices", "folder.png"));

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

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

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"));

//find big png, not exact match
found = findClosestIcon("folder", 200, iconThemes, baseDirs);
assert(found == buildPath("test", "Tango", "128x128/places", "folder.png"));

//svg is closer
found = findClosestIcon("folder", 200, iconThemes, baseDirs, [".png", ".svg"]);
assert(found == buildPath("test", "Tango", "scalable/places", "folder.svg"));

//lookup with fallback
found = findClosestIcon("pidgin", 96, iconThemes, baseDirs);
assert(found == buildPath("test", "pidgin.png"));

//lookup without fallback
found = findClosestIcon("pidgin", 96, iconThemes, baseDirs, defaultIconExtensions, No.allowFallbackIcon);
assert(found.empty);

found = findClosestIcon("text-plain", 48, iconThemes, baseDirs);
assert(found == buildPath("test", "hicolor", "48x48/mimetypes", "text-plain.png"));

found = findClosestIcon!(subdir => subdir.context == "MimeTypes")("text-plain", 48, iconThemes, baseDirs);
assert(found == buildPath("test", "hicolor", "48x48/mimetypes", "text-plain.png"));

found = findClosestIcon!(subdir => subdir.context == "Actions")("text-plain", 48, iconThemes, baseDirs);
assert(found.empty);

See Also

icontheme.paths.baseIconDirs, lookupIcon, findFallbackIcon, iconSizeDistance

Meta