MainWidget.cpp 11 KB

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