findClosestThemedIcon

Find icon of the closest size. It uses icon theme cache wherever possible. The first perfect match is used. It searches only for icons in themes.

  1. auto findClosestThemedIcon(string iconName, uint desiredSize, IconThemes iconThemes, BaseDirs searchIconDirs, Exts extensions)
    findClosestThemedIcon
    (
    alias subdirFilter = (a => true)
    IconThemes
    BaseDirs
    Exts
    )
    (
    string iconName
    ,,
    IconThemes iconThemes
    ,,)
  2. auto findClosestThemedIcon(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.

Return Value

Type: auto

IconSearchResult. filePath will be empty if icon is 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

1 auto baseDirs = ["test"];
2 auto iconThemes = [openIconTheme("Tango", baseDirs), openIconTheme("hicolor", baseDirs)];
3 
4 //exact match
5 auto found = findClosestThemedIcon("folder", 32, iconThemes, baseDirs);
6 assert(found.filePath == buildPath("test", "Tango", "32x32/places", "folder.png"));
7 assert(found.subdir.size == 32);
8 assert(found.subdir.context == "Places");
9 assert(found.iconTheme.internalName == "Tango");
10 
11 found = findClosestThemedIcon("folder", 24, iconThemes, baseDirs);
12 assert(found.filePath == buildPath("test", "Tango", "24x24/devices", "folder.png"));
13 assert(found.subdir.size == 24);
14 assert(found.subdir.context == "Devices");
15 
16 // with subdir filter
17 found = findClosestThemedIcon!(subdir => subdir.context == "Places")("folder", 32, iconThemes, baseDirs);
18 assert(found.filePath == buildPath("test", "Tango", "32x32/places", "folder.png"));
19 assert(found.subdir.size == 32);
20 
21 // non-exact match
22 found = findClosestThemedIcon!(subdir => subdir.context == "Places")("folder", 24, iconThemes, baseDirs);
23 assert(found.filePath == buildPath("test", "Tango", "32x32/places", "folder.png"));
24 assert(found.subdir.size == 32);
25 
26 // no match, wrong subdir
27 found = findClosestThemedIcon!(subdir => subdir.context == "MimeTypes")("folder", 32, iconThemes, baseDirs);
28 assert(found.filePath.empty);
29 
30 //hicolor has exact match, but Tango is more preferred.
31 found = findClosestThemedIcon("folder", 64, iconThemes, baseDirs);
32 assert(found.filePath == buildPath("test", "Tango", "32x32/places", "folder.png"));
33 assert(found.subdir.size == 32);
34 
35 //find xpm
36 found = findClosestThemedIcon("folder", 32, iconThemes, baseDirs, [".xpm"]);
37 assert(found.filePath == buildPath("test", "Tango", "32x32/places", "folder.xpm"));
38 assert(found.subdir.size == 32);
39 
40 //find big png, not exact match
41 found = findClosestThemedIcon("folder", 200, iconThemes, baseDirs);
42 assert(found.filePath == buildPath("test", "Tango", "128x128/places", "folder.png"));
43 assert(found.subdir.size == 128);
44 
45 //svg is closer
46 found = findClosestThemedIcon("folder", 200, iconThemes, baseDirs, [".png", ".svg"]);
47 assert(found.filePath == buildPath("test", "Tango", "scalable/places", "folder.svg"));
48 assert(found.subdir.type == IconSubDir.Type.Scalable);
49 
50 // exact match in hicolor
51 found = findClosestThemedIcon("text-plain", 48, iconThemes, baseDirs);
52 assert(found.filePath == buildPath("test", "hicolor", "48x48/mimetypes", "text-plain.png"));
53 assert(found.subdir.size == 48);
54 assert(found.subdir.context == "MimeTypes");
55 assert(found.iconTheme.internalName == "hicolor");
56 
57 // with subdir filter
58 found = findClosestThemedIcon!(subdir => subdir.context == "MimeTypes")("text-plain", 48, iconThemes, baseDirs);
59 assert(found.filePath == buildPath("test", "hicolor", "48x48/mimetypes", "text-plain.png"));
60 assert(found.subdir.size == 48);
61 
62 // no match
63 found = findClosestThemedIcon!(subdir => subdir.context == "Actions")("text-plain", 48, iconThemes, baseDirs);
64 assert(found.filePath.empty);

See Also

findClosestIcon, icontheme.paths.baseIconDirs, lookupIcon, iconSizeDistance

Meta