You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

189 lines
5.1 KiB

// const path = require('path')
import { BrowserWindow } from "electron"
import Shared from "./share"
import { getFileUrl } from "./util"
const electron = require("electron")
const setTray = require("./disk").default
// const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const app = electron.app
const dialog = electron.dialog
const ipcMain = electron.ipcMain
let newwin: BrowserWindow | null
let template = [
{
label: "选择保存目录",
click: function (item: any, focusedWindow: BrowserWindow) {
dialog
.showOpenDialog(focusedWindow, {
properties: ["openDirectory"],
})
.then(result => {
ipcMain.emit("@menu:selectDir", result)
})
.catch(err => {
throw err
})
},
},
{
label: "置顶",
key: "alwaysTop",
click: function (item: any, focusedWindow: BrowserWindow) {
if (Shared.data.mainWindow.isAlwaysOnTop()) {
Shared.data.mainWindow.setAlwaysOnTop(false)
} else {
Shared.data.mainWindow.setAlwaysOnTop(true)
}
},
},
{
label: "重载",
accelerator: "CmdOrCtrl+R",
click: function (item: any, focusedWindow: BrowserWindow) {
if (focusedWindow) {
// 重载之后, 刷新并关闭所有的次要窗体
if (focusedWindow.id === 1) {
BrowserWindow.getAllWindows().forEach(function (win) {
if (win.id > 1) {
win.close()
}
})
}
focusedWindow.reload()
}
},
},
{
label: "功能",
submenu: [
{
label: "悬浮窗",
click: function (item: any, focusedWindow: BrowserWindow) {
ipcMain.emit("showSuspensionWindow")
},
},
{
label: "最小化到托盘",
click: function (item: any, focusedWindow: BrowserWindow) {
Shared.data.lastChoice = 1
if (Shared.data.miniWindow) {
Shared.data.mainWindow.hide() // 调用 最小化实例方法
} else {
setTray(app, Shared.data.mainWindow)
}
},
},
{
label: "切换全屏",
accelerator: (function () {
if (process.platform === "darwin") {
return "Ctrl+Command+F"
} else {
return "F11"
}
})(),
click: function (item: any, focusedWindow: BrowserWindow) {
if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
}
},
},
],
},
{
label: "开发者",
submenu: [
{
label: "切换开发者工具",
accelerator: (function () {
if (process.platform === "darwin") {
return "Alt+Command+I"
} else {
return "Ctrl+Shift+I"
}
})(),
click: function (item: any, focusedWindow: BrowserWindow) {
if (focusedWindow) {
// @ts-ignore
focusedWindow.toggleDevTools()
}
},
},
],
},
// {
// label: '重新启动',
// click: function(item, focusedWindow) {
// app.exit()
// app.relaunch()
// // app.relaunch({ args: process.argv.slice(1).concat(['--relaunch']) })
// // app.quit()
// }
// },
{
label: "关于",
click: function (item: any, focusedWindow: BrowserWindow) {
// https://www.electronjs.org/docs/api/browser-window#winsetmenubarvisibilityvisible-windows-linux
if (focusedWindow && !newwin) {
newwin = new BrowserWindow({
width: 600,
height: 200,
minimizable: false,
darkTheme: true,
modal: true,
show: false,
resizable: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
// parent: focusedWindow // win是主窗口
})
// 隐藏菜单
newwin.setMenuBarVisibility(false)
// vue是单页面,需要改成多页面才行
newwin.loadURL(getFileUrl("about"))
newwin.on("ready-to-show", () => {
newwin?.show()
})
newwin.on("close", () => {
newwin = null
})
}
},
},
]
// function findTopItem() {
// const menu = Menu.getApplicationMenu()
// if (!menu) return
// let reopenMenuItem
// menu.items.forEach(function(item) {
// if (item.key === 'alwaysTop') {
// reopenMenuItem = item
// }
// // if (item.submenu) {
// // item.submenu.items.forEach(function(item) {
// // if (item.key === 'alwaysTop') {
// // reopenMenuItem = item
// // }
// // })
// // }
// })
// console.log(reopenMenuItem)
// return reopenMenuItem
// }
app.on("ready", function () {
const menu = Menu.buildFromTemplate(<any>template)
Menu.setApplicationMenu(menu)
})
app.on("browser-window-created", function () {
// let reopenMenuItem = findReopenMenuItem()
// if (reopenMenuItem) reopenMenuItem.enabled = false
})
app.on("window-all-closed", function () {
// let reopenMenuItem = findReopenMenuItem()
// if (reopenMenuItem) reopenMenuItem.enabled = true
})