-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsettings-window-renderer.js
More file actions
215 lines (184 loc) · 6.77 KB
/
Copy pathsettings-window-renderer.js
File metadata and controls
215 lines (184 loc) · 6.77 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
document.addEventListener('DOMContentLoaded', () => {
const bridge = window.powerdoro;
const hotkeyDisplay = document.getElementById('hotkey-display');
const hotkeyReset = document.getElementById('hotkey-reset');
const autoLaunchCheckbox = document.getElementById('auto-launch');
const autoLaunchLabel = document.getElementById('auto-launch-label');
const preset1 = document.getElementById('preset-1');
const preset2 = document.getElementById('preset-2');
const preset3 = document.getElementById('preset-3');
const dirDisplay = document.getElementById('dir-display');
const dirBrowse = document.getElementById('dir-browse');
const btnSave = document.getElementById('btn-save');
const btnCancel = document.getElementById('btn-cancel');
const feedback = document.getElementById('feedback');
if (!bridge || !hotkeyDisplay || !hotkeyReset || !autoLaunchCheckbox || !autoLaunchLabel || !preset1 || !preset2 || !preset3 || !dirDisplay || !dirBrowse || !btnSave || !btnCancel || !feedback) {
console.error('Powerdoro settings renderer failed to initialize required elements or bridge');
return;
}
let currentHotkey = 'CommandOrControl+Shift+P';
let isRecording = false;
// Platform-aware display
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
function formatAcceleratorForDisplay(accel) {
if (!accel) return '';
let display = accel;
if (isMac) {
display = display.replace(/CommandOrControl/g, 'Cmd');
display = display.replace(/CmdOrCtrl/g, 'Cmd');
} else {
display = display.replace(/CommandOrControl/g, 'Ctrl');
display = display.replace(/CmdOrCtrl/g, 'Ctrl');
}
return display;
}
function loadSettings() {
bridge.getSettings().then((settings) => {
currentHotkey = settings.hotkey;
hotkeyDisplay.textContent = formatAcceleratorForDisplay(settings.hotkey);
autoLaunchCheckbox.checked = settings.autoLaunch;
autoLaunchLabel.textContent = settings.autoLaunch ? 'On' : 'Off';
preset1.value = settings.timerPresets[0];
preset2.value = settings.timerPresets[1];
preset3.value = settings.timerPresets[2];
if (settings.retrospectDir) {
dirDisplay.textContent = settings.retrospectDir;
} else {
dirDisplay.textContent = 'Documents/Powerdoro/ (default)';
}
});
}
loadSettings();
// MAS mode: disable unavailable features
bridge.isMAS().then((mas) => {
if (mas) {
document.getElementById('hotkey-recorder').style.display = 'none';
document.getElementById('hotkey-desc').textContent = 'Global hotkeys are not available in App Store version';
document.getElementById('auto-launch-group').style.display = 'none';
}
});
// Auto-launch label update
autoLaunchCheckbox.addEventListener('change', () => {
autoLaunchLabel.textContent = autoLaunchCheckbox.checked ? 'On' : 'Off';
});
// --- Hotkey Recorder ---
function startRecording() {
isRecording = true;
hotkeyDisplay.classList.add('recording');
hotkeyDisplay.textContent = 'Press key combination...';
}
function stopRecording(newAccelerator) {
isRecording = false;
hotkeyDisplay.classList.remove('recording');
if (newAccelerator) {
currentHotkey = newAccelerator;
hotkeyDisplay.textContent = formatAcceleratorForDisplay(newAccelerator);
} else {
hotkeyDisplay.textContent = formatAcceleratorForDisplay(currentHotkey);
}
}
hotkeyDisplay.addEventListener('click', () => {
if (!isRecording) {
startRecording();
}
});
hotkeyDisplay.addEventListener('keydown', (e) => {
if (!isRecording) return;
e.preventDefault();
e.stopPropagation();
// Escape cancels recording
if (e.key === 'Escape') {
stopRecording(null);
return;
}
// Build accelerator parts
const parts = [];
if (e.metaKey || e.ctrlKey) parts.push('CommandOrControl');
if (e.altKey) parts.push('Alt');
if (e.shiftKey) parts.push('Shift');
// Check if a non-modifier key is pressed
const modifierKeys = ['Control', 'Shift', 'Alt', 'Meta', 'OS'];
if (modifierKeys.includes(e.key)) {
// Only modifiers pressed so far — show feedback
hotkeyDisplay.textContent = parts.join('+') + '+...';
return;
}
if (parts.length === 0) {
// No modifier pressed, ignore
return;
}
// Map key to Electron accelerator format
let key = e.key;
if (key === ' ') key = 'Space';
else if (key.length === 1) key = key.toUpperCase();
else if (key === 'ArrowUp') key = 'Up';
else if (key === 'ArrowDown') key = 'Down';
else if (key === 'ArrowLeft') key = 'Left';
else if (key === 'ArrowRight') key = 'Right';
else if (key === 'Enter') key = 'Return';
else if (key === 'Backspace') key = 'Backspace';
else if (key === 'Delete') key = 'Delete';
else if (key === 'Tab') key = 'Tab';
parts.push(key);
stopRecording(parts.join('+'));
});
hotkeyReset.addEventListener('click', () => {
stopRecording('CommandOrControl+Shift+P');
});
// --- Directory Picker ---
dirBrowse.addEventListener('click', () => {
bridge.selectDirectory().then((result) => {
if (result) {
const dirPath = typeof result === 'string' ? result : result.path;
dirDisplay.textContent = dirPath;
if (result && result.bookmark) {
dirDisplay.dataset.bookmark = result.bookmark;
}
}
});
});
// --- Save / Cancel ---
function showFeedback(msg, type) {
feedback.textContent = msg;
feedback.className = 'feedback ' + type;
setTimeout(() => {
feedback.textContent = '';
feedback.className = 'feedback';
}, 2000);
}
btnSave.addEventListener('click', () => {
const p1 = parseInt(preset1.value, 10);
const p2 = parseInt(preset2.value, 10);
const p3 = parseInt(preset3.value, 10);
// Client-side validation
for (const val of [p1, p2, p3]) {
if (isNaN(val) || val < 1 || val > 180) {
showFeedback('Presets must be 1-180 minutes', 'error');
return;
}
}
const settings = {
hotkey: currentHotkey,
autoLaunch: autoLaunchCheckbox.checked,
timerPresets: [p1, p2, p3],
retrospectDir: dirDisplay.textContent.includes('(default)') ? '' : dirDisplay.textContent,
};
if (dirDisplay.dataset.bookmark) {
settings.retrospectDirBookmark = dirDisplay.dataset.bookmark;
}
bridge.saveSettings(settings).then((result) => {
if (result && result.error) {
showFeedback(result.error, 'error');
} else {
showFeedback('Settings saved!', 'success');
setTimeout(() => window.close(), 600);
}
}).catch(() => {
showFeedback('Failed to save settings', 'error');
});
});
btnCancel.addEventListener('click', () => {
window.close();
});
});