-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
145 lines (130 loc) · 4.35 KB
/
index.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {app, BrowserWindow, globalShortcut, TouchBar, dialog, Menu, MenuItem} from "electron"
import * as register from "./commands"
import * as fs from "fs"
import * as process from "process"
import * as path from "path"
import {createTouchBar} from "./shim"
let window = null
const home = process.env.HOME
const GLOWBEAR_ACCELERATOR = "CommandOrControl+Shift+."
const GLOWBEAR_CONFIGDIR = `${home}/.config/glowbear`
const GLOWBEAR_YAML = "glowbear.yaml"
app.setName("Glowbear")
//app.dock.setIcon(path.join(__dirname, "..", "witchlines-bear-toy-silhouette-300px.png"))
//app.dock.setIcon(path.join(__dirname, "..", "bears2-300px.png"))
app.dock.setIcon(path.join(__dirname, "..", "lemmling-Gummy-bear-sort-of-300px.png"))
app.once("ready", () => {
ensureConfigDir()
window = new BrowserWindow({
//backgroundColor: 'rgb(0,0,0,50)',
frame: false,
//titleBarStyle: 'hidden-inset',
//titleBarStyle: 'hidden',
transparent: true,
hasShadow: false,
width: 400, height: 600
})
window.setVisibleOnAllWorkspaces(true)
window.loadURL('file://' + __dirname + "/index.html")
// When window is blurred, don't show it in Exposé
window.on("blur", () => {
window.hide()
})
// Global shortcut should toggle visibility of Glowy.
globalShortcut.register(GLOWBEAR_ACCELERATOR, () => {
if (window.isFocused()) {
window.blur()
window.hide()
} else {
window.show()
//window.focus()
}
})
let configFile = path.join(GLOWBEAR_CONFIGDIR, GLOWBEAR_YAML)
let cmds = register.commands(configFile)
let menu = Menu.buildFromTemplate(mainMenu)
let goMenu = new Menu()
let items = [new TouchBar.TouchBarSpacer({size: "small"}), new TouchBar.TouchBarLabel({label: "glowbear"})]
cmds.forEach((cmd, i) => {
// We do two things here: Add a TouchBar button and add a menu item with keyboard shortcut.
let fn = () => {
window.blur()
// Need to app.hide instead of win.hide because the former causes the next application to be
// focused. This is necessary for mapping commands that attach to the topmost window.
app.hide()
try {
cmd.run()
} catch (e) {
dialog.showErrorBox("Script Error", "Error running script:\n"+e.message)
}
}
if (cmd.showButton) {
items.push(new TouchBar.TouchBarButton({
label: cmd.name,
backgroundColor: cmd.color,
iconPosition: "left",
click: fn
}))
}
// Add a menu item and accelerator per registered button.
let hotkey = String(i+1)
if (cmd.accelerator) {
hotkey = cmd.accelerator
}
let mi = new MenuItem({
label: cmd.name,
accelerator: hotkey,
click: fn
})
goMenu.append(mi)
})
items.push(new TouchBar.TouchBarSpacer({size: "flexible"}))
let tb = createTouchBar(items)
window.setTouchBar(tb)
goMenu.append(new MenuItem({
label: "Hide",
accelerator: "esc",
click: () => { window.blur(); app.hide() }
}))
menu.append(new MenuItem({
label: "Go",
submenu: goMenu
}))
Menu.setApplicationMenu(menu)
})
app.on("will-quit", () => {
globalShortcut.unregister(GLOWBEAR_ACCELERATOR)
})
function ensureConfigDir() {
console.log(GLOWBEAR_CONFIGDIR)
let dstat = fs.stat(GLOWBEAR_CONFIGDIR, (err) => {
if (err) {
fs.mkdirSync(GLOWBEAR_CONFIGDIR, 0o755)
}
let cfg = path.join(GLOWBEAR_CONFIGDIR, GLOWBEAR_YAML)
fs.stat(cfg, (err) => {
if (err) {
fs.writeFile(cfg, "commmands: {}", (err) => {
console.log(err)
process.exit(1)
})
}
})
})
}
let mainMenu: Electron.MenuItemConstructorOptions[] = [
{
label: app.getName(),
submenu: [
{role: 'about'},
{type: 'separator'},
{role: 'services', submenu: []},
{type: 'separator'},
{role: 'hide'},
{role: 'hideothers'},
{role: 'unhide'},
{type: 'separator'},
{role: 'quit'}
]
}
]