Skip to content

Commit

Permalink
Ladybird: Move QApplication class to its own file
Browse files Browse the repository at this point in the history
We'll attach some global data to it in an upcoming commit, so it needs
to be accessible outside of main.cpp.
  • Loading branch information
ADKaster authored and trflynn89 committed Apr 17, 2024
1 parent 5e18d15 commit 336b661
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 34 deletions.
1 change: 1 addition & 0 deletions Ladybird/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ set(LADYBIRD_HEADERS
if (ENABLE_QT)
qt_add_executable(ladybird ${SOURCES})
target_sources(ladybird PRIVATE
Qt/Application.cpp
Qt/AutoComplete.cpp
Qt/BrowserWindow.cpp
Qt/EventLoopImplementationQt.cpp
Expand Down
40 changes: 40 additions & 0 deletions Ladybird/Qt/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024, Andrew Kaster <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "Application.h"
#include "StringUtils.h"
#include <LibWebView/URL.h>
#include <QFileOpenEvent>

namespace Ladybird {

Application::Application(int& argc, char** argv)
: QApplication(argc, argv)
{
}

bool Application::event(QEvent* event)
{
switch (event->type()) {
case QEvent::FileOpen: {
if (!on_open_file)
break;

auto const& open_event = *static_cast<QFileOpenEvent const*>(event);
auto file = 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);
}

}
26 changes: 26 additions & 0 deletions Ladybird/Qt/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024, Andrew Kaster <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/Function.h>
#include <LibURL/URL.h>
#include <QApplication>

namespace Ladybird {

class Application : public QApplication {
Q_OBJECT

public:
Application(int& argc, char** argv);

virtual bool event(QEvent* event) override;

Function<void(URL::URL)> on_open_file;
};

}
36 changes: 2 additions & 34 deletions Ladybird/Qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "Application.h"
#include "BrowserWindow.h"
#include "EventLoopImplementationQt.h"
#include "Settings.h"
Expand All @@ -20,8 +21,6 @@
#include <LibWebView/Database.h>
#include <LibWebView/ProcessManager.h>
#include <LibWebView/URL.h>
#include <QApplication>
#include <QFileOpenEvent>

#if defined(AK_OS_MACOS)
# include <Ladybird/MachPortServer.h>
Expand Down Expand Up @@ -59,42 +58,11 @@ static ErrorOr<void> handle_attached_debugger()
return {};
}

class LadybirdApplication : public QApplication {
public:
LadybirdApplication(int& argc, char** argv)
: QApplication(argc, argv)
{
}

Function<void(URL::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 = 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)
{
AK::set_rich_debug_enabled(true);

LadybirdApplication app(arguments.argc, arguments.argv);
Ladybird::Application app(arguments.argc, arguments.argv);

Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
Core::EventLoop event_loop;
Expand Down
2 changes: 2 additions & 0 deletions Meta/gn/secondary/Ladybird/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ group("Ladybird") {

moc_qt_objects("generate_moc") {
sources = [
"Qt/Application.h",
"Qt/AutoComplete.h",
"Qt/BrowserWindow.h",
"Qt/EventLoopImplementationQtEventTarget.h",
Expand Down Expand Up @@ -85,6 +86,7 @@ executable("ladybird_executable") {
configs += [ ":ladybird_qt_components" ]

sources += [
"Qt/Application.cpp",
"Qt/AutoComplete.cpp",
"Qt/BrowserWindow.cpp",
"Qt/EventLoopImplementationQt.cpp",
Expand Down

0 comments on commit 336b661

Please sign in to comment.