Something went wrong. Try again.
🎴 Comfortable GUI port of ddhx to SDL3 & ddui
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138/// Window icon: installed icon if present, else the one shipped beside the/// executable.////// The hicolor PNGs are for the .desktop entry, which is what launchers show,/// not used here./// Authors: dd86k <dd@dax.moe>module icon;
import std.algorithm.iteration : splitter;import std.conv : text;import std.file : exists, thisExePath;import std.path : buildPath, dirName;import std.process : environment;
import ddlogger;
import dentrite.host : Host;import dentrite.image : Image;import dentrite.png : loadPNG;
/// Only Wayland reads alternate sizes, others take one surface and scale it,/// so pass the largest. Smaller sizes from mkicon.d are for the theme and .ico.private enum int ICON_SIZE = 256;
/// Find the icon and set it on the window. Not fatal if none is found.void icon_apply(Host window){ // macOS: SDL sets the Dock icon, which would replace the app bundle's. version (OSX) { if (exists(buildPath(exeDir(), "..", "Info.plist"))) { logDebugging("window icon: app bundle's"); return; } }
Image icon = load(); if (icon is null) { logWarn("no window icon found"); return; }
window.icon(icon);}
/// Executable's directory (where assets/ ships); "." if unknown.string exeDir(){ try return thisExePath().dirName; catch (Exception e) { logWarn("thisExePath: %s", e.msg); return "."; }}
private:
/// First icon of the candidates that decodes, or null.Image load(){ foreach (string path; candidates(ICON_SIZE)) { if (exists(path) == false) { logDebugging("icon %s: absent", path); continue; } Image img; try img = loadPNG(path); catch (Exception e) { logDebugging("icon %s: %s", path, e.msg); continue; } logDebugging("window icon %dx%d from %s", img.width, img.height, path); return img; } return null;}
/// Candidate paths for a `size`-pixel icon. Installed locations first so a/// stale copy in the source tree can't shadow them.string[] candidates(int size){ string[] paths; string leaf = buildPath("vddhx", "icons", text("vddhx-", size, ".png"));
version (Posix) { // XDG base dir spec: XDG_DATA_HOME, then XDG_DATA_DIRS, with defaults. string home = environment.get("XDG_DATA_HOME"); if (home.length) { paths ~= buildPath(home, leaf); } else { string h = environment.get("HOME"); if (h.length) paths ~= buildPath(h, ".local", "share", leaf); }
foreach (string dir; environment.get("XDG_DATA_DIRS", "/usr/local/share:/usr/share").splitter(':')) { if (dir) paths ~= buildPath(dir, leaf); } }
paths ~= buildPath(exeDir(), "assets", "icon", text("vddhx-", size, ".png")); return paths;}
// The shipped icon reaches the window at full size.unittest{ import dentrite.backend.software : SoftwareRenderer; import dentrite.headless : HeadlessPlatform; import dentrite.platform : WindowOptions; import dentrite.text : FontFile;
static immutable ubyte[] font = cast(immutable(ubyte)[]) import("DejaVuSansMono.ttf"); WindowOptions o; o.fontFiles = FontFile(data: font); HeadlessPlatform p = new HeadlessPlatform(64, 64); Host h = new Host(p, new SoftwareRenderer, o); scope (exit) h.close(); icon_apply(h); assert(p.icon.length == 1 && p.icon[0].width == ICON_SIZE && p.icon[0].height == ICON_SIZE); size_t middle = (ICON_SIZE / 2 * ICON_SIZE + ICON_SIZE / 2) * 4; assert(p.icon[0].pixels[middle + 3] == 255, "opaque middle");}