-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotice.js
153 lines (151 loc) · 4.55 KB
/
notice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const { sleep } = require("asyncbox");
const { shell, screen, ipcMain } = require("electron");
const { BrowserWindow } = require('glasstron');
const { tween } = require('shifty');
const { EventEmitter } = require('events');
const { execSync } = require("child_process");
const log = require("electron-log");
class Notice extends EventEmitter {
constructor(app) {
super();
this.shown = false;
this.notices = [];
this.id = 0;
this.current = false;
}
async init(app) {
this.app = app;
this.window = new BrowserWindow({
width: 450,
height: 0,
minimizable: false,
maximizable: false,
alwaysOnTop: true,
movable: false,
resizable: false,
show: false,
titleBarStyle: 'hidden',
frame: false,
transparent: true,
backgroundColor: '#0000FFE0',
webPreferences: {
nodeIntegration: true,
experimentalFeatures: true
}
});
this.window.blurType = "acrylic";
this.window.setBlur(true);
this.window.setSkipTaskbar(true);
this.window.menuBarVisible = false;
ipcMain.on('notice', async (event, data) => {
if (data.type === 'resize') {
const size = data.size;
const contains = screen.getPrimaryDisplay().workAreaSize;
this.window.setBounds({ width: size.width, height: size.height, x: contains.width, y: contains.height - size.height - 8 });
this.window.showInactive();
this.window.moveTop();
//animation
await tween({
from: { width: size.width, height: size.height, x: contains.width, y: contains.height - size.height - 8 },
to: { width: size.width, height: size.height, x: contains.width - size.width - 8, y: contains.height - size.height - 8 },
duration: 400,
easing: 'easeInOutQuad',
render: ({ width, height, x, y }) => {
this.window.setBounds({ width, height, x: Math.floor(x), y });
}
});
this.window.webContents.send('notice', { type: 'shown' });
} else if (data.type === 'fixsize') {
const size = data.size;
const contains = screen.getPrimaryDisplay().workAreaSize;
this.window.setBounds({ width: size.width, height: size.height, x: contains.width - size.width - 8, y: contains.height - size.height - 8 });
} else if (data.type === 'activate') {
const url = data.url;
this.window.hide();
this.shown = false;
this.emit('interset', this.current);
try {
log.info('open: ' + url);
switch (process.platform) {
case "darwin":
execSync('open ' + url);
break;
case "win32":
execSync('start "" "' + url + '"');
break;
default:
execSync('xdg-open', [url]);
}
} catch(e) {
this.notices.push(this.current);
}
this.current = false;
this.delaySend();
} else if (data.type === 'close') {
this.window.hide();
this.shown = false;
this.emit('uninterset', this.current);
this.current = false;
this.delaySend();
} else if (data.type === 'read') {
this.window.hide();
this.shown = false;
this.emit('interset', this.current);
this.current = false;
this.delaySend();
}
})
await this.window.loadFile(`windows/notice.html`);
}
send(information) {
this.notices.push(information);
this.delaySend();
}
async delaySend() {
log.info('剩余未读:' + this.notices.length);
await sleep(1500);
this.doSend();
}
clear(matcher) {
this.notices = this.notices.filter(info => info.datetime >= +Date.now() - 86400000 && matcher.match(info));
}
doSend() {
if (!this.shown) {
if (this.notices.length) {
this.shown = true;
this.current = this.notices.shift();
log.info('剩余未读:' + this.notices.length);
this.window.setBounds({ height: 0 });
this.window.webContents.send('notice', {
type: 'message',
data: this.current
})
} else {
this.shown = false;
}
}
}
async pause(force = false) {
if (force) {
if (this.current) {
this.notices.unshift(this.current);
}
this.window.hide();
} else {
while (this.shown) {
await sleep(500);
}
}
this.shown = true;
}
async resume() {
this.shown = false;
this.delaySend();
}
close() {
this.notices = [];
this.window.hide();
this.shown = false;
}
}
module.exports = Notice;