1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | if (typeof b !== "function" && b !== null)
|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12 | extendStatics(d, b);
|
13 | function __() { this.constructor = d; }
|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15 | };
|
16 | })();
|
17 | var __assign = (this && this.__assign) || function () {
|
18 | __assign = Object.assign || function(t) {
|
19 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
20 | s = arguments[i];
|
21 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
22 | t[p] = s[p];
|
23 | }
|
24 | return t;
|
25 | };
|
26 | return __assign.apply(this, arguments);
|
27 | };
|
28 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
29 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
30 | return new (P || (P = Promise))(function (resolve, reject) {
|
31 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
32 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
33 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
34 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
35 | });
|
36 | };
|
37 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
38 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
39 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
40 | function verb(n) { return function (v) { return step([n, v]); }; }
|
41 | function step(op) {
|
42 | if (f) throw new TypeError("Generator is already executing.");
|
43 | while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
44 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
45 | if (y = 0, t) op = [op[0] & 2, t.value];
|
46 | switch (op[0]) {
|
47 | case 0: case 1: t = op; break;
|
48 | case 4: _.label++; return { value: op[1], done: false };
|
49 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
50 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
51 | default:
|
52 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
53 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
54 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
55 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
56 | if (t[2]) _.ops.pop();
|
57 | _.trys.pop(); continue;
|
58 | }
|
59 | op = body.call(thisArg, _);
|
60 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
61 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
62 | }
|
63 | };
|
64 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
65 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
66 | };
|
67 | Object.defineProperty(exports, "__esModule", { value: true });
|
68 | exports.Menubar = void 0;
|
69 | var electron_1 = require("electron");
|
70 | var electron_positioner_1 = __importDefault(require("electron-positioner"));
|
71 | var events_1 = require("events");
|
72 | var fs_1 = __importDefault(require("fs"));
|
73 | var path_1 = __importDefault(require("path"));
|
74 | var cleanOptions_1 = require("./util/cleanOptions");
|
75 | var getWindowPosition_1 = require("./util/getWindowPosition");
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | var Menubar = (function (_super) {
|
82 | __extends(Menubar, _super);
|
83 | function Menubar(app, options) {
|
84 | var _this = _super.call(this) || this;
|
85 | _this._blurTimeout = null;
|
86 | _this._app = app;
|
87 | _this._options = (0, cleanOptions_1.cleanOptions)(options);
|
88 | _this._isVisible = false;
|
89 | if (app.isReady()) {
|
90 |
|
91 | process.nextTick(function () {
|
92 | return _this.appReady().catch(function (err) { return console.error('menubar: ', err); });
|
93 | });
|
94 | }
|
95 | else {
|
96 | app.on('ready', function () {
|
97 | _this.appReady().catch(function (err) { return console.error('menubar: ', err); });
|
98 | });
|
99 | }
|
100 | return _this;
|
101 | }
|
102 | Object.defineProperty(Menubar.prototype, "app", {
|
103 | |
104 |
|
105 |
|
106 |
|
107 | get: function () {
|
108 | return this._app;
|
109 | },
|
110 | enumerable: false,
|
111 | configurable: true
|
112 | });
|
113 | Object.defineProperty(Menubar.prototype, "positioner", {
|
114 | |
115 |
|
116 |
|
117 |
|
118 | get: function () {
|
119 | if (!this._positioner) {
|
120 | throw new Error('Please access `this.positioner` after the `after-create-window` event has fired.');
|
121 | }
|
122 | return this._positioner;
|
123 | },
|
124 | enumerable: false,
|
125 | configurable: true
|
126 | });
|
127 | Object.defineProperty(Menubar.prototype, "tray", {
|
128 | |
129 |
|
130 |
|
131 | get: function () {
|
132 | if (!this._tray) {
|
133 | throw new Error('Please access `this.tray` after the `ready` event has fired.');
|
134 | }
|
135 | return this._tray;
|
136 | },
|
137 | enumerable: false,
|
138 | configurable: true
|
139 | });
|
140 | Object.defineProperty(Menubar.prototype, "window", {
|
141 | |
142 |
|
143 |
|
144 |
|
145 | get: function () {
|
146 | return this._browserWindow;
|
147 | },
|
148 | enumerable: false,
|
149 | configurable: true
|
150 | });
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | Menubar.prototype.getOption = function (key) {
|
157 | return this._options[key];
|
158 | };
|
159 | |
160 |
|
161 |
|
162 | Menubar.prototype.hideWindow = function () {
|
163 | if (!this._browserWindow || !this._isVisible) {
|
164 | return;
|
165 | }
|
166 | this.emit('hide');
|
167 | this._browserWindow.hide();
|
168 | this.emit('after-hide');
|
169 | this._isVisible = false;
|
170 | if (this._blurTimeout) {
|
171 | clearTimeout(this._blurTimeout);
|
172 | this._blurTimeout = null;
|
173 | }
|
174 | };
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | Menubar.prototype.setOption = function (key, value) {
|
182 | this._options[key] = value;
|
183 | };
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 | Menubar.prototype.showWindow = function (trayPos) {
|
190 | return __awaiter(this, void 0, void 0, function () {
|
191 | var noBoundsPosition, position, x, y;
|
192 | return __generator(this, function (_a) {
|
193 | switch (_a.label) {
|
194 | case 0:
|
195 | if (!this.tray) {
|
196 | throw new Error('Tray should have been instantiated by now');
|
197 | }
|
198 | if (!!this._browserWindow) return [3 , 2];
|
199 | return [4 , this.createWindow()];
|
200 | case 1:
|
201 | _a.sent();
|
202 | _a.label = 2;
|
203 | case 2:
|
204 |
|
205 | if (!this._browserWindow) {
|
206 | throw new Error('Window has been initialized just above. qed.');
|
207 | }
|
208 |
|
209 |
|
210 | if (['win32', 'linux'].includes(process.platform)) {
|
211 |
|
212 | this._options.windowPosition = (0, getWindowPosition_1.getWindowPosition)(this.tray);
|
213 | }
|
214 | this.emit('show');
|
215 | if (trayPos && trayPos.x !== 0) {
|
216 |
|
217 | this._cachedBounds = trayPos;
|
218 | }
|
219 | else if (this._cachedBounds) {
|
220 |
|
221 | trayPos = this._cachedBounds;
|
222 | }
|
223 | else if (this.tray.getBounds) {
|
224 |
|
225 | trayPos = this.tray.getBounds();
|
226 | }
|
227 | noBoundsPosition = undefined;
|
228 | if ((trayPos === undefined || trayPos.x === 0) &&
|
229 | this._options.windowPosition &&
|
230 | this._options.windowPosition.startsWith('tray')) {
|
231 | noBoundsPosition =
|
232 | process.platform === 'win32' ? 'bottomRight' : 'topRight';
|
233 | }
|
234 | position = this.positioner.calculate(this._options.windowPosition || noBoundsPosition, trayPos);
|
235 | x = this._options.browserWindow.x !== undefined
|
236 | ? this._options.browserWindow.x
|
237 | : position.x;
|
238 | y = this._options.browserWindow.y !== undefined
|
239 | ? this._options.browserWindow.y
|
240 | : position.y;
|
241 |
|
242 |
|
243 | this._browserWindow.setPosition(Math.round(x), Math.round(y));
|
244 | this._browserWindow.show();
|
245 | this._isVisible = true;
|
246 | this.emit('after-show');
|
247 | return [2 ];
|
248 | }
|
249 | });
|
250 | });
|
251 | };
|
252 | Menubar.prototype.appReady = function () {
|
253 | return __awaiter(this, void 0, void 0, function () {
|
254 | var trayImage, defaultClickEvent;
|
255 | var _this = this;
|
256 | return __generator(this, function (_a) {
|
257 | switch (_a.label) {
|
258 | case 0:
|
259 | if (this.app.dock && !this._options.showDockIcon) {
|
260 | this.app.dock.hide();
|
261 | }
|
262 | if (this._options.activateWithApp) {
|
263 | this.app.on('activate', function (_event, hasVisibleWindows) {
|
264 | if (!hasVisibleWindows) {
|
265 | _this.showWindow().catch(console.error);
|
266 | }
|
267 | });
|
268 | }
|
269 | trayImage = this._options.icon ||
|
270 | path_1.default.join(this._options.dir, 'IconTemplate.png');
|
271 | if (typeof trayImage === 'string' && !fs_1.default.existsSync(trayImage)) {
|
272 | trayImage = path_1.default.join(__dirname, '..', 'assets', 'IconTemplate.png');
|
273 | }
|
274 | defaultClickEvent = this._options.showOnRightClick
|
275 | ? 'right-click'
|
276 | : 'click';
|
277 | this._tray = this._options.tray || new electron_1.Tray(trayImage);
|
278 |
|
279 | if (!this.tray) {
|
280 | throw new Error('Tray has been initialized above');
|
281 | }
|
282 | this.tray.on(defaultClickEvent,
|
283 |
|
284 | this.clicked.bind(this));
|
285 |
|
286 | this.tray.on('double-click', this.clicked.bind(this));
|
287 | this.tray.setToolTip(this._options.tooltip);
|
288 | if (!this._options.windowPosition) {
|
289 |
|
290 | this._options.windowPosition = (0, getWindowPosition_1.getWindowPosition)(this.tray);
|
291 | }
|
292 | if (!this._options.preloadWindow) return [3 , 2];
|
293 | return [4 , this.createWindow()];
|
294 | case 1:
|
295 | _a.sent();
|
296 | _a.label = 2;
|
297 | case 2:
|
298 | this.emit('ready');
|
299 | return [2 ];
|
300 | }
|
301 | });
|
302 | });
|
303 | };
|
304 | |
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | Menubar.prototype.clicked = function (event, bounds) {
|
311 | return __awaiter(this, void 0, void 0, function () {
|
312 | return __generator(this, function (_a) {
|
313 | switch (_a.label) {
|
314 | case 0:
|
315 | if (event && (event.shiftKey || event.ctrlKey || event.metaKey)) {
|
316 | return [2 , this.hideWindow()];
|
317 | }
|
318 |
|
319 | if (this._blurTimeout) {
|
320 | clearInterval(this._blurTimeout);
|
321 | }
|
322 | if (this._browserWindow && this._isVisible) {
|
323 | return [2 , this.hideWindow()];
|
324 | }
|
325 | this._cachedBounds = bounds || this._cachedBounds;
|
326 | return [4 , this.showWindow(this._cachedBounds)];
|
327 | case 1:
|
328 | _a.sent();
|
329 | return [2 ];
|
330 | }
|
331 | });
|
332 | });
|
333 | };
|
334 | Menubar.prototype.createWindow = function () {
|
335 | return __awaiter(this, void 0, void 0, function () {
|
336 | var defaults;
|
337 | var _this = this;
|
338 | return __generator(this, function (_a) {
|
339 | switch (_a.label) {
|
340 | case 0:
|
341 | this.emit('create-window');
|
342 | defaults = {
|
343 | show: false,
|
344 | frame: false,
|
345 | };
|
346 | this._browserWindow = new electron_1.BrowserWindow(__assign(__assign({}, defaults), this._options.browserWindow));
|
347 | this._positioner = new electron_positioner_1.default(this._browserWindow);
|
348 | this._browserWindow.on('blur', function () {
|
349 | if (!_this._browserWindow) {
|
350 | return;
|
351 | }
|
352 |
|
353 | _this._browserWindow.isAlwaysOnTop()
|
354 | ? _this.emit('focus-lost')
|
355 | : (_this._blurTimeout = setTimeout(function () {
|
356 | _this.hideWindow();
|
357 | }, 100));
|
358 | });
|
359 | if (this._options.showOnAllWorkspaces !== false) {
|
360 |
|
361 | this._browserWindow.setVisibleOnAllWorkspaces(true, {
|
362 | skipTransformProcessType: true,
|
363 | });
|
364 | }
|
365 | this._browserWindow.on('close', this.windowClear.bind(this));
|
366 | this.emit('before-load');
|
367 | if (!(this._options.index !== false)) return [3 , 2];
|
368 | return [4 , this._browserWindow.loadURL(this._options.index, this._options.loadUrlOptions)];
|
369 | case 1:
|
370 | _a.sent();
|
371 | _a.label = 2;
|
372 | case 2:
|
373 | this.emit('after-create-window');
|
374 | return [2 ];
|
375 | }
|
376 | });
|
377 | });
|
378 | };
|
379 | Menubar.prototype.windowClear = function () {
|
380 | this._browserWindow = undefined;
|
381 | this.emit('after-close');
|
382 | };
|
383 | return Menubar;
|
384 | }(events_1.EventEmitter));
|
385 | exports.Menubar = Menubar;
|