Skip to content

Commit c8c818a

Browse files
Merge pull request #478 from dlitty/master
Added support for launching the application with a file on MacOS
2 parents fc590f5 + 7518acb commit c8c818a

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

ElectronNET.API/App.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,70 @@ internal set
398398
}
399399
private bool _isReady = false;
400400

401+
/// <summary>
402+
/// Emitted when a MacOS user wants to open a file with the application. The open-file event is usually emitted
403+
/// when the application is already open and the OS wants to reuse the application to open the file.
404+
/// open-file is also emitted when a file is dropped onto the dock and the application is not yet running.
405+
/// <para/>
406+
/// On Windows, you have to parse the arguments using App.CommandLine to get the filepath.
407+
/// </summary>
408+
public event Action<string> OpenFile
409+
{
410+
add
411+
{
412+
if (_openFile == null)
413+
{
414+
BridgeConnector.Socket.On("app-open-file" + GetHashCode(), (file) =>
415+
{
416+
_openFile(file.ToString());
417+
});
418+
419+
BridgeConnector.Socket.Emit("register-app-open-file-event", GetHashCode());
420+
}
421+
_openFile += value;
422+
}
423+
remove
424+
{
425+
_openFile -= value;
426+
427+
if (_openFile == null)
428+
BridgeConnector.Socket.Off("app-open-file" + GetHashCode());
429+
}
430+
}
431+
432+
private event Action<string> _openFile;
433+
434+
435+
/// <summary>
436+
/// Emitted when a MacOS user wants to open a URL with the application. Your application's Info.plist file must
437+
/// define the URL scheme within the CFBundleURLTypes key, and set NSPrincipalClass to AtomApplication.
438+
/// </summary>
439+
public event Action<string> OpenUrl
440+
{
441+
add
442+
{
443+
if (_openUrl == null)
444+
{
445+
BridgeConnector.Socket.On("app-open-url" + GetHashCode(), (url) =>
446+
{
447+
_openUrl(url.ToString());
448+
});
449+
450+
BridgeConnector.Socket.Emit("register-app-open-url-event", GetHashCode());
451+
}
452+
_openUrl += value;
453+
}
454+
remove
455+
{
456+
_openUrl -= value;
457+
458+
if (_openUrl == null)
459+
BridgeConnector.Socket.Off("app-open-url" + GetHashCode());
460+
}
461+
}
462+
463+
private event Action<string> _openUrl;
464+
401465
/// <summary>
402466
/// A <see cref="string"/> property that indicates the current application's name, which is the name in the
403467
/// application's package.json file.

ElectronNET.Host/main.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ let powerMonitor;
1313
let splashScreen, hostHook;
1414
let mainWindowId, nativeTheme;
1515
let dock;
16+
let launchFile;
17+
let launchUrl;
1618

1719
let manifestJsonFileName = 'electron.manifest.json';
1820
let watchable = false;
@@ -33,6 +35,18 @@ if (watchable) {
3335
manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
3436
}
3537

38+
// handle macOS events for opening the app with a file, etc
39+
app.on('will-finish-launching', () => {
40+
app.on('open-file', (evt, file) => {
41+
evt.preventDefault();
42+
launchFile = file;
43+
})
44+
app.on('open-url', (evt, url) => {
45+
evt.preventDefault();
46+
launchUrl = url;
47+
})
48+
});
49+
3650
const manifestJsonFile = require(manifestJsonFilePath);
3751
if (manifestJsonFile.singleInstance || manifestJsonFile.aspCoreBackendPort) {
3852
const mainInstance = app.requestSingleInstanceLock();
@@ -177,7 +191,7 @@ function startSocketApiBridge(port) {
177191
global['electronsocket'].setMaxListeners(0);
178192
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
179193

180-
appApi = require('./api/app')(socket, app);
194+
appApi = require('./api/app')(socket, app);
181195
browserWindows = require('./api/browserWindows')(socket, app);
182196
commandLine = require('./api/commandLine')(socket, app);
183197
autoUpdater = require('./api/autoUpdater')(socket);
@@ -194,7 +208,35 @@ function startSocketApiBridge(port) {
194208
browserView = require('./api/browserView')(socket);
195209
powerMonitor = require('./api/powerMonitor')(socket);
196210
nativeTheme = require('./api/nativeTheme')(socket);
197-
dock = require('./api/dock')(socket);
211+
dock = require('./api/dock')(socket);
212+
213+
socket.on('register-app-open-file-event', (id) => {
214+
electronSocket = socket;
215+
216+
app.on('open-file', (event, file) => {
217+
event.preventDefault();
218+
219+
electronSocket.emit('app-open-file' + id, file);
220+
});
221+
222+
if (launchFile) {
223+
electronSocket.emit('app-open-file' + id, launchFile);
224+
}
225+
});
226+
227+
socket.on('register-app-open-url-event', (id) => {
228+
electronSocket = socket;
229+
230+
app.on('open-url', (event, url) => {
231+
event.preventDefault();
232+
233+
electronSocket.emit('app-open-url' + id, url);
234+
});
235+
236+
if (launchUrl) {
237+
electronSocket.emit('app-open-url' + id, launchUrl);
238+
}
239+
});
198240

199241
try {
200242
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');

buildAll.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ echo "Restore & Build CLI"
1717
pushd $dir/ElectronNET.CLI
1818
dotnet restore
1919
dotnet build
20-
podp
20+
popd
2121

2222
echo "Restore & Build WebApp Demo"
2323
pushd $dir/ElectronNET.WebApp

0 commit comments

Comments
 (0)