Tab.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Tab.h"
  27. #include "BookmarksBarWidget.h"
  28. #include "ConsoleWidget.h"
  29. #include "DownloadWidget.h"
  30. #include "InspectorWidget.h"
  31. #include "WindowActions.h"
  32. #include <AK/StringBuilder.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/Button.h>
  37. #include <LibGUI/Clipboard.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/MenuBar.h>
  40. #include <LibGUI/StatusBar.h>
  41. #include <LibGUI/TabWidget.h>
  42. #include <LibGUI/TextBox.h>
  43. #include <LibGUI/ToolBar.h>
  44. #include <LibGUI/ToolBarContainer.h>
  45. #include <LibGUI/Window.h>
  46. #include <LibJS/Interpreter.h>
  47. #include <LibWeb/DOM/Element.h>
  48. #include <LibWeb/DOMTreeModel.h>
  49. #include <LibWeb/Dump.h>
  50. #include <LibWeb/Frame.h>
  51. #include <LibWeb/HtmlView.h>
  52. #include <LibWeb/Layout/LayoutBlock.h>
  53. #include <LibWeb/Layout/LayoutDocument.h>
  54. #include <LibWeb/Layout/LayoutInline.h>
  55. #include <LibWeb/Layout/LayoutNode.h>
  56. #include <LibWeb/Parser/CSSParser.h>
  57. #include <LibWeb/ResourceLoader.h>
  58. namespace Browser {
  59. extern bool g_use_new_html_parser;
  60. extern String g_home_url;
  61. Tab::Tab()
  62. {
  63. auto& widget = *this;
  64. set_layout<GUI::VerticalBoxLayout>();
  65. m_toolbar_container = widget.add<GUI::ToolBarContainer>();
  66. auto& toolbar = m_toolbar_container->add<GUI::ToolBar>();
  67. m_html_widget = widget.add<Web::HtmlView>();
  68. m_html_widget->set_use_new_parser(g_use_new_html_parser);
  69. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) {
  70. m_history.go_back();
  71. update_actions();
  72. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  73. m_html_widget->load(m_history.current());
  74. }, this);
  75. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
  76. m_history.go_forward();
  77. update_actions();
  78. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  79. m_html_widget->load(m_history.current());
  80. }, this);
  81. toolbar.add_action(*m_go_back_action);
  82. toolbar.add_action(*m_go_forward_action);
  83. toolbar.add_action(GUI::CommonActions::make_go_home_action([this](auto&) {
  84. m_html_widget->load(g_home_url);
  85. }, this));
  86. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) {
  87. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  88. m_html_widget->reload();
  89. }, this);
  90. toolbar.add_action(*m_reload_action);
  91. m_location_box = toolbar.add<GUI::TextBox>();
  92. m_location_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  93. m_location_box->set_preferred_size(0, 22);
  94. m_location_box->on_return_pressed = [this] {
  95. String location = m_location_box->text();
  96. if (!URL(location).is_valid()) {
  97. StringBuilder builder;
  98. builder.append("http://");
  99. builder.append(location);
  100. location = builder.build();
  101. }
  102. m_html_widget->load(location);
  103. m_location_box->set_focus(false);
  104. };
  105. m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste & Go", [this](auto&) {
  106. m_location_box->set_text(GUI::Clipboard::the().data());
  107. m_location_box->on_return_pressed();
  108. }));
  109. m_bookmark_button = toolbar.add<GUI::Button>();
  110. m_bookmark_button->set_button_style(Gfx::ButtonStyle::CoolBar);
  111. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-contour.png"));
  112. m_bookmark_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  113. m_bookmark_button->set_preferred_size(22, 22);
  114. m_bookmark_button->on_click = [this](auto) {
  115. auto url = m_html_widget->main_frame().document()->url().to_string();
  116. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  117. BookmarksBarWidget::the().remove_bookmark(url);
  118. } else {
  119. BookmarksBarWidget::the().add_bookmark(url, m_title);
  120. }
  121. update_bookmark_button(url);
  122. };
  123. m_html_widget->on_load_start = [this](auto& url) {
  124. m_location_box->set_text(url.to_string());
  125. if (m_should_push_loads_to_history)
  126. m_history.push(url);
  127. update_actions();
  128. update_bookmark_button(url.to_string());
  129. };
  130. m_html_widget->on_link_click = [this](auto& href, auto& target, unsigned modifiers) {
  131. if (target == "_blank" || modifiers == Mod_Ctrl) {
  132. auto url = m_html_widget->document()->complete_url(href);
  133. on_tab_open_request(url);
  134. } else {
  135. if (href.starts_with("#")) {
  136. auto anchor = href.substring_view(1, href.length() - 1);
  137. m_html_widget->scroll_to_anchor(anchor);
  138. } else {
  139. auto url = m_html_widget->document()->complete_url(href);
  140. m_html_widget->load(url);
  141. }
  142. }
  143. };
  144. m_link_context_menu = GUI::Menu::construct();
  145. m_link_context_menu->add_action(GUI::Action::create("Open", [this](auto&) {
  146. m_html_widget->on_link_click(m_link_context_menu_href, "", 0);
  147. }));
  148. m_link_context_menu->add_action(GUI::Action::create("Open in new tab", [this](auto&) {
  149. m_html_widget->on_link_click(m_link_context_menu_href, "_blank", 0);
  150. }));
  151. m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) {
  152. GUI::Clipboard::the().set_data(m_html_widget->document()->complete_url(m_link_context_menu_href).to_string());
  153. }));
  154. m_link_context_menu->add_separator();
  155. m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {
  156. auto window = GUI::Window::construct();
  157. window->set_rect(300, 300, 300, 150);
  158. auto url = m_html_widget->document()->complete_url(m_link_context_menu_href);
  159. window->set_title(String::format("0%% of %s", url.basename().characters()));
  160. window->set_resizable(false);
  161. window->set_main_widget<DownloadWidget>(url);
  162. window->show();
  163. (void)window.leak_ref();
  164. }));
  165. m_html_widget->on_link_context_menu_request = [this](auto& href, auto& screen_position) {
  166. m_link_context_menu_href = href;
  167. m_link_context_menu->popup(screen_position);
  168. };
  169. m_html_widget->on_link_middle_click = [this](auto& href) {
  170. m_html_widget->on_link_click(href, "_blank", 0);
  171. };
  172. m_html_widget->on_title_change = [this](auto& title) {
  173. if (title.is_null()) {
  174. m_title = m_html_widget->main_frame().document()->url().to_string();
  175. } else {
  176. m_title = title;
  177. }
  178. if (on_title_change)
  179. on_title_change(m_title);
  180. };
  181. m_html_widget->on_favicon_change = [this](auto& icon) {
  182. m_icon = icon;
  183. if (on_favicon_change)
  184. on_favicon_change(icon);
  185. };
  186. m_html_widget->on_set_document = [this](auto* document) {
  187. if (document && m_console_window) {
  188. auto* console_widget = static_cast<ConsoleWidget*>(m_console_window->main_widget());
  189. console_widget->set_interpreter(document->interpreter().make_weak_ptr());
  190. }
  191. };
  192. auto focus_location_box_action = GUI::Action::create(
  193. "Focus location box", { Mod_Ctrl, Key_L }, [this](auto&) {
  194. m_location_box->select_all();
  195. m_location_box->set_focus(true);
  196. },
  197. this);
  198. m_statusbar = widget.add<GUI::StatusBar>();
  199. m_html_widget->on_link_hover = [this](auto& href) {
  200. m_statusbar->set_text(href);
  201. };
  202. m_html_widget->on_url_drop = [this](auto& url) {
  203. m_html_widget->load(url);
  204. };
  205. m_menubar = GUI::MenuBar::construct();
  206. auto& app_menu = m_menubar->add_menu("Browser");
  207. app_menu.add_action(WindowActions::the().create_new_tab_action());
  208. app_menu.add_action(GUI::Action::create(
  209. "Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  210. on_tab_close_request(*this);
  211. },
  212. this));
  213. app_menu.add_action(*m_reload_action);
  214. app_menu.add_separator();
  215. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  216. GUI::Application::the().quit();
  217. }));
  218. auto& view_menu = m_menubar->add_menu("View");
  219. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  220. [this](auto&) {
  221. window()->set_fullscreen(!window()->is_fullscreen());
  222. auto is_fullscreen = window()->is_fullscreen();
  223. auto* tab_widget = static_cast<GUI::TabWidget*>(parent_widget());
  224. tab_widget->set_bar_visible(!is_fullscreen && tab_widget->children().size() > 1);
  225. m_toolbar_container->set_visible(!is_fullscreen);
  226. m_statusbar->set_visible(!is_fullscreen);
  227. },
  228. this));
  229. auto& inspect_menu = m_menubar->add_menu("Inspect");
  230. inspect_menu.add_action(GUI::Action::create(
  231. "View source", { Mod_Ctrl, Key_U }, [this](auto&) {
  232. ASSERT(m_html_widget->document());
  233. auto url = m_html_widget->document()->url().to_string();
  234. auto source = m_html_widget->document()->source();
  235. auto window = GUI::Window::construct();
  236. auto& editor = window->set_main_widget<GUI::TextEditor>();
  237. editor.set_text(source);
  238. editor.set_readonly(true);
  239. editor.set_ruler_visible(true);
  240. window->set_rect(150, 150, 640, 480);
  241. window->set_title(url);
  242. window->show();
  243. (void)window.leak_ref();
  244. },
  245. this));
  246. inspect_menu.add_action(GUI::Action::create(
  247. "Inspect DOM tree", { Mod_None, Key_F12 }, [this](auto&) {
  248. if (!m_dom_inspector_window) {
  249. m_dom_inspector_window = GUI::Window::construct();
  250. m_dom_inspector_window->set_rect(100, 100, 300, 500);
  251. m_dom_inspector_window->set_title("DOM inspector");
  252. m_dom_inspector_window->set_main_widget<InspectorWidget>();
  253. }
  254. auto* inspector_widget = static_cast<InspectorWidget*>(m_dom_inspector_window->main_widget());
  255. inspector_widget->set_document(m_html_widget->document());
  256. m_dom_inspector_window->show();
  257. m_dom_inspector_window->move_to_front();
  258. },
  259. this));
  260. inspect_menu.add_action(GUI::Action::create(
  261. "Open JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
  262. if (!m_console_window) {
  263. m_console_window = GUI::Window::construct();
  264. m_console_window->set_rect(100, 100, 500, 300);
  265. m_console_window->set_title("JS Console");
  266. m_console_window->set_main_widget<ConsoleWidget>();
  267. }
  268. auto* console_widget = static_cast<ConsoleWidget*>(m_console_window->main_widget());
  269. console_widget->set_interpreter(m_html_widget->document()->interpreter().make_weak_ptr());
  270. m_console_window->show();
  271. m_console_window->move_to_front();
  272. },
  273. this));
  274. auto& debug_menu = m_menubar->add_menu("Debug");
  275. debug_menu.add_action(GUI::Action::create(
  276. "Dump DOM tree", [this](auto&) {
  277. Web::dump_tree(*m_html_widget->document());
  278. },
  279. this));
  280. debug_menu.add_action(GUI::Action::create(
  281. "Dump Layout tree", [this](auto&) {
  282. Web::dump_tree(*m_html_widget->document()->layout_node());
  283. },
  284. this));
  285. debug_menu.add_action(GUI::Action::create(
  286. "Dump Style sheets", [this](auto&) {
  287. for (auto& sheet : m_html_widget->document()->stylesheets()) {
  288. dump_sheet(sheet);
  289. }
  290. },
  291. this));
  292. debug_menu.add_separator();
  293. auto line_box_borders_action = GUI::Action::create_checkable(
  294. "Line box borders", [this](auto& action) {
  295. m_html_widget->set_should_show_line_box_borders(action.is_checked());
  296. m_html_widget->update();
  297. },
  298. this);
  299. line_box_borders_action->set_checked(false);
  300. debug_menu.add_action(line_box_borders_action);
  301. auto& bookmarks_menu = m_menubar->add_menu("Bookmarks");
  302. bookmarks_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  303. auto& help_menu = m_menubar->add_menu("Help");
  304. help_menu.add_action(WindowActions::the().about_action());
  305. m_tab_context_menu = GUI::Menu::construct();
  306. m_tab_context_menu->add_action(GUI::Action::create("Reload Tab", [this](auto&) {
  307. m_reload_action->activate();
  308. }));
  309. m_tab_context_menu->add_action(GUI::Action::create("Close Tab", [this](auto&) {
  310. on_tab_close_request(*this);
  311. }));
  312. }
  313. Tab::~Tab()
  314. {
  315. }
  316. void Tab::load(const URL& url)
  317. {
  318. m_html_widget->load(url);
  319. }
  320. void Tab::update_actions()
  321. {
  322. m_go_back_action->set_enabled(m_history.can_go_back());
  323. m_go_forward_action->set_enabled(m_history.can_go_forward());
  324. }
  325. void Tab::update_bookmark_button(const String& url)
  326. {
  327. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  328. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-filled.png"));
  329. m_bookmark_button->set_tooltip("Remove Bookmark");
  330. } else {
  331. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-contour.png"));
  332. m_bookmark_button->set_tooltip("Add Bookmark");
  333. }
  334. }
  335. void Tab::did_become_active()
  336. {
  337. Web::ResourceLoader::the().on_load_counter_change = [this] {
  338. if (Web::ResourceLoader::the().pending_loads() == 0) {
  339. m_statusbar->set_text("");
  340. return;
  341. }
  342. m_statusbar->set_text(String::format("Loading (%d pending resources...)", Web::ResourceLoader::the().pending_loads()));
  343. };
  344. BookmarksBarWidget::the().on_bookmark_click = [this](auto& url, unsigned modifiers) {
  345. if (modifiers & Mod_Ctrl)
  346. on_tab_open_request(url);
  347. else
  348. m_html_widget->load(url);
  349. };
  350. BookmarksBarWidget::the().on_bookmark_hover = [this](auto&, auto& url) {
  351. m_statusbar->set_text(url);
  352. };
  353. BookmarksBarWidget::the().remove_from_parent();
  354. m_toolbar_container->add_child(BookmarksBarWidget::the());
  355. auto is_fullscreen = window()->is_fullscreen();
  356. m_toolbar_container->set_visible(!is_fullscreen);
  357. m_statusbar->set_visible(!is_fullscreen);
  358. GUI::Application::the().set_menubar(m_menubar);
  359. }
  360. void Tab::context_menu_requested(const Gfx::Point& screen_position)
  361. {
  362. m_tab_context_menu->popup(screen_position);
  363. }
  364. }