Tab.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 String g_home_url;
  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. }, this);
  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. }, this);
  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(g_home_url);
  83. }, this));
  84. m_reload_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. }, this);
  88. toolbar.add_action(*m_reload_action);
  89. m_location_box = toolbar.add<GUI::TextBox>();
  90. m_location_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  91. m_location_box->set_preferred_size(0, 22);
  92. m_location_box->on_return_pressed = [this] {
  93. String location = m_location_box->text();
  94. if (!URL(location).is_valid()) {
  95. StringBuilder builder;
  96. builder.append("http://");
  97. builder.append(location);
  98. location = builder.build();
  99. }
  100. m_html_widget->load(location);
  101. m_location_box->set_focus(false);
  102. };
  103. m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste & Go", [this](auto&) {
  104. m_location_box->set_text(GUI::Clipboard::the().data());
  105. m_location_box->on_return_pressed();
  106. }));
  107. m_bookmark_button = toolbar.add<GUI::Button>();
  108. m_bookmark_button->set_button_style(Gfx::ButtonStyle::CoolBar);
  109. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-black.png"));
  110. m_bookmark_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  111. m_bookmark_button->set_preferred_size(22, 22);
  112. m_bookmark_button->on_click = [this](auto) {
  113. auto url = m_html_widget->main_frame().document()->url().to_string();
  114. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  115. BookmarksBarWidget::the().remove_bookmark(url);
  116. } else {
  117. BookmarksBarWidget::the().add_bookmark(url, m_title);
  118. }
  119. update_bookmark_button(url);
  120. };
  121. m_html_widget->on_load_start = [this](auto& url) {
  122. m_location_box->set_text(url.to_string());
  123. if (m_should_push_loads_to_history)
  124. m_history.push(url);
  125. update_actions();
  126. update_bookmark_button(url.to_string());
  127. };
  128. m_html_widget->on_link_click = [this](auto& href, auto& target, unsigned modifiers) {
  129. if (target == "_blank" || modifiers == Mod_Ctrl) {
  130. auto url = m_html_widget->document()->complete_url(href);
  131. on_tab_open_request(url);
  132. } else {
  133. if (href.starts_with("#")) {
  134. auto anchor = href.substring_view(1, href.length() - 1);
  135. m_html_widget->scroll_to_anchor(anchor);
  136. } else {
  137. auto url = m_html_widget->document()->complete_url(href);
  138. m_html_widget->load(url);
  139. }
  140. }
  141. };
  142. m_link_context_menu = GUI::Menu::construct();
  143. m_link_context_menu->add_action(GUI::Action::create("Open", [this](auto&) {
  144. m_html_widget->on_link_click(m_link_context_menu_href, "", 0);
  145. }));
  146. m_link_context_menu->add_action(GUI::Action::create("Open in new tab", [this](auto&) {
  147. m_html_widget->on_link_click(m_link_context_menu_href, "_blank", 0);
  148. }));
  149. m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) {
  150. GUI::Clipboard::the().set_data(m_html_widget->document()->complete_url(m_link_context_menu_href).to_string());
  151. }));
  152. m_link_context_menu->add_separator();
  153. m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {
  154. auto window = GUI::Window::construct();
  155. window->set_rect(300, 300, 300, 150);
  156. auto url = m_html_widget->document()->complete_url(m_link_context_menu_href);
  157. window->set_title(String::format("0%% of %s", url.basename().characters()));
  158. window->set_resizable(false);
  159. window->set_main_widget<DownloadWidget>(url);
  160. window->show();
  161. (void)window.leak_ref();
  162. }));
  163. m_html_widget->on_link_context_menu_request = [this](auto& href, auto& screen_position) {
  164. m_link_context_menu_href = href;
  165. m_link_context_menu->popup(screen_position);
  166. };
  167. m_html_widget->on_link_middle_click = [this](auto& href) {
  168. m_html_widget->on_link_click(href, "_blank", 0);
  169. };
  170. m_html_widget->on_title_change = [this](auto& title) {
  171. if (title.is_null()) {
  172. m_title = m_html_widget->main_frame().document()->url().to_string();
  173. } else {
  174. m_title = title;
  175. }
  176. if (on_title_change)
  177. on_title_change(m_title);
  178. };
  179. m_html_widget->on_favicon_change = [this](auto& icon) {
  180. m_icon = icon;
  181. if (on_favicon_change)
  182. on_favicon_change(icon);
  183. };
  184. auto focus_location_box_action = GUI::Action::create(
  185. "Focus location box", { Mod_Ctrl, Key_L }, [this](auto&) {
  186. m_location_box->select_all();
  187. m_location_box->set_focus(true);
  188. },
  189. this);
  190. m_statusbar = widget.add<GUI::StatusBar>();
  191. m_html_widget->on_link_hover = [this](auto& href) {
  192. m_statusbar->set_text(href);
  193. };
  194. m_html_widget->on_url_drop = [this](auto& url) {
  195. m_html_widget->load(url);
  196. };
  197. m_menubar = GUI::MenuBar::construct();
  198. auto& app_menu = m_menubar->add_menu("Browser");
  199. app_menu.add_action(WindowActions::the().create_new_tab_action());
  200. app_menu.add_action(GUI::Action::create(
  201. "Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  202. on_tab_close_request(*this);
  203. },
  204. this));
  205. app_menu.add_action(*m_reload_action);
  206. app_menu.add_separator();
  207. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  208. GUI::Application::the().quit();
  209. }));
  210. auto& view_menu = m_menubar->add_menu("View");
  211. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  212. [this](auto&) {
  213. window()->set_fullscreen(!window()->is_fullscreen());
  214. auto is_fullscreen = window()->is_fullscreen();
  215. auto* tab_widget = static_cast<GUI::TabWidget*>(parent_widget());
  216. tab_widget->set_bar_visible(!is_fullscreen && tab_widget->children().size() > 1);
  217. m_toolbar_container->set_visible(!is_fullscreen);
  218. m_statusbar->set_visible(!is_fullscreen);
  219. },
  220. this));
  221. auto& inspect_menu = m_menubar->add_menu("Inspect");
  222. inspect_menu.add_action(GUI::Action::create(
  223. "View source", { Mod_Ctrl, Key_U }, [this](auto&) {
  224. ASSERT(m_html_widget->document());
  225. auto url = m_html_widget->document()->url().to_string();
  226. auto source = m_html_widget->document()->source();
  227. auto window = GUI::Window::construct();
  228. auto& editor = window->set_main_widget<GUI::TextEditor>();
  229. editor.set_text(source);
  230. editor.set_readonly(true);
  231. editor.set_ruler_visible(true);
  232. window->set_rect(150, 150, 640, 480);
  233. window->set_title(url);
  234. window->show();
  235. (void)window.leak_ref();
  236. },
  237. this));
  238. inspect_menu.add_action(GUI::Action::create(
  239. "Inspect DOM tree", { Mod_None, Key_F12 }, [this](auto&) {
  240. if (!m_dom_inspector_window) {
  241. m_dom_inspector_window = GUI::Window::construct();
  242. m_dom_inspector_window->set_rect(100, 100, 300, 500);
  243. m_dom_inspector_window->set_title("DOM inspector");
  244. m_dom_inspector_window->set_main_widget<InspectorWidget>();
  245. }
  246. auto* inspector_widget = static_cast<InspectorWidget*>(m_dom_inspector_window->main_widget());
  247. inspector_widget->set_document(m_html_widget->document());
  248. m_dom_inspector_window->show();
  249. m_dom_inspector_window->move_to_front();
  250. },
  251. this));
  252. inspect_menu.add_action(GUI::Action::create(
  253. "Open JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
  254. if (!m_console_window) {
  255. m_console_window = GUI::Window::construct();
  256. m_console_window->set_rect(100, 100, 500, 300);
  257. m_console_window->set_title("JS Console");
  258. m_console_window->set_main_widget<ConsoleWidget>();
  259. }
  260. auto* console_widget = static_cast<ConsoleWidget*>(m_console_window->main_widget());
  261. console_widget->set_interpreter(m_html_widget->document()->interpreter().make_weak_ptr());
  262. m_console_window->show();
  263. m_console_window->move_to_front();
  264. },
  265. this));
  266. auto& debug_menu = m_menubar->add_menu("Debug");
  267. debug_menu.add_action(GUI::Action::create(
  268. "Dump DOM tree", [this](auto&) {
  269. Web::dump_tree(*m_html_widget->document());
  270. },
  271. this));
  272. debug_menu.add_action(GUI::Action::create(
  273. "Dump Layout tree", [this](auto&) {
  274. Web::dump_tree(*m_html_widget->document()->layout_node());
  275. },
  276. this));
  277. debug_menu.add_action(GUI::Action::create(
  278. "Dump Style sheets", [this](auto&) {
  279. for (auto& sheet : m_html_widget->document()->stylesheets()) {
  280. dump_sheet(sheet);
  281. }
  282. },
  283. this));
  284. debug_menu.add_separator();
  285. auto line_box_borders_action = GUI::Action::create_checkable(
  286. "Line box borders", [this](auto& action) {
  287. m_html_widget->set_should_show_line_box_borders(action.is_checked());
  288. m_html_widget->update();
  289. },
  290. this);
  291. line_box_borders_action->set_checked(false);
  292. debug_menu.add_action(line_box_borders_action);
  293. auto& bookmarks_menu = m_menubar->add_menu("Bookmarks");
  294. bookmarks_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  295. auto& help_menu = m_menubar->add_menu("Help");
  296. help_menu.add_action(WindowActions::the().about_action());
  297. m_tab_context_menu = GUI::Menu::construct();
  298. m_tab_context_menu->add_action(GUI::Action::create("Reload Tab", [this](auto&) {
  299. m_reload_action->activate();
  300. }));
  301. m_tab_context_menu->add_action(GUI::Action::create("Close Tab", [this](auto&) {
  302. on_tab_close_request(*this);
  303. }));
  304. }
  305. Tab::~Tab()
  306. {
  307. }
  308. void Tab::load(const URL& url)
  309. {
  310. m_html_widget->load(url);
  311. }
  312. void Tab::update_actions()
  313. {
  314. m_go_back_action->set_enabled(m_history.can_go_back());
  315. m_go_forward_action->set_enabled(m_history.can_go_forward());
  316. }
  317. void Tab::update_bookmark_button(const String& url)
  318. {
  319. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  320. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-yellow.png"));
  321. m_bookmark_button->set_tooltip("Remove Bookmark");
  322. } else {
  323. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-contour.png"));
  324. m_bookmark_button->set_tooltip("Add Bookmark");
  325. }
  326. }
  327. void Tab::did_become_active()
  328. {
  329. Web::ResourceLoader::the().on_load_counter_change = [this] {
  330. if (Web::ResourceLoader::the().pending_loads() == 0) {
  331. m_statusbar->set_text("");
  332. return;
  333. }
  334. m_statusbar->set_text(String::format("Loading (%d pending resources...)", Web::ResourceLoader::the().pending_loads()));
  335. };
  336. BookmarksBarWidget::the().on_bookmark_click = [this](auto& url, unsigned modifiers) {
  337. if (modifiers & Mod_Ctrl)
  338. on_tab_open_request(url);
  339. else
  340. m_html_widget->load(url);
  341. };
  342. BookmarksBarWidget::the().on_bookmark_hover = [this](auto&, auto& url) {
  343. m_statusbar->set_text(url);
  344. };
  345. BookmarksBarWidget::the().remove_from_parent();
  346. m_toolbar_container->add_child(BookmarksBarWidget::the());
  347. auto is_fullscreen = window()->is_fullscreen();
  348. m_toolbar_container->set_visible(!is_fullscreen);
  349. m_statusbar->set_visible(!is_fullscreen);
  350. GUI::Application::the().set_menubar(m_menubar);
  351. }
  352. void Tab::context_menu_requested(const Gfx::Point& screen_position)
  353. {
  354. m_tab_context_menu->popup(screen_position);
  355. }
  356. }