Merge pull request #478 from dlitty/master

Added support for launching the application with a file on MacOS
This commit is contained in:
Gregor Biswanger
2021-01-10 23:55:20 +01:00
committed by GitHub
3 changed files with 109 additions and 3 deletions

View File

@@ -398,6 +398,70 @@ namespace ElectronNET.API
}
private bool _isReady = false;
/// <summary>
/// Emitted when a MacOS user wants to open a file with the application. The open-file event is usually emitted
/// when the application is already open and the OS wants to reuse the application to open the file.
/// open-file is also emitted when a file is dropped onto the dock and the application is not yet running.
/// <para/>
/// On Windows, you have to parse the arguments using App.CommandLine to get the filepath.
/// </summary>
public event Action<string> OpenFile
{
add
{
if (_openFile == null)
{
BridgeConnector.Socket.On("app-open-file" + GetHashCode(), (file) =>
{
_openFile(file.ToString());
});
BridgeConnector.Socket.Emit("register-app-open-file-event", GetHashCode());
}
_openFile += value;
}
remove
{
_openFile -= value;
if (_openFile == null)
BridgeConnector.Socket.Off("app-open-file" + GetHashCode());
}
}
private event Action<string> _openFile;
/// <summary>
/// Emitted when a MacOS user wants to open a URL with the application. Your application's Info.plist file must
/// define the URL scheme within the CFBundleURLTypes key, and set NSPrincipalClass to AtomApplication.
/// </summary>
public event Action<string> OpenUrl
{
add
{
if (_openUrl == null)
{
BridgeConnector.Socket.On("app-open-url" + GetHashCode(), (url) =>
{
_openUrl(url.ToString());
});
BridgeConnector.Socket.Emit("register-app-open-url-event", GetHashCode());
}
_openUrl += value;
}
remove
{
_openUrl -= value;
if (_openUrl == null)
BridgeConnector.Socket.Off("app-open-url" + GetHashCode());
}
}
private event Action<string> _openUrl;
/// <summary>
/// A <see cref="string"/> property that indicates the current application's name, which is the name in the
/// application's package.json file.

View File

@@ -13,6 +13,8 @@ let powerMonitor;
let splashScreen, hostHook;
let mainWindowId, nativeTheme;
let dock;
let launchFile;
let launchUrl;
let manifestJsonFileName = 'electron.manifest.json';
let watchable = false;
@@ -33,6 +35,18 @@ if (watchable) {
manifestJsonFilePath = path.join(currentBinPath, manifestJsonFileName);
}
// handle macOS events for opening the app with a file, etc
app.on('will-finish-launching', () => {
app.on('open-file', (evt, file) => {
evt.preventDefault();
launchFile = file;
})
app.on('open-url', (evt, url) => {
evt.preventDefault();
launchUrl = url;
})
});
const manifestJsonFile = require(manifestJsonFilePath);
if (manifestJsonFile.singleInstance || manifestJsonFile.aspCoreBackendPort) {
const mainInstance = app.requestSingleInstanceLock();
@@ -177,7 +191,7 @@ function startSocketApiBridge(port) {
global['electronsocket'].setMaxListeners(0);
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
appApi = require('./api/app')(socket, app);
appApi = require('./api/app')(socket, app);
browserWindows = require('./api/browserWindows')(socket, app);
commandLine = require('./api/commandLine')(socket, app);
autoUpdater = require('./api/autoUpdater')(socket);
@@ -194,7 +208,35 @@ function startSocketApiBridge(port) {
browserView = require('./api/browserView')(socket);
powerMonitor = require('./api/powerMonitor')(socket);
nativeTheme = require('./api/nativeTheme')(socket);
dock = require('./api/dock')(socket);
dock = require('./api/dock')(socket);
socket.on('register-app-open-file-event', (id) => {
electronSocket = socket;
app.on('open-file', (event, file) => {
event.preventDefault();
electronSocket.emit('app-open-file' + id, file);
});
if (launchFile) {
electronSocket.emit('app-open-file' + id, launchFile);
}
});
socket.on('register-app-open-url-event', (id) => {
electronSocket = socket;
app.on('open-url', (event, url) => {
event.preventDefault();
electronSocket.emit('app-open-url' + id, url);
});
if (launchUrl) {
electronSocket.emit('app-open-url' + id, launchUrl);
}
});
try {
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');

View File

@@ -17,7 +17,7 @@ echo "Restore & Build CLI"
pushd $dir/ElectronNET.CLI
dotnet restore
dotnet build
podp
popd
echo "Restore & Build WebApp Demo"
pushd $dir/ElectronNET.WebApp