ltk/theme/
search.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Theme directory search paths and the font-registry builder that walks the
//! active document's font block.

use std::path::PathBuf;

use super::active::ensure_active;
use super::font_registry::FontRegistry;

// ─── Search paths ────────────────────────────────────────────────────────────

/// Returns the ordered list of directories under which theme families are
/// looked up, highest-priority first.
///
/// `LTK_THEMES_DIR` (when set) takes absolute precedence — useful during
/// development to point the shells at an in-tree `themes/` directory without
/// having to install or symlink anything.
pub fn search_paths() -> Vec<PathBuf>
{
	let mut out = Vec::new();
	if let Some( dev ) = std::env::var_os( "LTK_THEMES_DIR" )
	{
		out.push( PathBuf::from( dev ) );
	}
	if let Some( home ) = std::env::var_os( "XDG_DATA_HOME" )
	{
		out.push( PathBuf::from( home ).join( "ltk/themes" ) );
	}
	else if let Some( home ) = std::env::var_os( "HOME" )
	{
		out.push( PathBuf::from( home ).join( ".local/share/ltk/themes" ) );
	}
	out.push( PathBuf::from( "/usr/share/ltk/themes" ) );
	out
}

/// Build a live [`FontRegistry`] from the active document's `fonts`
/// block, loading each declared source from disk. Sources that fail to
/// read or parse are skipped with a warning on stderr — the registry
/// degrades gracefully so one missing TTF does not take down the rest
/// of the family.
///
/// Returns `None` when the active document declares no families at
/// all (in which case the caller keeps the canvas' system-font
/// fallback). Callers should hand the returned registry to the
/// canvas via `Canvas::set_font_registry`; the draw loop already
/// does this at canvas creation time.
pub fn build_font_registry() -> Option<FontRegistry>
{
	let doc = ensure_active().document;
	if doc.fonts.is_empty() { return None; }
	Some( FontRegistry::from_families_lenient( &doc.fonts ) )
}