ConsoleWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ConsoleWidget.h"
  10. #include <AK/StringBuilder.h>
  11. #include <Applications/Browser/Browser.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/TextBox.h>
  15. #include <LibGfx/Font/FontDatabase.h>
  16. #include <LibJS/MarkupGenerator.h>
  17. #include <LibJS/SyntaxHighlighter.h>
  18. namespace Browser {
  19. ConsoleWidget::ConsoleWidget()
  20. {
  21. set_layout<GUI::VerticalBoxLayout>();
  22. set_fill_with_background_color(true);
  23. m_output_view = add<WebView::OutOfProcessWebView>();
  24. m_output_view->load("data:text/html,<html></html>"sv);
  25. // Wait until our output WebView is loaded, and then request any messages that occurred before we existed
  26. m_output_view->on_load_finish = [this](auto&) {
  27. if (on_request_messages)
  28. on_request_messages(0);
  29. };
  30. auto& bottom_container = add<GUI::Widget>();
  31. bottom_container.set_layout<GUI::HorizontalBoxLayout>();
  32. bottom_container.set_fixed_height(22);
  33. m_input = bottom_container.add<GUI::TextBox>();
  34. m_input->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  35. // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
  36. m_input->set_font(Gfx::FontDatabase::default_fixed_width_font());
  37. m_input->set_history_enabled(true);
  38. m_input->on_return_pressed = [this] {
  39. auto js_source = m_input->text();
  40. if (js_source.is_whitespace())
  41. return;
  42. m_input->add_current_text_to_history();
  43. m_input->clear();
  44. print_source_line(js_source);
  45. if (on_js_input)
  46. on_js_input(js_source);
  47. };
  48. set_focus_proxy(m_input);
  49. auto& clear_button = bottom_container.add<GUI::Button>();
  50. clear_button.set_fixed_size(22, 22);
  51. clear_button.set_icon(g_icon_bag.delete_icon);
  52. clear_button.set_tooltip("Clear the console output");
  53. clear_button.on_click = [this](auto) {
  54. clear_output();
  55. };
  56. }
  57. void ConsoleWidget::request_console_messages()
  58. {
  59. VERIFY(!m_waiting_for_messages);
  60. VERIFY(on_request_messages);
  61. on_request_messages(m_highest_received_message_index + 1);
  62. m_waiting_for_messages = true;
  63. }
  64. void ConsoleWidget::notify_about_new_console_message(i32 message_index)
  65. {
  66. if (message_index <= m_highest_received_message_index) {
  67. dbgln("Notified about console message we already have");
  68. return;
  69. }
  70. if (message_index <= m_highest_notified_message_index) {
  71. dbgln("Notified about console message we're already aware of");
  72. return;
  73. }
  74. m_highest_notified_message_index = message_index;
  75. if (!m_waiting_for_messages)
  76. request_console_messages();
  77. }
  78. void ConsoleWidget::handle_console_messages(i32 start_index, Vector<String> const& message_types, Vector<String> const& messages)
  79. {
  80. i32 end_index = start_index + message_types.size() - 1;
  81. if (end_index <= m_highest_received_message_index) {
  82. dbgln("Received old console messages");
  83. return;
  84. }
  85. for (size_t i = 0; i < message_types.size(); i++) {
  86. auto& type = message_types[i];
  87. auto& message = messages[i];
  88. if (type == "html") {
  89. print_html(message);
  90. } else if (type == "clear") {
  91. clear_output();
  92. } else if (type == "group") {
  93. begin_group(message, true);
  94. } else if (type == "groupCollapsed") {
  95. begin_group(message, false);
  96. } else if (type == "groupEnd") {
  97. end_group();
  98. } else {
  99. VERIFY_NOT_REACHED();
  100. }
  101. }
  102. m_highest_received_message_index = end_index;
  103. m_waiting_for_messages = false;
  104. if (m_highest_received_message_index < m_highest_notified_message_index)
  105. request_console_messages();
  106. }
  107. void ConsoleWidget::print_source_line(StringView source)
  108. {
  109. StringBuilder html;
  110. html.append("<span class=\"repl-indicator\">"sv);
  111. html.append("&gt; "sv);
  112. html.append("</span>"sv);
  113. html.append(JS::MarkupGenerator::html_from_source(source));
  114. print_html(html.string_view());
  115. }
  116. void ConsoleWidget::print_html(StringView line)
  117. {
  118. StringBuilder builder;
  119. int parent_id = m_group_stack.is_empty() ? 0 : m_group_stack.last().id;
  120. if (parent_id == 0) {
  121. builder.append(R"~~~(
  122. var parentGroup = document.body;
  123. )~~~"sv);
  124. } else {
  125. builder.appendff(R"~~~(
  126. var parentGroup = document.getElementById("group_{}");
  127. )~~~",
  128. parent_id);
  129. }
  130. builder.append(R"~~~(
  131. var p = document.createElement("p");
  132. p.innerHTML = ")~~~"sv);
  133. builder.append_escaped_for_json(line);
  134. builder.append(R"~~~("
  135. parentGroup.appendChild(p);
  136. )~~~"sv);
  137. m_output_view->run_javascript(builder.string_view());
  138. // FIXME: Make it scroll to the bottom, using `window.scrollTo()` in the JS above.
  139. // We used to call `m_output_view->scroll_to_bottom();` here, but that does not work because
  140. // it runs synchronously, meaning it happens before the HTML is output via IPC above.
  141. // (See also: begin_group())
  142. }
  143. void ConsoleWidget::clear_output()
  144. {
  145. m_group_stack.clear();
  146. m_output_view->run_javascript(R"~~~(
  147. document.body.innerHTML = "";
  148. )~~~"sv);
  149. }
  150. void ConsoleWidget::begin_group(StringView label, bool start_expanded)
  151. {
  152. StringBuilder builder;
  153. int parent_id = m_group_stack.is_empty() ? 0 : m_group_stack.last().id;
  154. if (parent_id == 0) {
  155. builder.append(R"~~~(
  156. var parentGroup = document.body;
  157. )~~~"sv);
  158. } else {
  159. builder.appendff(R"~~~(
  160. var parentGroup = document.getElementById("group_{}");
  161. )~~~",
  162. parent_id);
  163. }
  164. Group group;
  165. group.id = m_next_group_id++;
  166. group.label = label;
  167. builder.appendff(R"~~~(
  168. var group = document.createElement("details");
  169. group.id = "group_{}";
  170. var label = document.createElement("summary");
  171. label.innerHTML = ")~~~",
  172. group.id);
  173. builder.append_escaped_for_json(label);
  174. builder.append(R"~~~(";
  175. group.appendChild(label);
  176. parentGroup.appendChild(group);
  177. )~~~"sv);
  178. if (start_expanded)
  179. builder.append("group.open = true;"sv);
  180. m_output_view->run_javascript(builder.string_view());
  181. // FIXME: Scroll console to bottom - see note in print_html()
  182. m_group_stack.append(group);
  183. }
  184. void ConsoleWidget::end_group()
  185. {
  186. m_group_stack.take_last();
  187. }
  188. void ConsoleWidget::reset()
  189. {
  190. clear_output();
  191. m_highest_notified_message_index = -1;
  192. m_highest_received_message_index = -1;
  193. m_waiting_for_messages = false;
  194. }
  195. }