Repository navigation
Expand file tree
/
Copy pathsettings.js
More file actions
260 lines (227 loc) · 8.04 KB
/
Copy pathsettings.js
File metadata and controls
260 lines (227 loc) · 8.04 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Settings Tab JavaScript
let currentSettings = {
statusBar: {
enabled: true,
displayMode: 'compact',
alertsEnabled: true,
alertThreshold: 90,
updateFrequency: 5,
notifyOnReset: false
}
};
// Platform-specific UI text
const platformText = {
statusBarSectionTitle: {
darwin: 'Menu Bar',
win32: 'System Tray'
},
statusBarEnabledLabel: {
darwin: 'Show in menu bar',
win32: 'Show in system tray'
},
statusBarDescription: {
darwin: 'Display token usage monitor in macOS menu bar',
win32: 'Display token usage monitor in Windows system tray'
},
displayModeDescription: {
darwin: 'Choose what to display in the menu bar',
win32: 'Choose what to display in the system tray'
},
notificationPermissionError: {
darwin: 'Not receiving notifications?\nPlease enable notifications for Viber in System Preferences > Notifications.',
win32: 'Not receiving notifications?\nPlease enable notifications for Viber in Windows Settings > System > Notifications.'
}
};
// Get platform-specific text
function getPlatformText(key) {
const platform = window.electronAPI?.platform || 'darwin';
return platformText[key]?.[platform] || platformText[key]?.darwin || '';
}
// Initialize settings when the page loads
async function initSettings() {
try {
// Apply platform-specific text
applyPlatformText();
// Load current settings from main process
const settings = await window.electronAPI.loadSettings();
if (settings) {
currentSettings = settings;
updateSettingsUI();
}
} catch (error) {
console.error('Error loading settings:', error);
}
}
// Apply platform-specific text to UI elements
function applyPlatformText() {
const elementsToUpdate = [
'statusBarSectionTitle',
'statusBarEnabledLabel',
'statusBarDescription',
'displayModeDescription'
];
elementsToUpdate.forEach(id => {
const element = document.getElementById(id);
if (element) {
element.textContent = getPlatformText(id);
}
});
}
// Update UI to reflect current settings
function updateSettingsUI() {
// Status bar enabled
document.getElementById('statusBarEnabled').checked = currentSettings.statusBar.enabled;
// Display mode
document.getElementById('displayMode').value = currentSettings.statusBar.displayMode;
// Alerts
document.getElementById('alertsEnabled').checked = currentSettings.statusBar.alertsEnabled;
document.getElementById('alertThreshold').value = currentSettings.statusBar.alertThreshold;
document.getElementById('alertThresholdValue').textContent = `${currentSettings.statusBar.alertThreshold}%`;
// Update frequency
document.getElementById('updateFrequency').value = currentSettings.statusBar.updateFrequency;
document.getElementById('updateFrequencyValue').textContent = `${currentSettings.statusBar.updateFrequency} min`;
// Reset notification
document.getElementById('notifyOnReset').checked = currentSettings.statusBar.notifyOnReset;
// Enable/disable dependent fields
toggleStatusBarSettings(currentSettings.statusBar.enabled);
toggleAlertSettings(currentSettings.statusBar.alertsEnabled);
}
// Toggle status bar related settings
function toggleStatusBarSettings(enabled) {
const elements = document.querySelectorAll('.status-bar-setting');
elements.forEach(el => {
el.disabled = !enabled;
});
}
// Toggle alert related settings
function toggleAlertSettings(enabled) {
document.getElementById('alertThreshold').disabled = !enabled;
}
// Save settings to main process
async function saveSettings() {
try {
await window.electronAPI.saveSettings(currentSettings);
showSaveConfirmation();
} catch (error) {
console.error('Error saving settings:', error);
showSaveError();
}
}
// Show save confirmation
function showSaveConfirmation() {
const button = document.getElementById('saveSettings');
const originalText = button.textContent;
button.textContent = 'Saved!';
button.classList.add('saved');
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('saved');
}, 2000);
}
// Show save error
function showSaveError() {
const button = document.getElementById('saveSettings');
const originalText = button.textContent;
button.textContent = 'Error saving';
button.classList.add('error');
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('error');
}, 2000);
}
// Test alert functionality
async function testAlert() {
try {
// Test native notification
const notification = new Notification('Viber Alert Test', {
body: `Alert threshold reached: ${currentSettings.statusBar.alertThreshold}%`,
icon: 'resources/icon.png'
});
// Show success message
showTestResult('Alert test successful!');
} catch (error) {
// Check notification permission
const permissionErrorMsg = getPlatformText('notificationPermissionError');
if (Notification.permission === 'denied') {
showTestResult(permissionErrorMsg, true);
} else if (Notification.permission === 'default') {
// Request permission
const permission = await Notification.requestPermission();
if (permission === 'granted') {
testAlert(); // Retry after permission granted
} else {
showTestResult(permissionErrorMsg, true);
}
} else {
showTestResult('Error testing alert: ' + error.message, true);
}
}
}
// Show test result message
function showTestResult(message, isError = false) {
// Create or update message element
let messageEl = document.getElementById('alertTestMessage');
if (!messageEl) {
messageEl = document.createElement('div');
messageEl.id = 'alertTestMessage';
messageEl.style.cssText = `
margin-top: 10px;
padding: 8px 12px;
border-radius: 4px;
font-size: 13px;
line-height: 1.4;
white-space: pre-line;
`;
document.getElementById('testAlert').parentElement.appendChild(messageEl);
}
messageEl.textContent = message;
messageEl.style.backgroundColor = isError ? 'rgba(255, 59, 48, 0.1)' : 'rgba(52, 199, 89, 0.1)';
messageEl.style.color = isError ? '#ff3b30' : '#34c759';
messageEl.style.border = `1px solid ${isError ? 'rgba(255, 59, 48, 0.3)' : 'rgba(52, 199, 89, 0.3)'}`;
// Auto-hide success messages after 3 seconds
if (!isError) {
setTimeout(() => {
messageEl.style.display = 'none';
}, 3000);
} else {
// Keep error messages visible
messageEl.style.display = 'block';
}
}
// Event listeners
document.addEventListener('DOMContentLoaded', () => {
// Status bar enabled toggle
document.getElementById('statusBarEnabled').addEventListener('change', (e) => {
currentSettings.statusBar.enabled = e.target.checked;
toggleStatusBarSettings(e.target.checked);
});
// Display mode
document.getElementById('displayMode').addEventListener('change', (e) => {
currentSettings.statusBar.displayMode = e.target.value;
});
// Alerts enabled
document.getElementById('alertsEnabled').addEventListener('change', (e) => {
currentSettings.statusBar.alertsEnabled = e.target.checked;
toggleAlertSettings(e.target.checked);
});
// Alert threshold
document.getElementById('alertThreshold').addEventListener('input', (e) => {
currentSettings.statusBar.alertThreshold = parseInt(e.target.value);
document.getElementById('alertThresholdValue').textContent = `${e.target.value}%`;
});
// Update frequency
document.getElementById('updateFrequency').addEventListener('input', (e) => {
currentSettings.statusBar.updateFrequency = parseInt(e.target.value);
document.getElementById('updateFrequencyValue').textContent = `${e.target.value} min`;
});
// Notify on reset
document.getElementById('notifyOnReset').addEventListener('change', (e) => {
currentSettings.statusBar.notifyOnReset = e.target.checked;
});
// Save button
document.getElementById('saveSettings').addEventListener('click', saveSettings);
// Test alert button
document.getElementById('testAlert').addEventListener('click', testAlert);
// Initialize settings
initSettings();
});