import { app as rawApp , Menu , Tray , nativeImage , NativeImage } from 'electron' ; import { globalShortcut } from 'electron' ; import * as mainWindow from './windows/main' ; import { ExtendedAppMainProcess } from './types' ; import { getLogger } from '../shared/logger' ; import { join } from 'path' ; let tray = null ; let contextMenu = null ; const app = rawApp as ExtendedAppMainProcess ; const log = getLogger ( 'main/tray' ) ; let has_unread = false ; export function set_has_unread ( new_has_unread ) { has_unread = new_has_unread ; if ( tray ) { tray . setImage ( TrayImage ( ) ) ; } } export function TrayImage ( ) { const trayIconFolder = join ( __dirname , '..' , '..' , 'images/tray' ) ; if ( process . platform === 'darwin' ) { const image = nativeImage . createFromPath ( join ( trayIconFolder , 'tray-icon-mac.png' ) ) . resize ( { width : 24 } ) ; image . setTemplateImage ( true ) ; return image ; } else { const iconFormat = process . platform === 'win32' ? '.ico' : '.png' ; return `${ join ( trayIconFolder , ( has_unread ? 'deltachat-unread' : 'deltachat' ) + iconFormat ) }` ; } } export function mainWindowIsVisible ( ) { if ( process . platform === 'darwin' || process . platform === 'win32' ) { return mainWindow . window . isVisible ( ) ; } return mainWindow . window . isVisible ( ) && mainWindow . window . isFocused ( ) ; } export function closeDeltaChat ( ) { mainWindow . window . close ( ) ; } export function hideDeltaChat ( minimize ? ) { if ( minimize === true ) { mainWindow . window . minimize ( ) ; } mainWindow . window . hide ( ) ; if ( process . platform === 'linux' ) tray . setContextMenu ( getTrayMenu ( ) ) ; } export function showDeltaChat ( ) { mainWindow . window . show ( ) ; } export function hideOrShowDeltaChat ( ) { mainWindowIsVisible ( ) ? hideDeltaChat ( true ) : showDeltaChat ( ) ; } export function quitDeltaChat ( ) { globalShortcut . unregisterAll ( ) ; app . quit ( ) ; } export function updateTrayIcon ( ) { if ( ! app . rc [ 'minimized' ] && app . state . saved . minimizeToTray !== true ) { if ( tray != null ) destroyTrayIcon ( ) ; return ; } renderTrayIcon ( ) ; } export function destroyTrayIcon ( ) { log . info ( 'destroy icon tray' ) ; tray . destroy ( ) ; tray = null ; } export function getTrayMenu ( ) { if ( tray === null ) return ; const tx = app . translate ; if ( process . platform === 'darwin' ) { contextMenu = Menu . buildFromTemplate ( [ mainWindowIsVisible ( ) ? { id : 'reduce_window' , label : tx ( 'hide' ) , type : 'normal' , click ( ) { hideDeltaChat ( ) ; } , } : { id : 'open_windows' , label : tx ( 'activate' ) , type : 'normal' , click ( ) { showDeltaChat ( ) ; } , } , { id : 'quit_app' , label : tx ( 'global_menu_file_quit_desktop' ) , type : 'normal' , click ( ) { quitDeltaChat ( ) ; } , } , ] ) ; } else { contextMenu = Menu . buildFromTemplate ( [ { id : 'open_windows' , label : tx ( 'global_menu_file_open_desktop' ) , type : 'normal' , click ( ) { showDeltaChat ( ) ; } , } , { id : 'reduce_window' , label : tx ( 'global_menu_minimize_to_tray' ) , type : 'normal' , enabled : mainWindowIsVisible ( ) , click ( ) { hideDeltaChat ( ) ; } , } , { id : 'quit_app' , label : tx ( 'global_menu_file_quit_desktop' ) , type : 'normal' , click ( ) { quitDeltaChat ( ) ; } , } , ] ) ; } return contextMenu ; } export function TrayIcon ( ) { return new Tray ( TrayImage ( ) ) ; } export function renderTrayIcon ( ) { if ( tray != null ) { log . warn ( 'Tray icon not destroyed before render?' ) ; destroyTrayIcon ( ) ; } log . info ( 'add icon tray' ) ; tray = TrayIcon ( ) ; tray . setToolTip ( 'Delta Chat' ) ; if ( process . platform === 'darwin' ) { tray . on ( 'click' , ( ) => tray . popUpContextMenu ( getTrayMenu ( ) ) ) ; tray . on ( 'right-click' , ( ) => tray . popUpContextMenu ( getTrayMenu ( ) ) ) ; } else if ( process . platform === 'win32' ) { tray . on ( 'click' , hideOrShowDeltaChat ) ; tray . on ( 'right-click' , ( ) => tray . popUpContextMenu ( getTrayMenu ( ) ) ) ; } else { tray . on ( 'click' , hideOrShowDeltaChat ) ; tray . on ( 'double-click' , hideOrShowDeltaChat ) ; refreshTrayContextMenu ( ) ; } } export function refreshTrayContextMenu ( ) { tray ?. setContextMenu ( getTrayMenu ( ) ) ; }