XDGKit v1.0.0-1
C++ XDG Utilities
Loading...
Searching...
No Matches
🕹️ Examples

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>
using namespace XDG;
int main()
{
XDGKit::Options options;
// Load themes from cache when possible
options.useIconThemesCache = true;
// Automatically reload themes if a cache update is detected
options.autoReloadCache = true;
// Create an instance of XDGKit.
auto kit = XDGKit::Make(options);
// Search for an icon
const XDGIcon *firefox =
kit->iconThemeManager().findIcon(
"firefox", // Icon to search for
512, // Desired icon size (unscaled)
2, // Scale factor
XDGIcon::PNG | XDGIcon::SVG | XDGIcon::XMP, // File extensions to consider
{ "Adwaita", "" } // Theme names to search in order, "" as wildcard for all themes
);
// Check if the icon was found
if (firefox)
{
std::cout
<< "Theme: " << firefox->directory().theme().name() << "\n"
<< "Icon: " << firefox->name() << "\n"
<< "Size: " << firefox->directory().size() << "\n"
<< "Scale: " << firefox->directory().scale() << "\n";
if (firefox->extensions() & XDGIcon::PNG)
std::cout << "PNG Path: " << firefox->getPath(XDGIcon::PNG) << "\n";
if (firefox->extensions() & XDGIcon::SVG)
std::cout << "SVG Path: " << firefox->getPath(XDGIcon::SVG) << "\n";
if (firefox->extensions() & XDGIcon::XMP)
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