ConsoleWidget.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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>");
  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. // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
  41. if (js_source.is_empty())
  42. return;
  43. m_input->add_current_text_to_history();
  44. m_input->clear();
  45. print_source_line(js_source);
  46. if (on_js_input)
  47. on_js_input(js_source);
  48. };
  49. set_focus_proxy(m_input);
  50. auto& clear_button = bottom_container.add<GUI::Button>();
  51. clear_button.set_fixed_size(22, 22);
  52. clear_button.set_icon(g_icon_bag.delete_icon);
  53. clear_button.set_tooltip("Clear the console output");
  54. clear_button.on_click = [this](auto) {
  55. clear_output();
  56. };
  57. }
  58. void ConsoleWidget::request_console_messages()
  59. {
  60. VERIFY(!m_waiting_for_messages);
  61. VERIFY(on_request_messages);
  62. on_request_messages(m_highest_received_message_index + 1);
  63. m_waiting_for_messages = true;
  64. }
  65. void ConsoleWidget::notify_about_new_console_message(i32 message_index)
  66. {
  67. if (message_index <= m_highest_received_message_index) {
  68. dbgln("Notified about console message we already have");
  69. return;
  70. }
  71. if (message_index <= m_highest_notified_message_index) {
  72. dbgln("Notified about console message we're already aware of");
  73. return;
  74. }
  75. m_highest_notified_message_index = message_index;
  76. if (!m_waiting_for_messages)
  77. request_console_messages();
  78. }
  79. void ConsoleWidget::handle_console_messages(i32 start_index, Vector<String> const& message_types, Vector<String> const& messages)
  80. {
  81. i32 end_index = start_index + message_types.size() - 1;
  82. if (end_index <= m_highest_received_message_index) {
  83. dbgln("Received old console messages");
  84. return;
  85. }
  86. for (size_t i = 0; i < message_types.size(); i++) {
  87. auto& type = message_types[i];
  88. auto& message = messages[i];
  89. if (type == "html") {
  90. print_html(message);
  91. } else if (type == "clear") {
  92. clear_output();
  93. } else if (type == "group") {
  94. begin_group(message, true);
  95. } else if (type == "groupCollapsed") {
  96. begin_group(message, false);
  97. } else if (type == "groupEnd") {
  98. end_group();
  99. } else {
  100. VERIFY_NOT_REACHED();
  101. }
  102. }
  103. m_highest_received_message_index = end_index;
  104. m_waiting_for_messages = false;
  105. if (m_highest_received_message_index < m_highest_notified_message_index)
  106. request_console_messages();
  107. }
  108. void ConsoleWidget::print_source_line(StringView source)
  109. {
  110. StringBuilder html;
  111. html.append("<span class=\"repl-indicator\">");
  112. html.append("&gt; ");
  113. html.append("</span>");
  114. html.append(JS::MarkupGenerator::html_from_source(source));
  115. print_html(html.string_view());
  116. }
  117. void ConsoleWidget::print_html(StringView line)
  118. {
  119. StringBuilder builder;
  120. int parent_id = m_group_stack.is_empty() ? 0 : m_group_stack.last().id;
  121. if (parent_id == 0) {
  122. builder.append(R"~~~(
  123. var parentGroup = document.body;
  124. )~~~");
  125. } else {
  126. builder.appendff(R"~~~(
  127. var parentGroup = document.getElementById("group_{}");
  128. )~~~",
  129. parent_id);
  130. }
  131. builder.append(R"~~~(
  132. var p = document.createElement("p");
  133. p.innerHTML = ")~~~");
  134. builder.append_escaped_for_json(line);
  135. builder.append(R"~~~("
  136. parentGroup.appendChild(p);
  137. )~~~");
  138. m_output_view->run_javascript(builder.string_view());
  139. // FIXME: Make it scroll to the bottom, using `window.scrollTo()` in the JS above.
  140. // We used to call `m_output_view->scroll_to_bottom();` here, but that does not work because
  141. // it runs synchronously, meaning it happens before the HTML is output via IPC above.
  142. // (See also: begin_group())
  143. }
  144. void ConsoleWidget::clear_output()
  145. {
  146. m_group_stack.clear();
  147. m_output_view->run_javascript(R"~~~(
  148. document.body.innerHTML = "";
  149. )~~~");
  150. }
  151. void ConsoleWidget::begin_group(StringView label, bool start_expanded)
  152. {
  153. StringBuilder builder;
  154. int parent_id = m_group_stack.is_empty() ? 0 : m_group_stack.last().id;
  155. if (parent_id == 0) {
  156. builder.append(R"~~~(
  157. var parentGroup = document.body;
  158. )~~~");
  159. } else {
  160. builder.appendff(R"~~~(
  161. var parentGroup = document.getElementById("group_{}");
  162. )~~~",
  163. parent_id);
  164. }
  165. Group group;
  166. group.id = m_next_group_id++;
  167. group.label = label;
  168. builder.appendff(R"~~~(
  169. var group = document.createElement("details");
  170. group.id = "group_{}";
  171. var label = document.createElement("summary");
  172. label.innerText = ")~~~",
  173. group.id);
  174. builder.append_escaped_for_json(label);
  175. builder.append(R"~~~(";
  176. group.appendChild(label);
  177. parentGroup.appendChild(group);
  178. )~~~");
  179. if (start_expanded)
  180. builder.append("group.open = true;");
  181. m_output_view->run_javascript(builder.string_view());
  182. // FIXME: Scroll console to bottom - see note in print_html()
  183. m_group_stack.append(group);
  184. }
  185. void ConsoleWidget::end_group()
  186. {
  187. m_group_stack.take_last();
  188. }
  189. void ConsoleWidget::reset()
  190. {
  191. clear_output();
  192. m_highest_notified_message_index = -1;
  193. m_highest_received_message_index = -1;
  194. m_waiting_for_messages = false;
  195. }
  196. }