thedesk/app/main.js

173 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

2019-04-05 21:49:18 +11:00
2019-04-03 14:59:29 +11: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');
2018-03-27 13:39:35 +11:00
const Menu=electron.Menu
2019-01-27 01:27:35 +11:00
const join = require('path').join;
2018-01-28 23:22:43 +11:00
// アプリケーションをコントロールするモジュール
const app = electron.app;
// ウィンドウを作成するモジュール
const BrowserWindow = electron.BrowserWindow;
// メインウィンドウはGCされないようにグローバル宣言
let mainWindow;
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");
2018-09-12 02:49:41 +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()
console.log("disabled: HA");
}catch{
console.log("enabled: HA");
}
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-04-11 02:52:01 +10:00
function isFile(file){
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
// 全てのウィンドウが閉じたら終了
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
2018-03-18 02:00:53 +11:00
electron.session.defaultSession.clearCache(() => {})
2018-01-28 23:22:43 +11:00
app.quit();
}
});
2019-04-05 21:49:18 +11:00
// macOSでウィンドウを閉じた後に再度開けるようにする
app.on('activate', function() {
if (mainWindow == null) {
createWindow();
}
});
2018-01-28 23:22:43 +11:00
function createWindow() {
2019-04-11 02:52:01 +10:00
if(isFile(lang_path)) {
console.log("exist");
var lang = fs.readFileSync(lang_path, 'utf8');
} else {
var langs=app.getLocale();
console.log(langs);
if(~langs.indexOf("ja")){
lang="ja";
}else{
lang="en";
}
fs.mkdir(app.getPath("userData"), function (err) {
fs.writeFileSync(lang_path,lang);
});
}
console.log(app.getLocale());
console.log("launch:"+lang);
2018-01-28 23:22:43 +11:00
// メイン画面の表示。ウィンドウの幅、高さを指定できる
2018-04-16 23:58:14 +10:00
var platform=process.platform;
var bit=process.arch;
if(platform=="linux"){
2019-01-22 03:24:37 +11:00
var arg={width:window_size.width,height:window_size.height,x:window_size.x,y:window_size.y,icon: __dirname + '/desk.png'}
2019-03-13 02:51:07 +11:00
}else if(platform=="win32"){
var arg={width:window_size.width,height:window_size.height,x:window_size.x,y:window_size.y,simpleFullscreen:true}
}else if(platform=="darwin"){
2018-06-12 01:44:28 +10:00
var arg={width:window_size.width,height:window_size.height,x:window_size.x,y:window_size.y,simpleFullscreen:true}
2018-04-16 23:58:14 +10:00
}
mainWindow = new BrowserWindow(arg);
2018-01-28 23:22:43 +11:00
electron.session.defaultSession.clearCache(() => {})
2018-02-18 16:43:11 +11:00
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]+=(.+)/);
2018-02-18 16:43:11 +11:00
if(m){
var mode=m[1];
var code=m[2];
2018-09-12 02:49:41 +10:00
var plus='?mode='+mode+'&code='+code;
2018-02-18 16:43:11 +11:00
}else{
2018-09-12 02:49:41 +10:00
var plus="";
2018-02-18 16:43:11 +11:00
}
}else{
2018-09-12 02:49:41 +10:00
var plus="";
2018-02-18 16:43:11 +11:00
}
}else{
2018-09-12 02:49:41 +10:00
var plus="";
2018-02-18 16:43:11 +11:00
}
2019-04-03 14:59:29 +11:00
mainWindow.loadURL(base+lang+'/index.html'+plus);
2018-06-12 01:44:28 +10:00
if(!window_size.x && !window_size.y){
mainWindow.center();
}
if(window_size.max){
mainWindow.maximize();
}
2018-01-28 23:22:43 +11:00
// ウィンドウが閉じられたらアプリも終了
mainWindow.on('closed', function() {
electron.ipcMain.removeAllListeners();
2018-01-28 23:22:43 +11:00
mainWindow = null;
});
mainWindow.on('close', function() {
2018-06-12 01:44:28 +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}
}
fs.writeFileSync(info_path, JSON.stringify(size));
});
mainWindow.on('maximize', function() {
fs.writeFileSync(max_info_path, JSON.stringify(mainWindow.getBounds()));
2018-01-28 23:22:43 +11:00
});
2019-03-07 14:21:27 +11:00
2018-03-27 13:39:35 +11:00
var platform=process.platform;
var bit=process.arch;
if(platform=="darwin"){
2019-04-04 03:09:57 +11:00
Menu.setApplicationMenu(Menu.buildFromTemplate(language.template(lang,mainWindow,false,dir)));
2018-03-27 13:39:35 +11:00
}
2019-04-03 14:59:29 +11:00
//CSS
css.css(mainWindow);
//アップデータとダウンロード
dl.dl(mainWindow,lang_path,base);
//画像選択と画像処理
img.img(mainWindow,dir);
//NowPlaying
np.TheDeskNowPlaying(mainWindow);
//その他system
systemFunc.system(mainWindow,dir,lang);
2018-01-28 23:22:43 +11:00
}
// Electronの初期化完了後に実行
app.on('ready', createWindow);
2018-03-31 13:39:06 +11:00
var onError = function(err,response){
console.error(err,response);
};
2019-02-28 04:02:23 +11:00
app.setAsDefaultProtocolClient('thedesk')