From 8b7000e1519e8d1b2028ec9c2460793c2267bbf7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 Jul 2022 21:26:51 +0200 Subject: [PATCH] Ladybird: Add a location bar and allow navigating to new pages :^) --- Ladybird/.gitignore | 7 ++----- Ladybird/BrowserWindow.cpp | 27 +++++++++++++++++++++++++++ Ladybird/BrowserWindow.h | 23 +++++++++++++++++++++++ Ladybird/WebView.cpp | 3 ++- Ladybird/WebView.h | 1 + Ladybird/ladybird.pro | 4 ++-- Ladybird/main.cpp | 13 +++---------- 7 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 Ladybird/BrowserWindow.cpp create mode 100644 Ladybird/BrowserWindow.h diff --git a/Ladybird/.gitignore b/Ladybird/.gitignore index 671819ee4f1..a656279ac76 100644 --- a/Ladybird/.gitignore +++ b/Ladybird/.gitignore @@ -1,8 +1,5 @@ .qmake.stash Makefile -WebView.o ladybird -main.o -moc_WebView.cpp -moc_WebView.o -moc_predefs.h +*.o +moc_* diff --git a/Ladybird/BrowserWindow.cpp b/Ladybird/BrowserWindow.cpp new file mode 100644 index 00000000000..a20ab1ffda6 --- /dev/null +++ b/Ladybird/BrowserWindow.cpp @@ -0,0 +1,27 @@ +#include "BrowserWindow.h" +#include "WebView.h" +#include + +BrowserWindow::BrowserWindow() +{ + m_toolbar = new QToolBar; + m_toolbar->setFixedHeight(28); + m_location_edit = new QLineEdit; + m_toolbar->addWidget(m_location_edit); + + addToolBar(m_toolbar); + + m_view = new WebView; + setCentralWidget(m_view); + + QObject::connect(m_view, &WebView::linkHovered, statusBar(), &QStatusBar::showMessage); + QObject::connect(m_view, &WebView::linkUnhovered, statusBar(), &QStatusBar::clearMessage); + + QObject::connect(m_view, &WebView::loadStarted, m_location_edit, &QLineEdit::setText); + QObject::connect(m_location_edit, &QLineEdit::returnPressed, this, &BrowserWindow::location_edit_return_pressed); +} + +void BrowserWindow::location_edit_return_pressed() +{ + view().load(m_location_edit->text().toUtf8().data()); +} diff --git a/Ladybird/BrowserWindow.h b/Ladybird/BrowserWindow.h new file mode 100644 index 00000000000..bd285f4009d --- /dev/null +++ b/Ladybird/BrowserWindow.h @@ -0,0 +1,23 @@ +#include +#include +#include + +#pragma once + +class WebView; + +class BrowserWindow : public QMainWindow { + Q_OBJECT +public: + BrowserWindow(); + + WebView& view() { return *m_view; } + +public slots: + void location_edit_return_pressed(); + +private: + QToolBar* m_toolbar { nullptr }; + QLineEdit* m_location_edit { nullptr }; + WebView* m_view { nullptr }; +}; diff --git a/Ladybird/WebView.cpp b/Ladybird/WebView.cpp index 3f4805de428..9a06480b206 100644 --- a/Ladybird/WebView.cpp +++ b/Ladybird/WebView.cpp @@ -138,8 +138,9 @@ public: { } - virtual void page_did_start_loading(AK::URL const&) override + virtual void page_did_start_loading(AK::URL const& url) override { + emit m_view.loadStarted(url.to_string().characters()); } virtual void page_did_finish_loading(AK::URL const&) override diff --git a/Ladybird/WebView.h b/Ladybird/WebView.h index 5804d22344b..a72f087049e 100644 --- a/Ladybird/WebView.h +++ b/Ladybird/WebView.h @@ -31,6 +31,7 @@ public: signals: void linkHovered(QString, int timeout = 0); void linkUnhovered(); + void loadStarted(QString); private: OwnPtr m_page_client; diff --git a/Ladybird/ladybird.pro b/Ladybird/ladybird.pro index 571c343de5e..5adbe0db141 100644 --- a/Ladybird/ladybird.pro +++ b/Ladybird/ladybird.pro @@ -20,8 +20,8 @@ INCLUDEPATH += \ #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 # Input -HEADERS += WebView.h -SOURCES += main.cpp WebView.cpp +HEADERS += WebView.h BrowserWindow.h +SOURCES += main.cpp WebView.cpp BrowserWindow.cpp QMAKE_LIBDIR += /home/kling/src/serenity/Build/lagom/ diff --git a/Ladybird/main.cpp b/Ladybird/main.cpp index 2b87ee22c61..6c6e9b157eb 100644 --- a/Ladybird/main.cpp +++ b/Ladybird/main.cpp @@ -4,14 +4,13 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "BrowserWindow.h" #include "WebView.h" #include #include #include #include #include -#include -#include #include extern void initialize_web_engine(); @@ -29,24 +28,18 @@ ErrorOr serenity_main(Main::Arguments arguments) Core::EventLoop event_loop; QApplication app(arguments.argc, arguments.argv); - QMainWindow window; + BrowserWindow window; window.setWindowTitle("Ladybird"); window.resize(800, 600); window.show(); - WebView view; - window.setCentralWidget(&view); - - QObject::connect(&view, &WebView::linkHovered, window.statusBar(), &QStatusBar::showMessage); - QObject::connect(&view, &WebView::linkUnhovered, window.statusBar(), &QStatusBar::clearMessage); - auto qt_event_loop_driver = Core::Timer::create_repeating(50, [&] { app.processEvents(); }); qt_event_loop_driver->start(); if (!url.is_empty()) { - view.load(url); + window.view().load(url); } return event_loop.exec();