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.
414 lines
14 KiB
414 lines
14 KiB
const {BrowserWindow, desktopCapturer} = require('electron');
|
|
// const {net} = require('electron');
|
|
const electron = require('electron');
|
|
const ipc = require('electron').ipcMain;
|
|
// const screen = require('electron').screen;
|
|
const store = require('./windows/lib/store');
|
|
const {create: createMainWindow} = require('./windows/main')
|
|
// const { create: createLoginWindow } = require('./windows/login')
|
|
// const { create: createAppWindow } = require('./windows/app')
|
|
const childProcess = require('./child_process')
|
|
const {create: openWindow} = require('./windows/application')
|
|
// const querystring = require("querystring");
|
|
const {MAIN_WINDOW_FLAG, SUSPENSION_WINDOW_FLAG} = require('./constant');
|
|
const { ipcRenderer } = require('electron/renderer');
|
|
// const installApp = require('./ipc/installApp');
|
|
// const getApps = require('./ipc/getApps');
|
|
// const removeApp = require('./ipc/removeApp');
|
|
|
|
// connect redis
|
|
// const {createClient} = require('redis')
|
|
// ;(async () => {
|
|
// /**
|
|
// * createClient({
|
|
// * url: 'redis://alice:foobared@awesome.redis.server:6380'
|
|
// * });
|
|
// */
|
|
// const client = createClient(
|
|
// // {
|
|
// // url: "redis://localhost:3807"
|
|
// // }
|
|
// );
|
|
// // client.on('error', (err) => console.log('Redis Client Error', err));
|
|
// await client.connect()
|
|
|
|
// // 订阅者
|
|
// const subscriber = client.duplicate();
|
|
// await subscriber.connect();
|
|
|
|
// // 发布者
|
|
// const publisher = client.duplicate()
|
|
// await publisher.connect()
|
|
|
|
// // 通过main向外发布消息
|
|
// ipc.on('publishMessage', (events, {channel, ...message}) => {
|
|
// publisher.publish(channel, JSON.stringify(message))
|
|
// })
|
|
|
|
// // 订阅外部消息
|
|
// subscriber.subscribe('openMap', message => {
|
|
// const windows = BrowserWindow.getAllWindows()
|
|
// const found = windows.find((window) => {
|
|
// return window[MAIN_WINDOW_FLAG]
|
|
// });
|
|
// found.webContents.send('openMap', JSON.parse(message))
|
|
// })
|
|
|
|
// // 订阅外部消息(正则)
|
|
// subscriber.pSubscribe('openMap*', (message, channel) => {
|
|
// const windows = BrowserWindow.getAllWindows()
|
|
// const found = windows.find((window) => {
|
|
// return window[MAIN_WINDOW_FLAG]
|
|
// });
|
|
// found.webContents.send(channel, JSON.parse(message))
|
|
// })
|
|
// })();
|
|
|
|
module.exports = () => {
|
|
let winStartPostion = {x: 0, y: 0};
|
|
let mouseStartPosition = {x: 0, y: 0};
|
|
let movingInterVal = null;
|
|
ipc.on('getUnReadMessage', (events, callback) => {
|
|
const msg = store.get('unReadMessage');
|
|
// console.log('msg', msg);
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
found.webContents.send("getUnReadMessage", [msg])
|
|
});
|
|
ipc.on('setUnReadMessage', (events, message) => {
|
|
// console.log('set', message);
|
|
store.set('unReadMessage', message);
|
|
// 消息需要实时同步
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const win = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
if (win) {
|
|
// let winWidth = win.getSize()[0];
|
|
let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
|
|
const x = win.getPosition()[0];
|
|
const y = win.getPosition()[1];
|
|
let sWidth = areaSize.width;
|
|
// console.log(sWidth - x);
|
|
if (sWidth - x === 130) {
|
|
win.setSize(410, 130, true);
|
|
win.setPosition(sWidth - 660, y, true);
|
|
}
|
|
win.webContents.send("getUnReadMessage", [message])
|
|
}
|
|
|
|
});
|
|
ipc.on("showSuspensionWindow", () => {
|
|
let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const win = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
console.log('恢复原始大小')
|
|
win.setSize(410, 130, true);
|
|
const y = win.getPosition()[1];
|
|
win.setPosition(areaSize.width - win.getSize()[0], y, true);
|
|
win.webContents.send("winResize", [win.getSize()])
|
|
|
|
})
|
|
ipc.on('resizeWindow', () => {
|
|
let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const win = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
const startWidth = win.getSize()[0];
|
|
if (startWidth < 410) {
|
|
const x = win.getPosition()[0];
|
|
const y = win.getPosition()[1];
|
|
win.setSize(410, 130, true);
|
|
if (x > areaSize.width - win.getSize()[0]) {
|
|
win.setPosition(areaSize.width - win.getSize()[0] - 50, y, true);
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
ipc.on('windowMoveHandle', (events, canMoving) => {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const win = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
const screen = require('electron').screen;
|
|
/**
|
|
* 窗口移动事件
|
|
*/
|
|
if (!win) return;
|
|
if (canMoving) {
|
|
//读取原位置
|
|
const winPosition = win.getPosition();
|
|
winStartPostion = {x: winPosition[0], y: winPosition[1]};
|
|
mouseStartPosition = screen.getCursorScreenPoint();
|
|
//清除
|
|
if (movingInterVal) {
|
|
clearInterval(movingInterVal);
|
|
}
|
|
//新开
|
|
movingInterVal = setInterval(() => {
|
|
if(!win){
|
|
clearInterval(movingInterVal);
|
|
return;
|
|
}
|
|
const cursorPosition = screen.getCursorScreenPoint();
|
|
const x = winStartPostion.x + cursorPosition.x - mouseStartPosition.x;
|
|
const y = winStartPostion.y + cursorPosition.y - mouseStartPosition.y;
|
|
win.setPosition(x, y, true)
|
|
}, 20)
|
|
} else {
|
|
clearInterval(movingInterVal);
|
|
let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
|
|
const x = win.getPosition()[0];
|
|
const y = win.getPosition()[1];
|
|
store.set("position", {x, y});
|
|
let sWidth = areaSize.width;
|
|
let winWidth = win.getSize()[0];
|
|
// console.log(x + winWidth >= sWidth, sWidth - x - winWidth);
|
|
// 靠边吸附效果
|
|
console.log("0000000", x + winWidth >= sWidth, sWidth - x - winWidth <= 20)
|
|
if (x + winWidth >= sWidth || sWidth - x - winWidth <= 20) {
|
|
win.setResizable(true);
|
|
win.setSize(130, 130, true)
|
|
|
|
win.setPosition(sWidth - 130, y, true);
|
|
win.setResizable(false);
|
|
console.log("靠边吸附", win.getSize(), win.getPosition())
|
|
//通知渲染进程大小发生改变
|
|
} else {
|
|
win.setSize(410, 130, true)
|
|
}
|
|
win.webContents.send("winResize", [win.getSize()])
|
|
movingInterVal = null;
|
|
}
|
|
});
|
|
ipc.on('showMainWindow', (event, arg) => {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
if (found) {
|
|
found.restore();
|
|
found.show();
|
|
} else {
|
|
createMainWindow()
|
|
}
|
|
return true
|
|
});
|
|
ipc.on('hideMainWindow', () => {
|
|
console.log('最小化hideMainWindow');
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
if (found) {
|
|
found.hide();
|
|
}
|
|
return true
|
|
});
|
|
ipc.on('minimize', () => {
|
|
// const windows = BrowserWindow.getAllWindows()
|
|
// const found = windows.find((window) => {
|
|
// return window[MAIN_WINDOW_FLAG]
|
|
// })
|
|
// if (found) {
|
|
// found.minimize();
|
|
// }
|
|
// return true
|
|
try {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
for (let win of windows) {
|
|
win && win.minimize()
|
|
}
|
|
}catch (e) {}
|
|
});
|
|
|
|
ipc.on('hideSuspensionWindow', (event, arg) => {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
if (found) {
|
|
found.close()
|
|
}
|
|
return true
|
|
});
|
|
|
|
ipc.on('openDevTools', (event, arg) => {
|
|
event.sender.webContents.openDevTools()
|
|
return true
|
|
});
|
|
|
|
ipc.on('exitSystem', () => {
|
|
console.log('global',global.worker);
|
|
try {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
|
|
for (let win of windows) {
|
|
win && win.close()
|
|
}
|
|
global.worker === 'stop'
|
|
store.delete("unReadMessage");
|
|
}catch (e) {
|
|
|
|
}
|
|
});
|
|
// ipc.handle('openWidget', (event, widget = {}) => {
|
|
// createAppWindow(widget)
|
|
// return true
|
|
// });
|
|
//
|
|
// ipc.handle('openDevTools', (event, arg) => {
|
|
// event.sender.webContents.openDevTools()
|
|
// return true
|
|
// });
|
|
|
|
// ipc.handle('installApp', installApp);
|
|
//
|
|
// ipc.handle('getApps', getApps);
|
|
//
|
|
// ipc.handle('removeApp', removeApp);
|
|
|
|
/* 示例:在网页中调用ipcRenderer模块
|
|
ipcRenderer.send('spawn', {
|
|
command: '微信.exe',
|
|
args: null || ['--disable-cache'],
|
|
key: + new Date // 唯一key,用来做关闭识别,
|
|
})
|
|
*/
|
|
ipc.on('spawn', (event, arg) => {
|
|
const {pid, promise} = childProcess.start(arg.command, arg.args, arg.key)
|
|
event.sender.send('spawn-success', {
|
|
key: arg.key,
|
|
pid
|
|
})
|
|
promise.then(data => {
|
|
event.sender.send('spawn-success', {
|
|
key: arg.key,
|
|
data
|
|
})
|
|
})
|
|
promise.catch(err => {
|
|
event.sender.send('spawn-error', {
|
|
key: arg.key,
|
|
err
|
|
})
|
|
})
|
|
})
|
|
|
|
// ipcRenderer.send('kill-process', [pid])
|
|
ipc.on('kill-process', function (event, arg) {
|
|
childProcess.kill(arg[0])
|
|
})
|
|
|
|
// 通信桥梁
|
|
ipc.on('bridge', function (event, {channel, targetId, ...message}) {
|
|
console.log('bridge message', channel, message)
|
|
const windows = BrowserWindow.getAllWindows()
|
|
// const found = windows.find((window) => {
|
|
// return window[SUSPENSION_WINDOW_FLAG]
|
|
// });
|
|
for (let win of windows) {
|
|
if (!targetId || win[targetId]) {
|
|
win.webContents.send(channel, message);
|
|
}
|
|
}
|
|
|
|
})
|
|
const windowMap = new Map();
|
|
// 通过main打开新窗口
|
|
ipc.on('openWindow', function (event, arg) {
|
|
//如果存在第二块屏幕,在第二块屏幕打开
|
|
let displays = electron.screen.getAllDisplays();
|
|
let externalDisplay = displays.find((display)=>{
|
|
return display.bounds.x !== 0 || display.bounds.y !== 0
|
|
});
|
|
let options = {};
|
|
if(externalDisplay){
|
|
options = {
|
|
fullscreen:true,
|
|
x:externalDisplay.bounds.x,
|
|
y:externalDisplay.bounds.y,
|
|
};
|
|
|
|
//关闭上一个,同一时间在拓展屏只能打开两个,打开多个或引起卡顿
|
|
if(windowMap.size > 2){
|
|
const win = windowMap.values().next().value;
|
|
win.close();
|
|
windowMap.delete(windowMap.keys().next().value);
|
|
}
|
|
}
|
|
console.log('openWindow request from renderer process', arg);
|
|
const windows = BrowserWindow.getAllWindows();
|
|
const found = windows.find((window) => {
|
|
return window[arg.id]
|
|
});
|
|
if (!arg.id || !arg.entry) {
|
|
return;
|
|
}
|
|
//未打开过,新建窗口
|
|
if(!found){
|
|
const temp = openWindow(arg,options)
|
|
windowMap.set(arg.id,temp);
|
|
}else{//打开且未关闭,切换至前台
|
|
found.focus()
|
|
}
|
|
|
|
|
|
})
|
|
//返回上一个窗口
|
|
ipc.on("backPreviousApp",function (event,arg) {
|
|
const keys = [...windowMap.keys()];
|
|
const curKey = keys.findIndex(v=>v === arg.id);
|
|
if(keys.length > 1){
|
|
let preKey = curKey > 0? curKey - 1 : keys.length - 1;
|
|
const preId = keys[preKey];
|
|
windowMap.get(preId).focus();
|
|
}
|
|
|
|
});
|
|
//根据指定id关闭窗口
|
|
ipc.on('closeWindowById', function (event, arg) {
|
|
const windows = BrowserWindow.getAllWindows();
|
|
const found = windows.find((window) => {
|
|
return window[arg.id]
|
|
});
|
|
found.close();
|
|
windowMap.delete(arg.id);
|
|
})
|
|
// 监听调用截屏事件
|
|
ipc.on('PrtSc', function (event, arg) {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
if (found) {
|
|
found.webContents.send('PrtSc');
|
|
}
|
|
})
|
|
// 监听下载截屏的图片
|
|
ipc.on('downloadImg', function (event, arg) {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
if (found) {
|
|
found.webContents.send('downloadImg');
|
|
}
|
|
})
|
|
// 取消截屏显示
|
|
ipc.on('removeCanvas', function (event, arg) {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const found = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
if (found) {
|
|
found.webContents.send('removeCanvas');
|
|
}
|
|
})
|
|
}
|