forked from emilioastarita/lyricfier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lyricfier.ts
176 lines (154 loc) · 5.04 KB
/
Lyricfier.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import electron = require('electron');
import storage = require('electron-json-storage');
import BrowserWindow = Electron.BrowserWindow;
const platform = require('os').platform();
const path = require('path');
interface Settings {
alwaysOnTop: boolean;
}
export class Lyricfier {
protected rootDir = '';
protected window: Electron.BrowserWindow;
protected app: Electron.App;
protected appIcon: Electron.Tray;
protected settings: Settings = {
alwaysOnTop: false
};
constructor(app, root) {
this.app = app;
this.rootDir = root;
storage.get('settings', (err, savedSettings: Settings) => {
if (err) savedSettings = <Settings>{};
if (('alwaysOnTop' in savedSettings) === false) {
savedSettings['alwaysOnTop'] = this.settings.alwaysOnTop;
}
this.settings = savedSettings;
this.alwaysOnTopSetup();
});
this.setupEvents();
}
getTrayIcon() {
let trayImage = this.getImg('icon.png');
// Determine appropriate icon for platform
if (platform == 'darwin') {
trayImage = this.getImg('tray-icon-mac.png');
}
else if (platform == 'win32') {
trayImage = this.getImg('tray-icon-win.ico');
}
return trayImage;
}
createWindow() {
let options = {
width: 500,
height: 600,
icon: this.getTrayIcon(),
frame: false,
show: false
};
this.window = new electron.BrowserWindow(options);
this.window.on('close', (e) => {
e.preventDefault();
this.window.hide();
});
this.window.loadURL(this.getView('index'));
this.window.once('ready-to-show', () => {
this.window.show();
})
return this.window;
}
getWindow(): Electron.BrowserWindow {
return this.window;
}
createAppIcon() {
const iconPath = this.getTrayIcon();
this.appIcon = new electron.Tray(iconPath);
this.appIcon.setContextMenu(this.createTrayMenu());
}
setupEvents() {
this.app.on('ready', () => {
this.createAppIcon();
this.createWindow();
});
electron.ipcMain.on('get-settings', (event) => {
event.sender.send('settings-update', this.settings);
});
electron.ipcMain.on('save-settings', (event, newSettings) => {
let oldSettings = {};
for (let attr in newSettings) {
oldSettings[attr] = this.settings[attr];
}
if (JSON.stringify(newSettings) === JSON.stringify(oldSettings)) {
console.log('no modifications')
} else {
console.log('modified settings!')
for (let attr in newSettings) {
this.settings[attr] = newSettings[attr];
}
storage.set('settings', this.settings, (err) => {
if (err) console.log('Err saving settings', err);
});
}
event.sender.send('settings-update', this.settings);
});
}
getImg(name) {
return path.join(this.rootDir, 'render', 'img', name );
}
getView(name) {
return `file://${this.rootDir}/render/views/${name}.html`;
}
alwaysOnTopSetup() {
this.getWindow().setAlwaysOnTop(this.settings.alwaysOnTop)
this.appIcon.setContextMenu(this.createTrayMenu());
}
alwaysOnTopToggle() {
this.settings.alwaysOnTop = !this.settings.alwaysOnTop;
storage.set('settings', this.settings, (err) => {
if (err) console.log('Err saving settings', err);
});
this.alwaysOnTopSetup();
}
createTrayMenu() {
const alwaysOnTopChecked = this.settings.alwaysOnTop ? '✓' : '';
const menu = [
['Lyrics', 'showLyrics'],
['Always on top ' + alwaysOnTopChecked, 'alwaysOnTopToggle'],
['Open Developer Tools', 'openDeveloperTools'],
['Quit', 'quit']
];
const template = menu.map((item) => {
let [label, fn] = item;
return {
label: label,
click: this[fn].bind(this)
};
});
return electron.Menu.buildFromTemplate(template);
}
quit() {
process.exit(0);
}
showSettings() {
this.getOpenWindow().webContents.send('change-view', 'Settings');
}
showLyrics() {
this.getOpenWindow().webContents.send('change-view', 'SongLyrics');
}
getOpenWindow() {
if (!this.window.isVisible()) {
this.window.show();
}
if (this.window.isMinimized()) {
this.window.restore();
}
if (!this.window.isFocused()) {
this.window.focus();
}
return this.window;
}
openDeveloperTools() {
this.getOpenWindow().webContents.send('live-reload', true);
return this.getOpenWindow().webContents.openDevTools();
}
}