-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
tray-provider.ts
144 lines (134 loc) · 3.38 KB
/
tray-provider.ts
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
/**
* @ignore
* BEGIN HEADER
*
* Contains: TrayProvider
* CVM-Role: Service Provider
* Maintainer: Hendrik Erz
* License: GNU GPL v3
*
* Description: Tray provider of electron's Tray class.
*
* END HEADER
*/
import {
Tray,
Menu,
screen
} from 'electron'
import path from 'path'
import EventEmitter from 'events'
import { trans } from '../../common/i18n'
/**
* This class generates the Tray in the system notification area
*/
export default class TrayProvider extends EventEmitter {
/**
* The tray
*/
private _tray: Tray | null
/**
* Create the instance on program start and setup services.
*/
constructor () {
super()
global.log.verbose('Tray provider booting up ...')
this._tray = null
global.tray = {
/**
* Add a document to the list of recently opened documents
* @param {Object} doc A document exposing at least the metadata of the file
*/
add: (show: () => void, quit: () => void) => {
this._addTray(show, quit)
},
remove: () => {
this._removeTray()
}
}
}
/**
* Shuts down the provider
*
* @return {boolean} Always returns true
*/
shutdown (): boolean {
global.log.verbose('Tray provider shutting down ...')
return true // This provider needs no special shutdown logic
}
/**
* Return a suitable tray icon size
* @private
* @memberof TrayProvider
*/
private _calcTrayIconSize (): number {
let size = 32
const fitSize = (size: number): number => {
const sizeList = [ 32, 48, 64, 96, 128, 256 ]
for (let s of sizeList) {
if (s >= size) {
return s
}
}
return 32
}
const display = screen.getPrimaryDisplay()
size = display.workArea.y
if (size >= 8 && size <= 256) {
size = fitSize(size)
} else {
size = display.size.height - display.workArea.height
size = fitSize(size)
}
return size
}
/**
* Adds the Zettlr tray to the system notification area.
* @private
* @memberof TrayProvider
*/
private _addTray (show: () => void, quit: () => void): void {
if (this._tray == null) {
const platformIcons: { [key: string]: string } = {
'darwin': '/png/22x22_white.png',
'win32': '/icon.ico'
}
if (process.platform === 'linux') {
const size = this._calcTrayIconSize()
this._tray = new Tray(path.join(__dirname, `assets/icons/png/${size}x${size}.png`))
} else {
let iconPath = '/png/32x32.png'
if ([ 'darwin', 'win32' ].includes(process.platform)) {
iconPath = platformIcons[process.platform]
}
this._tray = new Tray(path.join(__dirname, 'assets/icons', iconPath))
}
const contextMenu = Menu.buildFromTemplate([
{
label: trans('tray.show_zettlr'),
click: show,
type: 'normal'
},
{ label: '', type: 'separator' },
{
label: trans('menu.quit'),
click: quit,
type: 'normal'
}
])
this._tray.setToolTip(trans('tray.tooltip'))
this._tray.setContextMenu(contextMenu)
}
}
/**
* Removes the Zettlr tray from the system notification area.
* @private
* @memberof TrayProvider
*/
private _removeTray (): void {
if (this._tray != null) {
this._tray.destroy()
}
this._tray = null
}
}