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.
309 lines
10 KiB
309 lines
10 KiB
const {BrowserWindow} = 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 {MAIN_WINDOW_FLAG, SUSPENSION_WINDOW_FLAG} = require('./constant');
|
|
// 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()
|
|
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(640, 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(640, 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 < 640){
|
|
const x = win.getPosition()[0];
|
|
const y = win.getPosition()[1];
|
|
win.setSize(640, 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 (canMoving) {
|
|
if (!win) return;
|
|
//读取原位置
|
|
const winPosition = win.getPosition();
|
|
winStartPostion = {x: winPosition[0], y: winPosition[1]};
|
|
mouseStartPosition = screen.getCursorScreenPoint();
|
|
//清除
|
|
if (movingInterVal) {
|
|
clearInterval(movingInterVal);
|
|
}
|
|
//新开
|
|
movingInterVal = setInterval(() => {
|
|
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(650,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', () => {
|
|
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
|
|
});
|
|
|
|
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', () => {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
const mainWindow = windows.find((window) => {
|
|
return window[MAIN_WINDOW_FLAG]
|
|
})
|
|
const suspWindow = windows.find((window) => {
|
|
return window[SUSPENSION_WINDOW_FLAG]
|
|
});
|
|
store.delete("unReadMessage");
|
|
mainWindow && mainWindow.close();
|
|
suspWindow && suspWindow.close();
|
|
});
|
|
// 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 }) {
|
|
const windows = BrowserWindow.getAllWindows()
|
|
for (let win of windows) {
|
|
if (win.webContents.id === event.sender.id) continue;
|
|
if (!targetId || win[targetId]) {
|
|
win.webContents.send(channel, message)
|
|
}
|
|
}
|
|
})
|
|
|
|
// 通过main打开新窗口
|
|
ipc.on('openWindow', function (event, arg) {
|
|
console.log('openWindow request from renderer process', arg)
|
|
if (!arg.id || !arg.entry) {
|
|
return;
|
|
}
|
|
openWindow(arg)
|
|
})
|
|
}
|