Skip to content

Commit

Permalink
Get mac apps from system profiler
Browse files Browse the repository at this point in the history
nukeop committed Oct 5, 2022
1 parent 1896d50 commit f950fc3
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion electron/main/args.ts
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ export const readCLIFlags = () => {

const opts = program.opts();

Logger.info("CLI flags:", opts);
Logger.debug("CLI flags:", opts);

ArgsProvider.flags = {
...config,
4 changes: 3 additions & 1 deletion electron/main/freedesktop/desktop-apps.ts
Original file line number Diff line number Diff line change
@@ -127,7 +127,9 @@ export const readDesktopEntries = async () => {
const startTime = process.hrtime();

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

4 changes: 3 additions & 1 deletion electron/typings.d.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ type RustDesktopEntry = {
};

declare module "*index.node" {
export const getDesktopApps: () => RustDesktopEntry[];
export const getDesktopApps: (
platform: NodeJS.Platform
) => RustDesktopEntry[];
export const getAppsMac: () => string;
}
6 changes: 3 additions & 3 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -11,11 +11,11 @@ fn get_desktop_apps<'a>(mut cx: CallContext<'a, JsObject>) -> JsResult<'a, JsArr
let strategy_str: Handle<JsString> = strategy.downcast(&mut cx).unwrap();
match strategy_str.value(&mut cx).as_str() {
"linux" => {
let entries = LinuxDesktopApps::to_js_array(&mut cx)?.clone();
let entries = LinuxDesktopApps::to_js_array(&mut cx);
Ok(entries)
}
"mac" => {
let entries = MacDesktopApps::to_js_array(&mut cx)?;
"darwin" => {
let entries = MacDesktopApps::to_js_array(&mut cx);
Ok(entries)
}
_ => cx.throw_error("Unsupported platform"),
30 changes: 25 additions & 5 deletions native/src/services/desktop_apps/mac.rs
Original file line number Diff line number Diff line change
@@ -18,11 +18,31 @@ impl DesktopAppsProvider for MacDesktopApps {
let json = String::from_utf8_lossy(&output.stdout);
let v: Value = serde_json::from_str(&json).unwrap();

for app in v.as_array().unwrap() {
let path = app["path"].as_str().unwrap().to_string();
let name = app["_name"].as_str().unwrap().to_string();

entries.push(DesktopApp::new(path, name, None, None));
match v.as_object().unwrap().get("SPApplicationsDataType") {
Some(apps) => {
for app in apps.as_array().unwrap() {
let path = app
.as_object()
.unwrap()
.get("path")
.unwrap()
.as_str()
.unwrap()
.to_string();

let name = app
.as_object()
.unwrap()
.get("_name")
.unwrap()
.as_str()
.unwrap()
.to_string();

entries.push(DesktopApp::new(path, name, None, None));
}
}
None => {}
}

entries
10 changes: 5 additions & 5 deletions native/src/services/desktop_apps/mod.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ impl DesktopApp {
}
}

pub fn to_js_object<'a>(&'a self, cx: &'a mut FunctionContext) -> JsResult<JsObject> {
pub fn to_js_object<'a>(&self, cx: &mut FunctionContext<'a>) -> JsResult<'a, JsObject> {
let obj = cx.empty_object();

let path = cx.string(&self.path);
@@ -51,15 +51,15 @@ impl DesktopApp {

pub trait DesktopAppsProvider {
fn get_desktop_entries() -> Vec<DesktopApp>;
fn to_js_array<'a>(mut cx: &mut CallContext<'a, JsObject>) -> JsResult<'a, JsArray> {
fn to_js_array<'a>(cx: &mut FunctionContext<'a>) -> Handle<'a, JsArray> {
let entries = cx.empty_array();

for (i, entry) in Self::get_desktop_entries().iter().enumerate() {
let obj = entry.to_js_object(&mut cx)?;
let obj = entry.to_js_object(cx).unwrap();

entries.set(cx, i as u32, obj)?;
entries.set(cx, i as u32, obj).unwrap();
}

Ok(entries)
entries
}
}

0 comments on commit f950fc3

Please sign in to comment.