-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
83 lines (74 loc) · 2.09 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// default node packages
const fs = require("fs");
const url = require("url");
const path = require("path");
// third party
const { Menu, globalShortcut } = require("electron");
const autoUpdater = require("electron-updater").autoUpdater;
const objc = require("objc");
const stringSimilarity = require("string-similarity");
const menubar = require("menubar");
const setting = require("./setting");
// variables
const assetsDirectory = path.join(__dirname, "assets");
const shortcutsDirectory = path.join(__dirname, "shortcuts");
const mb = menubar({
icon: path.join(__dirname, "/assets/icon.png"),
width: 300,
height: 400,
resizable: false,
showDockIcon: false,
preloadWindow: true
});
// setup objc bridge
objc.import("AppKit");
const { NSWorkspace, js } = objc;
function getCurrentApp() {
const currentAppProxy = NSWorkspace.sharedWorkspace()
.frontmostApplication()
.localizedName();
return js(currentAppProxy);
}
function hasShortcut() {
const availableShortcuts = fs.readdirSync(shortcutsDirectory);
const matches = stringSimilarity.findBestMatch(
getCurrentApp(),
availableShortcuts
);
return matches.bestMatch.rating > 0.5 ? true : false;
}
function toggleWindow() {
mb.window.isVisible() ? mb.hideWindow() : mb.showWindow();
}
mb.on("ready", function ready() {
// mb.window.webContents.toggleDevTools();
autoUpdater.checkForUpdatesAndNotify();
globalShortcut.register(
`${setting.getKeymodifier()}+${setting.getKeycode()}`,
toggleWindow
);
});
mb.on("show", () => {
const currentApp = getCurrentApp();
const currentAppFile = `${currentApp}.yml`;
mb.tray.setHighlightMode("always");
fs.access(
path.join(shortcutsDirectory, currentAppFile),
fs.constants.R_OK,
err => {
if (err) {
mb.window.webContents.send("noShortcuts", currentApp);
} else {
mb.window.webContents.send("currentApp", currentAppFile);
}
}
);
});
mb.on("hide", () => {
mb.tray.setHighlightMode("never");
Menu.sendActionToFirstResponder("hide:");
});
mb.app.on("will-quit", () => {
globalShortcut.unregisterAll();
mb.app.quit();
});