-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
236 lines (203 loc) · 6.17 KB
/
Copy pathmain.js
File metadata and controls
236 lines (203 loc) · 6.17 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
require('dotenv').config();
const { app, BrowserWindow, Tray, Menu, ipcMain } = require('electron');
const path = require('path');
const Positioner = require('electron-positioner');
const { handleAuthCallback, exchangeCodeForToken } = require('./notion-auth');
const Store = require('electron-store');
let tray = null;
let window = null;
let authWindow = null;
// Configuration for window size
const config = {
width: 300,
height: 450,
minWidth: 250,
minHeight: 400,
maxWidth: 800,
maxHeight: 600
};
const store = new Store();
function createWindow() {
try {
window = new BrowserWindow({
width: config.width,
height: config.height,
minWidth: config.minWidth,
minHeight: config.minHeight,
maxWidth: config.maxWidth,
maxHeight: config.maxHeight,
show: false,
frame: false,
fullscreenable: false,
resizable: true,
transparent: false, // Changed to false for debugging
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webviewTag: true,
preload: path.join(__dirname, 'preload.js'),
webSecurity: false, // Be cautious with this setting
allowRunningInsecureContent: true // Be cautious with this setting
}
});
window.loadFile('index.html');
window.webContents.on('did-finish-load', () => {
console.log('Window loaded successfully');
});
window.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
console.error('Failed to load window:', errorCode, errorDescription);
});
// Add resize event listener
window.on('resize', () => {
const [width, height] = window.getSize();
config.width = width;
config.height = height;
window.webContents.send('window-resized', { width, height });
});
// Open DevTools for debugging
window.webContents.openDevTools({ mode: 'detach' });
// Enable webview interactions
window.webContents.on('did-attach-webview', (event, webContents) => {
webContents.setWindowOpenHandler(({ url }) => {
require('electron').shell.openExternal(url);
return { action: 'deny' };
});
});
} catch (error) {
console.error('Error creating window:', error);
}
}
function createTray() {
tray = new Tray(path.join(__dirname, 'icon.png'));
tray.setIgnoreDoubleClickEvents(true);
tray.on('click', (event) => {
console.log('Tray clicked'); // Debugging log
toggleWindow();
});
}
function toggleWindow() {
console.log('Toggle window called'); // Debugging log
if (window.isVisible()) {
console.log('Window is visible, hiding it'); // Debugging log
window.hide();
} else {
console.log('Window is hidden, showing it'); // Debugging log
showWindow();
}
}
function showWindow() {
const trayPos = tray.getBounds();
const windowPos = window.getBounds();
let x, y = 0;
if (process.platform == 'darwin') {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2));
y = Math.round(trayPos.y + trayPos.height);
} else {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2));
y = Math.round(trayPos.y - windowPos.height);
}
console.log(`Setting window position to x: ${x}, y: ${y}`); // Debugging log
window.setPosition(x, y, false);
window.setSize(config.width, config.height); // Set size from config
window.show();
window.focus();
}
app.on('ready', () => {
console.log('App is ready'); // Debugging log
createTray();
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.on('start-oauth', (event) => {
console.log('start-oauth event received in main process');
if (!process.env.NOTION_CLIENT_ID) {
console.error('Notion Client ID is not configured');
event.reply('oauth-error', 'Notion Client ID is not configured');
return;
}
if (authWindow) {
authWindow.focus();
return;
}
authWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true
}
});
const authUrl = `https://api.notion.com/v1/oauth/authorize?client_id=${process.env.NOTION_CLIENT_ID}&response_type=code&owner=user&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback`;
console.log('Auth URL:', authUrl);
authWindow.loadURL(authUrl);
authWindow.show();
authWindow.webContents.on('will-navigate', (event, url) => {
console.log('will-navigate event:', url);
handleAuthCallback(url);
});
authWindow.webContents.on('will-redirect', (event, url) => {
console.log('will-redirect event:', url);
handleAuthCallback(url);
});
authWindow.webContents.on('did-navigate', (event, url) => {
console.log('did-navigate event:', url);
handleAuthCallback(url);
});
authWindow.on('closed', () => {
authWindow = null;
});
});
ipcMain.on('auth-success', (event, code) => {
console.log('Auth success received in main process');
if (window && window.webContents) {
window.webContents.send('auth-success', code);
}
if (authWindow) {
authWindow.close();
}
});
ipcMain.on('auth-error', (event, message) => {
console.log('Auth error received in main process:', message);
if (window && window.webContents) {
window.webContents.send('auth-error', message);
}
if (authWindow) {
authWindow.close();
}
});
ipcMain.handle('exchange-code', async (event) => {
try {
const token = await exchangeCodeForToken();
store.set('notionToken', token);
return token;
} catch (error) {
console.error('Error exchanging code for token:', error);
throw error;
}
});
ipcMain.handle('get-last-page', (event) => {
return store.get('lastNotionPage', 'https://www.notion.so/');
});
ipcMain.handle('set-last-page', (event, url) => {
store.set('lastNotionPage', url);
});
// Add this new IPC handler
ipcMain.on('resize-window', (event, width, height) => {
if (window) {
window.setSize(width, height);
}
});
// Add this near the other ipcMain handlers
ipcMain.handle('is-logged-in', () => {
return !!store.get('notionToken');
});