diff --git a/src/main/services/account-manager.ts b/src/main/services/account-manager.ts index c7509a1..0878702 100644 --- a/src/main/services/account-manager.ts +++ b/src/main/services/account-manager.ts @@ -1,5 +1,4 @@ -import { BrowserWindow, Notification, nativeImage, app } from 'electron' -import { join } from 'node:path' +import { BrowserWindow } from 'electron' import type { AccountConfig, ImapCredentials } from '@shared/types/account' import { AccountType } from '@shared/types/account' import type { IEmailProvider, FolderListResult, MessageListResult } from '@shared/types/provider' @@ -392,31 +391,6 @@ export class AccountManager { if (this.mainWindow && !this.mainWindow.isDestroyed()) { this.mainWindow.webContents.send('mail:new', { accountId, folderId, envelope }) } - - if (Notification.isSupported()) { - const from = envelope.from.name || envelope.from.email || 'Новое письмо' - const subject = envelope.subject || '(без темы)' - - const iconPath = app.isPackaged - ? join(process.resourcesPath, 'icon.png') - : join(app.getAppPath(), 'resources/icon.png') - const icon = nativeImage.createFromPath(iconPath) - - const n = new Notification({ - title: from, - body: subject, - icon: icon.isEmpty() ? undefined : icon, - silent: false, - }) - n.on('click', () => { - if (this.mainWindow && !this.mainWindow.isDestroyed()) { - this.mainWindow.show() - this.mainWindow.focus() - this.mainWindow.webContents.send('notification:click', { accountId, folderId, messageId: envelope.id }) - } - }) - n.show() - } }) } } diff --git a/src/renderer/src/components/notifications/NotificationContainer.vue b/src/renderer/src/components/notifications/NotificationContainer.vue index 3756192..831ddd0 100644 --- a/src/renderer/src/components/notifications/NotificationContainer.vue +++ b/src/renderer/src/components/notifications/NotificationContainer.vue @@ -47,20 +47,42 @@ function handleClick(id: string): void { removeToast(id) } +function showOsNotification(from: string, subject: string, onClick: () => void): void { + if (!('Notification' in window)) return + const send = (): void => { + const n = new window.Notification(from, { body: subject, silent: false }) + n.onclick = () => { onClick(); n.close() } + } + if (Notification.permission === 'granted') { + send() + } else if (Notification.permission !== 'denied') { + Notification.requestPermission().then(p => { if (p === 'granted') send() }) + } +} + onMounted(() => { + // Запрашиваем разрешение заранее, чтобы не задерживать первое уведомление + if ('Notification' in window && Notification.permission === 'default') { + Notification.requestPermission() + } + on('mail:new', ({ accountId, folderId, envelope }) => { const account = accountsStore.accounts.find(a => a.id === accountId) if (!account) return if (notificationsStore.isMuted(accountId, folderId)) return - const from = envelope.from.name || envelope.from.email - const subject = envelope.subject || '(без темы)' - addToast({ from, subject, accountEmail: account.email, accountId, folderId, messageId: envelope.id }) - }) - on('notification:click', ({ accountId, folderId, messageId }) => { - accountsStore.setActive(accountId) - foldersStore.setActiveFolder(folderId) - messagesStore.selectMessage(messageId) + const from = envelope.from.name || envelope.from.email || 'Новое письмо' + const subject = envelope.subject || '(без темы)' + + // Системное уведомление — показываем всегда; клик фокусирует окно и открывает письмо + showOsNotification(from, subject, () => { + accountsStore.setActive(accountId) + foldersStore.setActiveFolder(folderId) + messagesStore.selectMessage(envelope.id) + }) + + // In-app тост — дополнительная индикация внутри приложения + addToast({ from, subject, accountEmail: account.email, accountId, folderId, messageId: envelope.id }) }) }) diff --git a/src/shared/types/ipc.ts b/src/shared/types/ipc.ts index 7c84ff2..09a6ad4 100644 --- a/src/shared/types/ipc.ts +++ b/src/shared/types/ipc.ts @@ -64,7 +64,6 @@ export interface IpcEventMap { 'mail:new': { accountId: string; folderId: string; envelope: MessageEnvelope } 'account:status': { accountId: string; connected: boolean; error?: string } 'attachment:progress': { attachmentId: string; stage: 'downloading' | 'saving' | 'done' | 'error'; progress: number; error?: string } - 'notification:click': { accountId: string; folderId: string; messageId: string } } export type IpcChannel = keyof IpcChannelMap