Ladybird: Register Ladybird as a browser-type application on macOS

This allows Ladybird to be the default browser on macOS, and allows for
opening some file types (.html, .svg, .md, etc.) with Ladybird.

Note this currently only works in the GN build (and thus the Qt chrome).
The CMake build does not set up the Resources directory properly for
macOS to run the Ladybird app without $SERENITY_SOURCE_DIR set.
This commit is contained in:
Timothy Flynn 2023-11-13 14:45:38 -05:00 committed by Tim Flynn
parent 652bbe172b
commit 0415d03b4f
2 changed files with 87 additions and 1 deletions

View File

@ -20,5 +20,55 @@
<string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<string>True</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Web documents</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeIconFile</key>
<string>document.icns</string>
<key>LSItemContentTypes</key>
<array>
<string>public.html</string>
<string>public.xhtml</string>
<string>public.svg-image</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Other documents</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeIconFile</key>
<string>document.icns</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>md</string>
</array>
</dict>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Web site URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>http</string>
<string>https</string>
<string>gemini</string>
</array>
</dict>
<dict>
<key>CFBundleURLName</key>
<string>Local file URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>file</string>
</array>
</dict>
</array>
</dict>
</plist>

View File

@ -21,6 +21,7 @@
#include <LibWebView/Database.h>
#include <LibWebView/URL.h>
#include <QApplication>
#include <QFileOpenEvent>
namespace Ladybird {
@ -54,9 +55,40 @@ static ErrorOr<void> handle_attached_debugger()
return {};
}
class LadybirdApplication : public QApplication {
public:
LadybirdApplication(int& argc, char** argv)
: QApplication(argc, argv)
{
}
Function<void(URL)> on_open_file;
bool event(QEvent* event) override
{
switch (event->type()) {
case QEvent::FileOpen: {
if (!on_open_file)
break;
auto const& open_event = *static_cast<QFileOpenEvent const*>(event);
auto file = MUST(ak_string_from_qstring(open_event.file()));
if (auto file_url = WebView::sanitize_url(file); file_url.has_value())
on_open_file(file_url.release_value());
}
default:
break;
}
return QApplication::event(event);
}
};
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
QApplication app(arguments.argc, arguments.argv);
LadybirdApplication app(arguments.argc, arguments.argv);
Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
Core::EventLoop event_loop;
@ -111,6 +143,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Ladybird::BrowserWindow window(initial_urls, cookie_jar, webdriver_content_ipc_path, enable_callgrind_profiling ? WebView::EnableCallgrindProfiling::Yes : WebView::EnableCallgrindProfiling::No, use_lagom_networking ? Ladybird::UseLagomNetworking::Yes : Ladybird::UseLagomNetworking::No, use_gpu_painting ? WebView::EnableGPUPainting::Yes : WebView::EnableGPUPainting::No);
window.setWindowTitle("Ladybird");
app.on_open_file = [&](auto file_url) {
window.view().load(file_url);
};
if (Ladybird::Settings::the()->is_maximized()) {
window.showMaximized();
} else {