-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
208 lines (181 loc) · 6.37 KB
/
Copy pathmain.js
File metadata and controls
208 lines (181 loc) · 6.37 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
const { app, session } = require('electron')
// Enable PipeWire screen-capture support on Linux/Wayland before the app is ready.
if (process.platform === 'linux') {
app.commandLine.appendSwitch('enable-features', 'WebRTCPipeWireCapturer')
}
const environment = require('./src/config/environment')
const WindowManager = require('./src/services/WindowManager')
const RecordingManager = require('./src/services/RecordingManager')
const DriveService = require('./src/services/DriveService')
const YouTubeService = require('./src/services/YouTubeService')
const StorageService = require('./src/services/StorageService')
const RecoveryManager = require('./src/utils/recovery-manager')
const RecordingHandlers = require('./src/handlers/RecordingHandlers')
const WindowHandlers = require('./src/handlers/WindowHandlers')
const DriveHandlers = require('./src/handlers/DriveHandlers')
const YouTubeHandlers = require('./src/handlers/YouTubeHandlers')
const StorageHandlers = require('./src/handlers/StorageHandlers')
class StreamSnapApp {
constructor() {
this.windowManager = new WindowManager()
this.recordingManager = new RecordingManager()
this.driveService = new DriveService()
this.youtubeService = new YouTubeService()
this.storageService = new StorageService()
this.isInitialized = false
this.setupAppEventListeners()
this.setupHandlers()
this.setupRecordingEvents()
}
setupAppEventListeners() {
app.whenReady().then(() => this.handleAppReady())
app.on('window-all-closed', () => this.handleAllWindowsClosed())
app.on('activate', () => this.handleAppActivate())
app.on('web-contents-created', (event, contents) => {
contents.on('new-window', (event, navigationUrl) => {
event.preventDefault()
})
})
}
setupHandlers() {
new RecordingHandlers(this)
new WindowHandlers(this)
new DriveHandlers(this)
new YouTubeHandlers(this)
new StorageHandlers(this)
}
setupRecordingEvents() {
this.recordingManager.emitShortcutEvent = action => {
const mainWindow = this.windowManager.getWindow('main')
const floatingWindow = this.windowManager.getWindow('floating')
if (this.recordingManager.isRecording) {
if (mainWindow) {
mainWindow.webContents.send(`shortcut-${action}`)
}
if (floatingWindow) {
floatingWindow.webContents.send(`shortcut-${action}`)
}
}
}
}
async getSettings() {
try {
const mainWindow = this.windowManager.getWindow('main')
if (mainWindow) {
const settings = await mainWindow.webContents.executeJavaScript(
`window.screenRecorder?.settingsManager?.settings || {}`
)
return settings
}
} catch (error) {}
return {}
}
broadcastToWindows(event, data = null) {
try {
} catch (e) {}
Object.values(this.windowManager.windows).forEach(window => {
if (window && !window.isDestroyed()) {
try {
window.webContents.send(event, data)
} catch (e) {}
}
})
}
async handleAppReady() {
try {
RecoveryManager.cleanupOldVideos().catch(() => {})
// On Linux, getDisplayMedia() requires a handler in Electron 32+;
// without it the renderer receives NotSupportedError.
// Using useSystemPicker delegates the entire screen/window/tab picker to
// the OS (XDG portal on Wayland, native picker on X11) in a single dialog,
// avoiding the double-portal loop that the previous getSources() approach caused.
if (process.platform === 'linux') {
session.defaultSession.setDisplayMediaRequestHandler((_request, callback) => {
callback({ useSystemPicker: true })
}, { useSystemPicker: true })
}
if (process.platform === 'darwin') {
const path = require('path')
try {
app.dock.setIcon(path.join(__dirname, 'src/icon.icns'))
} catch (e) {}
}
let isHiddenLaunch = false
try {
isHiddenLaunch = (process.platform === 'darwin' && app.getLoginItemSettings().wasOpenedAsHidden) || process.argv.includes('--hidden')
} catch (e) {
isHiddenLaunch = process.argv.includes('--hidden')
}
await this.windowManager.createMainWindow(isHiddenLaunch)
this.isInitialized = true
try {
let isAuth = false
try {
if (this.driveService && typeof this.driveService.ensureValidAccessToken === 'function') {
isAuth = await this.driveService.ensureValidAccessToken()
} else {
isAuth = !!this.driveService.isAuthenticated && this.driveService.isAuthenticated()
}
} catch (e) {
isAuth = !!(this.driveService && this.driveService.isAuthenticated && this.driveService.isAuthenticated())
}
this.broadcastToWindows('drive-auth-updated', {
authenticated: !!isAuth,
accessToken: isAuth ? this.driveService.accessToken || null : null
})
} catch (e) {}
} catch (error) {
app.quit()
}
}
handleAllWindowsClosed() {
if (process.platform !== 'darwin') {
this.cleanup()
app.quit()
}
}
async handleAppActivate() {
if (!this.windowManager.hasOpenWindows()) {
await this.windowManager.createMainWindow()
}
}
cleanup() {
try {
const { globalShortcut } = require('electron')
globalShortcut.unregisterAll()
this.recordingManager.cleanup()
this.windowManager.closeAllWindows()
this.driveService.signOut()
} catch (error) {}
}
getStatus() {
return {
isInitialized: this.isInitialized,
environment: environment.isProduction ? 'production' : 'development',
recording: this.recordingManager.getRecordingState(),
windows: {
main: !!this.windowManager.getWindow('main'),
floating: !!this.windowManager.getWindow('floating'),
save: !!this.windowManager.getWindow('save'),
sourceSelector: !!this.windowManager.getWindow('sourceSelector')
},
drive: {
authenticated: this.driveService.isAuthenticated()
}
}
}
}
const streamSnapApp = new StreamSnapApp()
module.exports = StreamSnapApp
process.on('uncaughtException', error => {
if (environment.isProduction) {
streamSnapApp.cleanup()
app.quit()
}
})
process.on('unhandledRejection', (reason, promise) => {
if (environment.isProduction) {
streamSnapApp.cleanup()
app.quit()
}
})