2022-07-03 15:44:58 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2022-07-03 15:44:58 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-03 15:36:07 -03:00
|
|
|
#include <LibMain/Main.h>
|
2024-06-30 01:24:01 -03:00
|
|
|
#include <LibWebView/Application.h>
|
2025-03-15 18:00:58 -03:00
|
|
|
#include <LibWebView/BrowserProcess.h>
|
2023-10-13 10:52:06 -03:00
|
|
|
#include <LibWebView/URL.h>
|
2024-11-10 12:26:07 -03:00
|
|
|
#include <LibWebView/Utilities.h>
|
2024-11-09 14:50:33 -03:00
|
|
|
#include <UI/Qt/Application.h>
|
|
|
|
|
#include <UI/Qt/BrowserWindow.h>
|
|
|
|
|
#include <UI/Qt/Settings.h>
|
2022-07-03 15:36:07 -03:00
|
|
|
|
2026-06-01 05:40:42 -03:00
|
|
|
#include <QCoreApplication>
|
2026-05-31 21:29:15 -03:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
2024-11-23 14:29:18 -03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
|
|
|
|
# include <QStyleHints>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-08-02 14:52:59 -03:00
|
|
|
namespace Ladybird {
|
|
|
|
|
|
2024-07-14 13:29:33 -03:00
|
|
|
// FIXME: Find a place to put this declaration (and other helper functions).
|
|
|
|
|
bool is_using_dark_system_theme(QWidget&);
|
2023-08-02 14:52:59 -03:00
|
|
|
bool is_using_dark_system_theme(QWidget& widget)
|
|
|
|
|
{
|
2024-11-23 14:29:18 -03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
2024-11-24 22:08:22 -03:00
|
|
|
// Use the explicitly set or system default color scheme whenever available
|
2024-11-23 14:29:18 -03:00
|
|
|
auto color_scheme = QGuiApplication::styleHints()->colorScheme();
|
2024-11-24 22:08:22 -03:00
|
|
|
if (color_scheme != Qt::ColorScheme::Unknown)
|
|
|
|
|
return color_scheme == Qt::ColorScheme::Dark;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-11-23 14:29:18 -03:00
|
|
|
// Calculate luma based on Rec. 709 coefficients
|
|
|
|
|
// https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
|
2023-08-02 14:52:59 -03:00
|
|
|
auto color = widget.palette().color(widget.backgroundRole());
|
|
|
|
|
auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
|
|
|
|
|
return luma <= 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-07-03 15:36:07 -03:00
|
|
|
|
2025-07-08 07:14:08 -03:00
|
|
|
ErrorOr<int> ladybird_main(Main::Arguments arguments)
|
2022-07-03 15:36:07 -03:00
|
|
|
{
|
2023-12-11 15:19:41 -03:00
|
|
|
AK::set_rich_debug_enabled(true);
|
|
|
|
|
|
2026-05-31 21:29:15 -03:00
|
|
|
#ifdef AK_OS_MACOS
|
2026-06-01 05:40:42 -03:00
|
|
|
// The web content view is a native QRhiWidget child. Keep it from forcing
|
|
|
|
|
// every sibling in the tab UI to become native as well.
|
|
|
|
|
QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
|
2026-05-31 21:29:15 -03:00
|
|
|
#endif
|
|
|
|
|
|
2025-06-10 20:20:46 -03:00
|
|
|
auto app = TRY(Ladybird::Application::create(arguments));
|
2026-06-21 07:28:23 -03:00
|
|
|
app->initialize_macos_application_menu();
|
2025-06-10 20:20:46 -03:00
|
|
|
WebView::BrowserProcess browser_process;
|
2022-10-24 06:18:22 -03:00
|
|
|
|
2025-06-06 16:42:12 -03:00
|
|
|
WebView::copy_default_config_files(Ladybird::Settings::the()->directory());
|
2024-04-26 18:23:20 -03:00
|
|
|
|
2025-06-06 16:42:12 -03:00
|
|
|
if (auto const& browser_options = Ladybird::Application::browser_options(); !browser_options.headless_mode.has_value()) {
|
|
|
|
|
if (browser_options.force_new_process == WebView::ForceNewProcess::No) {
|
|
|
|
|
auto disposition = TRY(browser_process.connect(browser_options.raw_urls, browser_options.new_window));
|
2024-04-26 18:23:20 -03:00
|
|
|
|
2025-06-06 16:42:12 -03:00
|
|
|
if (disposition == WebView::BrowserProcess::ProcessDisposition::ExitProcess) {
|
|
|
|
|
outln("Opening in existing process");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-13 16:45:38 -03:00
|
|
|
|
2025-06-06 16:42:12 -03:00
|
|
|
app->on_open_file = [&](auto const& file_url) {
|
2026-06-21 07:28:23 -03:00
|
|
|
if (auto* window = app->active_window_if_any()) {
|
|
|
|
|
window->view().load(file_url);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
app->new_window({ file_url });
|
2025-06-06 16:42:12 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
browser_process.on_new_tab = [&](auto const& urls) {
|
2026-06-21 07:28:23 -03:00
|
|
|
if (!app->active_window_if_any()) {
|
|
|
|
|
app->new_window(urls);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& window = *app->active_window_if_any();
|
2025-06-06 16:42:12 -03:00
|
|
|
for (size_t i = 0; i < urls.size(); ++i) {
|
|
|
|
|
window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
|
|
|
|
|
}
|
|
|
|
|
window.show();
|
|
|
|
|
window.activateWindow();
|
|
|
|
|
window.raise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
browser_process.on_new_window = [&](auto const& urls) {
|
2026-06-21 07:28:23 -03:00
|
|
|
Ladybird::WindowConfiguration configuration {};
|
|
|
|
|
if (auto* previous_active_window = app->active_window_if_any()) {
|
|
|
|
|
configuration.width = previous_active_window->width();
|
|
|
|
|
configuration.height = previous_active_window->height();
|
|
|
|
|
configuration.maximized = previous_active_window->isMaximized();
|
|
|
|
|
}
|
2026-05-07 06:57:02 -03:00
|
|
|
app->new_window(urls, configuration);
|
2025-06-06 16:42:12 -03:00
|
|
|
};
|
|
|
|
|
|
2026-05-07 06:57:02 -03:00
|
|
|
auto last_size = Ladybird::Settings::the()->last_size();
|
|
|
|
|
Ladybird::WindowConfiguration configuration {
|
|
|
|
|
.width = last_size.width(),
|
|
|
|
|
.height = last_size.height(),
|
|
|
|
|
.maximized = Ladybird::Settings::the()->is_maximized(),
|
|
|
|
|
};
|
|
|
|
|
if (auto last_position = Ladybird::Settings::the()->last_position(); last_position.has_value()) {
|
|
|
|
|
configuration.x = last_position->x();
|
|
|
|
|
configuration.y = last_position->y();
|
2025-06-06 16:42:12 -03:00
|
|
|
}
|
2026-05-07 06:57:02 -03:00
|
|
|
auto& window = app->new_window(browser_options.urls, configuration);
|
|
|
|
|
window.setWindowTitle("Ladybird");
|
2025-06-06 16:42:12 -03:00
|
|
|
}
|
2022-07-03 15:36:07 -03:00
|
|
|
|
2024-07-30 15:01:05 -03:00
|
|
|
return app->execute();
|
2022-07-03 15:36:07 -03:00
|
|
|
}
|