Tab.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "DownloadWidget.h"
  29. #include "History.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/TextBox.h>
  42. #include <LibGUI/ToolBar.h>
  43. #include <LibGUI/ToolBarContainer.h>
  44. #include <LibGUI/Window.h>
  45. #include <LibWeb/CSS/StyleResolver.h>
  46. #include <LibWeb/DOM/Element.h>
  47. #include <LibWeb/DOMTreeModel.h>
  48. #include <LibWeb/Dump.h>
  49. #include <LibWeb/Frame.h>
  50. #include <LibWeb/HtmlView.h>
  51. #include <LibWeb/Layout/LayoutBlock.h>
  52. #include <LibWeb/Layout/LayoutDocument.h>
  53. #include <LibWeb/Layout/LayoutInline.h>
  54. #include <LibWeb/Layout/LayoutNode.h>
  55. #include <LibWeb/Parser/CSSParser.h>
  56. #include <LibWeb/Parser/HTMLParser.h>
  57. #include <LibWeb/ResourceLoader.h>
  58. namespace Browser {
  59. static const char* home_url = "file:///home/anon/www/welcome.html";
  60. Tab::Tab()
  61. {
  62. auto& widget = *this;
  63. set_layout<GUI::VerticalBoxLayout>();
  64. m_toolbar_container = widget.add<GUI::ToolBarContainer>();
  65. auto& toolbar = m_toolbar_container->add<GUI::ToolBar>();
  66. m_html_widget = widget.add<Web::HtmlView>();
  67. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) {
  68. m_history.go_back();
  69. update_actions();
  70. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  71. m_html_widget->load(m_history.current());
  72. });
  73. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
  74. m_history.go_forward();
  75. update_actions();
  76. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  77. m_html_widget->load(m_history.current());
  78. });
  79. toolbar.add_action(*m_go_back_action);
  80. toolbar.add_action(*m_go_forward_action);
  81. toolbar.add_action(GUI::CommonActions::make_go_home_action([this](auto&) {
  82. m_html_widget->load(home_url);
  83. }));
  84. toolbar.add_action(GUI::CommonActions::make_reload_action([this](auto&) {
  85. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  86. m_html_widget->reload();
  87. }));
  88. m_location_box = toolbar.add<GUI::TextBox>();
  89. m_location_box->on_return_pressed = [this] {
  90. String location = m_location_box->text();
  91. if (!URL(location).is_valid()) {
  92. StringBuilder builder;
  93. builder.append("http://");
  94. builder.append(location);
  95. location = builder.build();
  96. }
  97. m_html_widget->load(location);
  98. };
  99. m_bookmark_button = toolbar.add<GUI::Button>();
  100. m_bookmark_button->set_button_style(Gfx::ButtonStyle::CoolBar);
  101. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-black.png"));
  102. m_bookmark_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  103. m_bookmark_button->set_preferred_size(22, 22);
  104. m_bookmark_button->on_click = [this] {
  105. auto url = m_html_widget->main_frame().document()->url().to_string();
  106. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  107. BookmarksBarWidget::the().remove_bookmark(url);
  108. } else {
  109. BookmarksBarWidget::the().add_bookmark(url, m_title);
  110. }
  111. update_bookmark_button(url);
  112. };
  113. m_html_widget->on_load_start = [this](auto& url) {
  114. m_location_box->set_text(url.to_string());
  115. if (m_should_push_loads_to_history)
  116. m_history.push(url);
  117. update_actions();
  118. update_bookmark_button(url.to_string());
  119. };
  120. m_html_widget->on_link_click = [this](auto& href, auto& target, unsigned modifiers) {
  121. if (href.starts_with("#")) {
  122. auto anchor = href.substring_view(1, href.length() - 1);
  123. m_html_widget->scroll_to_anchor(anchor);
  124. } else {
  125. auto url = m_html_widget->document()->complete_url(href);
  126. if (target == "_blank" || modifiers == Mod_Ctrl)
  127. on_tab_open_request(url);
  128. else
  129. m_html_widget->load(url);
  130. }
  131. };
  132. m_link_context_menu = GUI::Menu::construct();
  133. m_link_context_menu->add_action(GUI::Action::create("Open", [this](auto&) {
  134. m_html_widget->on_link_click(m_link_context_menu_href, "", 0);
  135. }));
  136. m_link_context_menu->add_action(GUI::Action::create("Open in new tab", [this](auto&) {
  137. m_html_widget->on_link_click(m_link_context_menu_href, "_blank", 0);
  138. }));
  139. m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) {
  140. GUI::Clipboard::the().set_data(m_html_widget->document()->complete_url(m_link_context_menu_href).to_string());
  141. }));
  142. m_link_context_menu->add_separator();
  143. m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {
  144. auto window = GUI::Window::construct();
  145. window->set_rect(300, 300, 300, 150);
  146. auto url = m_html_widget->document()->complete_url(m_link_context_menu_href);
  147. window->set_title(String::format("0%% of %s", url.basename().characters()));
  148. window->set_resizable(false);
  149. window->set_main_widget<DownloadWidget>(url);
  150. window->show();
  151. (void)window.leak_ref();
  152. }));
  153. m_html_widget->on_link_context_menu_request = [this](auto& href, auto& screen_position) {
  154. m_link_context_menu_href = href;
  155. m_link_context_menu->popup(screen_position);
  156. };
  157. m_html_widget->on_link_middle_click = [this](auto& href) {
  158. m_html_widget->on_link_click(href, "_blank", 0);
  159. };
  160. m_html_widget->on_title_change = [this](auto& title) {
  161. if (title.is_null()) {
  162. m_title = m_html_widget->main_frame().document()->url().to_string();
  163. } else {
  164. m_title = title;
  165. }
  166. if (on_title_change)
  167. on_title_change(m_title);
  168. };
  169. m_html_widget->on_favicon_change = [this](auto& icon) {
  170. m_icon = icon;
  171. if (on_favicon_change)
  172. on_favicon_change(icon);
  173. };
  174. auto focus_location_box_action = GUI::Action::create(
  175. "Focus location box", { Mod_Ctrl, Key_L }, [this](auto&) {
  176. m_location_box->select_all();
  177. m_location_box->set_focus(true);
  178. },
  179. this);
  180. m_statusbar = widget.add<GUI::StatusBar>();
  181. m_html_widget->on_link_hover = [this](auto& href) {
  182. m_statusbar->set_text(href);
  183. };
  184. m_menubar = GUI::MenuBar::construct();
  185. auto& app_menu = m_menubar->add_menu("Browser");
  186. app_menu.add_action(WindowActions::the().create_new_tab_action());
  187. app_menu.add_action(GUI::Action::create(
  188. "Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  189. on_tab_close_request(*this);
  190. },
  191. this));
  192. app_menu.add_action(GUI::Action::create(
  193. "Reload", { Mod_None, Key_F5 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), [this](auto&) {
  194. TemporaryChange<bool> change(m_should_push_loads_to_history, false);
  195. m_html_widget->reload();
  196. },
  197. this));
  198. app_menu.add_separator();
  199. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  200. GUI::Application::the().quit();
  201. }));
  202. auto& inspect_menu = m_menubar->add_menu("Inspect");
  203. inspect_menu.add_action(GUI::Action::create(
  204. "View source", { Mod_Ctrl, Key_U }, [this](auto&) {
  205. ASSERT(m_html_widget->document());
  206. auto url = m_html_widget->document()->url().to_string();
  207. auto source = m_html_widget->document()->source();
  208. auto window = GUI::Window::construct();
  209. auto& editor = window->set_main_widget<GUI::TextEditor>();
  210. editor.set_text(source);
  211. editor.set_readonly(true);
  212. editor.set_ruler_visible(true);
  213. window->set_rect(150, 150, 640, 480);
  214. window->set_title(url);
  215. window->show();
  216. (void)window.leak_ref();
  217. },
  218. this));
  219. inspect_menu.add_action(GUI::Action::create(
  220. "Inspect DOM tree", { Mod_None, Key_F12 }, [this](auto&) {
  221. if (!m_dom_inspector_window) {
  222. m_dom_inspector_window = GUI::Window::construct();
  223. m_dom_inspector_window->set_rect(100, 100, 300, 500);
  224. m_dom_inspector_window->set_title("DOM inspector");
  225. m_dom_inspector_window->set_main_widget<InspectorWidget>();
  226. }
  227. auto* inspector_widget = static_cast<InspectorWidget*>(m_dom_inspector_window->main_widget());
  228. inspector_widget->set_document(m_html_widget->document());
  229. m_dom_inspector_window->show();
  230. m_dom_inspector_window->move_to_front();
  231. },
  232. this));
  233. auto& debug_menu = m_menubar->add_menu("Debug");
  234. debug_menu.add_action(GUI::Action::create(
  235. "Dump DOM tree", [this](auto&) {
  236. Web::dump_tree(*m_html_widget->document());
  237. },
  238. this));
  239. debug_menu.add_action(GUI::Action::create(
  240. "Dump Layout tree", [this](auto&) {
  241. Web::dump_tree(*m_html_widget->document()->layout_node());
  242. },
  243. this));
  244. debug_menu.add_action(GUI::Action::create(
  245. "Dump Style sheets", [this](auto&) {
  246. for (auto& sheet : m_html_widget->document()->stylesheets()) {
  247. dump_sheet(sheet);
  248. }
  249. },
  250. this));
  251. debug_menu.add_separator();
  252. auto line_box_borders_action = GUI::Action::create_checkable(
  253. "Line box borders", [this](auto& action) {
  254. m_html_widget->set_should_show_line_box_borders(action.is_checked());
  255. m_html_widget->update();
  256. },
  257. this);
  258. line_box_borders_action->set_checked(false);
  259. debug_menu.add_action(line_box_borders_action);
  260. auto& bookmarks_menu = m_menubar->add_menu("Bookmarks");
  261. bookmarks_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  262. auto& help_menu = m_menubar->add_menu("Help");
  263. help_menu.add_action(WindowActions::the().about_action());
  264. }
  265. Tab::~Tab()
  266. {
  267. }
  268. void Tab::load(const URL& url)
  269. {
  270. m_html_widget->load(url);
  271. }
  272. void Tab::update_actions()
  273. {
  274. m_go_back_action->set_enabled(m_history.can_go_back());
  275. m_go_forward_action->set_enabled(m_history.can_go_forward());
  276. }
  277. void Tab::update_bookmark_button(const String& url)
  278. {
  279. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  280. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-yellow.png"));
  281. m_bookmark_button->set_tooltip("Remove Bookmark");
  282. } else {
  283. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-contour.png"));
  284. m_bookmark_button->set_tooltip("Add Bookmark");
  285. }
  286. }
  287. void Tab::did_become_active()
  288. {
  289. Web::ResourceLoader::the().on_load_counter_change = [this] {
  290. if (Web::ResourceLoader::the().pending_loads() == 0) {
  291. m_statusbar->set_text("");
  292. return;
  293. }
  294. m_statusbar->set_text(String::format("Loading (%d pending resources...)", Web::ResourceLoader::the().pending_loads()));
  295. };
  296. BookmarksBarWidget::the().on_bookmark_click = [this](auto&, auto& url) {
  297. m_html_widget->load(url);
  298. };
  299. BookmarksBarWidget::the().on_bookmark_hover = [this](auto&, auto& url) {
  300. m_statusbar->set_text(url);
  301. };
  302. BookmarksBarWidget::the().remove_from_parent();
  303. m_toolbar_container->add_child(BookmarksBarWidget::the());
  304. GUI::Application::the().set_menubar(m_menubar);
  305. }
  306. }