1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 11:55:12 +00:00
serenity/Ladybird/Qt/ConsoleWidget.cpp
Timothy Flynn bf59e06d2a Ladybird+LibWebView: Extract common JS console functionality to a helper
This creates WebView::ConsoleClient to handle functionality that will be
common to the JS consoles of all Ladybird chromes. This will let each
chrome focus on just the UI.

Note that this includes the `console.group` functionality that only the
Serenity chrome previously had. This was a FIXME in the Qt chrome, and
it is implemented such that all chromes will receive this functionality
for free.
2023-08-30 09:24:59 +02:00

111 lines
3 KiB
C++

/*
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ConsoleWidget.h"
#include "StringUtils.h"
#include "WebContentView.h"
#include <LibWebView/ConsoleClient.h>
#include <QFontDatabase>
#include <QKeyEvent>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
namespace Ladybird {
bool is_using_dark_system_theme(QWidget&);
ConsoleWidget::ConsoleWidget(WebContentView& content_view)
{
setLayout(new QVBoxLayout);
m_output_view = new WebContentView({}, WebView::EnableCallgrindProfiling::No, UseLagomNetworking::No);
if (is_using_dark_system_theme(*this))
m_output_view->update_palette(WebContentView::PaletteMode::Dark);
m_console_client = make<WebView::ConsoleClient>(content_view, *m_output_view);
layout()->addWidget(m_output_view);
auto* bottom_container = new QWidget(this);
bottom_container->setLayout(new QHBoxLayout);
layout()->addWidget(bottom_container);
m_input = new ConsoleInputEdit(bottom_container, *this);
m_input->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
bottom_container->layout()->addWidget(m_input);
setFocusProxy(m_input);
auto* clear_button = new QPushButton(bottom_container);
bottom_container->layout()->addWidget(clear_button);
clear_button->setFixedSize(22, 22);
clear_button->setText("X");
clear_button->setToolTip("Clear the console output");
QObject::connect(clear_button, &QPushButton::pressed, [this] {
client().clear();
});
m_input->setFocus();
}
ConsoleWidget::~ConsoleWidget() = default;
void ConsoleWidget::reset()
{
m_console_client->reset();
}
void ConsoleInputEdit::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_Down: {
if (m_history.is_empty())
break;
auto last_index = m_history.size() - 1;
if (m_history_index < last_index) {
m_history_index++;
setText(qstring_from_ak_deprecated_string(m_history.at(m_history_index)));
} else if (m_history_index == last_index) {
m_history_index++;
clear();
}
break;
}
case Qt::Key_Up:
if (m_history_index > 0) {
m_history_index--;
setText(qstring_from_ak_deprecated_string(m_history.at(m_history_index)));
}
break;
case Qt::Key_Return: {
auto js_source = ak_deprecated_string_from_qstring(text());
if (js_source.is_whitespace())
return;
if (m_history.is_empty() || m_history.last() != js_source) {
m_history.append(js_source);
m_history_index = m_history.size();
}
m_console_widget.client().execute(js_source);
clear();
break;
}
default:
QLineEdit::keyPressEvent(event);
}
}
}