main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2018-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 "InspectorWidget.h"
  27. #include "Tab.h"
  28. #include "WindowActions.h"
  29. #include <LibCore/File.h>
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/TabWidget.h>
  34. #include <LibGUI/Window.h>
  35. #include <LibGfx/Bitmap.h>
  36. #include <LibWeb/ResourceLoader.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. static const char* home_url = "file:///home/anon/www/welcome.html";
  40. int main(int argc, char** argv)
  41. {
  42. if (getuid() == 0) {
  43. fprintf(stderr, "Refusing to run as root\n");
  44. return 1;
  45. }
  46. if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) {
  47. perror("pledge");
  48. return 1;
  49. }
  50. GUI::Application app(argc, argv);
  51. // Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
  52. Web::ResourceLoader::the();
  53. if (pledge("stdio shared_buffer accept cpath rpath wpath", nullptr) < 0) {
  54. perror("pledge");
  55. return 1;
  56. }
  57. if (unveil("/home", "rwc") < 0) {
  58. perror("unveil");
  59. return 1;
  60. }
  61. if (unveil("/res", "r") < 0) {
  62. perror("unveil");
  63. return 1;
  64. }
  65. unveil(nullptr, nullptr);
  66. auto window = GUI::Window::construct();
  67. window->set_rect(100, 100, 640, 480);
  68. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  69. window->set_title("Browser");
  70. auto& widget = window->set_main_widget<GUI::Widget>();
  71. widget.set_fill_with_background_color(true);
  72. widget.set_layout<GUI::VerticalBoxLayout>();
  73. widget.layout()->set_spacing(2);
  74. auto& tab_widget = widget.add<GUI::TabWidget>();
  75. tab_widget.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  76. tab_widget.set_container_padding(0);
  77. tab_widget.set_uniform_tabs(true);
  78. tab_widget.on_change = [&](auto& active_widget) {
  79. auto& tab = static_cast<Browser::Tab&>(active_widget);
  80. window->set_title(String::format("%s - Browser", tab.title().characters()));
  81. tab.did_become_active();
  82. };
  83. Browser::WindowActions window_actions(*window);
  84. auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
  85. ASSERT(default_favicon);
  86. Function<void(URL url, bool activate)> create_new_tab;
  87. create_new_tab = [&](auto url, auto activate) {
  88. auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab");
  89. tab_widget.set_tab_icon(new_tab, default_favicon);
  90. new_tab.on_title_change = [&](auto title) {
  91. tab_widget.set_tab_title(new_tab, title);
  92. if (tab_widget.active_widget() == &new_tab)
  93. window->set_title(String::format("%s - Browser", title.characters()));
  94. };
  95. new_tab.on_favicon_change = [&](auto& bitmap) {
  96. tab_widget.set_tab_icon(new_tab, &bitmap);
  97. };
  98. new_tab.on_tab_open_request = [&](auto& url) {
  99. create_new_tab(url, true);
  100. };
  101. new_tab.on_tab_close_request = [&](auto& tab) {
  102. tab_widget.deferred_invoke([&](auto&) {
  103. tab_widget.remove_tab(tab);
  104. if (tab_widget.children().is_empty())
  105. app.quit();
  106. });
  107. };
  108. new_tab.load(url);
  109. dbg() << "Added new tab " << &new_tab << ", loading " << url;
  110. if (activate)
  111. tab_widget.set_active_widget(&new_tab);
  112. };
  113. URL default_url = home_url;
  114. if (app.args().size() >= 1)
  115. default_url = URL::create_with_url_or_path(app.args()[0]);
  116. window_actions.on_create_new_tab = [&] {
  117. create_new_tab(default_url, true);
  118. };
  119. window_actions.on_next_tab = [&] {
  120. tab_widget.activate_next_tab();
  121. };
  122. window_actions.on_previous_tab = [&] {
  123. tab_widget.activate_previous_tab();
  124. };
  125. window_actions.on_about = [&] {
  126. GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window);
  127. };
  128. create_new_tab(default_url, true);
  129. window->show();
  130. return app.exec();
  131. }