-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
551 lines (530 loc) · 20.1 KB
/
main.js
File metadata and controls
551 lines (530 loc) · 20.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.state = void 0;
exports.clearQueues = clearQueues;
exports.createWindow = createWindow;
exports.getExtraScreenshotQueue = getExtraScreenshotQueue;
exports.getHasDebugged = getHasDebugged;
exports.getImagePreview = getImagePreview;
exports.getMainWindow = getMainWindow;
exports.getProblemInfo = getProblemInfo;
exports.getScreenshotHelper = getScreenshotHelper;
exports.getScreenshotQueue = getScreenshotQueue;
exports.getView = getView;
exports.hideMainWindow = hideMainWindow;
exports.moveWindowHorizontal = moveWindowHorizontal;
exports.moveWindowVertical = moveWindowVertical;
exports.resizeWindowToContent = resizeWindowToContent;
exports.setHasDebugged = setHasDebugged;
exports.setProblemInfo = setProblemInfo;
exports.setView = setView;
exports.showMainWindow = showMainWindow;
exports.takeScreenshot = takeScreenshot;
exports.toggleMainWindow = toggleMainWindow;
const electron_1 = require("electron");
const path_1 = __importDefault(require("path"));
const ipcHandlers_1 = require("./ipcHandlers");
const url_1 = require("./url");
const ProcessingHelper_1 = require("./ProcessingHelper");
const ScreenshotHelper_1 = require("./ScreenshotHelper");
const shortcuts_1 = require("./shortcuts");
// Set the app name that appears in task manager
electron_1.app.setName("ShadowIDE");
// Constants
const isDev = !electron_1.app.isPackaged;
// Application State
const state = {
// Window management properties
mainWindow: null,
isWindowVisible: false,
windowPosition: null,
windowSize: null,
screenWidth: 0,
screenHeight: 0,
step: 0,
currentX: 0,
currentY: 0,
// Application helpers
screenshotHelper: null,
shortcutsHelper: null,
processingHelper: null,
// View and state management
view: "queue",
problemInfo: null,
hasDebugged: false,
// Processing events
PROCESSING_EVENTS: {
UNAUTHORIZED: "processing-unauthorized",
NO_SCREENSHOTS: "processing-no-screenshots",
API_KEY_INVALID: "processing-api-key-invalid",
SCREENSHOT_ERROR: "screenshot-error",
INITIAL_START: "initial-start",
PROBLEM_EXTRACTED: "problem-extracted",
SOLUTION_SUCCESS: "solution-success",
INITIAL_SOLUTION_ERROR: "solution-error",
DEBUG_START: "debug-start",
DEBUG_SUCCESS: "debug-success",
DEBUG_ERROR: "debug-error",
},
};
exports.state = state;
// Initialize helpers
function initializeHelpers() {
state.screenshotHelper = new ScreenshotHelper_1.ScreenshotHelper(state.view);
state.processingHelper = new ProcessingHelper_1.ProcessingHelper({
getScreenshotHelper,
getMainWindow,
getView,
setView,
getProblemInfo,
setProblemInfo,
getScreenshotQueue,
getExtraScreenshotQueue,
clearQueues,
takeScreenshot,
getImagePreview,
setHasDebugged,
getHasDebugged,
PROCESSING_EVENTS: state.PROCESSING_EVENTS,
});
state.shortcutsHelper = new shortcuts_1.ShortcutsHelper({
getMainWindow,
takeScreenshot,
getImagePreview,
processingHelper: state.processingHelper,
clearQueues,
setView,
isVisible: () => state.isWindowVisible,
toggleMainWindow,
moveWindowLeft: () => moveWindowHorizontal((x) => x - state.step),
moveWindowRight: () => moveWindowHorizontal((x) => x + state.step),
moveWindowUp: () => moveWindowVertical((y) => y - state.step),
moveWindowDown: () => moveWindowVertical((y) => y + state.step),
resetWindowPosition,
PROCESSING_EVENTS: state.PROCESSING_EVENTS,
getScreenshotHelper,
});
}
// Window management functions
async function createWindow() {
if (state.mainWindow) {
if (state.mainWindow.isMinimized())
state.mainWindow.restore();
return;
}
const primaryDisplay = electron_1.screen.getPrimaryDisplay();
const workArea = primaryDisplay.workAreaSize;
const displayBounds = primaryDisplay.bounds;
state.screenWidth = displayBounds.width * 0.75; // 25% width reduction
state.screenHeight = displayBounds.height;
state.step = 60;
state.currentY = 0;
const windowSettings = {
width: displayBounds.width * 0.75, // 25% width reduction
height: 800, // Increased initial height for dropdown visibility
minWidth: displayBounds.width * 0.75, // 25% width reduction
minHeight: 600, // Increased minimum height for better dropdown visibility
maxWidth: displayBounds.width * 0.75, // 25% width reduction
// Remove maxHeight constraint to allow window to grow with content
x: 0,
y: 0,
icon: path_1.default.join(__dirname, "ShadowIDE_icon.png"),
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: isDev
? path_1.default.join(__dirname, "preload.js")
: path_1.default.join(__dirname, "preload.js"),
scrollBounce: true,
devTools: isDev, // Only enable DevTools in development
},
show: true,
frame: false,
transparent: true,
fullscreenable: false,
hasShadow: false,
backgroundColor: "#00000000",
skipTaskbar: true,
type: "panel",
paintWhenInitiallyHidden: true,
titleBarStyle: "hidden",
enableLargerThanScreen: true,
movable: false,
resizable: true, // Enable resizing to allow content-based height adjustment
};
state.mainWindow = new electron_1.BrowserWindow(windowSettings);
// Add more detailed logging for window events
state.mainWindow.webContents.on("did-finish-load", () => {
console.log("Window finished loading");
});
state.mainWindow.webContents.on("did-fail-load", async (event, errorCode, errorDescription) => {
console.error("Window failed to load:", errorCode, errorDescription);
// Log error but don't retry since we're using local files
});
if (isDev) {
// In development, load the built files directly instead of dev server
console.log("Loading built files in development mode:", path_1.default.join(__dirname, "index.html"));
state.mainWindow.loadFile(path_1.default.join(__dirname, "index.html"));
}
else {
// In production, load from the built files
console.log("Loading production build:", path_1.default.join(__dirname, "index.html"));
state.mainWindow.loadFile(path_1.default.join(__dirname, "index.html"));
}
// Configure window behavior
state.mainWindow.webContents.setZoomFactor(1);
// Set initial click-through behavior - forward mouse events
state.mainWindow.setIgnoreMouseEvents(true, { forward: true });
// Disable click-through when DevTools is opened
state.mainWindow.webContents.on('devtools-opened', () => {
console.log('DevTools opened - disabling click-through');
state.mainWindow.setIgnoreMouseEvents(false);
});
// Re-enable click-through when DevTools is closed
state.mainWindow.webContents.on('devtools-closed', () => {
console.log('DevTools closed - re-enabling click-through');
state.mainWindow.setIgnoreMouseEvents(true, { forward: true });
});
// if (isDev) {
// state.mainWindow.webContents.openDevTools();
// }
// Enhanced screen capture resistanced
state.mainWindow.setContentProtection(true);
state.mainWindow.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
});
state.mainWindow.setAlwaysOnTop(true, "screen-saver", 1);
// Additional screen capture resistance settings
if (process.platform === "darwin") {
// Prevent window from being captured in screenshots
state.mainWindow.setHiddenInMissionControl(true);
state.mainWindow.setWindowButtonVisibility(false);
state.mainWindow.setBackgroundColor("#00000000");
// Prevent window from being included in window switcher
state.mainWindow.setSkipTaskbar(true);
// Disable window shadow
state.mainWindow.setHasShadow(false);
}
// Prevent the window from being captured by screen recording
state.mainWindow.webContents.setBackgroundThrottling(false);
state.mainWindow.webContents.setFrameRate(60);
// Set up window listeners
state.mainWindow.on("move", handleWindowMove);
state.mainWindow.on("resize", handleWindowResize);
state.mainWindow.on("closed", handleWindowClosed);
// Initialize window state
const bounds = state.mainWindow.getBounds();
state.windowPosition = { x: bounds.x, y: bounds.y };
state.windowSize = { width: bounds.width, height: bounds.height };
state.currentX = 0;
state.currentY = 0;
state.isWindowVisible = true; // Start visible with instructions
// Show the window with content after a short delay to ensure content is loaded
state.mainWindow.webContents.once('did-finish-load', () => {
state.mainWindow.setOpacity(1);
state.mainWindow.show();
});
await (0, url_1.handleCustomProtocol)(state.mainWindow);
}
function handleWindowMove() {
if (!state.mainWindow)
return;
const bounds = state.mainWindow.getBounds();
state.windowPosition = { x: bounds.x, y: bounds.y };
state.currentX = bounds.x;
state.currentY = bounds.y;
}
function handleWindowResize() {
if (!state.mainWindow)
return;
const bounds = state.mainWindow.getBounds();
state.windowSize = { width: bounds.width, height: bounds.height };
}
function handleWindowClosed() {
state.mainWindow = null;
state.isWindowVisible = false;
state.windowPosition = null;
state.windowSize = null;
}
// Window visibility functions
function hideMainWindow() {
if (!state.mainWindow?.isDestroyed()) {
const bounds = state.mainWindow.getBounds();
state.windowPosition = { x: bounds.x, y: bounds.y };
state.windowSize = { width: bounds.width, height: bounds.height };
state.mainWindow.setIgnoreMouseEvents(true, { forward: true });
state.mainWindow.setAlwaysOnTop(true, "screen-saver", 1);
state.mainWindow.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
});
state.mainWindow.setOpacity(0);
state.mainWindow.hide();
state.isWindowVisible = false;
}
}
function showMainWindow() {
// If window is null or destroyed, try to create a new one
if (!state.mainWindow || state.mainWindow.isDestroyed()) {
console.log("Window is null or destroyed, creating new window");
createWindow().catch((err) => {
console.error("Failed to create new window:", err);
});
return;
}
try {
if (state.windowPosition && state.windowSize) {
state.mainWindow.setBounds({
...state.windowPosition,
...state.windowSize,
});
}
state.mainWindow.setIgnoreMouseEvents(true, { forward: true });
state.mainWindow.setAlwaysOnTop(true, "screen-saver", 1);
state.mainWindow.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
});
state.mainWindow.setContentProtection(true);
state.mainWindow.setOpacity(0);
state.mainWindow.showInactive();
state.mainWindow.setOpacity(1);
state.isWindowVisible = true;
}
catch (error) {
console.error("Error showing window:", error);
// If we encounter an error, try to create a new window
createWindow().catch((err) => {
console.error("Failed to create new window after error:", err);
});
}
}
function toggleMainWindow() {
if (state.isWindowVisible) {
hideMainWindow();
}
else {
showMainWindow();
}
}
// Window movement functions
function moveWindowHorizontal(updateFn) {
if (!state.mainWindow)
return;
const newX = updateFn(state.currentX);
// Allow much more flexible movement - window can move almost entirely off screen
// This allows content from the right edge to be brought to the left when content is wide
const windowWidth = state.windowSize?.width || 0;
const maxLeftLimit = -(windowWidth - 100); // Allow window to move left until only 100px is visible at right
const maxRightLimit = state.screenWidth - 100; // Allow window to move right until only 100px is visible at left
// Only update if within bounds
if (newX >= maxLeftLimit && newX <= maxRightLimit) {
state.currentX = newX;
state.mainWindow.setPosition(Math.round(state.currentX), Math.round(state.currentY));
}
}
function moveWindowVertical(updateFn) {
if (!state.mainWindow)
return;
const newY = updateFn(state.currentY);
// Allow much more flexible movement - window can move almost entirely off screen
// This allows bottom content to be brought to the top when content is large
const windowHeight = state.windowSize?.height || 0;
const maxUpLimit = -(windowHeight - 100); // Allow window to move up until only 100px is visible at bottom
const maxDownLimit = state.screenHeight - 100; // Allow window to move down until only 100px is visible at top
// Only update if within bounds
if (newY >= maxUpLimit && newY <= maxDownLimit) {
state.currentY = newY;
state.mainWindow.setPosition(Math.round(state.currentX), Math.round(state.currentY));
}
}
// Reset window position to initial state
function resetWindowPosition() {
if (!state.mainWindow)
return;
state.currentX = 0;
state.currentY = 0;
state.mainWindow.setPosition(state.currentX, state.currentY);
}
// Window dimension functions
function setWindowDimensions(width, height) {
if (!state.mainWindow?.isDestroyed()) {
const [currentX, currentY] = state.mainWindow.getPosition();
const primaryDisplay = electron_1.screen.getPrimaryDisplay();
const workArea = primaryDisplay.workAreaSize;
const maxWidth = Math.floor(workArea.width);
state.mainWindow.setBounds({
x: Math.min(currentX, workArea.width - width),
y: currentY,
width: Math.min(width, maxWidth),
height: Math.ceil(height),
});
}
}
// Dynamic window resizing based on content
function resizeWindowToContent(requestedWidth, requestedHeight) {
if (!state.mainWindow || state.mainWindow.isDestroyed()) {
console.log('Window not available for resizing');
return;
}
try {
const primaryDisplay = electron_1.screen.getPrimaryDisplay();
const displayBounds = primaryDisplay.bounds;
// Use full width and allow unlimited height for content expansion
const newWidth = displayBounds.width;
const newHeight = requestedHeight || displayBounds.height; // Remove height limitation completely
console.log('Resizing window to:', {
requestedWidth,
requestedHeight,
newWidth,
newHeight,
screenHeight: displayBounds.height
});
// Get current position
const [currentX, currentY] = state.mainWindow.getPosition();
// Set new bounds - allow content to extend beyond screen if needed
state.mainWindow.setBounds({
x: 0, // Keep at left edge
y: 0, // Keep at top to ensure content is always visible from the start
width: newWidth,
height: newHeight,
});
// Update state
state.windowSize = { width: newWidth, height: newHeight };
} catch (error) {
console.error('Error resizing window:', error);
}
}
// Initialize application
async function initializeApp() {
try {
initializeHelpers();
(0, ipcHandlers_1.initializeIpcHandlers)({
getMainWindow,
setWindowDimensions,
resizeWindowToContent,
getScreenshotQueue,
getExtraScreenshotQueue,
getImagePreview,
processingHelper: state.processingHelper,
PROCESSING_EVENTS: state.PROCESSING_EVENTS,
takeScreenshot,
getView,
toggleMainWindow,
clearQueues,
setView,
moveWindowLeft: () => moveWindowHorizontal((x) => x - state.step),
moveWindowRight: () => moveWindowHorizontal((x) => x + state.step),
moveWindowUp: () => moveWindowVertical((y) => y - state.step),
moveWindowDown: () => moveWindowVertical((y) => y + state.step),
});
await createWindow();
state.shortcutsHelper?.registerGlobalShortcuts();
}
catch (error) {
console.error("Failed to initialize application:", error);
electron_1.app.quit();
}
}
// Prevent multiple instances of the app
if (!electron_1.app.requestSingleInstanceLock()) {
electron_1.app.quit();
}
else {
electron_1.app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
electron_1.app.quit();
state.mainWindow = null;
}
});
}
electron_1.app.on("activate", () => {
if (electron_1.BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// State getter/setter functions
function getMainWindow() {
return state.mainWindow;
}
function getView() {
return state.view;
}
function setView(view) {
console.log("setView called - changing from", state.view, "to", view);
state.view = view;
state.screenshotHelper?.setView(view);
console.log("setView completed - current view is now:", state.view);
}
function getScreenshotHelper() {
return state.screenshotHelper;
}
function getProblemInfo() {
return state.problemInfo;
}
function setProblemInfo(problemInfo) {
state.problemInfo = problemInfo;
}
function getScreenshotQueue() {
return state.screenshotHelper?.getScreenshotQueue() || [];
}
function getExtraScreenshotQueue() {
return state.screenshotHelper?.getExtraScreenshotQueue() || [];
}
function clearQueues() {
console.log("clearQueues called - current view:", state.view);
state.screenshotHelper?.clearQueues();
state.problemInfo = null;
setView("queue");
console.log("clearQueues completed");
}
async function takeScreenshot() {
if (!state.mainWindow)
throw new Error("No main window available");
return state.screenshotHelper?.takeScreenshot() || "";
}
async function getImagePreview(filepath) {
return state.screenshotHelper?.getImagePreview(filepath) || "";
}
function setHasDebugged(value) {
state.hasDebugged = value;
}
function getHasDebugged() {
return state.hasDebugged;
}
electron_1.app.whenReady().then(initializeApp);