From 0415d03b4f11e0b3ca568fbaf676b583cc614afa Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 13 Nov 2023 14:45:38 -0500 Subject: [PATCH] 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. --- Ladybird/Info.plist | 50 ++++++++++++++++++++++++++++++++++++++++++++ Ladybird/Qt/main.cpp | 38 ++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/Ladybird/Info.plist b/Ladybird/Info.plist index ffa29253741..88d08e9093f 100644 --- a/Ladybird/Info.plist +++ b/Ladybird/Info.plist @@ -20,5 +20,55 @@ NSApplication NSHighResolutionCapable True + CFBundleDocumentTypes + + + CFBundleTypeName + Web documents + CFBundleTypeRole + Viewer + CFBundleTypeIconFile + document.icns + LSItemContentTypes + + public.html + public.xhtml + public.svg-image + + + + CFBundleTypeName + Other documents + CFBundleTypeRole + Viewer + CFBundleTypeIconFile + document.icns + CFBundleTypeExtensions + + md + + + + CFBundleURLTypes + + + CFBundleURLName + Web site URL + CFBundleURLSchemes + + http + https + gemini + + + + CFBundleURLName + Local file URL + CFBundleURLSchemes + + file + + + diff --git a/Ladybird/Qt/main.cpp b/Ladybird/Qt/main.cpp index 861d4768923..a72326806ce 100644 --- a/Ladybird/Qt/main.cpp +++ b/Ladybird/Qt/main.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace Ladybird { @@ -54,9 +55,40 @@ static ErrorOr handle_attached_debugger() return {}; } +class LadybirdApplication : public QApplication { +public: + LadybirdApplication(int& argc, char** argv) + : QApplication(argc, argv) + { + } + + Function 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(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 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 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 {