-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (144 loc) · 3.92 KB
/
main.js
File metadata and controls
167 lines (144 loc) · 3.92 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 } = require('electron');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
// Enable launch on startup
const exeName = path.basename(process.execPath);
app.setLoginItemSettings({
openAtLogin: true,
path: process.execPath,
args: [
'--process-start-args', `"--hidden"` // Optional args if we wanted to start hidden
]
});
const stateFile = path.join(app.getPath('userData'), 'notes-state.json');
function getSavedState() {
try {
if (fs.existsSync(stateFile)) {
const data = fs.readFileSync(stateFile);
return JSON.parse(data);
}
} catch (e) {
console.error('Failed to load state', e);
}
return { notes: {} }; // Changed from array of IDs to object of { id: { x, y, width, height } }
}
function saveState(state) {
try {
fs.writeFileSync(stateFile, JSON.stringify(state));
} catch (e) {
console.error('Failed to save state', e);
}
}
// Keep track of windows
const windows = new Set();
// Debounce timer for saving state
let saveTimer = null;
function scheduleSave() {
if (saveTimer) clearTimeout(saveTimer);
saveTimer = setTimeout(() => {
const state = { notes: {} };
windows.forEach(win => {
if (!win.isDestroyed()) {
const bounds = win.getBounds();
state.notes[win.noteId] = bounds;
}
});
saveState(state);
}, 1000); // Save 1 second after last movement
}
// Helper to calculate cascading position
function getCascadingPosition() {
const focusedWindow = BrowserWindow.getFocusedWindow();
if (focusedWindow) {
const [x, y] = focusedWindow.getPosition();
return { x: x + 30, y: y + 30 };
}
return null;
}
function createWindow(noteId, bounds = null) {
if (!noteId) {
noteId = crypto.randomUUID();
}
const windowOptions = {
width: 350,
height: 350,
frame: false,
transparent: true,
resizable: true,
skipTaskbar: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
};
if (bounds) {
if (bounds.x) windowOptions.x = bounds.x;
if (bounds.y) windowOptions.y = bounds.y;
if (bounds.width) windowOptions.width = bounds.width;
if (bounds.height) windowOptions.height = bounds.height;
}
const win = new BrowserWindow(windowOptions);
win.noteId = noteId;
win.loadFile('index.html', { query: { id: noteId } });
// Event listeners for state saving
win.on('moved', scheduleSave);
win.on('resized', scheduleSave);
win.on('closed', () => {
windows.delete(win);
// We don't save state here immediately because 'delete-note' handles explicit deletion.
// If it's closed via app quit, we want to keep it.
});
windows.add(win);
return noteId;
}
app.whenReady().then(() => {
const state = getSavedState();
const noteIds = Object.keys(state.notes);
if (noteIds.length > 0) {
noteIds.forEach(id => {
createWindow(id, state.notes[id]);
});
} else {
// First run
createWindow();
scheduleSave();
}
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
const state = getSavedState();
if (Object.keys(state.notes).length === 0) {
createWindow();
scheduleSave();
}
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Create a new note
ipcMain.on('create-new-note', () => {
const position = getCascadingPosition();
createWindow(null, position);
scheduleSave();
});
// Close/Delete a specific note
ipcMain.on('delete-note', (event) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
const noteId = win.noteId;
// Remove from memory
windows.delete(win);
win.close();
// Remove from storage immediately
const state = getSavedState();
if (state.notes[noteId]) {
delete state.notes[noteId];
saveState(state);
}
}
});