Skip to content

Commit

Permalink
Merge branch 'dev-v1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmader committed Dec 9, 2018
2 parents c192eaf + 4c3a690 commit e0a9f5a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ const { app, BrowserWindow, Menu, ipcMain } = require("electron")

const edit_conf = require("./edit_conf.js")
const { buildMenu } = require("./menu.js")
const { displayTray } = require("./tray.js")

let mainWindow = null

const icon = path.join(__dirname, "assets", "AriaNg.png")

app.commandLine.appendSwitch("ignore-certificate-errors") // 忽略证书相关错误, 适用于使用自签名证书将Aria2的RPC配置成HTTPS协议的情况

app.on("window-all-closed", function () {
Expand All @@ -31,7 +34,7 @@ app.on("ready", function () {
height: 600,
minWidth: 400,
minHeight: 400,
icon: path.join(__dirname, "assets", "AriaNg.png"),
icon,
show: false,
webPreferences: {
nodeIntegration: false,
Expand Down Expand Up @@ -94,6 +97,12 @@ app.on("ready", function () {
mainWindow.show()
})

mainWindow.on("close", function (e) {
e.preventDefault()
mainWindow.hide()
displayTray(icon)
})

mainWindow.on("closed", function () {
killAria2()
mainWindow = null
Expand Down
24 changes: 22 additions & 2 deletions app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const process = require("process")
const { Menu } = require("electron")
const Translate = require("./translate.js")
const { destroyTray, destroyMainWindow } = require("./tray.js")

const isDev = process.argv.pop() == "dev"

Expand Down Expand Up @@ -44,9 +45,26 @@ const appMenuTemplate = [
}
]

const trayMenuTemplate = [
// {
// label: "打开下载文件夹",
// },
{
label: "显示窗口",
click() {
destroyTray()
}
},
{
label: "退出",
click() {
destroyMainWindow()
}
}
]

const buildMenu = (locale) => {

const _buildMenuFromTemplate = (menuTemplate) => {
if (locale != "zh-CN") {
menuTemplate = Translate(menuTemplate, locale)
Expand All @@ -56,10 +74,12 @@ const buildMenu = (locale) => {

const contextMenu = _buildMenuFromTemplate(contextMenuTemplate)
const appMenu = _buildMenuFromTemplate(appMenuTemplate)
const trayMenu = _buildMenuFromTemplate(trayMenuTemplate)

return {
contextMenu,
appMenu
appMenu,
trayMenu
}
}

Expand Down
1 change: 1 addition & 0 deletions app/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const translations = {
"切换开发者工具": "Toggle Developer Tools",
"退出": "Quit",
"编辑": "Edit",
"显示窗口": "Display Window",
}

const Translate = (menuTemplate, locale = "en-US") => {
Expand Down
65 changes: 65 additions & 0 deletions app/tray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*!
* AriaNg GUI
*
* Copyright (c) 2018 Xmader
* Released under the MIT license
*
* Source Code: https://github.com/Xmader/aria-ng-gui
*
*/

const { app, Tray, BrowserWindow } = require("electron")
const { dialog } = require("electron")

let tray = null
let trayMenu = null

const getTrayMenu = () => {
const locale = app.getLocale().includes("zh") ? "zh-CN" : "en-US"
const { trayMenu: _trayMenu } = require("./menu.js").buildMenu(locale)
trayMenu = _trayMenu
return _trayMenu
}

const displayTray = (icon) => {
tray = new Tray(icon)
tray.setToolTip("AriaNg GUI v" + app.getVersion())
tray.setContextMenu(trayMenu || getTrayMenu())

const title = "AriaNg GUI 已最小化到托盘"
const content = "可以右键单击托盘图标完全退出"
if (process.platform == "win32") {
tray.displayBalloon({
icon,
title,
content
})
} else {
dialog.showMessageBox({
type: "info",
icon,
title: "AriaNg GUI",
message: title,
detail: content
})
}
}

const destroyTray = () => {
const mainWindow = BrowserWindow.getAllWindows()[0]

tray.destroy()
mainWindow.show()
}

const destroyMainWindow = () => {
const mainWindow = BrowserWindow.getAllWindows()[0]

mainWindow.destroy()
}

module.exports = {
displayTray,
destroyTray,
destroyMainWindow
}

0 comments on commit e0a9f5a

Please sign in to comment.