Finding Icons
The following example demonstrates how to load icons from available themes.
XDGKit searches for themes in the standard directories defined by the XDG specification. Additional directories can be specified for icon lookup by setting the XDG_DATA_DIRS
environment variable before creating an XDGKit instance, as explained in XDG::XDGIconThemeManager::searchDirs().
Icon themes are indexed lazily when an icon is first searched within them. This process can be slow depending on the number of subdirectories and icons in the theme.
To improve performance, XDGKit uses cache files. To generate or update the cache, run the xdgkit-icon-theme-indexer
utility, which scans available themes and stores their information in a compact, serialized format under /var/cache/xdgkit/icon_themes
. When XDGKkit searches for an icon within a theme for the first time, it first checks for an existing cache file. If no cache file is found, the application loads and indexes the information manually (slow). Using the cache also reduces RAM usage, as all the information is directly mapped from cached files rather than being stored in memory.
#include <XDGKit/XDGKit.h>
#include <iostream>
int main()
{
auto kit = XDGKit::Make(options);
kit->iconThemeManager().findIcon(
"firefox",
512,
2,
XDGIcon::PNG | XDGIcon::SVG | XDGIcon::XMP,
{ "Adwaita", "" }
);
if (firefox)
{
std::cout
<<
"Icon: " << firefox->
name() <<
"\n"
std::cout <<
"PNG Path: " << firefox->
getPath(XDGIcon::PNG) <<
"\n";
std::cout <<
"SVG Path: " << firefox->
getPath(XDGIcon::SVG) <<
"\n";
std::cout <<
"XMP Path: " << firefox->
getPath(XDGIcon::XMP) <<
"\n";
return 0;
}
else
{
std::cout << "Could not find an icon named 'firefox'." << "\n";
return 1;
}
}
int32_t scale() const noexcept
Retrieves the scale factor of the icons.
Definition XDGIconDirectory.h:86
XDGIconTheme & theme() const noexcept
Retrieves the theme to which this directory belongs.
Definition XDGIconDirectory.h:138
int32_t size() const noexcept
Retrieves the nominal (unscaled) size of icons in the directory.
Definition XDGIconDirectory.h:65
Properties of an icon.
Definition XDGIcon.h:16
XDGIconDirectory & directory() const noexcept
Retrieves the directory to which this icon belongs.
Definition XDGIcon.h:69
std::filesystem::path getPath(Extension ext) const noexcept
Retrieves the absolute path of the icon for a specified file extension.
Definition XDGIcon.cpp:6
uint32_t extensions() const noexcept
Retrieves the bitset of file extensions found for this icon.
Definition XDGIcon.h:43
const std::string_view & name() const noexcept
Retrieves the name of the icon.
Definition XDGIcon.h:50
const std::string & name() const noexcept
Retrieves the theme's directory base name.
Definition XDGIconTheme.h:50
Namespace.
Definition XDGMap.h:7
Configuration options.
Definition XDGKit.h:23
bool autoReloadCache
Automatically reload icon themes.
Definition XDGKit.h:45
bool useIconThemesCache
Enables icon theme caching.
Definition XDGKit.h:35