2022-10-06 10:39:02 +02:00
|
|
|
/*
|
|
|
|
|
* 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"
|
2023-08-05 10:42:26 -06:00
|
|
|
#include "StringUtils.h"
|
2023-04-22 21:57:08 -04:00
|
|
|
#include "WebContentView.h"
|
2023-08-29 11:44:18 -04:00
|
|
|
#include <LibWebView/ConsoleClient.h>
|
2023-04-26 08:34:24 -04:00
|
|
|
#include <QFontDatabase>
|
2023-07-19 14:27:30 +02:00
|
|
|
#include <QKeyEvent>
|
2022-10-06 10:39:02 +02:00
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
2023-04-29 18:35:06 +02:00
|
|
|
namespace Ladybird {
|
2023-04-22 21:57:08 -04:00
|
|
|
|
2023-08-02 11:52:59 -06:00
|
|
|
bool is_using_dark_system_theme(QWidget&);
|
|
|
|
|
|
2023-08-29 11:44:18 -04:00
|
|
|
ConsoleWidget::ConsoleWidget(WebContentView& content_view)
|
2022-10-06 10:39:02 +02:00
|
|
|
{
|
|
|
|
|
setLayout(new QVBoxLayout);
|
|
|
|
|
|
2023-08-07 16:44:20 +02:00
|
|
|
m_output_view = new WebContentView({}, WebView::EnableCallgrindProfiling::No, UseLagomNetworking::No);
|
2023-04-22 21:57:08 -04:00
|
|
|
if (is_using_dark_system_theme(*this))
|
|
|
|
|
m_output_view->update_palette(WebContentView::PaletteMode::Dark);
|
2022-10-06 10:39:02 +02:00
|
|
|
|
2023-08-29 11:44:18 -04:00
|
|
|
m_console_client = make<WebView::ConsoleClient>(content_view, *m_output_view);
|
2023-04-22 21:57:08 -04:00
|
|
|
|
|
|
|
|
layout()->addWidget(m_output_view);
|
2022-10-06 10:39:02 +02:00
|
|
|
|
|
|
|
|
auto* bottom_container = new QWidget(this);
|
|
|
|
|
bottom_container->setLayout(new QHBoxLayout);
|
|
|
|
|
|
|
|
|
|
layout()->addWidget(bottom_container);
|
|
|
|
|
|
2023-07-19 14:27:30 +02:00
|
|
|
m_input = new ConsoleInputEdit(bottom_container, *this);
|
2023-04-26 08:34:24 -04:00
|
|
|
m_input->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
|
2022-10-06 10:39:02 +02:00
|
|
|
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] {
|
2023-08-29 11:44:18 -04:00
|
|
|
client().clear();
|
2022-10-06 10:39:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_input->setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 11:44:18 -04:00
|
|
|
ConsoleWidget::~ConsoleWidget() = default;
|
2022-10-06 10:39:02 +02:00
|
|
|
|
|
|
|
|
void ConsoleWidget::reset()
|
|
|
|
|
{
|
2023-08-29 11:44:18 -04:00
|
|
|
m_console_client->reset();
|
2022-10-06 10:39:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-19 14:27:30 +02:00
|
|
|
void ConsoleInputEdit::keyPressEvent(QKeyEvent* event)
|
|
|
|
|
{
|
|
|
|
|
switch (event->key()) {
|
|
|
|
|
case Qt::Key_Down: {
|
2023-07-31 23:07:49 +02:00
|
|
|
if (m_history.is_empty())
|
|
|
|
|
break;
|
2023-07-19 14:27:30 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2023-08-29 11:44:18 -04:00
|
|
|
|
2023-07-19 14:27:30 +02:00
|
|
|
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;
|
2023-08-29 11:44:18 -04:00
|
|
|
|
2023-07-19 14:27:30 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 11:44:18 -04:00
|
|
|
m_console_widget.client().execute(js_source);
|
2023-07-19 14:27:30 +02:00
|
|
|
clear();
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-08-29 11:44:18 -04:00
|
|
|
|
2023-07-19 14:27:30 +02:00
|
|
|
default:
|
|
|
|
|
QLineEdit::keyPressEvent(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 10:39:02 +02:00
|
|
|
}
|