Mac: Support "Open With"

This commit is contained in:
Ben Olden-Cooligan 2024-04-01 15:42:02 -07:00
parent 02afb40db7
commit b3bff025d8
3 changed files with 95 additions and 4 deletions

View File

@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Basic metadata -->
<key>CFBundleName</key>
<string>NAPS2</string>
<key>CFBundleIdentifier</key>
@ -17,10 +19,77 @@
<key>CFBundleIconFile</key>
<string>Icon.icns</string>
<key>NSPrincipalClass</key>
<!-- We start as a background application in case we're in worker mode and don't want to show a UI.
If we're in GUI mode we dynamically switch to a foreground application (see MacEntryPoint.cs). -->
<string>NSApplication</string>
<key>LSBackgroundOnly</key>
<true/>
<!-- File types for "Open With" -->
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>JPG</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>JP2</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg-2000</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>PNG</string>
<key>LSItemContentTypes</key>
<array>
<string>public.png</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>TIFF</string>
<key>LSItemContentTypes</key>
<array>
<string>public.tiff</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>BMP</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.bmp</string>
</array>
</dict>
</array>
</dict>
</plist>

View File

@ -6,6 +6,7 @@ using Eto.Mac.Drawing;
using NAPS2.EtoForms.Layout;
using NAPS2.EtoForms.Widgets;
using NAPS2.Images.Mac;
using NAPS2.Remoting;
namespace NAPS2.EtoForms.Mac;
@ -23,7 +24,9 @@ public class MacEtoPlatform : EtoPlatform
public override Application CreateApplication()
{
return new Application(Platforms.macOS);
var application = new Application(Platforms.macOS);
((NSApplication) application.ControlObject).Delegate = new MacAppDelegate();
return application;
}
public override void Invoke(Application application, Action action)
@ -160,4 +163,20 @@ public class MacEtoPlatform : EtoPlatform
});
control.UnLoad += (_, _) => NSEvent.RemoveMonitor(monitor);
}
private class MacAppDelegate : AppDelegate
{
public override bool OpenFile(NSApplication sender, string filename)
{
Task.Run(() =>
ProcessCoordinator.CreateDefault().OpenFile(Process.GetCurrentProcess(), 100, filename));
return true;
}
public override void OpenFiles(NSApplication sender, string[] filenames)
{
Task.Run(() =>
ProcessCoordinator.CreateDefault().OpenFile(Process.GetCurrentProcess(), 100, filenames));
}
}
}

View File

@ -65,9 +65,12 @@ public class ProcessCoordinator(string instanceLockPath, string pipeNameFormat)
TrySendMessage(recipient, timeout,
client => client.ScanWithDevice(new ScanWithDeviceRequest { Device = device }));
public bool OpenFile(Process recipient, int timeout, string path) =>
TrySendMessage(recipient, timeout,
client => client.OpenFile(new OpenFileRequest { Path = { path } }));
public bool OpenFile(Process recipient, int timeout, params string[] paths)
{
var req = new OpenFileRequest();
req.Path.AddRange(paths);
return TrySendMessage(recipient, timeout, client => client.OpenFile(req));
}
public bool TryTakeInstanceLock()
{