ConsoleWidget.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ConsoleWidget.h"
  10. #include "StringUtils.h"
  11. #include "WebContentView.h"
  12. #include <LibWebView/ConsoleClient.h>
  13. #include <QFontDatabase>
  14. #include <QKeyEvent>
  15. #include <QLineEdit>
  16. #include <QPushButton>
  17. #include <QVBoxLayout>
  18. namespace Ladybird {
  19. bool is_using_dark_system_theme(QWidget&);
  20. ConsoleWidget::ConsoleWidget(WebContentView& content_view)
  21. {
  22. setLayout(new QVBoxLayout);
  23. m_output_view = new WebContentView({}, WebView::EnableCallgrindProfiling::No, UseLagomNetworking::No, WebView::EnableGPUPainting::No);
  24. if (is_using_dark_system_theme(*this))
  25. m_output_view->update_palette(WebContentView::PaletteMode::Dark);
  26. m_console_client = make<WebView::ConsoleClient>(content_view, *m_output_view);
  27. layout()->addWidget(m_output_view);
  28. auto* bottom_container = new QWidget(this);
  29. bottom_container->setLayout(new QHBoxLayout);
  30. layout()->addWidget(bottom_container);
  31. m_input = new ConsoleInputEdit(bottom_container, *this);
  32. m_input->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
  33. bottom_container->layout()->addWidget(m_input);
  34. setFocusProxy(m_input);
  35. auto* clear_button = new QPushButton(bottom_container);
  36. bottom_container->layout()->addWidget(clear_button);
  37. clear_button->setFixedSize(22, 22);
  38. clear_button->setText("X");
  39. clear_button->setToolTip("Clear the console output");
  40. QObject::connect(clear_button, &QPushButton::pressed, [this] {
  41. client().clear();
  42. });
  43. m_input->setFocus();
  44. }
  45. ConsoleWidget::~ConsoleWidget() = default;
  46. Optional<String> ConsoleWidget::previous_history_item()
  47. {
  48. return m_console_client->previous_history_item();
  49. }
  50. Optional<String> ConsoleWidget::next_history_item()
  51. {
  52. return m_console_client->next_history_item();
  53. }
  54. void ConsoleWidget::reset()
  55. {
  56. m_console_client->reset();
  57. }
  58. void ConsoleInputEdit::keyPressEvent(QKeyEvent* event)
  59. {
  60. switch (event->key()) {
  61. case Qt::Key_Up:
  62. if (auto script = m_console_widget.previous_history_item(); script.has_value())
  63. setText(qstring_from_ak_string(*script));
  64. break;
  65. case Qt::Key_Down:
  66. if (auto script = m_console_widget.next_history_item(); script.has_value())
  67. setText(qstring_from_ak_string(*script));
  68. break;
  69. case Qt::Key_Return: {
  70. auto js_source = MUST(ak_string_from_qstring(text()));
  71. if (js_source.bytes_as_string_view().is_whitespace())
  72. return;
  73. m_console_widget.client().execute(std::move(js_source));
  74. clear();
  75. break;
  76. }
  77. default:
  78. QLineEdit::keyPressEvent(event);
  79. }
  80. }
  81. }