MainWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "MainWidget.h"
  10. #include <AK/String.h>
  11. #include <AK/URL.h>
  12. #include <Applications/Help/HelpWindowGML.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/File.h>
  15. #include <LibCore/System.h>
  16. #include <LibDesktop/Launcher.h>
  17. #include <LibGUI/Action.h>
  18. #include <LibGUI/Application.h>
  19. #include <LibGUI/Clipboard.h>
  20. #include <LibGUI/ListView.h>
  21. #include <LibGUI/Menu.h>
  22. #include <LibGUI/Menubar.h>
  23. #include <LibGUI/MessageBox.h>
  24. #include <LibGUI/Statusbar.h>
  25. #include <LibGUI/TabWidget.h>
  26. #include <LibGUI/TextBox.h>
  27. #include <LibGUI/Toolbar.h>
  28. #include <LibGUI/TreeView.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Bitmap.h>
  31. #include <LibMain/Main.h>
  32. #include <LibMarkdown/Document.h>
  33. namespace Help {
  34. MainWidget::MainWidget()
  35. {
  36. load_from_gml(help_window_gml);
  37. m_toolbar = find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  38. m_tab_widget = find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  39. m_search_container = find_descendant_of_type_named<GUI::Widget>("search_container");
  40. m_search_box = find_descendant_of_type_named<GUI::TextBox>("search_box");
  41. m_search_box->on_change = [this] {
  42. m_filter_model->set_filter_term(m_search_box->text());
  43. };
  44. m_search_box->on_down_pressed = [this] {
  45. m_search_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  46. };
  47. m_search_box->on_up_pressed = [this] {
  48. m_search_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  49. };
  50. m_search_view = find_descendant_of_type_named<GUI::ListView>("search_view");
  51. m_search_view->set_should_hide_unnecessary_scrollbars(true);
  52. m_search_view->on_selection_change = [this] {
  53. auto const& index = m_search_view->selection().first();
  54. if (!index.is_valid())
  55. return;
  56. auto* view_model = m_search_view->model();
  57. if (!view_model) {
  58. m_web_view->load_empty_document();
  59. return;
  60. }
  61. auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
  62. auto const& mapped_index = search_model.map(index);
  63. String path = m_manual_model->page_path(mapped_index);
  64. if (path.is_null()) {
  65. m_web_view->load_empty_document();
  66. return;
  67. }
  68. m_browse_view->selection().clear();
  69. m_browse_view->selection().add(mapped_index);
  70. m_history.push(path);
  71. open_page(path);
  72. };
  73. m_browse_view = find_descendant_of_type_named<GUI::TreeView>("browse_view");
  74. m_browse_view->on_selection_change = [this] {
  75. String path = m_manual_model->page_path(m_browse_view->selection().first());
  76. if (path.is_null())
  77. return;
  78. m_history.push(path);
  79. open_page(path);
  80. };
  81. m_browse_view->on_toggle = [this](GUI::ModelIndex const& index, bool open) {
  82. m_manual_model->update_section_node_on_toggle(index, open);
  83. };
  84. m_web_view = find_descendant_of_type_named<WebView::OutOfProcessWebView>("web_view");
  85. m_web_view->on_link_click = [this](auto& url, auto&, unsigned) {
  86. if (url.protocol() == "file") {
  87. auto path = url.path();
  88. if (!path.starts_with("/usr/share/man/"sv)) {
  89. open_external(url);
  90. return;
  91. }
  92. auto browse_view_index = m_manual_model->index_from_path(path);
  93. if (browse_view_index.has_value()) {
  94. dbgln("Found path _{}_ in m_manual_model at index {}", path, browse_view_index.value());
  95. m_browse_view->selection().set(browse_view_index.value());
  96. return;
  97. }
  98. m_history.push(path);
  99. open_page(path);
  100. } else if (url.protocol() == "help") {
  101. if (url.host() == "man") {
  102. if (url.paths().size() != 2) {
  103. dbgln("Bad help page URL '{}'", url);
  104. return;
  105. }
  106. auto const section = url.paths()[0];
  107. auto const page = url.paths()[1];
  108. auto const path = String::formatted("/usr/share/man/man{}/{}.md", section, page);
  109. m_history.push(path);
  110. open_url(URL::create_with_file_scheme(path, url.fragment()));
  111. } else {
  112. dbgln("Bad help operation '{}' in URL '{}'", url.host(), url);
  113. }
  114. } else {
  115. open_external(url);
  116. }
  117. };
  118. m_web_view->on_context_menu_request = [this](auto& screen_position) {
  119. m_copy_action->set_enabled(!m_web_view->selected_text().is_empty());
  120. m_context_menu->popup(screen_position);
  121. };
  122. m_web_view->on_link_hover = [this](URL const& url) {
  123. if (url.is_valid())
  124. m_statusbar->set_text(url.to_string());
  125. else
  126. m_statusbar->set_text({});
  127. };
  128. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) {
  129. m_history.go_back();
  130. open_page(m_history.current());
  131. });
  132. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
  133. m_history.go_forward();
  134. open_page(m_history.current());
  135. });
  136. m_go_back_action->set_enabled(false);
  137. m_go_forward_action->set_enabled(false);
  138. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) {
  139. String path = "/usr/share/man/man7/Help-index.md";
  140. m_history.push(path);
  141. open_page(path);
  142. });
  143. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  144. auto selected_text = m_web_view->selected_text();
  145. if (!selected_text.is_empty())
  146. GUI::Clipboard::the().set_plain_text(selected_text);
  147. });
  148. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  149. m_web_view->select_all();
  150. });
  151. m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  152. GUI::Application::the()->on_action_enter = [this](GUI::Action const& action) {
  153. m_statusbar->set_override_text(action.status_tip());
  154. };
  155. GUI::Application::the()->on_action_leave = [this](GUI::Action const&) {
  156. m_statusbar->set_override_text({});
  157. };
  158. }
  159. void MainWidget::set_start_page(StringView start_page, u32 section)
  160. {
  161. bool set_start_page = false;
  162. if (!start_page.is_null()) {
  163. if (section != 0) {
  164. // > Help [section] [name]
  165. String path = String::formatted("/usr/share/man/man{}/{}.md", section, start_page);
  166. m_history.push(path);
  167. open_page(path);
  168. set_start_page = true;
  169. } else if (URL url = URL::create_with_url_or_path(start_page); url.is_valid() && url.path().ends_with(".md"sv)) {
  170. // > Help [/path/to/documentation/file.md]
  171. m_history.push(url.path());
  172. open_page(url.path());
  173. set_start_page = true;
  174. } else {
  175. // > Help [query]
  176. // First, see if we can find the page by name
  177. char const* sections[] = {
  178. "1",
  179. "2",
  180. "3",
  181. "4",
  182. "5",
  183. "6",
  184. "7",
  185. "8"
  186. };
  187. for (auto s : sections) {
  188. String path = String::formatted("/usr/share/man/man{}/{}.md", s, start_page);
  189. if (Core::File::exists(path)) {
  190. m_history.push(path);
  191. open_page(path);
  192. set_start_page = true;
  193. break;
  194. }
  195. }
  196. // No match, so treat the input as a search query
  197. if (!set_start_page) {
  198. m_tab_widget->set_active_widget(m_search_container);
  199. m_search_box->set_focus(true);
  200. m_search_box->set_text(start_page);
  201. m_search_box->select_all();
  202. m_filter_model->set_filter_term(m_search_box->text());
  203. }
  204. }
  205. }
  206. if (!set_start_page)
  207. m_go_home_action->activate();
  208. }
  209. ErrorOr<void> MainWidget::initialize_fallibles(GUI::Window& window)
  210. {
  211. (void)TRY(m_toolbar->try_add_action(*m_go_back_action));
  212. (void)TRY(m_toolbar->try_add_action(*m_go_forward_action));
  213. (void)TRY(m_toolbar->try_add_action(*m_go_home_action));
  214. auto file_menu = TRY(window.try_add_menu("&File"));
  215. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
  216. GUI::Application::the()->quit();
  217. })));
  218. auto go_menu = TRY(window.try_add_menu("&Go"));
  219. TRY(go_menu->try_add_action(*m_go_back_action));
  220. TRY(go_menu->try_add_action(*m_go_forward_action));
  221. TRY(go_menu->try_add_action(*m_go_home_action));
  222. auto help_menu = TRY(window.try_add_menu("&Help"));
  223. TRY(help_menu->try_add_action(GUI::Action::create("&Contents", { Key_F1 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"sv)), [&](auto&) {
  224. String path = "/usr/share/man/man1/Help.md";
  225. open_page(path);
  226. })));
  227. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", TRY(GUI::Icon::try_create_default_icon("app-help"sv)), &window)));
  228. m_context_menu = TRY(GUI::Menu::try_create());
  229. TRY(m_context_menu->try_add_action(*m_go_back_action));
  230. TRY(m_context_menu->try_add_action(*m_go_forward_action));
  231. TRY(m_context_menu->try_add_action(*m_go_home_action));
  232. TRY(m_context_menu->try_add_separator());
  233. TRY(m_context_menu->try_add_action(*m_copy_action));
  234. TRY(m_context_menu->try_add_action(*m_select_all_action));
  235. m_manual_model = TRY(ManualModel::create());
  236. m_browse_view->set_model(*m_manual_model);
  237. m_filter_model = TRY(GUI::FilteringProxyModel::create(*m_manual_model));
  238. m_search_view->set_model(*m_filter_model);
  239. m_filter_model->set_filter_term(""sv);
  240. return {};
  241. }
  242. void MainWidget::open_url(URL const& url)
  243. {
  244. m_go_back_action->set_enabled(m_history.can_go_back());
  245. m_go_forward_action->set_enabled(m_history.can_go_forward());
  246. if (url.protocol() == "file") {
  247. m_web_view->load(url);
  248. m_web_view->scroll_to_top();
  249. GUI::Application::the()->deferred_invoke([&, path = url.path()] {
  250. auto browse_view_index = m_manual_model->index_from_path(path);
  251. if (browse_view_index.has_value()) {
  252. m_browse_view->expand_tree(browse_view_index.value().parent());
  253. String page_and_section = m_manual_model->page_and_section(browse_view_index.value());
  254. window()->set_title(String::formatted("{} - Help", page_and_section));
  255. } else {
  256. window()->set_title("Help");
  257. }
  258. });
  259. }
  260. }
  261. void MainWidget::open_external(URL const& url)
  262. {
  263. if (!Desktop::Launcher::open(url))
  264. GUI::MessageBox::show(window(), String::formatted("The link to '{}' could not be opened.", url), "Failed to open link"sv, GUI::MessageBox::Type::Error);
  265. }
  266. void MainWidget::open_page(String const& path)
  267. {
  268. m_go_back_action->set_enabled(m_history.can_go_back());
  269. m_go_forward_action->set_enabled(m_history.can_go_forward());
  270. if (path.is_null()) {
  271. window()->set_title("Help");
  272. m_web_view->load_empty_document();
  273. return;
  274. }
  275. open_url(URL::create_with_url_or_path(path));
  276. }
  277. }