MainWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/")) {
  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. open_url(URL::create_with_file_scheme(String::formatted("/usr/share/man/man{}/{}.md", section, page), url.fragment()));
  109. } else {
  110. dbgln("Bad help operation '{}' in URL '{}'", url.host(), url);
  111. }
  112. } else {
  113. open_external(url);
  114. }
  115. };
  116. m_web_view->on_context_menu_request = [this](auto& screen_position) {
  117. m_copy_action->set_enabled(!m_web_view->selected_text().is_empty());
  118. m_context_menu->popup(screen_position);
  119. };
  120. m_web_view->on_link_hover = [this](URL const& url) {
  121. if (url.is_valid())
  122. m_statusbar->set_text(url.to_string());
  123. else
  124. m_statusbar->set_text({});
  125. };
  126. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) {
  127. m_history.go_back();
  128. open_page(m_history.current());
  129. });
  130. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
  131. m_history.go_forward();
  132. open_page(m_history.current());
  133. });
  134. m_go_back_action->set_enabled(false);
  135. m_go_forward_action->set_enabled(false);
  136. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) {
  137. String path = "/usr/share/man/man7/Help-index.md";
  138. m_history.push(path);
  139. open_page(path);
  140. });
  141. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  142. auto selected_text = m_web_view->selected_text();
  143. if (!selected_text.is_empty())
  144. GUI::Clipboard::the().set_plain_text(selected_text);
  145. });
  146. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  147. m_web_view->select_all();
  148. });
  149. m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  150. GUI::Application::the()->on_action_enter = [this](GUI::Action const& action) {
  151. m_statusbar->set_override_text(action.status_tip());
  152. };
  153. GUI::Application::the()->on_action_leave = [this](GUI::Action const&) {
  154. m_statusbar->set_override_text({});
  155. };
  156. }
  157. void MainWidget::set_start_page(StringView start_page, u32 section)
  158. {
  159. bool set_start_page = false;
  160. if (!start_page.is_null()) {
  161. if (section != 0) {
  162. // > Help [section] [name]
  163. String path = String::formatted("/usr/share/man/man{}/{}.md", section, start_page);
  164. m_history.push(path);
  165. open_page(path);
  166. set_start_page = true;
  167. } else if (URL url = URL::create_with_url_or_path(start_page); url.is_valid() && url.path().ends_with(".md")) {
  168. // > Help [/path/to/documentation/file.md]
  169. m_history.push(url.path());
  170. open_page(url.path());
  171. set_start_page = true;
  172. } else {
  173. // > Help [query]
  174. // First, see if we can find the page by name
  175. char const* sections[] = {
  176. "1",
  177. "2",
  178. "3",
  179. "4",
  180. "5",
  181. "6",
  182. "7",
  183. "8"
  184. };
  185. for (auto s : sections) {
  186. String path = String::formatted("/usr/share/man/man{}/{}.md", s, start_page);
  187. if (Core::File::exists(path)) {
  188. m_history.push(path);
  189. open_page(path);
  190. set_start_page = true;
  191. break;
  192. }
  193. }
  194. // No match, so treat the input as a search query
  195. if (!set_start_page) {
  196. m_tab_widget->set_active_widget(m_search_container);
  197. m_search_box->set_focus(true);
  198. m_search_box->set_text(start_page);
  199. m_search_box->select_all();
  200. m_filter_model->set_filter_term(m_search_box->text());
  201. }
  202. }
  203. }
  204. if (!set_start_page)
  205. m_go_home_action->activate();
  206. }
  207. ErrorOr<void> MainWidget::initialize_fallibles(GUI::Window& window)
  208. {
  209. (void)TRY(m_toolbar->try_add_action(*m_go_back_action));
  210. (void)TRY(m_toolbar->try_add_action(*m_go_forward_action));
  211. (void)TRY(m_toolbar->try_add_action(*m_go_home_action));
  212. auto file_menu = TRY(window.try_add_menu("&File"));
  213. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
  214. GUI::Application::the()->quit();
  215. })));
  216. auto go_menu = TRY(window.try_add_menu("&Go"));
  217. TRY(go_menu->try_add_action(*m_go_back_action));
  218. TRY(go_menu->try_add_action(*m_go_forward_action));
  219. TRY(go_menu->try_add_action(*m_go_home_action));
  220. auto help_menu = TRY(window.try_add_menu("&Help"));
  221. 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")), [&](auto&) {
  222. String path = "/usr/share/man/man1/Help.md";
  223. open_page(path);
  224. })));
  225. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", TRY(GUI::Icon::try_create_default_icon("app-help")), &window)));
  226. m_context_menu = TRY(GUI::Menu::try_create());
  227. TRY(m_context_menu->try_add_action(*m_go_back_action));
  228. TRY(m_context_menu->try_add_action(*m_go_forward_action));
  229. TRY(m_context_menu->try_add_action(*m_go_home_action));
  230. TRY(m_context_menu->try_add_separator());
  231. TRY(m_context_menu->try_add_action(*m_copy_action));
  232. TRY(m_context_menu->try_add_action(*m_select_all_action));
  233. m_manual_model = TRY(ManualModel::create());
  234. m_browse_view->set_model(*m_manual_model);
  235. m_filter_model = TRY(GUI::FilteringProxyModel::create(*m_manual_model));
  236. m_search_view->set_model(*m_filter_model);
  237. m_filter_model->set_filter_term("");
  238. return {};
  239. }
  240. void MainWidget::open_url(URL const& url)
  241. {
  242. if (url.protocol() == "file") {
  243. auto path = url.path();
  244. auto source_result = m_manual_model->page_view(path);
  245. if (source_result.is_error()) {
  246. GUI::MessageBox::show(window(), String::formatted("{}", source_result.error()), "Failed to open man page", GUI::MessageBox::Type::Error);
  247. return;
  248. }
  249. auto source = source_result.value();
  250. String html;
  251. {
  252. auto md_document = Markdown::Document::parse(source);
  253. VERIFY(md_document);
  254. html = md_document->render_to_html();
  255. }
  256. m_web_view->load_html(html, url);
  257. m_web_view->scroll_to_top();
  258. GUI::Application::the()->deferred_invoke([&, path = url.path()] {
  259. auto browse_view_index = m_manual_model->index_from_path(path);
  260. if (browse_view_index.has_value()) {
  261. m_browse_view->expand_tree(browse_view_index.value().parent());
  262. String page_and_section = m_manual_model->page_and_section(browse_view_index.value());
  263. window()->set_title(String::formatted("{} - Help", page_and_section));
  264. } else {
  265. window()->set_title("Help");
  266. }
  267. });
  268. }
  269. }
  270. void MainWidget::open_external(URL const& url)
  271. {
  272. if (!Desktop::Launcher::open(url))
  273. GUI::MessageBox::show(window(), String::formatted("The link to '{}' could not be opened.", url), "Failed to open link", GUI::MessageBox::Type::Error);
  274. }
  275. void MainWidget::open_page(String const& path)
  276. {
  277. m_go_back_action->set_enabled(m_history.can_go_back());
  278. m_go_forward_action->set_enabled(m_history.can_go_forward());
  279. if (path.is_null()) {
  280. window()->set_title("Help");
  281. m_web_view->load_empty_document();
  282. return;
  283. }
  284. open_url(URL::create_with_url_or_path(path));
  285. }
  286. }