record-desktop
Version:
Record gifs and take screenshots on linux, built with electron.
218 lines (178 loc) • 6.89 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _electronSaveFile = require('electron-save-file');
var _electronSaveFile2 = _interopRequireDefault(_electronSaveFile);
var _electron = require('electron');
var _logger = require('./logger');
var _utils = require('./utils');
var _shortcuts = require('./shortcuts');
var registerShortcuts = _interopRequireWildcard(_shortcuts);
var _config = require('./config');
var config = _interopRequireWildcard(_config);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var WindowHolder = function () {
function WindowHolder(app) {
_classCallCheck(this, WindowHolder);
this.app = app;
this.indexProd = 'file://' + _path2.default.resolve(__dirname, '..', 'public', 'index.html');
this.indexDev = 'file://' + _path2.default.resolve(__dirname, '..', 'public', 'index-dev.html');
this.indexHtml = process.env.NODE_ENV === 'production' ? this.indexProd : this.indexDev;
this.indexIdle = 'file://' + _path2.default.resolve(__dirname, '..', 'public', 'index-idle.html');
this.hasShortcuts = registerShortcuts.hasShortcuts();
this.initUrl = this.indexHtml + '#' + (this.hasShortcuts ? '' : 'settings');
this.defaultIcon = _path2.default.resolve(__dirname + '/../icon.png');
this.recordingIcon = _path2.default.resolve(__dirname + '/../icon-recording.png');
this.mainWindow;
this.appIcon;
this.updateTrayMenu();
}
_createClass(WindowHolder, [{
key: 'destroy',
value: function destroy() {
this._mainWindow = this._appIcon = null;
}
}, {
key: 'createWindow',
value: function createWindow() {
var _this = this;
var result = void 0;
if (process.env.NODE_ENV === 'production') {
result = new _electron.BrowserWindow({ width: 800, height: 900, show: !this.hasShortcuts });
result.loadURL(this.initUrl);
} else {
result = new _electron.BrowserWindow({ width: 1200, height: 400 });
result.loadURL(this.initUrl);
result.openDevTools();
}
result.on('minimize', function () {
_this.mainWindow.setSkipTaskbar(true);
_this.mainWindow.hide();
_this.offloadContent();
(0, _logger.log)('minimize');
});
result.on('restore', function () {
_this.mainWindow.setSkipTaskbar(false);
(0, _logger.log)('restore');
});
result.on('closed', function () {
_this.destroy();
});
return result;
}
}, {
key: 'offloadContent',
value: function offloadContent() {
this.mainWindow.loadURL(this.indexIdle);
}
}, {
key: 'sendWebContents',
value: function sendWebContents(event, body) {
this.mainWindow.webContents.send(event, body);
}
}, {
key: 'createAppIcon',
value: function createAppIcon() {
var _this2 = this;
var result = new _electron.Tray(this.defaultIcon);
result.on('click', function () {
(0, _logger.log)('click appIcon ' + _this2.mainWindow.isVisible());
if (!_this2.mainWindow.isVisible()) {
_this2.mainWindow.show();
_this2.mainWindow.loadURL(_this2.indexHtml + '#');
} else {
_this2.mainWindow.hide();
_this2.offloadContent();
}
});
return result;
}
}, {
key: 'setAppIcon',
value: function setAppIcon(isRecording) {
this.appIcon.setImage(isRecording ? this.recordingIcon : this.defaultIcon);
}
}, {
key: 'updateTrayMenu',
value: function updateTrayMenu() {
var _this3 = this;
return (0, _utils.getFiles)(config.getFolder()).then(function (files) {
if (!_this3.appIcon) {
_this3.createAppIcon();
}
_this3.appIcon.setContextMenu(_electron.Menu.buildFromTemplate([{
label: 'Latest',
submenu: files.slice(0, 5).map(function (file) {
return {
label: file.filename,
submenu: [{
label: 'Upload to imgur',
click: function click() {
return (0, _utils.uploadFile)(file.url);
}
}, {
label: 'Delete',
click: function click() {
return (0, _utils.deleteFile)(file.url);
}
}, {
label: 'Save as',
click: function click() {
return (0, _electronSaveFile2.default)(file.url);
}
}]
};
})
}, {
label: 'Browse Images',
click: function click() {
_this3.mainWindow.show();
_this3.mainWindow.loadURL(_this3.indexHtml + '#');
}
}, {
label: 'Open a folder',
click: function click() {
return (0, _utils.openFile)(config.getFolder());
}
}, { type: 'separator' }, {
label: 'Settings',
click: function click() {
_this3.mainWindow.show();
_this3.mainWindow.loadURL(_this3.indexHtml + '#settings');
}
}, {
label: 'Exit',
click: function click() {
return _this3.app.quit();
}
}]));
}).catch(function (err) {
return (0, _logger.log)(err.stack);
});
}
}, {
key: 'mainWindow',
get: function get() {
if (!this._mainWindow) {
this._mainWindow = this.createWindow();
}
return this._mainWindow;
}
}, {
key: 'appIcon',
get: function get() {
if (!this._appIcon) {
this._appIcon = this.createAppIcon();
}
return this._appIcon;
}
}]);
return WindowHolder;
}();
exports.default = WindowHolder;