thedesk/app/main.js

288 lines
8.3 KiB
JavaScript
Raw Normal View History

2019-06-15 19:52:28 +10:00
var dirname = __dirname;
2019-05-19 17:39:30 +10:00
var dir = 'file://' + __dirname;
var base = dir + '/view/';
2018-01-28 23:22:43 +11:00
// Electronのモジュール
const electron = require("electron");
const fs = require("fs");
2019-04-03 14:59:29 +11:00
const language = require('./main/language.js');
const css = require('./main/css.js');
const dl = require('./main/dl.js');
const img = require('./main/img.js');
const np = require('./main/np.js');
const systemFunc = require('./main/system.js');
2019-05-19 17:39:30 +10:00
const Menu = electron.Menu
2019-01-27 01:27:35 +11:00
const join = require('path').join;
2019-09-22 20:55:59 +10:00
2018-01-28 23:22:43 +11:00
// アプリケーションをコントロールするモジュール
const app = electron.app;
// ウィンドウを作成するモジュール
const BrowserWindow = electron.BrowserWindow;
// メインウィンドウはGCされないようにグローバル宣言
let mainWindow;
// アプリが多重起動しないようにする
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', () => {
// 多重起動を試みた場合、既に存在するウィンドウにフォーカスを移す
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
2019-09-22 20:55:59 +10:00
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
}
2019-08-02 01:02:52 +10:00
if (process.argv.indexOf("--dev") === -1) {
var packaged = true;
} else {
var packaged = false;
console.log(
2019-09-22 20:55:59 +10:00
"||\\\\\\ \n" +
"|||| \\\\\\\\ \n" +
"|||| \\\\\\\\ \n" +
"|||| Am I a \\\\\\\\ \n" +
"|||| cat? ^ ^ \\\\\\\\\\ _____ _ ____ _ \n" +
"|||| (.-.) \\\\\\\\\\ |_ _| |__ ___| _ \\ ___ ___| | __\n" +
"|||| ___> ) ||||| | | | '_ \\ / _ \\ | | |/ _ \\/ __| |/ /\n" +
"|||| < _ _) ////// | | | | | | __/ |_| | __/\__ \\ < \n" +
"|||| |_||_| ///// |_| |_| |_|\\___|____/ \\___||___/_|\\_\\ \n" +
"|||| ///// \n" +
"|||| /////\n" +
"|||| /////\n" +
2019-08-02 01:02:52 +10:00
"||||//////"
)
console.log("Welcome!")
}
2018-01-28 23:22:43 +11:00
var info_path = join(app.getPath("userData"), "window-size.json");
2018-06-12 01:44:28 +10:00
var max_info_path = join(app.getPath("userData"), "max-window-size.json");
2019-05-19 17:39:30 +10:00
var lang_path = join(app.getPath("userData"), "language");
var ha_path = join(app.getPath("userData"), "hardwareAcceleration");
try {
fs.readFileSync(ha_path, 'utf8');
app.disableHardwareAcceleration()
2019-09-22 20:55:59 +10:00
if (!packaged) console.log("disabled: Hardware Acceleration");
2019-05-19 17:39:30 +10:00
} catch{
2019-09-22 20:55:59 +10:00
if (!packaged) console.log("enabled: Hardware Acceleration");
}
2018-01-28 23:22:43 +11:00
var window_size;
try {
window_size = JSON.parse(fs.readFileSync(info_path, 'utf8'));
} catch (e) {
window_size = {
width: 1000,
height: 750
}; // デフォルトバリュー
}
2018-06-12 01:44:28 +10:00
var max_window_size;
try {
max_window_size = JSON.parse(fs.readFileSync(max_info_path, 'utf8'));
} catch (e) {
max_window_size = {
width: "string",
height: "string",
x: "string",
y: "string"
2019-03-07 14:21:27 +11:00
2018-06-12 01:44:28 +10:00
}; // デフォルトバリュー
}
2019-05-19 17:39:30 +10:00
function isFile(file) {
2019-04-11 02:52:01 +10:00
try {
fs.statSync(file);
return true
} catch (err) {
if (err.code === 'ENOENT') return false
2019-03-07 14:21:27 +11:00
}
}
2018-01-28 23:22:43 +11:00
// 全てのウィンドウが閉じたら終了
2019-05-19 17:39:30 +10:00
app.on('window-all-closed', function () {
2018-01-28 23:22:43 +11:00
if (process.platform != 'darwin') {
2019-05-19 17:39:30 +10:00
electron.session.defaultSession.clearCache(() => { })
2018-01-28 23:22:43 +11:00
app.quit();
}
});
2019-04-05 21:49:18 +11:00
// macOSでウィンドウを閉じた後に再度開けるようにする
2019-05-19 17:39:30 +10:00
app.on('activate', function () {
2019-04-05 21:49:18 +11:00
if (mainWindow == null) {
createWindow();
}
});
2018-01-28 23:22:43 +11:00
function createWindow() {
2019-08-08 00:14:20 +10:00
if (isFile(lang_path)) {
2019-04-11 02:52:01 +10:00
var lang = fs.readFileSync(lang_path, 'utf8');
} else {
2019-05-19 17:39:30 +10:00
var langs = app.getLocale();
2019-04-11 02:52:01 +10:00
console.log(langs);
2019-05-19 17:39:30 +10:00
if (~langs.indexOf("ja")) {
lang = "ja";
2019-08-08 00:14:20 +10:00
} else if (~langs.indexOf("de")) {
lang = "de";
} else if (~langs.indexOf("cs")) {
lang = "cs";
} else if (~langs.indexOf("bg")) {
lang = "bg";
2019-05-19 17:39:30 +10:00
} else {
lang = "en";
2019-04-11 02:52:01 +10:00
}
fs.mkdir(app.getPath("userData"), function (err) {
2019-05-19 17:39:30 +10:00
fs.writeFileSync(lang_path, lang);
2019-04-11 02:52:01 +10:00
});
}
2019-09-22 20:55:59 +10:00
if (!packaged) console.log("your lang:" + app.getLocale());
if (!packaged) console.log("launch:" + lang);
2018-01-28 23:22:43 +11:00
// メイン画面の表示。ウィンドウの幅、高さを指定できる
2019-05-19 17:39:30 +10:00
var platform = process.platform;
var bit = process.arch;
if (platform == "linux") {
2019-06-15 00:17:16 +10:00
var arg = {
webPreferences: {
2019-06-16 02:08:10 +10:00
webviewTag: true,
2019-06-15 00:17:16 +10:00
nodeIntegration: false,
contextIsolation: true,
2019-06-16 02:08:10 +10:00
preload: join(__dirname, "js", "platform", "preload.js")
2019-06-15 00:17:16 +10:00
},
2019-08-11 03:52:32 +10:00
width: window_size.width, height: window_size.height, x: window_size.x, y: window_size.y, icon: __dirname + '/desk.png', show: false
2019-06-15 00:17:16 +10:00
}
2019-05-19 17:39:30 +10:00
} else if (platform == "win32") {
2019-06-15 00:17:16 +10:00
var arg = {
webPreferences: {
2019-06-16 02:08:10 +10:00
webviewTag: true,
2019-06-15 00:17:16 +10:00
nodeIntegration: false,
contextIsolation: true,
2019-06-16 02:08:10 +10:00
preload: join(__dirname, "js", "platform", "preload.js")
2019-06-15 00:17:16 +10:00
},
2019-08-11 03:52:32 +10:00
width: window_size.width, height: window_size.height, x: window_size.x, y: window_size.y, simpleFullscreen: true, show: false
2019-06-15 00:17:16 +10:00
}
2019-05-19 17:39:30 +10:00
} else if (platform == "darwin") {
2019-06-15 00:17:16 +10:00
var arg = {
webPreferences: {
2019-06-16 02:08:10 +10:00
webviewTag: true,
2019-06-15 00:17:16 +10:00
nodeIntegration: false,
contextIsolation: true,
2019-06-16 02:08:10 +10:00
preload: join(__dirname, "js", "platform", "preload.js")
2019-06-15 00:17:16 +10:00
},
2019-08-11 03:52:32 +10:00
width: window_size.width, height: window_size.height, x: window_size.x, y: window_size.y, simpleFullscreen: true, show: false
2019-06-15 00:17:16 +10:00
}
2018-04-16 23:58:14 +10:00
}
mainWindow = new BrowserWindow(arg);
2019-08-11 03:52:32 +10:00
mainWindow.once('page-title-updated', () => {
mainWindow.show()
if (window_size.max) {
mainWindow.maximize();
}
2019-09-22 20:55:59 +10:00
})
2019-05-19 17:39:30 +10:00
electron.session.defaultSession.clearCache(() => { })
if (process.argv) {
if (process.argv[1]) {
2018-03-11 01:22:59 +11:00
var m = process.argv[1].match(/([a-zA-Z0-9]+)\/\?[a-zA-Z-0-9]+=(.+)/);
2019-05-19 17:39:30 +10:00
if (m) {
var mode = m[1];
var code = m[2];
var plus = '?mode=' + mode + '&code=' + code;
} else {
var plus = "";
2018-02-18 16:43:11 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
var plus = "";
2018-02-18 16:43:11 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
var plus = "";
2018-02-18 16:43:11 +11:00
}
2019-05-19 17:39:30 +10:00
mainWindow.loadURL(base + lang + '/index.html' + plus);
if (!window_size.x && !window_size.y) {
2018-06-12 01:44:28 +10:00
mainWindow.center();
}
2018-01-28 23:22:43 +11:00
// ウィンドウが閉じられたらアプリも終了
2019-05-19 17:39:30 +10:00
mainWindow.on('closed', function () {
electron.ipcMain.removeAllListeners();
2018-01-28 23:22:43 +11:00
mainWindow = null;
});
2019-09-22 20:55:59 +10:00
closeArg = false
mainWindow.on('close', function (e, arg) {
if(!closeArg){
e.preventDefault()
}
const promise = new Promise(function (resolve) {
mainWindow.webContents.send('asReadEnd', "")
setTimeout(function () { resolve() }, 3000)
})
promise.then((function (response) {
writePos(mainWindow)
closeArg = true
mainWindow.close()
})
);
});
electron.ipcMain.on('sendMarkersComplete', function (e, arg) {
closeArg = true
mainWindow.close()
})
function writePos(mainWindow){
2019-05-19 17:39:30 +10:00
if (
max_window_size.width == mainWindow.getBounds().width &&
max_window_size.height == mainWindow.getBounds().height &&
max_window_size.x == mainWindow.getBounds().x &&
max_window_size.y == mainWindow.getBounds().y
) {
var size = { width: mainWindow.getBounds().width, height: mainWindow.getBounds().height, x: mainWindow.getBounds().x, y: mainWindow.getBounds().y, max: true }
} else {
var size = { width: mainWindow.getBounds().width, height: mainWindow.getBounds().height, x: mainWindow.getBounds().x, y: mainWindow.getBounds().y }
2018-06-12 01:44:28 +10:00
}
2019-09-22 20:55:59 +10:00
fs.writeFileSync(info_path, JSON.stringify(size))
}
2019-05-19 17:39:30 +10:00
mainWindow.on('maximize', function () {
2018-06-12 01:44:28 +10:00
fs.writeFileSync(max_info_path, JSON.stringify(mainWindow.getBounds()));
2018-01-28 23:22:43 +11:00
});
2019-09-22 20:55:59 +10:00
mainWindow.on('minimize', function () {
mainWindow.webContents.send('asRead', "")
})
2019-05-19 17:39:30 +10:00
var platform = process.platform;
var bit = process.arch;
2019-08-26 22:06:07 +10:00
Menu.setApplicationMenu(Menu.buildFromTemplate(language.template(lang, mainWindow, packaged, dir, dirname)));
2019-04-03 14:59:29 +11:00
//CSS
css.css(mainWindow);
//アップデータとダウンロード
2019-06-15 19:52:28 +10:00
dl.dl(mainWindow, lang_path, base, dirname);
2019-04-03 14:59:29 +11:00
//画像選択と画像処理
2019-05-19 17:39:30 +10:00
img.img(mainWindow, dir);
2019-04-03 14:59:29 +11:00
//NowPlaying
np.TheDeskNowPlaying(mainWindow);
//その他system
2019-06-15 19:52:28 +10:00
systemFunc.system(mainWindow, dir, lang, dirname);
2019-09-22 20:55:59 +10:00
setInterval(function () { mouseTrack(mainWindow) }, 1000);
}
var x = 0
var y = 0
var unchanged = 0
var locked = false
function mouseTrack(mainWindow) {
let mousePos = electron.screen.getCursorScreenPoint()
let xNow = mousePos.x
let yNow = mousePos.x
if (x != xNow || y != yNow) {
unchanged = 0
locked = false
} else {
unchanged++
if (unchanged > 60 && !locked) {
unchanged = 0
locked = true
mainWindow.webContents.send('asRead', "")
}
}
x = xNow
y = yNow
2018-01-28 23:22:43 +11:00
}
// Electronの初期化完了後に実行
app.on('ready', createWindow);
2019-05-19 17:39:30 +10:00
var onError = function (err, response) {
console.error(err, response);
2018-03-31 13:39:06 +11:00
};
2019-02-28 04:02:23 +11:00
app.setAsDefaultProtocolClient('thedesk')