|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- const {
- app,
- BrowserWindow,
- dialog,
- ipcMain,
- shell,
- Tray,
- Menu,
- screen,
- } = require('electron');
- const path = require('path');
- const url = require('url');
- const { initialStorageEvents, storage } = require('./storage');
-
- const gotTheLock = app.requestSingleInstanceLock();
-
- if (!gotTheLock) {
- app.quit();
- return;
- }
-
- let mainWindow;
-
- const isDev = process.env.NODE_ENV === 'development';
-
- function createWindow() {
- //创建窗口
- mainWindow = new BrowserWindow({
- height: 900,
- webPreferences: {
- preload: path.join(__dirname, 'preload.js'),
- webSecurity: false,
- nodeIntegration: true,
- },
- backgroundColor: '#2e2c29',
- darkTheme: true,
- title: 'Locking',
- width: 1700,
- frame: false,
- minWidth: 1300,
- minHeight: 900,
- });
- // 隐藏菜单栏
- mainWindow.setMenuBarVisibility(false);
- if (process.env.NODE_ENV === 'development') {
- // 开发环境
- // 加载页面并打开调试工具,根据 NODE_ENV
- // umijs 在dev时会给出相应的url,直接加载即可
- mainWindow.loadURL('http://localhost:8000/');
- mainWindow.webContents.openDevTools();
- } else {
- //生产环境
- // 加载html文件
- // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
- mainWindow.loadFile(path.join(__dirname, './dist/index.html'));
- // mainWindow.webContents.openDevTools();
- // mainWindow.loadURL(
- // url.format({
- // pathname: path.join(__dirname, '../dist/index.html'),
- // protocol: 'file:',
- // slashes: true,
- // }),
- // );
- }
-
- mainWindow.webContents.on('did-finish-load', () => {
- // 加载初始localStorage数据
- mainWindow.webContents.send('initialStorageData', storage.getAllItem());
- });
- mainWindow.on('closed', () => {
- mainWindow = null;
- });
-
- // 创建系统通知区菜单
- tray = new Tray(path.join(__dirname, 'logo.ico'));
- const contextMenu = Menu.buildFromTemplate([
- {
- label: '最大化',
- click: () => {
- mainWindow.maximize();
- },
- },
- {
- label: '最小化',
- click: () => {
- mainWindow.minimize();
- },
- },
- {
- label: '还原',
- click: () => {
- mainWindow.restore();
- },
- },
- {
- label: '退出',
- click: () => {
- mainWindow.destroy();
- notifyWindow.destroy();
- },
- }, //我们需要在这里有一个真正的退出(这里直接强制退出)
- ]);
- tray.setToolTip('LOCKING盒子');
- tray.setContextMenu(contextMenu);
- tray.on('click', () => {
- //我们这里模拟桌面程序点击通知区图标实现打开关闭应用的功能
- mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
- mainWindow.isVisible()
- ? mainWindow.setSkipTaskbar(false)
- : mainWindow.setSkipTaskbar(true);
- });
- }
-
- // 窗口操作事件
- ipcMain.handle('window:close', (event) => {
- const iWindow = BrowserWindow.fromId(event.sender.id);
- iWindow.hide();
- if (iWindow === mainWindow) {
- iWindow.setSkipTaskbar(true);
- }
- event.preventDefault();
- // BrowserWindow.fromId(event.sender.id)?.close();
- });
- ipcMain.handle('window:zoom', (event) => {
- const iWindow = BrowserWindow.fromId(event.sender.id);
- if (!iWindow) return;
- iWindow.isMaximized() ? iWindow.unmaximize() : iWindow.maximize();
- });
- ipcMain.handle('window:minimize', (event) => {
- BrowserWindow.fromId(event.sender.id)?.minimize();
- });
- ipcMain.handle('window:hide', (event) => {
- // console.log(event.sender.id, event.senderFrame, event);
- BrowserWindow.fromId(event.sender.id)?.hide();
- });
-
- // 选择文件夹
- ipcMain.handle('select-folder', async (event, args) => {
- const res = await dialog.showOpenDialog({
- properties: ['openDirectory'],
- });
- return res;
- });
- // 打开浏览器
- ipcMain.handle('open-browser', (event, url) => {
- shell.openExternal(url);
- });
- // 打开文件所在位置
- ipcMain.handle('open-file-position', (event, message) => {
- shell.showItemInFolder(message.absolutePath);
- });
-
- // 主窗口给消息窗口notifyWindow发送消息
- ipcMain.handle('notify', (event, messageObj) => {
- notifyWindow.show();
- notifyWindow.webContents.send('on-notify', messageObj);
- });
- // 消息窗口给主窗口发送重新同步文件的命令
- ipcMain.handle('re-sync-file', (event, messageObj) => {
- mainWindow.webContents.send('request-resync-file', messageObj);
- });
-
- ipcMain.handle('download-file', (event, messageObj) => {
- mainWindow.webContents.send('request-download-file', messageObj);
- });
-
- // 初始化electron-store相关API
- initialStorageEvents(ipcMain);
-
- app.on('ready', () => {
- const { width: windowWidth, height: windowHeight } =
- screen.getPrimaryDisplay().workAreaSize;
- createWindow();
- createNotifycationWindow(windowWidth, windowHeight);
- app.on('activate', function () {
- // On macOS it's common to re-create a window in the app when the
- // dock icon is clicked and there are no other windows open.
- if (BrowserWindow.getAllWindows().length === 0) createWindow();
- });
- });
-
- app.on('second-instance', (event, commandLine, workingDirectory) => {
- // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
- if (mainWindow) {
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.focus();
- mainWindow.show();
- }
- });
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
-
- let notifyWindow;
- function createNotifycationWindow(windowWidth, windowHeight) {
- if (notifyWindow) return notifyWindow;
-
- //创建窗口
- notifyWindow = new BrowserWindow({
- width: 504,
- height: 219, // 184,
- x: windowWidth - 480 - 20,
- y: windowHeight - 219 + 10,
- webPreferences: {
- preload: path.join(__dirname, 'preload.js'),
- webSecurity: false,
- nodeIntegration: true,
- },
- frame: false,
- resizable: false,
- transparent: true,
- alwaysOnTop: true,
- });
- // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
- if (isDev) {
- notifyWindow.webContents.openDevTools();
- }
- notifyWindow.loadFile(path.join(__dirname, 'notifycation.html'));
- notifyWindow.setSkipTaskbar(true);
- notifyWindow.hide();
- // return notifyWindow;
- }
|