ConsoleWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "ConsoleWidget.h"
  9. #include <AK/StringBuilder.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/TextBox.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibJS/MarkupGenerator.h>
  15. #include <LibJS/SyntaxHighlighter.h>
  16. namespace Browser {
  17. ConsoleWidget::ConsoleWidget()
  18. {
  19. set_layout<GUI::VerticalBoxLayout>();
  20. set_fill_with_background_color(true);
  21. m_output_view = add<Web::OutOfProcessWebView>();
  22. m_output_view->load("data:text/html,<html></html>");
  23. // Wait until our output WebView is loaded, and then request any messages that occurred before we existed
  24. m_output_view->on_load_finish = [this](auto&) {
  25. if (on_request_messages)
  26. on_request_messages(0);
  27. };
  28. auto& bottom_container = add<GUI::Widget>();
  29. bottom_container.set_layout<GUI::HorizontalBoxLayout>();
  30. bottom_container.set_fixed_height(22);
  31. m_input = bottom_container.add<GUI::TextBox>();
  32. m_input->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  33. // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
  34. m_input->set_font(Gfx::FontDatabase::default_fixed_width_font());
  35. m_input->set_history_enabled(true);
  36. m_input->on_return_pressed = [this] {
  37. auto js_source = m_input->text();
  38. // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
  39. if (js_source.is_empty())
  40. return;
  41. m_input->add_current_text_to_history();
  42. m_input->clear();
  43. print_source_line(js_source);
  44. if (on_js_input)
  45. on_js_input(js_source);
  46. };
  47. set_focus_proxy(m_input);
  48. auto& clear_button = bottom_container.add<GUI::Button>();
  49. clear_button.set_fixed_size(22, 22);
  50. clear_button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png").release_value_but_fixme_should_propagate_errors());
  51. clear_button.set_tooltip("Clear the console output");
  52. clear_button.on_click = [this](auto) {
  53. clear_output();
  54. };
  55. }
  56. ConsoleWidget::~ConsoleWidget()
  57. {
  58. }
  59. void ConsoleWidget::request_console_messages()
  60. {
  61. VERIFY(!m_waiting_for_messages);
  62. VERIFY(on_request_messages);
  63. on_request_messages(m_highest_received_message_index + 1);
  64. m_waiting_for_messages = true;
  65. }
  66. void ConsoleWidget::notify_about_new_console_message(i32 message_index)
  67. {
  68. if (message_index <= m_highest_received_message_index) {
  69. dbgln("Notified about console message we already have");
  70. return;
  71. }
  72. if (message_index <= m_highest_notified_message_index) {
  73. dbgln("Notified about console message we're already aware of");
  74. return;
  75. }
  76. m_highest_notified_message_index = message_index;
  77. if (!m_waiting_for_messages)
  78. request_console_messages();
  79. }
  80. void ConsoleWidget::handle_console_messages(i32 start_index, const Vector<String>& message_types, const Vector<String>& messages)
  81. {
  82. i32 end_index = start_index + message_types.size() - 1;
  83. if (end_index <= m_highest_received_message_index) {
  84. dbgln("Received old console messages");
  85. return;
  86. }
  87. for (size_t i = 0; i < message_types.size(); i++) {
  88. auto& type = message_types[i];
  89. auto& message = messages[i];
  90. if (type == "html") {
  91. print_html(message);
  92. } else if (type == "clear") {
  93. clear_output();
  94. } else {
  95. VERIFY_NOT_REACHED();
  96. }
  97. }
  98. m_highest_received_message_index = end_index;
  99. m_waiting_for_messages = false;
  100. if (m_highest_received_message_index < m_highest_notified_message_index)
  101. request_console_messages();
  102. }
  103. void ConsoleWidget::print_source_line(const StringView& source)
  104. {
  105. StringBuilder html;
  106. html.append("<span class=\"repl-indicator\">");
  107. html.append("&gt; ");
  108. html.append("</span>");
  109. html.append(JS::MarkupGenerator::html_from_source(source));
  110. print_html(html.string_view());
  111. }
  112. void ConsoleWidget::print_html(StringView const& line)
  113. {
  114. StringBuilder builder;
  115. builder.append(R"~~~(
  116. var p = document.createElement("p");
  117. p.innerHTML = ")~~~");
  118. builder.append_escaped_for_json(line);
  119. builder.append(R"~~~("
  120. document.body.appendChild(p);
  121. )~~~");
  122. m_output_view->run_javascript(builder.string_view());
  123. // FIXME: Make it scroll to the bottom, using `window.scrollTo()` in the JS above.
  124. // We used to call `m_output_view->scroll_to_bottom();` here, but that does not work because
  125. // it runs synchronously, meaning it happens before the HTML is output via IPC above.
  126. }
  127. void ConsoleWidget::clear_output()
  128. {
  129. m_output_view->run_javascript(R"~~~(
  130. document.body.innerHTML = "";
  131. )~~~");
  132. }
  133. void ConsoleWidget::reset()
  134. {
  135. clear_output();
  136. m_highest_notified_message_index = -1;
  137. m_highest_received_message_index = -1;
  138. m_waiting_for_messages = false;
  139. }
  140. }