no message

This commit is contained in:
kuaifan 2021-07-09 18:15:39 +08:00
parent 276654a7e3
commit 4c71366b77
5 changed files with 42 additions and 51 deletions

45
electron/main.js vendored
View File

@ -1,6 +1,6 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const {app, BrowserWindow} = require('electron') const {app, BrowserWindow, ipcMain} = require('electron')
let willQuitApp = false, let willQuitApp = false,
devloadCachePath = path.resolve(__dirname, ".devload"), devloadCachePath = path.resolve(__dirname, ".devload"),
@ -9,13 +9,25 @@ if (fs.existsSync(devloadCachePath)) {
devloadUrl = fs.readFileSync(devloadCachePath, 'utf8') devloadUrl = fs.readFileSync(devloadCachePath, 'utf8')
} }
function getCounterValue(title) { function runNum(str, fixed) {
const itemCountRegex = /[([{]([\d.,]*)\+?[}\])]/; let _s = Number(str);
const match = itemCountRegex.exec(title); if (_s + "" === "NaN") {
return match ? match[1] : undefined; _s = 0;
}
if (/^[0-9]*[1-9][0-9]*$/.test(fixed)) {
_s = _s.toFixed(fixed);
let rs = _s.indexOf('.');
if (rs < 0) {
_s += ".";
for (let i = 0; i < fixed; i++) {
_s += "0";
}
}
}
return _s;
} }
function createWindow(setDockBadge) { function createWindow() {
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
width: 1280, width: 1280,
height: 800, height: 800,
@ -36,15 +48,6 @@ function createWindow(setDockBadge) {
}) })
} }
mainWindow.on('page-title-updated', function (event, title) {
const counterValue = getCounterValue(title);
if (counterValue) {
setDockBadge(counterValue);
} else {
setDockBadge('');
}
})
mainWindow.on('close', function (e) { mainWindow.on('close', function (e) {
if (!willQuitApp) { if (!willQuitApp) {
e.preventDefault(); e.preventDefault();
@ -54,10 +57,10 @@ function createWindow(setDockBadge) {
} }
app.whenReady().then(() => { app.whenReady().then(() => {
createWindow(app.dock.setBadge) createWindow()
app.on('activate', function () { app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow(app.dock.setBadge) if (BrowserWindow.getAllWindows().length === 0) createWindow()
}) })
}) })
@ -68,3 +71,11 @@ app.on('window-all-closed', function () {
app.on('before-quit', () => { app.on('before-quit', () => {
willQuitApp = true willQuitApp = true
}); });
ipcMain.on('setDockBadge', (event, arg) => {
if (runNum(arg) > 0) {
app.dock.setBadge(String(arg))
} else {
app.dock.setBadge("")
}
});

View File

@ -6,7 +6,7 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"start": "electron-forge start", "start": "electron-forge start",
"start-quiet": "electron-forge start &> /dev/null", "start-quiet": "sleep 3 && electron-forge start &> /dev/null",
"build": "electron-builder", "build": "electron-builder",
"build-mac-intel": "electron-builder --mac", "build-mac-intel": "electron-builder --mac",
"build-mac-m1": "electron-builder --mac --arm64", "build-mac-m1": "electron-builder --mac --arm64",

View File

@ -46,7 +46,9 @@ Vue.component('EDropdown', Dropdown);
Vue.component('EDropdownMenu', DropdownMenu); Vue.component('EDropdownMenu', DropdownMenu);
Vue.component('EDropdownItem', DropdownItem); Vue.component('EDropdownItem', DropdownItem);
Vue.prototype.isElectron = false;
if (!!__IS_ELECTRON) { if (!!__IS_ELECTRON) {
Vue.prototype.isElectron = true;
Vue.prototype.$electron = require('electron') Vue.prototype.$electron = require('electron')
} }

View File

@ -26,19 +26,7 @@ export default {
computed: { computed: {
...mapState([ ...mapState(['userId']),
'userId',
'dialogs',
]),
msgAllUnread() {
let num = 0;
this.dialogs.map(({unread}) => {
num += unread;
})
return num;
},
}, },
watch: { watch: {
@ -68,9 +56,6 @@ export default {
}, },
setPageTile(title) { setPageTile(title) {
if (this.userId && this.msgAllUnread > 0) {
title+= " (" + this.msgAllUnread + ")"
}
document.title = title; document.title = title;
} }
} }

View File

@ -187,8 +187,6 @@ export default {
allProjectShow: false, allProjectShow: false,
archivedProjectShow: false, archivedProjectShow: false,
titleInterval: null,
natificationHidden: false, natificationHidden: false,
natificationReady: false, natificationReady: false,
notificationClass: null, notificationClass: null,
@ -199,14 +197,16 @@ export default {
this.$store.dispatch("getUserInfo"); this.$store.dispatch("getUserInfo");
this.$store.dispatch("getTaskPriority"); this.$store.dispatch("getTaskPriority");
// //
this.startCountTitle();
this.notificationInit(); this.notificationInit();
this.onVisibilityChange(); this.onVisibilityChange();
//
if (this.isElectron) {
this.$electron.ipcRenderer.send('setDockBadge', 0);
}
}, },
deactivated() { deactivated() {
this.addShow = false; this.addShow = false;
clearInterval(this.titleInterval);
}, },
computed: { computed: {
@ -280,6 +280,12 @@ export default {
id > 0 && this.$Modal.resetIndex(); id > 0 && this.$Modal.resetIndex();
}, },
msgAllUnread(val) {
if (this.isElectron) {
this.$electron.ipcRenderer.send('setDockBadge', val);
}
},
dialogMsgPush(data) { dialogMsgPush(data) {
if (this.natificationHidden && this.natificationReady) { if (this.natificationHidden && this.natificationReady) {
const {id, dialog_id, type, msg} = data; const {id, dialog_id, type, msg} = data;
@ -444,19 +450,6 @@ export default {
} }
}, },
startCountTitle() {
this.titleInterval = setInterval(() => {
let {title} = document;
let newTitle = title.replace(/^(.*?)\((\d+)\)$/g, "$1")
if (this.userId && this.msgAllUnread > 0) {
newTitle+= " (" + this.msgAllUnread + ")"
}
if (title != newTitle) {
document.title = newTitle;
}
}, 1000)
},
notificationInit() { notificationInit() {
this.notificationClass = new notificationKoro(this.$L("打开通知成功")); this.notificationClass = new notificationKoro(this.$L("打开通知成功"));
if (this.notificationClass.support) { if (this.notificationClass.support) {