mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 01:41:59 -05:00
Ladybird+LibWebView: Move console history tracking to ConsoleClient
This will allow other chromes to make use of history in their console implementations.
This commit is contained in:
parent
ed315dd950
commit
204a6f9241
6 changed files with 69 additions and 34 deletions
|
@ -141,7 +141,7 @@ static constexpr CGFloat const WINDOW_HEIGHT = 600;
|
|||
auto script = Ladybird::ns_string_to_string(ns_script);
|
||||
|
||||
if (!script.bytes_as_string_view().is_whitespace()) {
|
||||
m_console_client->execute(script);
|
||||
m_console_client->execute(move(script));
|
||||
[text_view setString:@""];
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,16 @@ ConsoleWidget::ConsoleWidget(WebContentView& content_view)
|
|||
|
||||
ConsoleWidget::~ConsoleWidget() = default;
|
||||
|
||||
Optional<String> ConsoleWidget::previous_history_item()
|
||||
{
|
||||
return m_console_client->previous_history_item();
|
||||
}
|
||||
|
||||
Optional<String> ConsoleWidget::next_history_item()
|
||||
{
|
||||
return m_console_client->next_history_item();
|
||||
}
|
||||
|
||||
void ConsoleWidget::reset()
|
||||
{
|
||||
m_console_client->reset();
|
||||
|
@ -66,38 +76,22 @@ void ConsoleWidget::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)));
|
||||
}
|
||||
if (auto script = m_console_widget.previous_history_item(); script.has_value())
|
||||
setText(qstring_from_ak_string(*script));
|
||||
break;
|
||||
|
||||
case Qt::Key_Down:
|
||||
if (auto script = m_console_widget.next_history_item(); script.has_value())
|
||||
setText(qstring_from_ak_string(*script));
|
||||
break;
|
||||
|
||||
case Qt::Key_Return: {
|
||||
auto js_source = ak_deprecated_string_from_qstring(text());
|
||||
if (js_source.is_whitespace())
|
||||
auto js_source = MUST(ak_string_from_qstring(text()));
|
||||
if (js_source.bytes_as_string_view().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);
|
||||
m_console_widget.client().execute(std::move(js_source));
|
||||
clear();
|
||||
|
||||
break;
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibWebView/Forward.h>
|
||||
#include <QLineEdit>
|
||||
#include <QWidget>
|
||||
|
@ -26,6 +25,9 @@ public:
|
|||
explicit ConsoleWidget(WebContentView& content_view);
|
||||
virtual ~ConsoleWidget();
|
||||
|
||||
Optional<String> previous_history_item();
|
||||
Optional<String> next_history_item();
|
||||
|
||||
WebView::ConsoleClient& client() { return *m_console_client; }
|
||||
WebContentView& view() { return *m_output_view; }
|
||||
|
||||
|
@ -51,8 +53,6 @@ private:
|
|||
virtual void keyPressEvent(QKeyEvent* event) override;
|
||||
|
||||
ConsoleWidget& m_console_widget;
|
||||
Vector<DeprecatedString> m_history;
|
||||
size_t m_history_index { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ ConsoleWidget::ConsoleWidget(WebView::OutOfProcessWebView& content_view)
|
|||
m_input->add_current_text_to_history();
|
||||
m_input->clear();
|
||||
|
||||
m_console_client->execute(js_source);
|
||||
m_console_client->execute(MUST(String::from_deprecated_string(js_source)));
|
||||
};
|
||||
|
||||
set_focus_proxy(m_input);
|
||||
|
|
|
@ -47,10 +47,44 @@ ConsoleClient::~ConsoleClient()
|
|||
m_content_web_view.on_received_console_messages = nullptr;
|
||||
}
|
||||
|
||||
void ConsoleClient::execute(StringView script)
|
||||
void ConsoleClient::execute(String script)
|
||||
{
|
||||
print_source(script);
|
||||
m_content_web_view.js_console_input(script);
|
||||
m_content_web_view.js_console_input(script.to_deprecated_string());
|
||||
|
||||
if (m_history.is_empty() || m_history.last() != script) {
|
||||
m_history.append(move(script));
|
||||
m_history_index = m_history.size();
|
||||
}
|
||||
}
|
||||
|
||||
Optional<String> ConsoleClient::previous_history_item()
|
||||
{
|
||||
if (m_history_index == 0)
|
||||
return {};
|
||||
|
||||
--m_history_index;
|
||||
return m_history.at(m_history_index);
|
||||
}
|
||||
|
||||
Optional<String> ConsoleClient::next_history_item()
|
||||
{
|
||||
if (m_history.is_empty())
|
||||
return {};
|
||||
|
||||
auto last_index = m_history.size() - 1;
|
||||
|
||||
if (m_history_index < last_index) {
|
||||
++m_history_index;
|
||||
return m_history.at(m_history_index);
|
||||
}
|
||||
|
||||
if (m_history_index == last_index) {
|
||||
++m_history_index;
|
||||
return String {};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void ConsoleClient::clear()
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibWebView/Forward.h>
|
||||
|
@ -20,7 +21,10 @@ public:
|
|||
explicit ConsoleClient(ViewImplementation& content_web_view, ViewImplementation& console_web_view);
|
||||
~ConsoleClient();
|
||||
|
||||
void execute(StringView);
|
||||
void execute(String);
|
||||
|
||||
Optional<String> previous_history_item();
|
||||
Optional<String> next_history_item();
|
||||
|
||||
void clear();
|
||||
void reset();
|
||||
|
@ -50,6 +54,9 @@ private:
|
|||
};
|
||||
Vector<Group> m_group_stack;
|
||||
int m_next_group_id { 1 };
|
||||
|
||||
Vector<String> m_history;
|
||||
size_t m_history_index { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue