Skip to content

Commit

Permalink
Get desktop entries on linux using rust
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Oct 3, 2022
1 parent 2958f18 commit d0be51c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 22 deletions.
6 changes: 3 additions & 3 deletions electron/main/freedesktop/desktop-apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
DesktopEntry,
DESKTOP_ENTRY_HEADER,
} from "../../../common/desktop-entries";
import rustModules from "../../../native/index.node";
import { ArgsProvider } from "../args";
import Logger from "../logger";
import freedesktopIcons from "freedesktop-icons";
import fs from "fs";
import path from "path";
import { xdgDataDirectories } from "xdg-basedir";
Expand All @@ -14,7 +14,7 @@ export type DesktopEntryWithPath = {
entry: DesktopEntry;
};
export class DesktopEntriesProvider {
static desktopEntries: DesktopEntryWithPath[] = [];
static desktopEntries: RustDesktopEntry[] = [];
static isDone = false;

private constructor() {}
Expand Down Expand Up @@ -127,7 +127,7 @@ export const readDesktopEntries = async () => {
const startTime = process.hrtime();

if (!DesktopEntriesProvider.isDone && ArgsProvider.flags.mode === "apps") {
DesktopEntriesProvider.desktopEntries = await getDesktopEntries();
DesktopEntriesProvider.desktopEntries = rustModules.getDesktopEntries();
DesktopEntriesProvider.isDone = true;
}

Expand Down
6 changes: 0 additions & 6 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ const indexHtml = join(ROOT_PATH.dist, "index.html");
const startTime = process.hrtime();
await readCLIFlags();

// console.log(rustModules.hello());

// console.log(rustModules.getDesktopEntries());
console.log(JSON.parse(rustModules.getAppsMac()));
app.exit();

// Set application name for Windows 10+ notifications
if (process.platform === "win32") app.setAppUserModelId(app.getName());

Expand Down
7 changes: 4 additions & 3 deletions electron/main/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ export class OptionsProvider {

static getOptionsFromDesktopEntries = async (): Promise<LauncherOption[]> => {
await readDesktopEntries();

return DesktopEntriesProvider.desktopEntries.map(
(entry, index) =>
({
id: (index + 1).toString(),
name: entry.entry[DESKTOP_ENTRY_HEADER].Name,
description: entry.entry[DESKTOP_ENTRY_HEADER].Comment,
icon: entry.entry[DESKTOP_ENTRY_HEADER].Icon,
name: entry.name,
description: entry.description,
icon: entry.icon && `file://${entry.icon}`,
onAction: {
type: LauncherActionType.RunDesktopFile,
payload: path.basename(entry.path),
Expand Down
9 changes: 8 additions & 1 deletion electron/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
type RustDesktopEntry = {
path: string;
name: string;
description: string;
icon?: string;
};

declare module "*index.node" {
export const getDesktopEntries: () => object;
export const getDesktopEntries: () => RustDesktopEntry[];
export const getAppsMac: () => string;
}
71 changes: 71 additions & 0 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib"]

[dependencies]
"freedesktop-desktop-entry" = "0.5.0"
freedesktop-icons = "0.2.1"

[dependencies.neon]
version = "0.10"
Expand Down
34 changes: 25 additions & 9 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::fs;

mod mac;

use std::fs;

use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter};
use mac::get_apps_json;
use freedesktop_icons::lookup;
use neon::prelude::*;

use mac::get_apps_json;

fn get_desktop_entries(mut cx: FunctionContext) -> JsResult<JsArray> {
let entries = cx.empty_array();

Expand All @@ -15,15 +17,33 @@ fn get_desktop_entries(mut cx: FunctionContext) -> JsResult<JsArray> {
let locale = Some("en-US");
let obj = cx.empty_object();

let path_str = path.to_str().unwrap();
let path_js_str = cx.string(path_str);
obj.set(&mut cx, "path", path_js_str)?;

let name = cx.string(entry.name(locale).unwrap_or_default().to_string());
obj.set(&mut cx, "name", name)?;

let description = cx.string(entry.comment(locale).unwrap_or_default().to_string());
obj.set(&mut cx, "description", description)?;

let icon = cx.string(entry.icon().unwrap_or_default().to_string());
let icon_name = entry.icon().unwrap_or_default().to_string();
let icon = lookup(&icon_name)
.with_size(48)
.find()
.unwrap_or_default()
.into_os_string()
.into_string()
.unwrap();

if (icon.len() > 0) {
let icon_value = cx.string(icon);

obj.set(&mut cx, "icon", icon)?;
obj.set(&mut cx, "icon", icon_value)?;
}

let len = entries.len(&mut cx);
entries.set(&mut cx, len, obj)?;
}
}
}
Expand All @@ -35,10 +55,6 @@ fn get_apps_mac(mut cx: FunctionContext) -> JsResult<JsString> {
let process = get_apps_json();
let json = String::from_utf8_lossy(&process.stdout);

// entries.p

// Ok(entries)

Ok(cx.string(json))
}

Expand Down

0 comments on commit d0be51c

Please sign in to comment.