-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (145 loc) · 4.64 KB
/
Copy pathmain.js
File metadata and controls
167 lines (145 loc) · 4.64 KB
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
const { app, BrowserWindow, ipcMain, globalShortcut } = require('electron');
const path = require('path');
const fs = require('fs');
const CONFIG_PATH = path.join(app.getPath('userData'), 'config.json');
const DEFAULT_CONFIG = {
// Appearance
fontSize: 34,
lineHeight: 1.7,
opacity: 0.85,
textColor: '#ffffff',
bgColor: '#000000',
visibleLines: 3,
fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", "Noto Sans CJK SC", "Noto Sans JP", "Noto Sans KR", "Apple Color Emoji", "Segoe UI Emoji", sans-serif',
mirror: false,
language: 'en',
direction: 'auto',
// Behavior
scrollSpeed: 40,
contentProtection: true,
arrowControl: true,
// Window position
bounds: null,
// Script
script: '',
pageDelimiter: '\\n\\n'
};
function loadConfig() {
try {
const raw = fs.readFileSync(CONFIG_PATH, 'utf-8');
return Object.assign({}, DEFAULT_CONFIG, JSON.parse(raw));
} catch {
return Object.assign({}, DEFAULT_CONFIG);
}
}
function saveConfig(cfg) {
try {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2), 'utf-8');
} catch (e) {
console.error('Failed to save config', e);
}
}
let config = loadConfig();
let prompterWin = null;
let controlWin = null;
let locked = false; // Locked mode: mouse clicks pass through the prompter.
function createPrompter() {
const b = config.bounds;
prompterWin = new BrowserWindow({
width: b ? b.width : 720,
height: b ? b.height : 240,
x: b ? b.x : undefined,
y: b ? b.y : undefined,
frame: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
resizable: true,
focusable: false,
hasShadow: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
prompterWin.setAlwaysOnTop(true, 'screen-saver');
prompterWin.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
applyContentProtection();
prompterWin.loadFile('prompter.html');
prompterWin.on('moved', persistBounds);
prompterWin.on('resized', persistBounds);
}
function persistBounds() {
if (!prompterWin) return;
config.bounds = prompterWin.getBounds();
saveConfig(config);
}
function applyContentProtection() {
if (prompterWin) prompterWin.setContentProtection(!!config.contentProtection);
}
function createControl() {
controlWin = new BrowserWindow({
width: 420,
height: 680,
title: 'Presentation Prompter Controls',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
controlWin.loadFile('control.html');
controlWin.on('closed', () => { controlWin = null; });
}
function sendToPrompter(channel, payload) {
if (prompterWin && !prompterWin.isDestroyed()) {
prompterWin.webContents.send(channel, payload);
}
}
function toggleLock() {
locked = !locked;
if (prompterWin) {
prompterWin.setIgnoreMouseEvents(locked, { forward: true });
}
sendToPrompter('lock-changed', locked);
}
function registerShortcuts() {
globalShortcut.unregisterAll();
const M = 'CommandOrControl';
if (config.arrowControl) {
globalShortcut.register('Down', () => sendToPrompter('cmd', 'nextLine'));
globalShortcut.register('Up', () => sendToPrompter('cmd', 'prevLine'));
}
globalShortcut.register(`${M}+Shift+Space`, () => sendToPrompter('cmd', 'togglePlay'));
globalShortcut.register(`${M}+Shift+Down`, () => sendToPrompter('cmd', 'faster'));
globalShortcut.register(`${M}+Shift+Up`, () => sendToPrompter('cmd', 'slower'));
globalShortcut.register(`${M}+Shift+L`, () => toggleLock());
globalShortcut.register(`${M}+Shift+H`, () => { if (prompterWin) prompterWin.isVisible() ? prompterWin.hide() : prompterWin.show(); });
globalShortcut.register(`${M}+Shift+Q`, () => app.quit());
}
app.whenReady().then(() => {
createPrompter();
createControl();
registerShortcuts();
sendToPrompter('config', config);
});
app.on('will-quit', () => globalShortcut.unregisterAll());
app.on('window-all-closed', () => app.quit());
// ---- IPC ----
ipcMain.handle('get-config', () => config);
ipcMain.on('set-config', (_e, partial) => {
config = Object.assign({}, config, partial);
saveConfig(config);
applyContentProtection();
if ('arrowControl' in partial) registerShortcuts();
sendToPrompter('config', config);
});
ipcMain.on('toggle-lock', () => toggleLock());
ipcMain.on('open-control', () => {
if (controlWin) controlWin.focus(); else createControl();
});
ipcMain.on('quit-app', () => app.quit());
ipcMain.on('set-ignore-mouse', (_e, ignore) => {
if (prompterWin) prompterWin.setIgnoreMouseEvents(ignore, { forward: true });
});