Skip to content

Commit

Permalink
Merge pull request #31 from rolandbernard/devel
Browse files Browse the repository at this point in the history
v0.0.21
  • Loading branch information
rolandbernard committed Mar 12, 2021
2 parents 720a4a3 + b4a2f1d commit 9ee32b9
Show file tree
Hide file tree
Showing 10 changed files with 1,381 additions and 1,033 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marvin",
"version": "0.0.20",
"version": "0.0.21",
"license": "MIT",
"scripts": {
"dev": "electron-webpack dev",
Expand Down
4 changes: 2 additions & 2 deletions src/common/local/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export function getTranslation(config, text) {

export function getAllTranslation(text) {
return Object.keys(SUPPORTED_LANGUAGES)
.map((lang) => TRANSLATIONS[lang][text])
.filter((trans) => trans);
.map((lang) => [TRANSLATIONS[lang][text], lang])
.filter(([trans, _]) => trans);
}
2 changes: 1 addition & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { app } from 'electron';
import { loadConfig, config } from './config';
import { loadConfig } from './config';
import { initModules, deinitModules } from './executor';

app.commandLine.appendSwitch("disable-gpu"); // Transparancy will not work without this
Expand Down
3 changes: 2 additions & 1 deletion src/main/modules/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ const BookmarksModule = {
return query.trim().length >= 1;
},
search: async (query, regex) => {
const language = config.general.language;
const bookmarks = [
...(await getChromiumBookmarks()),
...(await getMidoriBookmarks()),
...(await getFirefoxBookmarks())
];
const bookmark_match = Math.max(...(
getAllTranslation('bookmarks').map((trans) => stringMatchQuality(query, trans, regex))
getAllTranslation('bookmarks').map(([trans, lang]) => (lang === language ? 1 : 0.5) * stringMatchQuality(query, trans, regex))
));
return bookmarks.map((bookmark) => ({
type: 'icon_list_item',
Expand Down
3 changes: 2 additions & 1 deletion src/main/modules/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ const ClipboardModule = {
return query.trim().length > 0;
},
search: async (query, regex) => {
const language = config.general.language;
const clipboard_match = Math.max(...(
getAllTranslation('clipboard').map((trans) => stringMatchQuality(query, trans, regex))
getAllTranslation('clipboard').map(([trans, lang]) => (lang === language ? 1 : 0.5) * stringMatchQuality(query, trans, regex))
));
return clipboard_history.map((text) => ({
type: 'icon_text',
Expand Down
3 changes: 3 additions & 0 deletions src/main/modules/deepl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const DeeplModule = {
init: async () => {
if (config.modules.deepl.active) {
window = new BrowserWindow({
webPreferences: {
contextIsolation: false,
},
show: false,
width: 1000,
height: 1000,
Expand Down
94 changes: 60 additions & 34 deletions src/main/modules/linux-applications.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import { config } from "../config";
import { readdir, readFile, exists, writeFileSync, existsSync, readFileSync } from "fs";
import { readdir, readFile, writeFileSync, existsSync, readFileSync, stat } from "fs";
import { app, ipcMain } from 'electron';
import path, { join } from 'path';
import path from 'path';
import { exec } from "child_process";
import { stringMatchQuality } from '../search';

Expand Down Expand Up @@ -33,11 +33,11 @@ function getProp(object, name, fallback) {
: (object[name] || fallback);
}

function getProps(object, name, fallback) {
function getProps(object, name) {
if (object[name] instanceof Object) {
return Object.values(object[name]);
return Object.keys(object[name]).map(key => [object[name][key], key.toLowerCase()]);
} else if (object[name]) {
return [object[name]];
return [ [object[name], ''] ];
} else {
return [];
}
Expand Down Expand Up @@ -123,28 +123,37 @@ function createIconIndex(theme, fallback_theme) {
}

function findIconPath(name) {
return new Promise((resolve) => {
exists(name, (exist) => {
if (exist) {
resolve(name);
} else {
const possible = [
`${name}`,
`${name}.svg`,
`${name}.png`,
`${name.toLowerCase()}`,
`${name.toLowerCase()}.svg`,
`${name.toLowerCase()}.png`
];
for (let file of possible) {
if (icon_index[file]) {
resolve(icon_index[file]);
return;
}
return new Promise(async (resolve) => {
if (name) {
const possible = [
`${name}.svg`,
`${name}.png`,
`${name}.jpg`,
`${name}.jpeg`,
`${name.toLowerCase()}.svg`,
`${name.toLowerCase()}.png`,
`${name.toLowerCase()}.jpg`,
`${name.toLowerCase()}.jpeg`,
`${name.toLowerCase()}`,
`${name}`,
];
for (const file of possible) {
const stats = await new Promise((res) => {
stat(file, (_, stats) => {
res(stats);
});
});
if (stats?.isFile()) {
resolve(file);
return;
} else if (icon_index[file]) {
resolve(icon_index[file]);
return;
}
resolve(null);
}
});
}
resolve(null);
return;
});
}

Expand All @@ -155,6 +164,8 @@ function pathToDataUrl(path) {
const mime_endings = {
'__default__': 'text/plain',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
};
let mime = mime_endings[Object.keys(mime_endings).find((ending) => path.endsWith(ending)) || '__default__'];
Expand Down Expand Up @@ -191,7 +202,13 @@ async function loadApplications() {
entry = null;
}
} else if (entry) {
let option = line.split('=').map((value) => value.trim());
let option;
if (line.includes('=')) {
const split = line.indexOf('=');
option = [ line.substr(0, split).trim(), line.substr(split + 1).trim() ];
} else {
option = [ line.trim() ];
}
if (option[0].endsWith(']')) {
let index = option[0].split('[');
index[1] = index[1].replace(']', '');
Expand Down Expand Up @@ -235,6 +252,14 @@ async function loadApplications() {
})));
updateCache();
}

function getQualityForProp(object, prop, text, regex, scale) {
return Math.max(...(getProps(object, prop)
.map(([value, lang]) =>
scale * (lang === 'default' || lang.includes(config.general.language) ? 1 : 0.5) * stringMatchQuality(text, value, regex)
)
));
}

ipcMain.on('update-applications', (_) => {
loadApplications();
Expand All @@ -261,15 +286,16 @@ const LinuxApplicationModule = {
return query.trim().length >= 1;
},
search: async (query, regex) => {
const language = config.general.language;
return applications.map((app) => {
const name = getProp(app.desktop, 'Name', app.application.replace('.desktop', ''));
const app_match = Math.max(
...(getProps(app.desktop, 'Name').map((prop) => stringMatchQuality(query, prop, regex))),
...(getProps(app.desktop, 'Comment').map((prop) => 0.75 * stringMatchQuality(query, prop, regex))),
...(getProps(app.desktop, 'Keywords').map((prop) => 0.75 * stringMatchQuality(query, prop, regex))),
...(getProps(app.desktop, 'Categories').map((prop) => 0.75 * stringMatchQuality(query, prop, regex))),
...(getProps(app.desktop, '.desktop').map((prop) => 0.75 * stringMatchQuality(query, prop, regex))),
...(getProps(app.desktop, 'GenericName').map((prop) => 0.75 * stringMatchQuality(query, prop, regex))),
getQualityForProp(app.desktop, 'Name', query, regex, 1),
getQualityForProp(app.desktop, 'Keywords', query, regex, 0.5),
getQualityForProp(app.desktop, 'Categories', query, regex, 0.5),
getQualityForProp(app.desktop, 'Comment', query, regex, 0.5),
getQualityForProp(app.desktop, 'GenericName', query, regex, 0.5),
getQualityForProp(app.desktop, '.desktop', query, regex, 0.5),
);
const icon = app.desktop.icon;
return Object.values(app).filter((value) => value instanceof Object).map((value) => ({
Expand All @@ -280,8 +306,8 @@ const LinuxApplicationModule = {
executable: true,
quality: Math.max(
app_match,
...(getProps(value, 'Name').map((prop) => 0.5 * stringMatchQuality(query, prop, regex))),
...(getProps(value, 'Comment').map((prop) => 0.25 * stringMatchQuality(query, prop, regex)))
getQualityForProp(value, 'Name', query, regex, 0.5),
getQualityForProp(value, 'Comment', query, regex, 0.25),
),
app: value,
}));
Expand Down
3 changes: 2 additions & 1 deletion src/main/modules/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ const MainModule = {
return query.trim().length >= 1;
},
search: async (query, regex) => {
const language = config.general.language;
const quit_match = Math.max(...(
getAllTranslation('quit').map((trans) => stringMatchQuality(query, trans, regex))
getAllTranslation('quit').map(([trans, lang]) => (lang === language ? 1 : 0.5) * stringMatchQuality(query, trans, regex))
));
return [{
type: 'icon_list_item',
Expand Down
3 changes: 2 additions & 1 deletion src/main/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ const SettingsModule = {
return query.trim().length >= 1;
},
search: async (query, regex) => {
const language = config.general.language;
const settings_match = Math.max(...(
getAllTranslation('settings').map((trans) => stringMatchQuality(query, trans, regex))
getAllTranslation('settings').map(([trans, lang]) => (lang === language ? 1 : 0.5) * stringMatchQuality(query, trans, regex))
));
return [{
type: 'icon_list_item',
Expand Down
Loading

0 comments on commit 9ee32b9

Please sign in to comment.