thedesk/app/main.js

330 lines
9.3 KiB
JavaScript
Raw Normal View History

2019-11-18 03:07:44 +11:00
var dirname = __dirname
var dir = 'file://' + __dirname
var base = dir + '/view/'
2018-01-28 23:22:43 +11:00
// Electronのモジュール
2019-11-18 03:07:44 +11:00
const electron = require('electron')
2020-05-10 14:26:07 +10:00
// アプリケーションをコントロールするモジュール
const app = electron.app
// Electronの初期化完了後に実行
app.on('ready', createWindow)
2019-11-18 03:07:44 +11:00
const fs = require('fs')
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')
const Menu = electron.Menu
const join = require('path').join
2018-01-28 23:22:43 +11:00
// ウィンドウを作成するモジュール
2019-11-18 03:07:44 +11:00
const BrowserWindow = electron.BrowserWindow
2018-01-28 23:22:43 +11:00
// メインウィンドウはGCされないようにグローバル宣言
2019-11-18 03:07:44 +11:00
let mainWindow
// アプリが多重起動しないようにする
2019-11-18 03:07:44 +11:00
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
2019-11-18 03:07:44 +11:00
app.quit()
} else {
2019-11-18 03:07:44 +11:00
app.on('second-instance', () => {
// 多重起動を試みた場合、既に存在するウィンドウにフォーカスを移す
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
2019-11-18 03:07:44 +11:00
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
2019-11-18 03:07:44 +11:00
})
}
2020-05-10 14:26:07 +10:00
// 全てのウィンドウが閉じたら終了
app.on('window-all-closed', function () {
electron.session.defaultSession.clearCache(() => {})
app.quit()
})
2019-05-19 17:39:30 +10:00
function isFile(file) {
2019-04-11 02:52:01 +10:00
try {
2019-11-18 03:07:44 +11:00
fs.statSync(file)
return true
2019-04-11 02:52:01 +10:00
} catch (err) {
2019-11-18 03:07:44 +11:00
if (err.code === 'ENOENT') return false
2019-03-07 14:21:27 +11:00
}
}
2020-05-10 14:26:07 +10:00
function createWindow() {
2020-05-10 18:32:49 +10:00
var lang_path = join(app.getPath('userData'), 'language')
if (isFile(lang_path)) {
var lang = fs.readFileSync(lang_path, 'utf8')
} else {
var langs = app.getLocale()
console.log(langs)
if (~langs.indexOf('ja')) {
lang = 'ja'
} else if (~langs.indexOf('de')) {
lang = 'de'
} else if (~langs.indexOf('cs')) {
lang = 'cs'
} else if (~langs.indexOf('bg')) {
lang = 'bg'
} else {
lang = 'en'
}
fs.mkdir(app.getPath('userData'), function (err) {
fs.writeFileSync(lang_path, lang)
})
}
if (!packaged) console.log('your lang:' + app.getLocale())
if (!packaged) console.log('launch:' + lang)
2020-05-10 14:26:07 +10:00
//Opening
const package = fs.readFileSync(__dirname + '/package.json')
2020-05-10 18:32:49 +10:00
if(lang == 'ja') {
const maxims = JSON.parse(fs.readFileSync(__dirname + '/maxim.ja.json'))
var show = maxims[Math.floor(Math.random() * maxims.length)]
2020-06-16 12:06:22 +10:00
} else if(lang == 'ja-KS') {
//ja-KSも作れたらいいね
const maxims = JSON.parse(fs.readFileSync(__dirname + '/maxim.ja.json'))
var show = maxims[Math.floor(Math.random() * maxims.length)]
} else{
2020-05-10 20:39:49 +10:00
var show = 'TheDesk 2018'
2020-05-10 18:32:49 +10:00
}
2020-05-10 14:26:07 +10:00
const data = JSON.parse(package)
const version = data.version
const codename = data.codename
var openingWindow = new BrowserWindow({
width: 300,
height: 400,
transparent: false,
frame: false,
resizable: false,
})
2020-05-10 18:32:49 +10:00
openingWindow.loadURL(`${__dirname}/opening.html?ver=${version}&codename=${codename}&maxim=${encodeURI(show)}`)
2020-05-10 14:26:07 +10:00
if (process.argv.indexOf('--dev') === -1) {
var packaged = true
} else {
2020-05-10 14:26:07 +10:00
var packaged = false
console.log(
'||\\\\\\ \n' +
'|||| \\\\\\\\ \n' +
'|||| \\\\\\\\ \n' +
'|||| Am I a \\\\\\\\ \n' +
'|||| cat? ^ ^ \\\\\\\\\\ _____ _ ____ _ \n' +
'|||| (.-.) \\\\\\\\\\ |_ _| |__ ___| _ \\ ___ ___| | __\n' +
"|||| ___> ) ||||| | | | '_ \\ / _ \\ | | |/ _ \\/ __| |/ /\n" +
'|||| < _ _) ////// | | | | | | __/ |_| | __/__ \\ < \n' +
'|||| |_||_| ///// |_| |_| |_|\\___|____/ \\___||___/_|\\_\\ \n' +
'|||| ///// \n' +
'|||| /////\n' +
'|||| /////\n' +
'||||//////'
)
console.log('If it does not show the window, you might forget `npm run construct`.')
}
var info_path = join(app.getPath('userData'), 'window-size.json')
var max_info_path = join(app.getPath('userData'), 'max-window-size.json')
var ha_path = join(app.getPath('userData'), 'hardwareAcceleration')
var ua_path = join(app.getPath('userData'), 'useragent')
try {
fs.readFileSync(ha_path, 'utf8')
app.disableHardwareAcceleration()
if (!packaged) console.log('disabled: Hardware Acceleration')
} catch {
if (!packaged) console.log('enabled: Hardware Acceleration')
}
var window_size
try {
window_size = JSON.parse(fs.readFileSync(info_path, 'utf8'))
} catch (e) {
window_size = {
width: 1000,
height: 750,
} // デフォルトバリュー
}
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',
} // デフォルトバリュー
}
2018-01-28 23:22:43 +11:00
// メイン画面の表示。ウィンドウの幅、高さを指定できる
2019-11-18 03:07:44 +11:00
var platform = process.platform
var bit = process.arch
2020-05-10 14:26:07 +10:00
var arg = {
webPreferences: {
webviewTag: true,
nodeIntegration: false,
contextIsolation: true,
2020-09-17 22:41:55 +10:00
spellcheck: false,
2020-05-10 14:26:07 +10:00
preload: join(__dirname, 'js', 'platform', 'preload.js'),
},
width: window_size.width,
height: window_size.height,
x: window_size.x,
y: window_size.y,
2020-09-23 04:14:37 +10:00
show: false,
2020-05-10 14:26:07 +10:00
}
2019-11-18 03:07:44 +11:00
if (platform == 'linux') {
2020-05-10 14:26:07 +10:00
arg.resizable = true
arg.icon = __dirname + '/desk.png'
2019-11-18 03:07:44 +11:00
} else if (platform == 'win32') {
2020-05-10 14:26:07 +10:00
arg.simpleFullscreen = true
2019-11-18 03:07:44 +11:00
} else if (platform == 'darwin') {
2020-05-10 14:26:07 +10:00
arg.simpleFullscreen = true
2018-04-16 23:58:14 +10:00
}
2019-11-18 03:07:44 +11:00
mainWindow = new BrowserWindow(arg)
mainWindow.once('page-title-updated', () => {
2020-05-10 14:26:07 +10:00
openingWindow.close()
2019-11-18 03:07:44 +11:00
mainWindow.show()
console.log('Accessibility: ' + app.accessibilitySupportEnabled)
if (window_size.max) {
2019-11-18 03:07:44 +11:00
mainWindow.maximize()
2019-08-11 03:52:32 +10:00
}
2019-11-18 03:07:44 +11:00
})
mainWindow.webContents.on('page-title-updated', () => {
const url = mainWindow.webContents.getURL()
if(url.match(/https:\/\/crowdin.com\/profile/)) {
app.relaunch()
app.exit()
}
})
2019-11-18 03:07:44 +11:00
if (!packaged) mainWindow.toggleDevTools()
electron.session.defaultSession.clearCache(() => {})
2019-05-19 17:39:30 +10:00
if (process.argv) {
if (process.argv[1]) {
2019-11-18 03:07:44 +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) {
2019-11-18 03:07:44 +11:00
var mode = m[1]
var code = m[2]
var plus = '?mode=' + mode + '&code=' + code
2019-05-19 17:39:30 +10:00
} else {
2019-11-18 03:07:44 +11:00
var plus = ''
2018-02-18 16:43:11 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-18 03:07:44 +11:00
var plus = ''
2018-02-18 16:43:11 +11:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-18 03:07:44 +11:00
var plus = ''
2018-02-18 16:43:11 +11:00
}
2019-11-18 03:07:44 +11:00
var ua
2019-10-26 03:16:33 +11:00
try {
2019-11-18 03:07:44 +11:00
ua = fs.readFileSync(ua_path, 'utf8')
2019-10-26 03:16:33 +11:00
} catch (e) {
//default UA Example:
// Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) thedesk/18.11.3 Chrome/76.0.3809.146 Electron/6.0.12 Safari/537.36
2019-11-18 03:07:44 +11:00
const crypto = require('crypto')
const N = 100
2020-05-10 14:26:07 +10:00
var ua = 'Mastodon client: ' + crypto.randomBytes(N).toString('base64').substring(0, N)
2019-10-26 03:16:33 +11:00
}
2019-11-18 03:07:44 +11:00
mainWindow.loadURL(base + lang + '/index.html' + plus, { userAgent: ua })
if (!window_size.x && !window_size.y) {
mainWindow.center()
}
2018-01-28 23:22:43 +11:00
// ウィンドウが閉じられたらアプリも終了
2020-05-10 14:26:07 +10:00
mainWindow.on('closed', function () {
2019-11-18 03:07:44 +11:00
electron.ipcMain.removeAllListeners()
mainWindow = null
})
closeArg = false
2020-05-10 14:26:07 +10:00
mainWindow.on('close', function (e, arg) {
writePos(mainWindow)
2019-10-26 03:16:33 +11:00
if (!closeArg) {
2019-11-18 03:07:44 +11:00
e.preventDefault()
2019-10-26 03:16:33 +11:00
}
2020-05-10 14:26:07 +10:00
const promise = new Promise(function (resolve) {
2019-11-18 03:07:44 +11:00
mainWindow.webContents.send('asReadEnd', '')
let wait = 3000
const url = mainWindow.webContents.getURL()
if(!url.match(/index.html/)) wait = 0
2020-05-10 14:26:07 +10:00
setTimeout(function () {
2019-11-18 03:07:44 +11:00
resolve()
}, wait)
2019-11-18 03:07:44 +11:00
})
2020-05-10 14:26:07 +10:00
promise.then(function (response) {
2019-11-18 03:07:44 +11:00
closeArg = true
mainWindow.close()
})
})
2020-05-10 14:26:07 +10:00
electron.ipcMain.on('sendMarkersComplete', function (e, arg) {
2019-11-18 03:07:44 +11:00
closeArg = true
mainWindow.close()
})
function writePos(mainWindow) {
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,
2020-05-10 14:26:07 +10:00
max: true,
}
} else {
var size = {
width: mainWindow.getBounds().width,
height: mainWindow.getBounds().height,
x: mainWindow.getBounds().x,
2020-05-10 14:26:07 +10:00
y: mainWindow.getBounds().y,
}
}
fs.writeFileSync(info_path, JSON.stringify(size))
}
2020-05-10 14:26:07 +10:00
mainWindow.on('maximize', function () {
writePos(mainWindow)
fs.writeFileSync(max_info_path, JSON.stringify(mainWindow.getBounds()))
})
2020-05-10 14:26:07 +10:00
mainWindow.on('minimize', function () {
writePos(mainWindow)
2019-11-18 03:07:44 +11:00
mainWindow.webContents.send('asRead', '')
})
2019-05-19 17:39:30 +10:00
2019-11-18 03:07:44 +11:00
var platform = process.platform
var bit = process.arch
2020-09-18 22:49:55 +10:00
Menu.setApplicationMenu(Menu.buildFromTemplate(language.template(lang, mainWindow, packaged, dir, dirname)))
2019-04-03 14:59:29 +11:00
//CSS
2019-11-18 03:07:44 +11:00
css.css(mainWindow)
2019-04-03 14:59:29 +11:00
//アップデータとダウンロード
2019-11-18 03:07:44 +11:00
dl.dl(mainWindow, lang_path, base, dirname)
2019-04-03 14:59:29 +11:00
//画像選択と画像処理
2019-11-18 03:07:44 +11:00
img.img(mainWindow, dir)
2019-04-03 14:59:29 +11:00
//NowPlaying
2019-11-18 03:07:44 +11:00
np.TheDeskNowPlaying(mainWindow)
2019-04-03 14:59:29 +11:00
//その他system
2019-11-18 03:07:44 +11:00
systemFunc.system(mainWindow, dir, lang, dirname)
2020-05-10 14:26:07 +10:00
setInterval(function () {
2019-11-18 03:07:44 +11:00
mouseTrack(mainWindow)
}, 1000)
2019-09-22 20:55:59 +10:00
}
2019-11-18 03:07:44 +11:00
var x = 0
var y = 0
var unchanged = 0
var locked = false
2019-09-22 20:55:59 +10:00
function mouseTrack(mainWindow) {
2019-11-18 03:07:44 +11:00
let mousePos = electron.screen.getCursorScreenPoint()
let xNow = mousePos.x
let yNow = mousePos.x
2019-09-22 20:55:59 +10:00
if (x != xNow || y != yNow) {
2019-11-18 03:07:44 +11:00
unchanged = 0
locked = false
2019-09-22 20:55:59 +10:00
} else {
2019-11-18 03:07:44 +11:00
unchanged++
2019-09-22 20:55:59 +10:00
if (unchanged > 60 && !locked) {
2019-11-18 03:07:44 +11:00
unchanged = 0
locked = true
mainWindow.webContents.send('asRead', '')
2019-09-22 20:55:59 +10:00
}
}
2019-11-18 03:07:44 +11:00
x = xNow
y = yNow
2018-01-28 23:22:43 +11:00
}
2019-02-28 04:02:23 +11:00
2019-11-18 03:07:44 +11:00
app.setAsDefaultProtocolClient('thedesk')