@domicile-desktop/system-apps 0.0.0-placeholder → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -0
- package/package.json +57 -5
- package/src/app-icons.ts +413 -0
- package/src/app-id-name.ts +10 -0
- package/src/apps-desktop.ts +12 -0
- package/src/bookmark.ts +39 -0
- package/src/content-type.ts +75 -0
- package/src/curl.ts +54 -0
- package/src/data-dirs.ts +29 -0
- package/src/data-url.ts +12 -0
- package/src/describe-apps.ts +110 -0
- package/src/desktop-entry.ts +98 -0
- package/src/environment.ts +38 -0
- package/src/escapes.ts +16 -0
- package/src/exec.ts +60 -0
- package/src/fake-desktop.ts +209 -0
- package/src/favicon.ts +164 -0
- package/src/favicons.ts +117 -0
- package/src/find-apps.ts +21 -0
- package/src/glob.ts +33 -0
- package/src/icon-theme-index.ts +148 -0
- package/src/installed.ts +135 -0
- package/src/mime-apps.ts +82 -0
- package/src/omit.ts +24 -0
- package/src/openers.ts +137 -0
- package/src/opens.ts +43 -0
- package/src/ranked.ts +60 -0
- package/src/results.ts +48 -0
- package/src/supertypes.ts +70 -0
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @domicile-desktop/system-apps
|
|
2
|
+
|
|
3
|
+
Installed applications and bookmarks for a shell's launcher, read through the
|
|
4
|
+
`files`, `processes` and `dbus` namespaces of
|
|
5
|
+
the [`Domicile`](../chrome-sdk/README.md) a shell is handed. Manganese's launcher
|
|
6
|
+
uses it (`src/launcher/app-search.ts`), and the component library's portal
|
|
7
|
+
dialogs name applications with it.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { appIcons } from "@domicile-desktop/system-apps/app-icons";
|
|
13
|
+
import { dataDirs } from "@domicile-desktop/system-apps/data-dirs";
|
|
14
|
+
import { findApps } from "@domicile-desktop/system-apps/find-apps";
|
|
15
|
+
import { installedApps } from "@domicile-desktop/system-apps/installed";
|
|
16
|
+
|
|
17
|
+
const dirs = (await dataDirs(domicile)).unwrapOr([]);
|
|
18
|
+
const apps = (await installedApps(domicile, dirs)).unwrapOr([]);
|
|
19
|
+
const icon = appIcons(domicile, dirs);
|
|
20
|
+
const editors = findApps(apps, "edit", 50);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Modules
|
|
24
|
+
|
|
25
|
+
| Module | What it is |
|
|
26
|
+
| --- | --- |
|
|
27
|
+
| `./apps-desktop` | `AppsDesktop`: the slice of the `Domicile` the library calls. |
|
|
28
|
+
| `./data-dirs` | The XDG data directories, from the desktop's environment (`env -0`). |
|
|
29
|
+
| `./installed` | Every desktop entry under them; an earlier directory hides a later one's ID. |
|
|
30
|
+
| `./desktop-entry` | One entry parsed: name, comment, argv, icon and `X-Domicile-Preview`. |
|
|
31
|
+
| `./find-apps`, `./bookmark` | The launcher's matching and ranking; bookmarks and their Zod schema. |
|
|
32
|
+
| `./describe-apps` | Application IDs named and drawn by their entries, in the config's icon theme, for a shell to say who asks. |
|
|
33
|
+
| `./mime-apps` | A content type's default applications from every `mimeapps.list`, the desktop's own (`domicile-mimeapps.list`) first. |
|
|
34
|
+
| `./openers` | The applications that open a file, its default first, and the argv each opens it with. The type comes from shared-mime-info's `mime/globs2`, by name. |
|
|
35
|
+
| `./omit` | Desktop file IDs left out, as globs. |
|
|
36
|
+
| `./app-icons` | Icon names resolved to `data:` URLs (PNG, SVG, 128 KiB at most) by the icon theme spec: a theme, its `Inherits`, `hicolor`, then `pixmaps`, at the size asked for, in `apps` or the contexts given. `themedIcons` falls back to `<name>-symbolic` and says which icons are symbolic. Pass the theme `readIconTheme` reads. |
|
|
37
|
+
| `./favicon`, `./curl`, `./favicons` | A bookmark's icon: picked from the page, fetched with `curl`, kept and retried. |
|
|
38
|
+
| `./fake-desktop` | A desktop's `files`, `processes` and `dbus` over an in-memory tree, for tests. Its Settings portal serves the icon theme it is given. |
|
|
39
|
+
|
|
40
|
+
- Calls resolve `Result`s with the SDK's `SystemError`; a missing or
|
|
41
|
+
unreadable file or directory is skipped, as the specs say.
|
|
42
|
+
- `./curl` needs `curl` on the desktop's `PATH`. Domicile's Nix package adds
|
|
43
|
+
it.
|
|
44
|
+
- Launching stays with the shell: pass an entry's `command`, or an
|
|
45
|
+
`openers` result's `command(app)`, to `spawn`.
|
|
46
|
+
|
|
47
|
+
## Test
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
bun run turbo test --filter @domicile-desktop/system-apps
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Entry parsing is tested on files under `src/recorded/`, copied from real
|
|
54
|
+
installs.
|
package/package.json
CHANGED
|
@@ -1,7 +1,59 @@
|
|
|
1
1
|
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"@cprussin/option-result": "^2.0.1",
|
|
4
|
+
"@domicile-desktop/sdk": "1.0.0",
|
|
5
|
+
"zod": "^4.5.4"
|
|
6
|
+
},
|
|
7
|
+
"description": "Installed applications and bookmarks for a Domicile shell's launcher, read through @domicile-desktop/sdk/system.",
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"@cprussin/tsconfig": "^5.0.0",
|
|
10
|
+
"@domicile-desktop/test-support": "0.0.1",
|
|
11
|
+
"@types/bun": "^1.4.2",
|
|
12
|
+
"typescript": "^7.0.2"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"bun": ">=1.4.2",
|
|
16
|
+
"node": ">=24"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
"./app-icons": "./src/app-icons.ts",
|
|
20
|
+
"./app-id-name": "./src/app-id-name.ts",
|
|
21
|
+
"./apps-desktop": "./src/apps-desktop.ts",
|
|
22
|
+
"./bookmark": "./src/bookmark.ts",
|
|
23
|
+
"./curl": "./src/curl.ts",
|
|
24
|
+
"./data-dirs": "./src/data-dirs.ts",
|
|
25
|
+
"./describe-apps": "./src/describe-apps.ts",
|
|
26
|
+
"./desktop-entry": "./src/desktop-entry.ts",
|
|
27
|
+
"./fake-desktop": "./src/fake-desktop.ts",
|
|
28
|
+
"./favicon": "./src/favicon.ts",
|
|
29
|
+
"./favicons": "./src/favicons.ts",
|
|
30
|
+
"./find-apps": "./src/find-apps.ts",
|
|
31
|
+
"./installed": "./src/installed.ts",
|
|
32
|
+
"./mime-apps": "./src/mime-apps.ts",
|
|
33
|
+
"./omit": "./src/omit.ts",
|
|
34
|
+
"./openers": "./src/openers.ts",
|
|
35
|
+
"./opens": "./src/opens.ts"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"src/**",
|
|
39
|
+
"!src/**/*.test.ts",
|
|
40
|
+
"!src/recorded/**"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
2
43
|
"name": "@domicile-desktop/system-apps",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
44
|
+
"private": false,
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"directory": "packages/system-apps",
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/cprussin/domicile.git"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"test:types": "tsc --noEmit",
|
|
55
|
+
"test:unit": "bun test"
|
|
56
|
+
},
|
|
57
|
+
"type": "module",
|
|
58
|
+
"version": "1.0.0"
|
|
59
|
+
}
|
package/src/app-icons.ts
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// Icon names resolved to `data:` URLs, for a desktop entry's `Icon` and
|
|
2
|
+
// `X-Domicile-Preview` and a tray menu's `icon-name`.
|
|
3
|
+
//
|
|
4
|
+
// Follows the icon theme spec: the theme, its `Inherits` and then `hicolor`,
|
|
5
|
+
// each under every data directory's `icons`, then `pixmaps`. A theme's
|
|
6
|
+
// `index.theme` says which directory suits a size. `hicolor` without one is
|
|
7
|
+
// read as `<size>/<context>`, the layout applications install into.
|
|
8
|
+
|
|
9
|
+
import type { Option, Result } from "@cprussin/option-result";
|
|
10
|
+
import { None, Ok, Some } from "@cprussin/option-result";
|
|
11
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
12
|
+
import type { AppsDesktop } from "./apps-desktop";
|
|
13
|
+
|
|
14
|
+
import { dataUrl } from "./data-url";
|
|
15
|
+
import type { ThemeDirectory } from "./icon-theme-index";
|
|
16
|
+
import {
|
|
17
|
+
DirectoryType,
|
|
18
|
+
parseIndexTheme,
|
|
19
|
+
sizeDistance,
|
|
20
|
+
} from "./icon-theme-index";
|
|
21
|
+
import { allOk, orAbsent } from "./results";
|
|
22
|
+
|
|
23
|
+
/** The theme every theme inherits from last. */
|
|
24
|
+
const HICOLOR = "hicolor";
|
|
25
|
+
|
|
26
|
+
/** The suffix of an icon drawn in one color, to be tinted. */
|
|
27
|
+
const SYMBOLIC = "-symbolic";
|
|
28
|
+
|
|
29
|
+
/** Extensions a page can draw, preferred first, and their MIME types. */
|
|
30
|
+
const KINDS: Readonly<Record<string, string>> = {
|
|
31
|
+
png: "image/png",
|
|
32
|
+
svg: "image/svg+xml",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Largest icon read. Every matched row draws one, so a larger file is skipped. */
|
|
36
|
+
const LARGEST = 128 * 1024;
|
|
37
|
+
|
|
38
|
+
/** An icon by name, as a `data:` URL, or nothing if none is usable. */
|
|
39
|
+
export type IconLookup = (
|
|
40
|
+
name: string,
|
|
41
|
+
) => Promise<Result<Option<string>, SystemError>>;
|
|
42
|
+
|
|
43
|
+
/** An icon found by {@link themedIcons}. */
|
|
44
|
+
export type ThemedIcon = {
|
|
45
|
+
url: string;
|
|
46
|
+
/**
|
|
47
|
+
* Drawn in one color, to be tinted to the text around it, such as a CSS
|
|
48
|
+
* mask over `currentColor`.
|
|
49
|
+
*/
|
|
50
|
+
symbolic: boolean;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type ThemedIconLookup = (
|
|
54
|
+
name: string,
|
|
55
|
+
) => Promise<Result<Option<ThemedIcon>, SystemError>>;
|
|
56
|
+
|
|
57
|
+
export type IconOptions = {
|
|
58
|
+
/** The icon theme searched before `hicolor`, such as `readIconTheme` reads. */
|
|
59
|
+
theme?: string | undefined;
|
|
60
|
+
/** The size drawn at, in CSS pixels. 48, a launcher row's, by default. */
|
|
61
|
+
size?: number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** {@link themedIcons}' URLs alone, for pictures that keep their own colors. */
|
|
65
|
+
export const appIcons = (
|
|
66
|
+
domicile: AppsDesktop,
|
|
67
|
+
dataDirs: readonly string[],
|
|
68
|
+
contexts: readonly string[] = ["apps"],
|
|
69
|
+
options: IconOptions = {},
|
|
70
|
+
): IconLookup => {
|
|
71
|
+
const lookup = themedIcons(domicile, dataDirs, contexts, options);
|
|
72
|
+
return async (name) =>
|
|
73
|
+
(await lookup(name)).map((icon) => icon.map(({ url }) => url));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Icons under `dataDirs`, highest priority first, in `contexts` (`apps`,
|
|
78
|
+
* `actions`, `status` and so on). A name with no icon falls back to its
|
|
79
|
+
* `-symbolic` one. An absolute name is read as a file, as the spec allows.
|
|
80
|
+
*
|
|
81
|
+
* Reads the themes on the first lookup, and each name once. Make a new
|
|
82
|
+
* lookup to see newly installed icons.
|
|
83
|
+
*/
|
|
84
|
+
export const themedIcons = (
|
|
85
|
+
domicile: AppsDesktop,
|
|
86
|
+
dataDirs: readonly string[],
|
|
87
|
+
contexts: readonly string[] = ["apps"],
|
|
88
|
+
{ size = 48, theme }: IconOptions = {},
|
|
89
|
+
): ThemedIconLookup => {
|
|
90
|
+
let listed: Promise<Result<Listed[][], SystemError>> | undefined;
|
|
91
|
+
const looked = new Map<
|
|
92
|
+
string,
|
|
93
|
+
Promise<Result<Option<ThemedIcon>, SystemError>>
|
|
94
|
+
>();
|
|
95
|
+
return (name) => {
|
|
96
|
+
const known = looked.get(name);
|
|
97
|
+
if (known === undefined) {
|
|
98
|
+
listed ??= listings(domicile, dataDirs, contexts, theme);
|
|
99
|
+
const lookup = name.startsWith("/")
|
|
100
|
+
? read(domicile, name)
|
|
101
|
+
: listed.then((themes) =>
|
|
102
|
+
themes.andThenAsync((each) =>
|
|
103
|
+
orSymbolic(domicile, each, size, name),
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
looked.set(name, lookup);
|
|
107
|
+
return lookup;
|
|
108
|
+
} else {
|
|
109
|
+
return known;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** A directory icons may be in, the file names it holds, and its sizes. */
|
|
115
|
+
type Listed = {
|
|
116
|
+
dir: string;
|
|
117
|
+
names: ReadonlySet<string>;
|
|
118
|
+
/** Absent for `pixmaps`, which suits every size. */
|
|
119
|
+
directory: ThemeDirectory | undefined;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/** A theme's name and the directories it lists. */
|
|
123
|
+
type Theme = { name: string; directories: readonly ThemeDirectory[] };
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Every directory an icon may be in: one list per theme, in the order to
|
|
127
|
+
* search them, then `pixmaps`.
|
|
128
|
+
*/
|
|
129
|
+
const listings = async (
|
|
130
|
+
domicile: AppsDesktop,
|
|
131
|
+
dataDirs: readonly string[],
|
|
132
|
+
contexts: readonly string[],
|
|
133
|
+
theme: string | undefined,
|
|
134
|
+
): Promise<Result<Listed[][], SystemError>> => {
|
|
135
|
+
const themes = await chain(domicile, dataDirs, contexts, theme);
|
|
136
|
+
return themes.andThenAsync(async (each) =>
|
|
137
|
+
allOk(
|
|
138
|
+
await Promise.all([
|
|
139
|
+
...each.map((one) => themeListing(domicile, dataDirs, one)),
|
|
140
|
+
pixmaps(domicile, dataDirs),
|
|
141
|
+
]),
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* `theme` and the themes it inherits, depth first and each once, then
|
|
148
|
+
* `hicolor`. A theme installed nowhere is skipped.
|
|
149
|
+
*/
|
|
150
|
+
const chain = async (
|
|
151
|
+
domicile: AppsDesktop,
|
|
152
|
+
dataDirs: readonly string[],
|
|
153
|
+
contexts: readonly string[],
|
|
154
|
+
theme: string | undefined,
|
|
155
|
+
): Promise<Result<Theme[], SystemError>> => {
|
|
156
|
+
const seen = new Set<string>();
|
|
157
|
+
const visit = async (name: string): Promise<Result<Theme[], SystemError>> => {
|
|
158
|
+
if (seen.has(name)) {
|
|
159
|
+
return Ok([]);
|
|
160
|
+
} else {
|
|
161
|
+
seen.add(name);
|
|
162
|
+
const loaded = await loadTheme(domicile, dataDirs, contexts, name);
|
|
163
|
+
return loaded.andThenAsync((found) =>
|
|
164
|
+
found.match({
|
|
165
|
+
None: () => Promise.resolve(Ok<Theme[], SystemError>([])),
|
|
166
|
+
Some: async ({ directories, inherits }) =>
|
|
167
|
+
// One parent at a time, so `seen` holds the earlier ones'.
|
|
168
|
+
(
|
|
169
|
+
await inherits.reduce(
|
|
170
|
+
async (earlier, parent) =>
|
|
171
|
+
(
|
|
172
|
+
await earlier
|
|
173
|
+
).andThenAsync(async (themes) =>
|
|
174
|
+
(await visit(parent)).map((more) => [...themes, ...more]),
|
|
175
|
+
),
|
|
176
|
+
Promise.resolve(Ok<Theme[], SystemError>([])),
|
|
177
|
+
)
|
|
178
|
+
).map((parents) => [{ directories, name }, ...parents]),
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
const themed = await visit(theme ?? HICOLOR);
|
|
184
|
+
return themed.andThenAsync(async (themes) =>
|
|
185
|
+
(await visit(HICOLOR)).map((hicolor) => [...themes, ...hicolor]),
|
|
186
|
+
);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Theme `name`'s parents and the directories in `contexts`, from the first
|
|
191
|
+
* `index.theme` found, or nothing if it is not installed.
|
|
192
|
+
*/
|
|
193
|
+
const loadTheme = async (
|
|
194
|
+
domicile: AppsDesktop,
|
|
195
|
+
dataDirs: readonly string[],
|
|
196
|
+
contexts: readonly string[],
|
|
197
|
+
name: string,
|
|
198
|
+
): Promise<
|
|
199
|
+
Result<
|
|
200
|
+
Option<{ inherits: readonly string[]; directories: ThemeDirectory[] }>,
|
|
201
|
+
SystemError
|
|
202
|
+
>
|
|
203
|
+
> => {
|
|
204
|
+
for (const dir of dataDirs) {
|
|
205
|
+
const text = orAbsent(
|
|
206
|
+
(await domicile.files.readText(`${dir}/icons/${name}/index.theme`)).map(
|
|
207
|
+
(read): Option<string> => Some(read),
|
|
208
|
+
),
|
|
209
|
+
None(),
|
|
210
|
+
);
|
|
211
|
+
if (text.mapOr(true, (found) => found.isSome())) {
|
|
212
|
+
return text.map((found) =>
|
|
213
|
+
found.map((read) => {
|
|
214
|
+
const { directories, inherits } = parseIndexTheme(read);
|
|
215
|
+
return {
|
|
216
|
+
directories: directories.filter(
|
|
217
|
+
({ context }) =>
|
|
218
|
+
context === undefined || contexts.includes(context),
|
|
219
|
+
),
|
|
220
|
+
inherits,
|
|
221
|
+
};
|
|
222
|
+
}),
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return name === HICOLOR
|
|
227
|
+
? (await laidOut(domicile, dataDirs, contexts)).map((directories) =>
|
|
228
|
+
Some({ directories, inherits: [] }),
|
|
229
|
+
)
|
|
230
|
+
: Ok(None());
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/** `hicolor`'s `<size>/<context>` directories, read from its layout. */
|
|
234
|
+
const laidOut = async (
|
|
235
|
+
domicile: AppsDesktop,
|
|
236
|
+
dataDirs: readonly string[],
|
|
237
|
+
contexts: readonly string[],
|
|
238
|
+
): Promise<Result<ThemeDirectory[], SystemError>> =>
|
|
239
|
+
allOk(
|
|
240
|
+
await Promise.all(
|
|
241
|
+
dataDirs.map(async (dir) =>
|
|
242
|
+
orAbsent(
|
|
243
|
+
await domicile.files.readDir(`${dir}/icons/${HICOLOR}`),
|
|
244
|
+
[],
|
|
245
|
+
).map((entries) => entries.map((entry) => entry.name)),
|
|
246
|
+
),
|
|
247
|
+
),
|
|
248
|
+
).map((listed) => {
|
|
249
|
+
const sizes = [...new Set(listed.flat())].flatMap(sized);
|
|
250
|
+
return contexts.flatMap((context) =>
|
|
251
|
+
sizes.map((directory) => ({
|
|
252
|
+
...directory,
|
|
253
|
+
context,
|
|
254
|
+
path: `${directory.path}/${context}`,
|
|
255
|
+
})),
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
/** A `hicolor` size directory such as `48x48` or `scalable`. */
|
|
260
|
+
const sized = (name: string): Omit<ThemeDirectory, "context">[] => {
|
|
261
|
+
const pixels = /^(\d+)x\1$/.exec(name)?.[1];
|
|
262
|
+
if (name === "scalable") {
|
|
263
|
+
return [
|
|
264
|
+
{
|
|
265
|
+
maxSize: 512,
|
|
266
|
+
minSize: 1,
|
|
267
|
+
path: name,
|
|
268
|
+
size: 128,
|
|
269
|
+
threshold: 2,
|
|
270
|
+
type: DirectoryType.Scalable,
|
|
271
|
+
},
|
|
272
|
+
];
|
|
273
|
+
} else {
|
|
274
|
+
return pixels === undefined
|
|
275
|
+
? []
|
|
276
|
+
: [
|
|
277
|
+
{
|
|
278
|
+
maxSize: Number(pixels),
|
|
279
|
+
minSize: Number(pixels),
|
|
280
|
+
path: name,
|
|
281
|
+
size: Number(pixels),
|
|
282
|
+
threshold: 2,
|
|
283
|
+
type: DirectoryType.Threshold,
|
|
284
|
+
},
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/** `theme`'s directories that exist, each directory under every data directory. */
|
|
290
|
+
const themeListing = async (
|
|
291
|
+
domicile: AppsDesktop,
|
|
292
|
+
dataDirs: readonly string[],
|
|
293
|
+
{ directories, name }: Theme,
|
|
294
|
+
): Promise<Result<Listed[], SystemError>> => {
|
|
295
|
+
const roots = allOk(
|
|
296
|
+
await Promise.all(
|
|
297
|
+
dataDirs.map(async (dir) => {
|
|
298
|
+
const root = `${dir}/icons/${name}`;
|
|
299
|
+
return orAbsent(await domicile.files.readDir(root), []).map(
|
|
300
|
+
(entries) => ({
|
|
301
|
+
root,
|
|
302
|
+
tops: new Set(entries.map((entry) => entry.name)),
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
}),
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
return roots.andThenAsync(async (present) =>
|
|
309
|
+
allOk(
|
|
310
|
+
await Promise.all(
|
|
311
|
+
directories.flatMap((directory) =>
|
|
312
|
+
present
|
|
313
|
+
.filter(({ tops }) => tops.has(directory.path.split("/")[0] ?? ""))
|
|
314
|
+
.map(({ root }) =>
|
|
315
|
+
listing(domicile, `${root}/${directory.path}`, directory),
|
|
316
|
+
),
|
|
317
|
+
),
|
|
318
|
+
),
|
|
319
|
+
),
|
|
320
|
+
);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const pixmaps = async (
|
|
324
|
+
domicile: AppsDesktop,
|
|
325
|
+
dataDirs: readonly string[],
|
|
326
|
+
): Promise<Result<Listed[], SystemError>> =>
|
|
327
|
+
allOk(
|
|
328
|
+
await Promise.all(
|
|
329
|
+
dataDirs.map((dir) => listing(domicile, `${dir}/pixmaps`, undefined)),
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const listing = async (
|
|
334
|
+
domicile: AppsDesktop,
|
|
335
|
+
dir: string,
|
|
336
|
+
directory: ThemeDirectory | undefined,
|
|
337
|
+
): Promise<Result<Listed, SystemError>> =>
|
|
338
|
+
orAbsent(await domicile.files.readDir(dir), []).map((entries) => ({
|
|
339
|
+
dir,
|
|
340
|
+
directory,
|
|
341
|
+
names: new Set(entries.map((entry) => entry.name)),
|
|
342
|
+
}));
|
|
343
|
+
|
|
344
|
+
/** `name`'s icon, else its `-symbolic` one's. */
|
|
345
|
+
const orSymbolic = async (
|
|
346
|
+
domicile: AppsDesktop,
|
|
347
|
+
themes: readonly Listed[][],
|
|
348
|
+
size: number,
|
|
349
|
+
name: string,
|
|
350
|
+
): Promise<Result<Option<ThemedIcon>, SystemError>> => {
|
|
351
|
+
const plain = await named(domicile, themes, size, name);
|
|
352
|
+
return plain.mapOr(true, (found) => found.isSome()) || name.endsWith(SYMBOLIC)
|
|
353
|
+
? plain
|
|
354
|
+
: named(domicile, themes, size, `${name}${SYMBOLIC}`);
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* `name`'s best icon: in the first theme that has one, the file whose
|
|
359
|
+
* directory suits `size` best, earliest listed among equals.
|
|
360
|
+
*/
|
|
361
|
+
const named = async (
|
|
362
|
+
domicile: AppsDesktop,
|
|
363
|
+
themes: readonly Listed[][],
|
|
364
|
+
size: number,
|
|
365
|
+
name: string,
|
|
366
|
+
): Promise<Result<Option<ThemedIcon>, SystemError>> => {
|
|
367
|
+
for (const dirs of themes) {
|
|
368
|
+
const candidates = dirs
|
|
369
|
+
.flatMap(({ dir, directory, names }) =>
|
|
370
|
+
Object.keys(KINDS)
|
|
371
|
+
.map((kind) => `${name}.${kind}`)
|
|
372
|
+
.filter((file) => names.has(file))
|
|
373
|
+
.map((file) => ({
|
|
374
|
+
distance:
|
|
375
|
+
directory === undefined ? 0 : sizeDistance(directory, size),
|
|
376
|
+
path: `${dir}/${file}`,
|
|
377
|
+
})),
|
|
378
|
+
)
|
|
379
|
+
.toSorted((a, b) => a.distance - b.distance);
|
|
380
|
+
for (const { path } of candidates) {
|
|
381
|
+
const icon = await read(domicile, path);
|
|
382
|
+
if (icon.mapOr(true, (found) => found.isSome())) {
|
|
383
|
+
return icon;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return Ok(None());
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/** The file at `path` as an icon, or nothing if it cannot be drawn. */
|
|
391
|
+
const read = async (
|
|
392
|
+
domicile: AppsDesktop,
|
|
393
|
+
path: string,
|
|
394
|
+
): Promise<Result<Option<ThemedIcon>, SystemError>> => {
|
|
395
|
+
const dot = path.lastIndexOf(".");
|
|
396
|
+
const mime = KINDS[path.slice(dot + 1)];
|
|
397
|
+
return mime === undefined
|
|
398
|
+
? Ok(None())
|
|
399
|
+
: orAbsent(
|
|
400
|
+
await (await domicile.files.stat(path)).andThenAsync(
|
|
401
|
+
async ({ size }) =>
|
|
402
|
+
size > LARGEST
|
|
403
|
+
? Ok(None<ThemedIcon>())
|
|
404
|
+
: (await domicile.files.read(path)).map((bytes) =>
|
|
405
|
+
Some({
|
|
406
|
+
symbolic: path.slice(0, dot).endsWith(SYMBOLIC),
|
|
407
|
+
url: dataUrl(mime, bytes),
|
|
408
|
+
}),
|
|
409
|
+
),
|
|
410
|
+
),
|
|
411
|
+
None(),
|
|
412
|
+
);
|
|
413
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// A readable name for an application known only by its id.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `id` as a person reads it: the last part of a reverse-DNS id, capitalized,
|
|
5
|
+
* with `-` and `_` as spaces. `com.chromium.Chromium` is `Chromium`.
|
|
6
|
+
*/
|
|
7
|
+
export const appIdName = (id: string): string => {
|
|
8
|
+
const words = (id.split(".").at(-1) ?? id).replaceAll(/[-_]/g, " ");
|
|
9
|
+
return `${words.charAt(0).toUpperCase()}${words.slice(1)}`;
|
|
10
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The parts of the desktop the apps library reads through.
|
|
2
|
+
|
|
3
|
+
import type { Dbus } from "@domicile-desktop/sdk/desktop/dbus";
|
|
4
|
+
import type { Files } from "@domicile-desktop/sdk/desktop/files";
|
|
5
|
+
import type { Processes } from "@domicile-desktop/sdk/desktop/processes";
|
|
6
|
+
|
|
7
|
+
/** What the library calls: files and processes, and D-Bus for the icon theme. */
|
|
8
|
+
export type AppsDesktop = {
|
|
9
|
+
readonly dbus: Pick<Dbus, "call">;
|
|
10
|
+
readonly files: Pick<Files, "read" | "readDir" | "readText" | "stat">;
|
|
11
|
+
readonly processes: Pick<Processes, "run" | "spawn">;
|
|
12
|
+
};
|
package/src/bookmark.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Web pages a launcher offers by name, beside the applications.
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import { ranked } from "./ranked";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A launcher row that opens `url`. Only `http` and `https`: the launcher opens
|
|
9
|
+
* it as a page and fetches the site's icon from it.
|
|
10
|
+
*/
|
|
11
|
+
const bookmarkSchema = z.strictObject({
|
|
12
|
+
/** The row's label, which queries match. */
|
|
13
|
+
name: z.string(),
|
|
14
|
+
url: z
|
|
15
|
+
.string()
|
|
16
|
+
.regex(/^https?:\/\/./i, "a bookmark's URL must be http or https"),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/** A shell's bookmarks, as its config gives them. */
|
|
20
|
+
export const bookmarksSchema = z.array(bookmarkSchema).readonly();
|
|
21
|
+
|
|
22
|
+
export type Bookmark = z.infer<typeof bookmarkSchema>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Up to `limit` bookmarks matching `query`, best first.
|
|
26
|
+
*
|
|
27
|
+
* Matches as `findApps` does, so rows agree: every query word must appear,
|
|
28
|
+
* case-insensitively, in the name or URL.
|
|
29
|
+
*/
|
|
30
|
+
export const findBookmarks = (
|
|
31
|
+
bookmarks: readonly Bookmark[],
|
|
32
|
+
query: string,
|
|
33
|
+
limit: number,
|
|
34
|
+
): Bookmark[] =>
|
|
35
|
+
ranked(bookmarks, query, limit, {
|
|
36
|
+
name: ({ name }) => name,
|
|
37
|
+
tieBreak: ({ url }) => url,
|
|
38
|
+
words: ({ name, url }) => `${name} ${url}`.toLowerCase(),
|
|
39
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// A file's content type, by name, from shared-mime-info's `mime/globs2`. See
|
|
2
|
+
// https://specifications.freedesktop.org/shared-mime-info-spec/latest/.
|
|
3
|
+
|
|
4
|
+
import type { Option, Result } from "@cprussin/option-result";
|
|
5
|
+
import { None, Ok, Some } from "@cprussin/option-result";
|
|
6
|
+
import type { SystemError } from "@domicile-desktop/sdk/system";
|
|
7
|
+
import { FileType } from "@domicile-desktop/sdk/system";
|
|
8
|
+
import type { AppsDesktop } from "./apps-desktop";
|
|
9
|
+
|
|
10
|
+
import { globOf } from "./glob";
|
|
11
|
+
import { allOk, orAbsent } from "./results";
|
|
12
|
+
|
|
13
|
+
/** The type shared-mime-info gives every directory. */
|
|
14
|
+
const DIRECTORY = "inode/directory";
|
|
15
|
+
|
|
16
|
+
/** One `globs2` line. */
|
|
17
|
+
type Glob = { weight: number; type: string; pattern: string; glob: RegExp };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `path`'s content type, or nothing when no glob matches its name.
|
|
21
|
+
*
|
|
22
|
+
* Matches the name only; contents are not sniffed. Reads `mime/globs2` under
|
|
23
|
+
* each of `dataDirs`, missing ones skipped. The heaviest match wins, then the
|
|
24
|
+
* longest pattern, then the earliest directory.
|
|
25
|
+
*/
|
|
26
|
+
export const contentTypeOf = async (
|
|
27
|
+
domicile: AppsDesktop,
|
|
28
|
+
dataDirs: readonly string[],
|
|
29
|
+
path: string,
|
|
30
|
+
): Promise<Result<Option<string>, SystemError>> =>
|
|
31
|
+
(await domicile.files.stat(path)).andThenAsync(async ({ fileType }) =>
|
|
32
|
+
fileType === FileType.Directory
|
|
33
|
+
? Ok<Option<string>, SystemError>(Some(DIRECTORY))
|
|
34
|
+
: allOk(
|
|
35
|
+
await Promise.all(
|
|
36
|
+
dataDirs.map(async (dir) =>
|
|
37
|
+
orAbsent(
|
|
38
|
+
await domicile.files.readText(`${dir}/mime/globs2`),
|
|
39
|
+
"",
|
|
40
|
+
).map(globsIn),
|
|
41
|
+
),
|
|
42
|
+
),
|
|
43
|
+
).map((globs) => matching(globs.flat(), nameOf(path))),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
/** The type of the best glob matching `name`. */
|
|
47
|
+
const matching = (globs: readonly Glob[], name: string): Option<string> => {
|
|
48
|
+
const best = globs
|
|
49
|
+
.filter(({ glob }) => glob.test(name))
|
|
50
|
+
.toSorted(
|
|
51
|
+
(a, b) => b.weight - a.weight || b.pattern.length - a.pattern.length,
|
|
52
|
+
)[0];
|
|
53
|
+
return best === undefined ? None() : Some(best.type);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** The globs `text` lists: `weight:type:glob[:flags]`. */
|
|
57
|
+
const globsIn = (text: string): Glob[] =>
|
|
58
|
+
text
|
|
59
|
+
.split("\n")
|
|
60
|
+
.filter((line) => line !== "" && !line.startsWith("#"))
|
|
61
|
+
.map((line) => {
|
|
62
|
+
const [weight = "", type = "", pattern = "", flags = ""] =
|
|
63
|
+
line.split(":");
|
|
64
|
+
const glob = globOf(pattern);
|
|
65
|
+
return {
|
|
66
|
+
glob: flags.split(",").includes("cs")
|
|
67
|
+
? glob
|
|
68
|
+
: new RegExp(glob.source, `${glob.flags}i`),
|
|
69
|
+
pattern,
|
|
70
|
+
type,
|
|
71
|
+
weight: Number(weight),
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const nameOf = (path: string): string => path.slice(path.lastIndexOf("/") + 1);
|