Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
163 lines (140 loc) · 3.84 KB
/
Copy pathmain.js
File metadata and controls
163 lines (140 loc) · 3.84 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
const { app, BrowserWindow, Tray, Menu, nativeImage, shell } = require('electron');
const path = require('path');
const fs = require('fs');
let mainWindow = null;
let tray = null;
let isQuitting = false;
let saveTimer = null;
const stateFile = path.join(app.getPath('userData'), 'window-state.json');
const iconPath = path.join(__dirname, 'assets', 'tray-icon.png');
if (process.platform === 'win32') {
app.setAppUserModelId('com.DiffView.app');
}
app.setName('DiffView');
const gotLock = app.requestSingleInstanceLock();
if (!gotLock) {
app.quit();
} else {
app.on('second-instance', () => showWindow());
}
function loadState() {
try {
const raw = fs.readFileSync(stateFile, 'utf8');
const data = JSON.parse(raw);
if (data && data.bounds && typeof data.bounds.width === 'number') {
return data;
}
} catch (_) {}
return { maximized: true, bounds: { width: 1280, height: 820 } };
}
function saveState() {
if (!mainWindow || mainWindow.isDestroyed()) return;
try {
const maximized = mainWindow.isMaximized();
const bounds = maximized ? mainWindow.getNormalBounds() : mainWindow.getBounds();
fs.writeFileSync(stateFile, JSON.stringify({ maximized, bounds }));
} catch (e) {
console.error('saveState failed:', e);
}
}
function debouncedSaveState() {
clearTimeout(saveTimer);
saveTimer = setTimeout(saveState, 400);
}
function createWindow() {
const state = loadState();
mainWindow = new BrowserWindow({
width: state.bounds.width,
height: state.bounds.height,
x: state.bounds.x,
y: state.bounds.y,
show: false,
backgroundColor: '#1e1e1e',
icon: iconPath,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
spellcheck: false,
backgroundThrottling: true,
},
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadFile(path.join(__dirname, 'index.html'));
mainWindow.once('ready-to-show', () => {
if (state.maximized) {
mainWindow.maximize();
}
mainWindow.show();
});
mainWindow.on('resize', debouncedSaveState);
mainWindow.on('move', debouncedSaveState);
mainWindow.on('maximize', saveState);
mainWindow.on('unmaximize', saveState);
mainWindow.on('close', (e) => {
if (!isQuitting) {
e.preventDefault();
mainWindow.hide();
}
});
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
}
function showWindow() {
if (!mainWindow || mainWindow.isDestroyed()) return;
if (mainWindow.isMinimized()) mainWindow.restore();
if (!mainWindow.isVisible()) mainWindow.show();
mainWindow.focus();
}
function toggleWindow() {
if (!mainWindow || mainWindow.isDestroyed()) return;
if (mainWindow.isVisible() && mainWindow.isFocused()) {
mainWindow.hide();
} else {
showWindow();
}
}
function createTray() {
const image = nativeImage.createFromPath(iconPath);
if (image.isEmpty()) {
console.error('Tray icon empty:', iconPath);
return;
}
tray = new Tray(image);
tray.setToolTip('DiffView');
const menu = Menu.buildFromTemplate([
{ label: 'Show DiffView', click: () => showWindow() },
{ type: 'separator' },
{ label: 'Force Quit', click: () => forceQuit() },
]);
tray.setContextMenu(menu);
tray.on('click', () => toggleWindow());
tray.on('double-click', () => showWindow());
}
function forceQuit() {
isQuitting = true;
saveState();
app.quit();
}
app.whenReady().then(() => {
createWindow();
createTray();
});
app.on('window-all-closed', () => {
if (isQuitting) app.quit();
});
app.on('before-quit', () => {
isQuitting = true;
saveState();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
} else {
showWindow();
}
});