forked from mikaa123/linear
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
172 lines (150 loc) · 3.97 KB
/
main.js
File metadata and controls
172 lines (150 loc) · 3.97 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const globalShortcut = electron.globalShortcut;
const ipc = electron.ipcMain;
const Tray = electron.Tray;
const Menu = electron.Menu;
const path = require('path');
let rulers = [];
let mainWindow;
let settingsWindow;
/**
* Create a ruler window and push it to the `rulers` array. The ruler window
* is frameless, transparent and resizable. This type of windows might not be
* well supported on every platforms.
* @param {Object} windowInfo - Size and position of the window
*/
function createNewRuler(windowInfo) {
let _windowInfo = windowInfo || {};
let ruler = new BrowserWindow({
width: _windowInfo.width || 151,
height: _windowInfo.height || 126,
frame: false,
transparent: true,
alwaysOnTop: true,
x: _windowInfo.x,
y: _windowInfo.y,
'min-width': 151,
'min-height': 126,
'standard-window': false,
'use-content-size': true
});
ruler.loadURL(`file://${__dirname}/src/app/ruler/ruler.html`);
ruler.on('closed', () => {
rulers.splice(rulers.indexOf(ruler), 1);
ruler = undefined;
});
rulers.push(ruler);
}
let hidden = false;
/**
* Toggle a ruler asynchronously and give back a promise.
* @param {BrowserWindow} ruler - The ruler to toggle
* @return {Promise}
*/
let toggleRuler = (ruler) => {
return new Promise((resolve) => setTimeout(() => {
ruler[hidden ? 'hide' : 'show']();
resolve();
}));
};
/**
* Show/Hide rulers on the screen.
*/
function toggleRulerCommand() {
if (!rulers.length) {
return;
}
hidden = !hidden;
// Close all rulers sequencially. Doing otherwise doesn't toggle them
// properly. This is most likely an issue with Electron.
rulers.slice(1).reduce((acc, val) => {
return acc.then(() => toggleRuler(val));
}, toggleRuler(rulers[0]));
}
app.on('ready', function() {
let trayIcon = new Tray(path.join(__dirname, 'src/assets/images/lrTemplate.png'));
var trayMenuTemplate = [
{
label: 'New ruler',
accelerator: 'Command+Ctrl+R',
click: createNewRuler
},
{
label: 'Toggle rulers',
accelerator: 'Command+Ctrl+T',
click: toggleRulerCommand,
enabled: false
},
{
type: 'separator'
},
{
label: 'Settings',
click: () => {
if (settingsWindow) {
return;
}
settingsWindow = new BrowserWindow({
width: 200,
height: 170,
alwaysOnTop: true
});
settingsWindow.loadURL(`file://${__dirname}/src/app/settings/settings.html`);
settingsWindow.on('closed', () => {
settingsWindow = null;
});
}
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: () => app.quit()
}
];
let trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
let toggleRulersMenu = trayMenu.items[1];
trayIcon.setContextMenu(trayMenu);
// Application menu
let appMenu = Menu.buildFromTemplate([{
label: require('electron').app.getName(),
submenu: [{
label: 'Quit',
accelerator: 'Command+Q',
click: () => app.quit()
}]
},{
label: 'Window',
role: 'window',
submenu: [{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close'
}]
}]);
Menu.setApplicationMenu(appMenu);
// We observe changes on the `rulers` array to enable/disable the toggle menu.
Array.observe(rulers, () => toggleRulersMenu.enabled = !!rulers.length);
globalShortcut.register('Command + Control + T', toggleRulerCommand);
globalShortcut.register('Command + Control + R', createNewRuler);
});
// We make sure not to quit when every windows are closed.
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
// Let rulers know that a config changed. For instance, the application uses
// 'em' instead of 'px' as units.
ipc.on('settings-changed', () => {
rulers.forEach((ruler) => ruler.send('settings-changed'));
});
// Duplicate a given ruler.
ipc.on('duplicate-ruler', (evt, duplicateInfo) => {
createNewRuler(duplicateInfo);
});