Tab.cpp 15 KB

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