MainWidget.cpp 13 KB

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