ladybird/Ladybird/Qt/ConsoleWidget.cpp

112 lines
3 KiB
C++
Raw Normal View History

/*
* 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);
}
}
}