Icons: follow the icon theme spec so KDE apps in the dock get real icons
CI / test (push) Failing after 19s
CI / test (push) Failing after 19s
Pinned KDE apps (Konsole, Dolphin, Kate, KWrite, System Settings) showed letter tiles because their Icon= names a theme icon (utilities-terminal, system-file-manager, preferences-system) that only breeze has, and breeze keeps icons in <context>/<size> (apps/48), which the hard-coded <size>x<size>/apps lookup never looked in. The lookup now reads each theme's index.theme (Directories, sizes, Type, Scale), picks the closest directory as the spec says, searches the user's theme (kdeglobals [Icons] Theme, GTK settings.ini, $CHANDRA_ICON_THEME) and its Inherits chain, then hicolor, then breeze and Adwaita. It also tries the desktop id and its last dotted part, and falls back to the theme's generic application-x-executable icon before the letter tile. Misses are logged. The dock's first pins are now the installed default browser, file manager, terminal and editor (Settings > Apps) instead of the catalog's first five. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hkNwArmSLRGcuxUMhw
This commit is contained in:
4 files changed
+553
-54
No files matched your search
@@ -86,6 +86,50 @@ fn DesktopShell::dock_app_ids(self : DesktopShell) -> Array[String] {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///| The dock's first pins: the default web browser, file manager, terminal and text editor (as
|
||||||
|
/// Settings > Apps has them, `defaults` being each category's current app id or "") that are
|
||||||
|
/// installed, topped up from the catalog's first apps when that gives fewer
|
||||||
|
/// than three. Only apps in `apps` (the installed ones) are pinned.
|
||||||
|
fn default_dock_pins(defaults : Array[String], apps : Array[@types.AppEntry]) -> Array[String] {
|
||||||
|
let installed = apps.map(a => a.id)
|
||||||
|
let out : Array[String] = []
|
||||||
|
let add = fn(id : String) {
|
||||||
|
if id.length() > 0 && installed.contains(id) && !out.contains(id) && out.length() < @dock.default_pin_count {
|
||||||
|
out.push(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for cat in ["browser", "files", "terminal", "editor"] {
|
||||||
|
for i, c in app_categories {
|
||||||
|
if c.id == cat && i < defaults.length() {
|
||||||
|
add(defaults[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if out.length() < 3 {
|
||||||
|
for a in apps {
|
||||||
|
add(a.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "the dock's first pins are the installed default apps" {
|
||||||
|
let app = fn(id : String, cat : String) -> @types.AppEntry {
|
||||||
|
{ id, name: id, comment: "", category: cat, glyph: "X" }
|
||||||
|
}
|
||||||
|
let apps = [
|
||||||
|
app("org.kde.kate", "Utility"), app("org.kde.konsole", "System"), app("org.kde.dolphin", "System"),
|
||||||
|
app("firefox", "Network"), app("systemsettings", "Settings"), app("org.kde.kwrite", "Utility"),
|
||||||
|
]
|
||||||
|
// browser, email, files, terminal, editor, ...; the email client isn't pinned, a missing editor
|
||||||
|
// is skipped.
|
||||||
|
let defaults = ["firefox", "thunderbird", "org.kde.dolphin", "org.kde.konsole", "gone-editor"]
|
||||||
|
assert_eq(default_dock_pins(defaults, apps), ["firefox", "org.kde.dolphin", "org.kde.konsole"])
|
||||||
|
// Nothing configured: the catalog's first apps.
|
||||||
|
assert_eq(default_dock_pins([], apps[:2].to_array()), ["org.kde.kate", "org.kde.konsole"])
|
||||||
|
}
|
||||||
|
|
||||||
///| Save the dock's first pinned list the first time it is seen without one (see the top).
|
///| Save the dock's first pinned list the first time it is seen without one (see the top).
|
||||||
fn DesktopShell::settle_dock_pins(self : DesktopShell) -> Unit {
|
fn DesktopShell::settle_dock_pins(self : DesktopShell) -> Unit {
|
||||||
if self.apps.length() == 0 {
|
if self.apps.length() == 0 {
|
||||||
@@ -95,7 +139,8 @@ fn DesktopShell::settle_dock_pins(self : DesktopShell) -> Unit {
|
|||||||
if w.is_dock() {
|
if w.is_dock() {
|
||||||
let cfg = dock_config(w)
|
let cfg = dock_config(w)
|
||||||
if cfg.pinned is None {
|
if cfg.pinned is None {
|
||||||
self.set_module_setting(w.slot, "pinned", @dock.encode_pins(cfg.pins(self.apps)))
|
let defaults = self.default_app_choices().map(c => c.current_id)
|
||||||
|
self.set_module_setting(w.slot, "pinned", @dock.encode_pins(default_dock_pins(defaults, self.apps)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+400
-53
@@ -23,21 +23,244 @@ fn icon_base_dirs() -> Array[String] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///| Themes searched: hicolor is where apps install their icons; the others cover apps whose icon
|
///| Themes searched: hicolor is where apps install their icons; the others cover apps whose icon
|
||||||
/// only ships with a desktop theme.
|
|
||||||
let icon_themes : Array[String] = ["hicolor", "Adwaita", "breeze"]
|
///| Themes tried after the user's own (and hicolor) for an icon the user's theme lacks, in this
|
||||||
|
/// order. KDE apps such as Konsole and Dolphin ship no icon of their own: they name a theme icon
|
||||||
|
/// (Icon=utilities-terminal, Icon=system-file-manager) that only desktop themes have.
|
||||||
|
let fallback_icon_themes : Array[String] = ["breeze", "Adwaita"]
|
||||||
|
|
||||||
///| Fixed-size directories, best first for a ~22-48 px icon (scaled down well); the scalable SVG
|
///| Fixed-size directories, best first for a ~22-48 px icon (scaled down well); the scalable SVG
|
||||||
/// goes after the first three when the host can draw SVG.
|
/// goes after the first three when the host can draw SVG. Only for themes without an index.theme.
|
||||||
let icon_sizes : Array[String] = [
|
let icon_sizes : Array[String] = [
|
||||||
"48x48", "64x64", "32x32", "96x96", "128x128", "256x256", "24x24", "22x22", "16x16",
|
"48x48", "64x64", "32x32", "96x96", "128x128", "256x256", "24x24", "22x22", "16x16",
|
||||||
"512x512",
|
"512x512",
|
||||||
]
|
]
|
||||||
|
|
||||||
///| Candidate files for icon `name` (an Icon= value), in lookup order. `svg_ok`: whether the
|
///| The size icons are looked up at: the dock's largest icons are 28 px, so 48 leaves room for
|
||||||
/// image loader can read SVG (the host does; the native software renderer only reads PNG).
|
/// magnification and scales down cleanly.
|
||||||
|
let icon_lookup_size : Int = 48
|
||||||
|
|
||||||
|
///| The freedesktop name of the generic application icon, drawn for an app whose own icon no
|
||||||
|
/// theme has.
|
||||||
|
let generic_app_icon : String = "application-x-executable"
|
||||||
|
|
||||||
|
///| How a theme directory's icons may be scaled (index.theme `Type=`).
|
||||||
|
priv enum IconDirKind {
|
||||||
|
Fixed
|
||||||
|
Scalable
|
||||||
|
Threshold
|
||||||
|
} derive(Eq, Debug)
|
||||||
|
|
||||||
|
///| One directory of an icon theme, from its index.theme group.
|
||||||
|
priv struct IconDir {
|
||||||
|
/// Relative to the theme's directory: "apps/48" (breeze), "48x48/apps" (hicolor, Adwaita).
|
||||||
|
path : String
|
||||||
|
size : Int
|
||||||
|
scale : Int
|
||||||
|
min : Int
|
||||||
|
max : Int
|
||||||
|
threshold : Int
|
||||||
|
kind : IconDirKind
|
||||||
|
} derive(Debug)
|
||||||
|
|
||||||
|
///| An icon theme as its index.theme describes it.
|
||||||
|
priv struct IconTheme {
|
||||||
|
/// Its directory name under an icons base dir ("breeze-dark").
|
||||||
|
name : String
|
||||||
|
/// Inherits=, in order.
|
||||||
|
inherits : Array[String]
|
||||||
|
/// Directories= then ScaledDirectories=.
|
||||||
|
dirs : Array[IconDir]
|
||||||
|
} derive(Debug)
|
||||||
|
|
||||||
|
///| A comma-separated list as its non-empty parts.
|
||||||
|
fn comma_list(s : String) -> Array[String] {
|
||||||
|
let out : Array[String] = []
|
||||||
|
for p in s.split(",") {
|
||||||
|
let t = p.trim(chars=" ").to_string()
|
||||||
|
if t.length() > 0 {
|
||||||
|
out.push(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| An int value, or `dflt` when missing or not a number.
|
||||||
|
fn ini_int(keys : Map[String, String], key : String, dflt : Int) -> Int {
|
||||||
|
match keys.get(key) {
|
||||||
|
Some(v) => parse_int_str(v).unwrap_or(dflt)
|
||||||
|
None => dflt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The groups of an ini-style file (index.theme, kdeglobals, settings.ini) as group -> key ->
|
||||||
|
/// value.
|
||||||
|
fn ini_groups(text : String) -> Map[String, Map[String, String]] {
|
||||||
|
let out : Map[String, Map[String, String]] = {}
|
||||||
|
let mut cur : Map[String, String] = {}
|
||||||
|
for raw in text.split("\n") {
|
||||||
|
let line = raw.trim(chars=" \r\t").to_string()
|
||||||
|
if line.length() == 0 || line.has_prefix("#") || line.has_prefix(";") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if line.has_prefix("[") && line.has_suffix("]") {
|
||||||
|
let name = line[1:line.length() - 1].to_string()
|
||||||
|
cur = match out.get(name) {
|
||||||
|
Some(m) => m
|
||||||
|
None => {
|
||||||
|
let m : Map[String, String] = {}
|
||||||
|
out.set(name, m)
|
||||||
|
m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
match line.find("=") {
|
||||||
|
None => ()
|
||||||
|
Some(eq) =>
|
||||||
|
cur.set(line[:eq].trim(chars=" ").to_string(), line[eq + 1:].trim(chars=" ").to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Parse theme `name`'s index.theme.
|
||||||
|
fn parse_index_theme(name : String, text : String) -> IconTheme {
|
||||||
|
let groups = ini_groups(text)
|
||||||
|
let head = groups.get("Icon Theme").unwrap_or({})
|
||||||
|
let dirs : Array[IconDir] = []
|
||||||
|
let listed = comma_list(head.get("Directories").unwrap_or(""))
|
||||||
|
listed.append(comma_list(head.get("ScaledDirectories").unwrap_or("")))
|
||||||
|
for path in listed {
|
||||||
|
match groups.get(path) {
|
||||||
|
None => ()
|
||||||
|
Some(g) => {
|
||||||
|
let size = ini_int(g, "Size", 0)
|
||||||
|
if size <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let kind = match g.get("Type").unwrap_or("Threshold") {
|
||||||
|
"Fixed" => Fixed
|
||||||
|
"Scalable" => Scalable
|
||||||
|
_ => Threshold
|
||||||
|
}
|
||||||
|
dirs.push({
|
||||||
|
path,
|
||||||
|
size,
|
||||||
|
scale: ini_int(g, "Scale", 1),
|
||||||
|
min: ini_int(g, "MinSize", size),
|
||||||
|
max: ini_int(g, "MaxSize", size),
|
||||||
|
threshold: ini_int(g, "Threshold", 2),
|
||||||
|
kind,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ name, inherits: comma_list(head.get("Inherits").unwrap_or("")), dirs }
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Whether the directory holds icons for `size` at scale 1 (the spec's DirectoryMatchesSize).
|
||||||
|
fn IconDir::matches(self : IconDir, size : Int) -> Bool {
|
||||||
|
if self.scale != 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
match self.kind {
|
||||||
|
Fixed => self.size == size
|
||||||
|
Scalable => self.min <= size && size <= self.max
|
||||||
|
Threshold => self.size - self.threshold <= size && size <= self.size + self.threshold
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///| How far the directory's icons are from `size` (the spec's DirectorySizeDistance).
|
||||||
|
fn IconDir::distance(self : IconDir, size : Int) -> Int {
|
||||||
|
let s = self.scale
|
||||||
|
match self.kind {
|
||||||
|
Fixed => (self.size * s - size).abs()
|
||||||
|
Scalable =>
|
||||||
|
if size < self.min * s {
|
||||||
|
self.min * s - size
|
||||||
|
} else if size > self.max * s {
|
||||||
|
size - self.max * s
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
Threshold =>
|
||||||
|
if size < (self.size - self.threshold) * s {
|
||||||
|
self.min * s - size
|
||||||
|
} else if size > (self.size + self.threshold) * s {
|
||||||
|
size - self.max * s
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Candidate files for icon `name` in `theme`, best first: the directories matching `size`,
|
||||||
|
/// then the others by size distance, bigger first at a tie (they scale down better).
|
||||||
|
fn theme_candidates(
|
||||||
|
theme : IconTheme,
|
||||||
|
name : String,
|
||||||
|
size : Int,
|
||||||
|
svg_ok : Bool,
|
||||||
|
bases : Array[String],
|
||||||
|
) -> Array[String] {
|
||||||
|
let exts = if svg_ok { [".png", ".svg"] } else { [".png"] }
|
||||||
|
let order : Array[(Int, Int)] = [] // (distance, index); exact matches 0
|
||||||
|
for i, d in theme.dirs {
|
||||||
|
order.push((if d.matches(size) { 0 } else { d.distance(size) + 1 }, i))
|
||||||
|
}
|
||||||
|
order.sort_by((a, b) => {
|
||||||
|
if a.0 != b.0 {
|
||||||
|
return a.0.compare(b.0)
|
||||||
|
}
|
||||||
|
let sa = theme.dirs[a.1].size * theme.dirs[a.1].scale
|
||||||
|
let sb = theme.dirs[b.1].size * theme.dirs[b.1].scale
|
||||||
|
if sa != sb {
|
||||||
|
sb.compare(sa)
|
||||||
|
} else {
|
||||||
|
a.1.compare(b.1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
let out : Array[String] = []
|
||||||
|
for o in order {
|
||||||
|
let d = theme.dirs[o.1]
|
||||||
|
for base in bases {
|
||||||
|
for ext in exts {
|
||||||
|
out.push(base + "/" + theme.name + "/" + d.path + "/" + name + ext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Candidates in a theme without an index.theme: the usual `<size>/apps` layout.
|
||||||
|
fn plain_theme_candidates(
|
||||||
|
theme : String,
|
||||||
|
name : String,
|
||||||
|
svg_ok : Bool,
|
||||||
|
bases : Array[String],
|
||||||
|
) -> Array[String] {
|
||||||
|
let out : Array[String] = []
|
||||||
|
for base in bases {
|
||||||
|
let dir = base + "/" + theme
|
||||||
|
for i = 0; i < icon_sizes.length(); i = i + 1 {
|
||||||
|
if i == 3 && svg_ok {
|
||||||
|
out.push(dir + "/scalable/apps/" + name + ".svg")
|
||||||
|
}
|
||||||
|
out.push(dir + "/" + icon_sizes[i] + "/apps/" + name + ".png")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Candidate files for icon `name` (an Icon= value), in lookup order: through `themes` (as
|
||||||
|
/// `icon_theme_chain` orders them; None: a theme without an index.theme), then the pixmaps dirs.
|
||||||
|
/// `svg_ok`: whether the image loader can read SVG (the host does; the native software renderer
|
||||||
|
/// only reads PNG).
|
||||||
fn icon_candidates(
|
fn icon_candidates(
|
||||||
name : String,
|
name : String,
|
||||||
svg_ok : Bool,
|
svg_ok : Bool,
|
||||||
|
themes : Array[(String, IconTheme?)],
|
||||||
bases : Array[String],
|
bases : Array[String],
|
||||||
pixmaps : Array[String],
|
pixmaps : Array[String],
|
||||||
) -> Array[String] {
|
) -> Array[String] {
|
||||||
@@ -58,15 +281,10 @@ fn icon_candidates(
|
|||||||
name
|
name
|
||||||
}
|
}
|
||||||
let out : Array[String] = []
|
let out : Array[String] = []
|
||||||
for theme in icon_themes {
|
for t in themes {
|
||||||
for base in bases {
|
match t.1 {
|
||||||
let dir = base + "/" + theme
|
Some(theme) => out.append(theme_candidates(theme, stem, icon_lookup_size, svg_ok, bases))
|
||||||
for i = 0; i < icon_sizes.length(); i = i + 1 {
|
None => out.append(plain_theme_candidates(t.0, stem, svg_ok, bases))
|
||||||
if i == 3 && svg_ok {
|
|
||||||
out.push(dir + "/scalable/apps/" + stem + ".svg")
|
|
||||||
}
|
|
||||||
out.push(dir + "/" + icon_sizes[i] + "/apps/" + stem + ".png")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for p in pixmaps {
|
for p in pixmaps {
|
||||||
@@ -78,10 +296,111 @@ fn icon_candidates(
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///| The user's icon theme, or "" when nothing names one. `$CHANDRA_ICON_THEME` (`env`) wins, then
|
||||||
|
/// kdeglobals `[Icons] Theme=`, then GTK's settings.ini `gtk-icon-theme-name`; a kdeglobals
|
||||||
|
/// without one means KDE's default, breeze. `config` reads a file under the config dir.
|
||||||
|
fn user_icon_theme(env : String, config : (String) -> String) -> String {
|
||||||
|
if env.length() > 0 {
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
let kde = config("kdeglobals")
|
||||||
|
match ini_groups(kde).get("Icons") {
|
||||||
|
Some(g) =>
|
||||||
|
match g.get("Theme") {
|
||||||
|
Some(t) if t.length() > 0 => return t
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
for f in ["gtk-4.0/settings.ini", "gtk-3.0/settings.ini"] {
|
||||||
|
match ini_groups(config(f)).get("Settings") {
|
||||||
|
Some(g) =>
|
||||||
|
match g.get("gtk-icon-theme-name") {
|
||||||
|
Some(t) => {
|
||||||
|
let t = t.trim(chars="\"' ").to_string()
|
||||||
|
if t.length() > 0 {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if kde.length() > 0 {
|
||||||
|
"breeze"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The themes to search, in order: `user` and what it inherits (depth first), hicolor (where
|
||||||
|
/// apps install their own icons), then the fallback themes and theirs. `load` reads a theme's
|
||||||
|
/// index.theme (None: not installed); themes that aren't installed are left out, hicolor stays
|
||||||
|
/// (some installs have hicolor dirs without its index.theme).
|
||||||
|
fn icon_theme_chain(user : String, load : (String) -> IconTheme?) -> Array[(String, IconTheme?)] {
|
||||||
|
let out : Array[(String, IconTheme?)] = []
|
||||||
|
let seen : Map[String, Bool] = {}
|
||||||
|
fn visit(name : String, depth : Int) -> Unit {
|
||||||
|
if name.length() == 0 || name == "hicolor" || seen.contains(name) || depth > 8 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen.set(name, true)
|
||||||
|
match load(name) {
|
||||||
|
None => ()
|
||||||
|
Some(t) => {
|
||||||
|
out.push((name, Some(t)))
|
||||||
|
for p in t.inherits {
|
||||||
|
visit(p, depth + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visit(user, 0)
|
||||||
|
out.push(("hicolor", load("hicolor")))
|
||||||
|
for f in fallback_icon_themes {
|
||||||
|
visit(f, 0)
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The first base dir's index.theme for theme `name`, parsed.
|
||||||
|
fn load_icon_theme(name : String, bases : Array[String]) -> IconTheme? {
|
||||||
|
for base in bases {
|
||||||
|
let b = @sys.read_file_bytes(base + "/" + name + "/index.theme")
|
||||||
|
if b.length() > 0 {
|
||||||
|
return Some(parse_index_theme(name, @utf8.decode_lossy(b)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
///| A file under $XDG_CONFIG_HOME (or ~/.config) as text ("" when missing).
|
||||||
|
fn read_config_text(rel : String) -> String {
|
||||||
|
let x = @sys.getenv("XDG_CONFIG_HOME")
|
||||||
|
let dir = if x.length() > 0 { x } else { @sys.getenv("HOME") + "/.config" }
|
||||||
|
@utf8.decode_lossy(@sys.read_file_bytes(dir + "/" + rel))
|
||||||
|
}
|
||||||
|
|
||||||
|
///| Where icons are looked up: the base dirs and the theme chain, read once per shell.
|
||||||
|
struct IconSearch {
|
||||||
|
bases : Array[String]
|
||||||
|
themes : Array[(String, IconTheme?)]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn IconSearch::from_env() -> IconSearch {
|
||||||
|
let bases = icon_base_dirs()
|
||||||
|
let user = user_icon_theme(@sys.getenv("CHANDRA_ICON_THEME"), read_config_text)
|
||||||
|
let themes = icon_theme_chain(user, n => load_icon_theme(n, bases))
|
||||||
|
@sys.log("chandra: icon themes: " + themes.map(t => t.0).join(", "))
|
||||||
|
{ bases, themes }
|
||||||
|
}
|
||||||
|
|
||||||
///| The icon file for Icon= value `name`, or None when no theme has it.
|
///| The icon file for Icon= value `name`, or None when no theme has it.
|
||||||
fn find_icon_file(name : String, svg_ok : Bool) -> String? {
|
fn IconSearch::find(self : IconSearch, name : String, svg_ok : Bool) -> String? {
|
||||||
let pixmaps = ["/usr/share/pixmaps"]
|
for path in icon_candidates(name, svg_ok, self.themes, self.bases, ["/usr/share/pixmaps"]) {
|
||||||
for path in icon_candidates(name, svg_ok, icon_base_dirs(), pixmaps) {
|
|
||||||
if @sys.read_file_bytes(path).length() > 0 {
|
if @sys.read_file_bytes(path).length() > 0 {
|
||||||
return Some(path)
|
return Some(path)
|
||||||
}
|
}
|
||||||
@@ -89,8 +408,68 @@ fn find_icon_file(name : String, svg_ok : Bool) -> String? {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
///| The image handle of the icon for windows carrying `app_id` (0: none). Cached per app id,
|
///| The icon names tried for an app, best first: its Icon= value, its desktop id or window app
|
||||||
/// so each app's themes are searched once.
|
/// id (many apps' icons are named after it), that id's last dotted part lower-cased
|
||||||
|
/// (`org.kde.kwrite` -> `kwrite`), and last the generic application icon, so an app whose own
|
||||||
|
/// icon is nowhere still looks like an app (the letter tile is drawn only when even that is
|
||||||
|
/// missing).
|
||||||
|
fn icon_names_for(icon : String, id : String) -> Array[String] {
|
||||||
|
let out : Array[String] = []
|
||||||
|
let add = fn(n : String) { if n.length() > 0 && !out.contains(n) { out.push(n) } }
|
||||||
|
add(icon)
|
||||||
|
add(id)
|
||||||
|
let parts = id.split(".").to_array()
|
||||||
|
if parts.length() > 1 {
|
||||||
|
add(parts[parts.length() - 1].to_string().to_lower())
|
||||||
|
}
|
||||||
|
add(generic_app_icon)
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The icon search, read on first use.
|
||||||
|
fn DesktopShell::icon_search(self : DesktopShell) -> IconSearch {
|
||||||
|
match self.icon_search {
|
||||||
|
Some(s) => s
|
||||||
|
None => {
|
||||||
|
let s = IconSearch::from_env()
|
||||||
|
self.icon_search = Some(s)
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The image handle for app `id` with Icon= value `icon` (0: none), cached per id so each app's
|
||||||
|
/// themes are searched once.
|
||||||
|
fn DesktopShell::lookup_icon(self : DesktopShell, id : String, icon : String) -> UInt {
|
||||||
|
match self.icon_handles.get(id) {
|
||||||
|
Some(h) => return h
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
let search = self.icon_search()
|
||||||
|
let svg_ok = @sys.host_raster()
|
||||||
|
let mut h = 0U
|
||||||
|
for name in icon_names_for(icon, id) {
|
||||||
|
match search.find(name, svg_ok) {
|
||||||
|
Some(path) => {
|
||||||
|
h = @raster.load_image(path)
|
||||||
|
if h != 0U {
|
||||||
|
if name == generic_app_icon && icon != generic_app_icon {
|
||||||
|
@sys.log("chandra: no icon for " + id + " (Icon=" + icon + "), using " + path)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h == 0U {
|
||||||
|
@sys.log("chandra: no icon for " + id + " (Icon=" + icon + ")")
|
||||||
|
}
|
||||||
|
self.icon_handles.set(id, h)
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The image handle of the icon for windows carrying `app_id` (0: none).
|
||||||
fn DesktopShell::app_icon(self : DesktopShell, app_id : String) -> UInt {
|
fn DesktopShell::app_icon(self : DesktopShell, app_id : String) -> UInt {
|
||||||
match self.icon_handles.get(app_id) {
|
match self.icon_handles.get(app_id) {
|
||||||
Some(h) => return h
|
Some(h) => return h
|
||||||
@@ -100,29 +479,12 @@ fn DesktopShell::app_icon(self : DesktopShell, app_id : String) -> UInt {
|
|||||||
Some(id) => self.icon_names.get(id).unwrap_or("")
|
Some(id) => self.icon_names.get(id).unwrap_or("")
|
||||||
None => ""
|
None => ""
|
||||||
}
|
}
|
||||||
// No desktop file: many apps' icons are named after their app id anyway.
|
self.lookup_icon(app_id, icon)
|
||||||
let name = if icon.length() > 0 { icon } else { app_id }
|
|
||||||
let h = match find_icon_file(name, @sys.host_raster()) {
|
|
||||||
Some(path) => @raster.load_image(path)
|
|
||||||
None => 0U
|
|
||||||
}
|
|
||||||
self.icon_handles.set(app_id, h)
|
|
||||||
h
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///| The image handle of desktop app `id`'s icon (Icon= value `icon`), sharing `app_icon`'s cache.
|
///| The image handle of desktop app `id`'s icon (Icon= value `icon`), sharing `app_icon`'s cache.
|
||||||
fn DesktopShell::desktop_icon(self : DesktopShell, id : String, icon : String) -> UInt {
|
fn DesktopShell::desktop_icon(self : DesktopShell, id : String, icon : String) -> UInt {
|
||||||
match self.icon_handles.get(id) {
|
self.lookup_icon(id, icon)
|
||||||
Some(h) => return h
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
let name = if icon.length() > 0 { icon } else { id }
|
|
||||||
let h = match find_icon_file(name, @sys.host_raster()) {
|
|
||||||
Some(path) => @raster.load_image(path)
|
|
||||||
None => 0U
|
|
||||||
}
|
|
||||||
self.icon_handles.set(id, h)
|
|
||||||
h
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///| The image handle of the app icon for a window with `app_id` (0: none), for the compositor's
|
///| The image handle of the app icon for a window with `app_id` (0: none), for the compositor's
|
||||||
@@ -161,18 +523,3 @@ fn DesktopShell::publish_icons(self : DesktopShell) -> Unit {
|
|||||||
@wasm.publish(Icons, t)
|
@wasm.publish(Icons, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
test "icon lookup tries the themes' sized PNGs, then scalable SVG when it can, then pixmaps" {
|
|
||||||
let c = icon_candidates("firefox", true, ["/u/icons"], ["/p"])
|
|
||||||
assert_eq(c[0], "/u/icons/hicolor/48x48/apps/firefox.png")
|
|
||||||
assert_eq(c[3], "/u/icons/hicolor/scalable/apps/firefox.svg")
|
|
||||||
assert_eq(c[c.length() - 2], "/p/firefox.png")
|
|
||||||
assert_true(c.contains("/u/icons/breeze/16x16/apps/firefox.png"))
|
|
||||||
// The software renderer reads PNG only.
|
|
||||||
assert_false(icon_candidates("firefox", false, ["/u/icons"], ["/p"]).iter().any(p => p.has_suffix(".svg")))
|
|
||||||
// Icon=foo.png means foo; absolute paths are taken as they are.
|
|
||||||
assert_eq(icon_candidates("foo.png", false, ["/b"], [])[0], "/b/hicolor/48x48/apps/foo.png")
|
|
||||||
assert_eq(icon_candidates("/opt/app/icon.png", false, ["/b"], []), ["/opt/app/icon.png"])
|
|
||||||
assert_eq(icon_candidates("/opt/app/icon.svg", false, ["/b"], []), [])
|
|
||||||
assert_eq(icon_candidates("", true, ["/b"], []), [])
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
///| A trimmed copy of breeze's index.theme (breeze keeps icons in `<context>/<size>`, not
|
||||||
|
/// hicolor's `<size>x<size>/<context>`).
|
||||||
|
let breeze_index : String =
|
||||||
|
#|[Icon Theme]
|
||||||
|
#|Name=Breeze
|
||||||
|
#|Inherits=hicolor
|
||||||
|
#|Directories=apps/16,apps/32,apps/48,mimetypes/64
|
||||||
|
#|ScaledDirectories=apps/16@2x
|
||||||
|
#|[apps/16]
|
||||||
|
#|Size=16
|
||||||
|
#|Context=Applications
|
||||||
|
#|Type=Fixed
|
||||||
|
#|[apps/16@2x]
|
||||||
|
#|Size=16
|
||||||
|
#|Scale=2
|
||||||
|
#|Type=Fixed
|
||||||
|
#|[apps/32]
|
||||||
|
#|Size=32
|
||||||
|
#|Type=Fixed
|
||||||
|
#|[apps/48]
|
||||||
|
#|Size=48
|
||||||
|
#|Context=Applications
|
||||||
|
#|Type=Scalable
|
||||||
|
#|MinSize=48
|
||||||
|
#|MaxSize=256
|
||||||
|
#|[mimetypes/64]
|
||||||
|
#|Size=64
|
||||||
|
#|Type=Fixed
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "index.theme directories, sizes and Inherits are read" {
|
||||||
|
let t = parse_index_theme("breeze", breeze_index)
|
||||||
|
assert_eq(t.inherits, ["hicolor"])
|
||||||
|
assert_eq(t.dirs.map(d => d.path), ["apps/16", "apps/32", "apps/48", "mimetypes/64", "apps/16@2x"])
|
||||||
|
assert_true(t.dirs[2].matches(48))
|
||||||
|
assert_true(t.dirs[2].matches(128))
|
||||||
|
assert_false(t.dirs[0].matches(48))
|
||||||
|
assert_false(t.dirs[4].matches(32)) // scale 2
|
||||||
|
assert_eq(t.dirs[3].distance(48), 16)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "breeze's apps/48 is searched for a theme icon like Konsole's utilities-terminal" {
|
||||||
|
let t = parse_index_theme("breeze", breeze_index)
|
||||||
|
let c = icon_candidates("utilities-terminal", true, [("breeze", Some(t))], ["/u/icons"], [])
|
||||||
|
assert_eq(c[0], "/u/icons/breeze/apps/48/utilities-terminal.png")
|
||||||
|
assert_eq(c[1], "/u/icons/breeze/apps/48/utilities-terminal.svg")
|
||||||
|
// Then the nearest sizes, bigger first.
|
||||||
|
assert_eq(c[2], "/u/icons/breeze/mimetypes/64/utilities-terminal.png")
|
||||||
|
// The software renderer reads PNG only.
|
||||||
|
let png = icon_candidates("utilities-terminal", false, [("breeze", Some(t))], ["/u/icons"], [])
|
||||||
|
assert_false(png.iter().any(p => p.has_suffix(".svg")))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "themes without an index.theme use the hicolor layout, then pixmaps" {
|
||||||
|
let c = icon_candidates("firefox", true, [("hicolor", None)], ["/u/icons"], ["/p"])
|
||||||
|
assert_eq(c[0], "/u/icons/hicolor/48x48/apps/firefox.png")
|
||||||
|
assert_eq(c[3], "/u/icons/hicolor/scalable/apps/firefox.svg")
|
||||||
|
assert_eq(c[c.length() - 2], "/p/firefox.png")
|
||||||
|
// Icon=foo.png means foo; absolute paths are taken as they are.
|
||||||
|
assert_eq(icon_candidates("foo.png", false, [("hicolor", None)], ["/b"], [])[0], "/b/hicolor/48x48/apps/foo.png")
|
||||||
|
assert_eq(icon_candidates("/opt/app/icon.png", false, [], ["/b"], []), ["/opt/app/icon.png"])
|
||||||
|
assert_eq(icon_candidates("/opt/app/icon.svg", false, [], ["/b"], []), [])
|
||||||
|
assert_eq(icon_candidates("", true, [], ["/b"], []), [])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "theme chain: the user's theme and what it inherits, hicolor, then breeze and Adwaita" {
|
||||||
|
let installed : Map[String, String] = {
|
||||||
|
"breeze-dark": "[Icon Theme]\nInherits=breeze,hicolor\n",
|
||||||
|
"breeze": breeze_index,
|
||||||
|
"Adwaita": "[Icon Theme]\nInherits=hicolor\n",
|
||||||
|
"hicolor": "[Icon Theme]\n",
|
||||||
|
}
|
||||||
|
let load = fn(n : String) { installed.get(n).map(t => parse_index_theme(n, t)) }
|
||||||
|
assert_eq(icon_theme_chain("breeze-dark", load).map(t => t.0), [
|
||||||
|
"breeze-dark", "breeze", "hicolor", "Adwaita",
|
||||||
|
])
|
||||||
|
assert_eq(icon_theme_chain("", load).map(t => t.0), ["hicolor", "breeze", "Adwaita"])
|
||||||
|
// A theme that isn't installed is skipped; hicolor stays even without its index.theme.
|
||||||
|
let none = fn(_n : String) -> IconTheme? { None }
|
||||||
|
assert_eq(icon_theme_chain("Papirus", none).map(t => t.0), ["hicolor"])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "the user's icon theme comes from kdeglobals, then GTK settings.ini" {
|
||||||
|
let files = fn(m : Map[String, String]) { fn(f : String) { m.get(f).unwrap_or("") } }
|
||||||
|
let kde = files({ "kdeglobals": "[General]\nColorScheme=BreezeDark\n[Icons]\nTheme=breeze-dark\n" })
|
||||||
|
assert_eq(user_icon_theme("", kde), "breeze-dark")
|
||||||
|
assert_eq(user_icon_theme("Papirus", kde), "Papirus")
|
||||||
|
assert_eq(user_icon_theme("", files({ "kdeglobals": "[General]\nfoo=1\n" })), "breeze")
|
||||||
|
let gtk = files({ "gtk-3.0/settings.ini": "[Settings]\ngtk-icon-theme-name=\"Yaru\"\n" })
|
||||||
|
assert_eq(user_icon_theme("", gtk), "Yaru")
|
||||||
|
assert_eq(user_icon_theme("", files({})), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "an app's icon names: Icon=, its id, the id's last part, the generic app icon" {
|
||||||
|
assert_eq(icon_names_for("utilities-terminal", "org.kde.konsole"), [
|
||||||
|
"utilities-terminal", "org.kde.konsole", "konsole", "application-x-executable",
|
||||||
|
])
|
||||||
|
assert_eq(icon_names_for("", "foot"), ["foot", "application-x-executable"])
|
||||||
|
}
|
||||||
@@ -237,6 +237,8 @@ pub(all) struct DesktopShell {
|
|||||||
/// (0: no icon), for the `icons` capability.
|
/// (0: no icon), for the `icons` capability.
|
||||||
icon_names : Map[String, String]
|
icon_names : Map[String, String]
|
||||||
icon_handles : Map[String, UInt]
|
icon_handles : Map[String, UInt]
|
||||||
|
/// Icon base dirs and theme chain (icons.mbt), read on first use.
|
||||||
|
mut icon_search : IconSearch?
|
||||||
/// Wayland socket handed to launched apps as WAYLAND_DISPLAY.
|
/// Wayland socket handed to launched apps as WAYLAND_DISPLAY.
|
||||||
display : String
|
display : String
|
||||||
/// Panel modules in panel order; a module with `stretch` splits the panel into a left and a right group.
|
/// Panel modules in panel order; a module with `stretch` splits the panel into a left and a right group.
|
||||||
@@ -382,6 +384,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
|
|||||||
wm_classes,
|
wm_classes,
|
||||||
icon_names,
|
icon_names,
|
||||||
icon_handles: {},
|
icon_handles: {},
|
||||||
|
icon_search: None,
|
||||||
display,
|
display,
|
||||||
widgets: [],
|
widgets: [],
|
||||||
next_slot: 1,
|
next_slot: 1,
|
||||||
|
|||||||
Reference in new issue
Block a user