import * as mainWindow from './windows/main' ; import { app , ipcMain } from 'electron' ; import DeltaChatController from './deltachat/controller' ; import { set_has_unread } from './tray' ; export default function setupUnreadBadge ( dc ) { if ( process . platform !== 'linux' && process . platform !== 'darwin' ) return ; let reUpdateTimeOut ; async function update ( ) { const count = await dc . callMethod ( null , 'chatList.getGeneralFreshMessageCounter' ) ; app . setBadgeCount ( count ) ; set_has_unread ( count !== 0 ) ; } dc . on ( 'DC_EVENT_INCOMING_MSG' , ( _accountId , _chatId , _msgId ) => { if ( mainWindow . window . hidden ) update ( ) ; if ( reUpdateTimeOut ) clearTimeout ( reUpdateTimeOut ) ; reUpdateTimeOut = setTimeout ( ( ) => { update ( ) ; } , 4000 ) ; } ) ; dc . on ( 'ready' , ( ) => { update ( ) ; } ) ; ipcMain . on ( 'update-badge' , ( ) => { if ( reUpdateTimeOut ) clearTimeout ( reUpdateTimeOut ) ; reUpdateTimeOut = setTimeout ( ( ) => update ( ) , 200 ) ; } ) ; }